-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
5 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package golinters | ||
|
||
import ( | ||
"github.com/stbenjam/go-sprintf-host-port/pkg/analyzer" | ||
"golang.org/x/tools/go/analysis" | ||
|
||
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis" | ||
) | ||
|
||
func NewGoSprintfHostPort() *goanalysis.Linter { | ||
a := analyzer.Analyzer | ||
|
||
return goanalysis.NewLinter( | ||
a.Name, | ||
a.Doc, | ||
[]*analysis.Analyzer{a}, | ||
nil, | ||
).WithLoadMode(goanalysis.LoadModeSyntax) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package testdata | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
) | ||
|
||
func _() { | ||
_ = fmt.Sprintf("gopher://%s/foo", net.JoinHostPort("foo", "80")) | ||
|
||
_ = fmt.Sprintf("postgres://%s:%s@127.0.0.1/%s", "foo", "bar", "baz") | ||
|
||
_ = fmt.Sprintf("http://%s/foo", net.JoinHostPort("foo", "80")) | ||
|
||
_ = fmt.Sprintf("http://api.%s:6443/foo", "example.com") | ||
|
||
_ = fmt.Sprintf("http://api.%s/foo", "example.com") | ||
|
||
_ = fmt.Sprintf("telnet+ssl://%s/foo", net.JoinHostPort("foo", "80")) | ||
|
||
_ = fmt.Sprintf("http://%s/foo:bar", net.JoinHostPort("foo", "80")) | ||
|
||
_ = fmt.Sprintf("http://user:password@%s/foo:bar", net.JoinHostPort("foo", "80")) | ||
|
||
_ = fmt.Sprintf("http://example.com:9211") | ||
|
||
_ = fmt.Sprintf("gopher://%s:%d", "myHost", 70) // ERROR "host:port in url should be constructed with net.JoinHostPort and not directly with fmt.Sprintf" | ||
|
||
_ = fmt.Sprintf("telnet+ssl://%s:%d", "myHost", 23) // ERROR "host:port in url should be constructed with net.JoinHostPort and not directly with fmt.Sprintf" | ||
|
||
_ = fmt.Sprintf("https://user@%s:%d", "myHost", 8443) // ERROR "host:port in url should be constructed with net.JoinHostPort and not directly with fmt.Sprintf" | ||
|
||
_ = fmt.Sprintf("postgres://%s:%s@%s:5050/%s", "foo", "bar", "baz", "qux") // ERROR "host:port in url should be constructed with net.JoinHostPort and not directly with fmt.Sprintf" | ||
|
||
_ = fmt.Sprintf("https://%s:%d", "myHost", 8443) // ERROR "host:port in url should be constructed with net.JoinHostPort and not directly with fmt.Sprintf" | ||
|
||
_ = fmt.Sprintf("https://%s:9211", "myHost") // ERROR "host:port in url should be constructed with net.JoinHostPort and not directly with fmt.Sprintf" | ||
|
||
ip := "fd00::1" | ||
_ = fmt.Sprintf("http://%s:1936/healthz", ip) // ERROR "host:port in url should be constructed with net.JoinHostPort and not directly with fmt.Sprintf" | ||
} |