Skip to content

Commit

Permalink
More
Browse files Browse the repository at this point in the history
  • Loading branch information
stbenjam committed Apr 6, 2022
1 parent f688bc9 commit e5f7683
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
gosprintfhostport.so
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
all: plugin
.PHONY: all plugin

lint:
golangci-lint run ./...

plugin:
go build -buildmode=plugin plugin/gosprintfhostport.go
23 changes: 16 additions & 7 deletions pkg/analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,22 @@ func run(pass *analysis.Pass) (interface{}, error) {
// Remove quotes
fs := fsRaw.Value[1 : len(fsRaw.Value)-1]

// - Check to see if looks like a URI with a port, basically scheme://%s:<something else>
// - Scheme, as per RFC3986 is ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
// - A format string substitution in the host portion
// - A colon indicating a port will be specified
urlRegex := regexp.MustCompile(`[a-zA-Z0-9+-.]*://[^/]*%[^/]*:.*`)
if urlRegex.MatchString(fs) {
pass.Reportf(node.Pos(), "host:port in url should be constructed with net.JoinHostPort and not directly with fmt.Sprintf")
regexes := []*regexp.Regexp{
// - Check to see if it looks like a URI with a port, basically scheme://%s:<something else>
// - Scheme, as per RFC3986 is ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
// - A format string substitution in the host portion
// - A colon indicating a port will be specified
regexp.MustCompile(`[a-zA-Z0-9+-.]*://%s:.*`),

// Same as above, but allowing a username/password
regexp.MustCompile(`[a-zA-Z0-9+-.]*://[^/]*@%s:.*`),
}

for _, re := range regexes {
if re.MatchString(fs) {
pass.Reportf(node.Pos(), "host:port in url should be constructed with net.JoinHostPort and not directly with fmt.Sprintf")
break
}
}
})

Expand Down
7 changes: 7 additions & 0 deletions testdata/src/p/p.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,20 @@ func _() {

_ = 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) // want "should be constructed with net.JoinHostPort"

_ = fmt.Sprintf("telnet+ssl://%s:%d", "myHost", 23) // want "should be constructed with net.JoinHostPort"

_ = fmt.Sprintf("https://user@%s:%d", "myHost", 8443) // want "should be constructed with net.JoinHostPort"

_ = fmt.Sprintf("https://%s:%d", "myHost", 8443) // want "should be constructed with net.JoinHostPort"

_ = fmt.Sprintf("https://%s:9211", "myHost") // want "should be constructed with net.JoinHostPort"

routerIP := "fd00::1"
_ = fmt.Sprintf("http://%s:1936/healthz", routerIP) // want "should be constructed with net.JoinHostPort"
}

0 comments on commit e5f7683

Please sign in to comment.