Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 576aef4

Browse files
authored
chore: Add linter rule to catch missing return after http writes (#2702)
1 parent 09cb778 commit 576aef4

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

coderd/users.go

+1
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,7 @@ func (api *API) postLogin(rw http.ResponseWriter, r *http.Request) {
663663
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
664664
Message: "Internal error.",
665665
})
666+
return
666667
}
667668
if !equal {
668669
// This message is the same as above to remove ease in detecting whether

coderd/workspaceapps.go

+1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ func (api *API) workspaceAppsProxyPath(rw http.ResponseWriter, r *http.Request)
8080
httpapi.Write(rw, http.StatusBadRequest, httpapi.Response{
8181
Message: "No agents exist.",
8282
})
83+
return
8384
}
8485

8586
agent := agents[0]

scripts/rules.go

+23
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,29 @@ func HttpAPIErrorMessage(m dsl.Matcher) {
151151
Report("Field \"Message\" should be a proper sentence with a capitalized first letter and ending in punctuation. $m")
152152
}
153153

154+
// HttpAPIReturn will report a linter violation if the http function is not
155+
// returned after writing a response to the client.
156+
func HttpAPIReturn(m dsl.Matcher) {
157+
m.Import("github.com/coder/coder/coderd/httpapi")
158+
159+
// Manually enumerate the httpapi function rather then a 'Where' condition
160+
// as this is a bit more efficient.
161+
m.Match(`
162+
if $*_ {
163+
httpapi.Write($*a)
164+
}
165+
`, `
166+
if $*_ {
167+
httpapi.Forbidden($*a)
168+
}
169+
`, `
170+
if $*_ {
171+
httpapi.ResourceNotFound($*a)
172+
}
173+
`).At(m["a"]).
174+
Report("Forgot to return early after writing to the http response writer.")
175+
}
176+
154177
// ProperRBACReturn ensures we always write to the response writer after a
155178
// call to Authorize. If we just do a return, the client will get a status code
156179
// 200, which is incorrect.

0 commit comments

Comments
 (0)