golangci / golangci-lint Public
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add nosprintfhostport linter #2749
Conversation
Hey, thank you for opening your first Pull Request ! |
In order for a pull request adding a linter to be reviewed, the linter and the PR must follow some requirements. Pull Request Description
Linter
The Linter Tests Inside Golangci-lint
|
@stbenjam, is it necessary to have the prefix I think And it looks like a more general problem
What do you think? |
No, I can fix that.
Do you have an example of what this would look like? I think this turns any quick URL construction into many lines. Not sure I would want that forced on me -- the problem (for me anyway) is pretty limited to using Sprintf to construct the
Looks like
|
Hi, I think I've addressed what I can from the checklist and comments. Could you have another look? Thank you |
I tested the linter with several large code-base, and I haven't seen false positives or false negatives.
I feel that this linter can have this problem, but as I don't have any proof of that, I will approve.
The Go linter go-sprintf-host-port checks that sprintf is not used to construct a host:port combination in a URL. A frequent pattern is for a developer to construct a URL like this: ```go fmt.Sprintf("http://%s:%d/foo", host, port) ``` However, if "host" is an IPv6 address like 2001:4860:4860::8888, the URL constructed will be invalid. IPv6 addresses must be bracketed, like this: ``` http://[2001:4860:4860::8888]:9443 ``` The linter is naive, and really only looks for the most obvious cases, but where it's possible to infer that a URL is being constructed with Sprintf containing a :, this informs the user to use net.JoinHostPort instead. Running it against some real world code bases like OpenShift and Kubernetes has found a number of cases that would break in IPv6 environments.
- Alphabetized manager.go - Added go glangci.example.yml - Used tag for go.mod
@ldez Could you look again? I had to rebase as lintersdb got a new entry that caused a conflict. |
Repo:
https://github.com/stbenjam/no-sprintf-host-port
Description:
no-sprintf-host-port checks that Sprintf is not used to construct a host:port
combination in a URL that possibly contains an IPv6 address.
Details:
A frequent pattern is for a developer to construct a URL like this:
However, if "host" is an IPv6 address like 2001:4860:4860::8888, the URL
constructed will be invalid. IPv6 addresses must be bracketed, like
this:
The linter really only looks for the most obvious cases, but where it's
possible to infer that a URL is being constructed with Sprintf containing
a :, this informs the user to use net.JoinHostPort instead.
Running it against some real world code bases like OpenShift and
Kubernetes has found a number of cases that would break in IPv6
environments.