diff --git a/go.mod b/go.mod index 50147f8c3814..ab687ffb8a32 100644 --- a/go.mod +++ b/go.mod @@ -153,6 +153,7 @@ require ( github.com/spf13/afero v1.6.0 // indirect github.com/spf13/cast v1.4.1 // indirect github.com/spf13/jwalterweatherman v1.1.0 // indirect + github.com/stbenjam/go-sprintf-host-port v0.0.0-20220406232701-e6c52ffc7e9c // indirect github.com/stretchr/objx v0.1.1 // indirect github.com/subosito/gotenv v1.2.0 // indirect github.com/tklauser/go-sysconf v0.3.10 // indirect diff --git a/go.sum b/go.sum index 621d32160335..27c6e3ccc1d0 100644 --- a/go.sum +++ b/go.sum @@ -692,6 +692,8 @@ github.com/spf13/viper v1.10.1 h1:nuJZuYpG7gTj/XqiUwg8bA0cp1+M2mC3J4g5luUYBKk= github.com/spf13/viper v1.10.1/go.mod h1:IGlFPqhNAPKRxohIzWpI5QEy4kuI7tcl5WvR+8qy1rU= github.com/ssgreg/nlreturn/v2 v2.2.1 h1:X4XDI7jstt3ySqGU86YGAURbxw3oTDPK9sPEi6YEwQ0= github.com/ssgreg/nlreturn/v2 v2.2.1/go.mod h1:E/iiPB78hV7Szg2YfRgyIrk1AD6JVMTRkkxBiELzh2I= +github.com/stbenjam/go-sprintf-host-port v0.0.0-20220406232701-e6c52ffc7e9c h1:2Yj2o3KLwc+8v+JvRN47OlufMOfI0PllzKjp43mkgu0= +github.com/stbenjam/go-sprintf-host-port v0.0.0-20220406232701-e6c52ffc7e9c/go.mod h1:zL9IPLqGeN2rek8FGf1SCvR90PPflm9wfrWaAHfB68I= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= diff --git a/pkg/golinters/gosprintfhostport.go b/pkg/golinters/gosprintfhostport.go new file mode 100644 index 000000000000..4156922810d1 --- /dev/null +++ b/pkg/golinters/gosprintfhostport.go @@ -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) +} diff --git a/pkg/lint/lintersdb/manager.go b/pkg/lint/lintersdb/manager.go index adf91cbc601d..f35566ae983b 100644 --- a/pkg/lint/lintersdb/manager.go +++ b/pkg/lint/lintersdb/manager.go @@ -407,6 +407,11 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { WithPresets(linter.PresetStyle). WithURL("https://github.com/jirfag/go-printf-func-name"), + linter.NewConfig(golinters.NewGoSprintfHostPort()). + WithSince("v1.46.0"). + WithPresets(linter.PresetStyle). + WithURL("https://github.com/stbenjam/go-sprintf-host-port"), + linter.NewConfig(golinters.NewGosec(gosecCfg)). WithSince("v1.0.0"). WithLoadForGoAnalysis(). diff --git a/test/testdata/gosprintfhostport.go b/test/testdata/gosprintfhostport.go new file mode 100644 index 000000000000..70429d401496 --- /dev/null +++ b/test/testdata/gosprintfhostport.go @@ -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" +}