Skip to content

Commit 89e6cd6

Browse files
authored
Add nosprintfhostport linter (#2749)
1 parent f79bc88 commit 89e6cd6

File tree

6 files changed

+76
-0
lines changed

6 files changed

+76
-0
lines changed

.golangci.example.yml

+2
Original file line numberDiff line numberDiff line change
@@ -1679,6 +1679,7 @@ linters:
16791679
- nlreturn
16801680
- noctx
16811681
- nolintlint
1682+
- nosprintfhostport
16821683
- paralleltest
16831684
- prealloc
16841685
- predeclared
@@ -1769,6 +1770,7 @@ linters:
17691770
- nlreturn
17701771
- noctx
17711772
- nolintlint
1773+
- nosprintfhostport
17721774
- paralleltest
17731775
- prealloc
17741776
- predeclared

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ require (
8383
github.com/spf13/pflag v1.0.5
8484
github.com/spf13/viper v1.11.0
8585
github.com/ssgreg/nlreturn/v2 v2.2.1
86+
github.com/stbenjam/no-sprintf-host-port v0.1.0
8687
github.com/stretchr/testify v1.7.1
8788
github.com/sylvia7788/contextcheck v1.0.4
8889
github.com/tdakkota/asciicheck v0.1.1

go.sum

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/golinters/nosprintfhostport.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package golinters
2+
3+
import (
4+
"github.com/stbenjam/no-sprintf-host-port/pkg/analyzer"
5+
"golang.org/x/tools/go/analysis"
6+
7+
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
8+
)
9+
10+
func NewNoSprintfHostPort() *goanalysis.Linter {
11+
a := analyzer.Analyzer
12+
13+
return goanalysis.NewLinter(
14+
a.Name,
15+
a.Doc,
16+
[]*analysis.Analyzer{a},
17+
nil,
18+
).WithLoadMode(goanalysis.LoadModeSyntax)
19+
}

pkg/lint/lintersdb/manager.go

+5
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,11 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
531531
WithPresets(linter.PresetStyle).
532532
WithURL("https://github.com/firefart/nonamedreturns"),
533533

534+
linter.NewConfig(golinters.NewNoSprintfHostPort()).
535+
WithSince("v1.46.0").
536+
WithPresets(linter.PresetStyle).
537+
WithURL("https://github.com/stbenjam/no-sprintf-host-port"),
538+
534539
linter.NewConfig(golinters.NewParallelTest()).
535540
WithSince("v1.33.0").
536541
WithPresets(linter.PresetStyle, linter.PresetTest).

test/testdata/nosprintfhostport.go

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//args: -Enosprintfhostport
2+
package testdata
3+
4+
import (
5+
"fmt"
6+
"net"
7+
)
8+
9+
func _() {
10+
11+
_ = fmt.Sprintf("postgres://%s:%[email protected]/%s", "foo", "bar", "baz")
12+
13+
_ = fmt.Sprintf("http://api.%s/foo", "example.com")
14+
15+
_ = fmt.Sprintf("http://api.%s:6443/foo", "example.com")
16+
17+
_ = fmt.Sprintf("http://%s/foo", net.JoinHostPort("foo", "80"))
18+
19+
_ = fmt.Sprintf("9invalidscheme://%s:%d", "myHost", 70)
20+
21+
_ = fmt.Sprintf("gopher://%s/foo", net.JoinHostPort("foo", "80"))
22+
23+
_ = fmt.Sprintf("telnet+ssl://%s/foo", net.JoinHostPort("foo", "80"))
24+
25+
_ = fmt.Sprintf("http://%s/foo:bar", net.JoinHostPort("foo", "80"))
26+
27+
_ = fmt.Sprintf("http://user:password@%s/foo:bar", net.JoinHostPort("foo", "80"))
28+
29+
_ = fmt.Sprintf("http://example.com:9211")
30+
31+
_ = fmt.Sprintf("gopher://%s:%d", "myHost", 70) // ERROR "host:port in url should be constructed with net.JoinHostPort and not directly with fmt.Sprintf"
32+
33+
_ = 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"
34+
35+
_ = fmt.Sprintf("weird3.6://%s:%d", "myHost", 23) // ERROR "host:port in url should be constructed with net.JoinHostPort and not directly with fmt.Sprintf"
36+
37+
_ = 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"
38+
39+
_ = 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"
40+
41+
_ = fmt.Sprintf("https://%s:%d", "myHost", 8443) // ERROR "host:port in url should be constructed with net.JoinHostPort and not directly with fmt.Sprintf"
42+
43+
_ = fmt.Sprintf("https://%s:9211", "myHost") // ERROR "host:port in url should be constructed with net.JoinHostPort and not directly with fmt.Sprintf"
44+
45+
ip := "fd00::1"
46+
_ = fmt.Sprintf("http://%s:1936/healthz", ip) // ERROR "host:port in url should be constructed with net.JoinHostPort and not directly with fmt.Sprintf"
47+
}

0 commit comments

Comments
 (0)