-
Notifications
You must be signed in to change notification settings - Fork 961
chore: Add linter rule to catch missing return after http writes #2702
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
227e1c5
aec49c5
56885dd
120309b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -151,6 +151,28 @@ func HttpAPIErrorMessage(m dsl.Matcher) { | |
Report("Field \"Message\" should be a proper sentence with a capitalized first letter and ending in punctuation. $m") | ||
} | ||
|
||
// HttpAPIReturn will report a linter violation if the http function is not | ||
// returned after writing a response to the client. | ||
func HttpAPIReturn(m dsl.Matcher) { | ||
m.Import("github.com/coder/coder/coderd/httpapi") | ||
|
||
// Manually enumerate the httpapi function rather then a 'Where' condition | ||
// as this is a bit more efficient. | ||
m.Match(` | ||
if err != nil { | ||
httpapi.Write($*_) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thought: Does this, or should we, handle the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm that is a good question. This is definitely the common case, but you are right about more possible situations. I am unsure if I can match
I think I will merge this in as is for now. But expanding it would be nice, I just don't think ruleguard can match different AST nodes very well aside from enumerating all cases. Another idea I had was trying to match the correct case like this: m.Match(`
httpapi.Write($*a)
$r
`).Where(!m["r"].Text.Matches("return")) So the next line must be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah interesting, thanks for checking into it. Yup feel free to merge as-is, fixing the other cases is more of a nice-to-have anyway, def. don't want it becoming slow. |
||
`, ` | ||
if err != nil { | ||
httpapi.Forbidden($*_) | ||
} | ||
`, ` | ||
if err != nil { | ||
httpapi.ResourceNotFound($*_) | ||
} | ||
`).Report("Forgot to return early after the httpapi.Write call") | ||
} | ||
|
||
// ProperRBACReturn ensures we always write to the response writer after a | ||
// call to Authorize. If we just do a return, the client will get a status code | ||
// 200, which is incorrect. | ||
|
Uh oh!
There was an error while loading. Please reload this page.