-
Notifications
You must be signed in to change notification settings - Fork 930
fix(site): prevent ExtractAPIKey from dirtying the HTML output #8450
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
Conversation
90f3a55
to
df0ce70
Compare
If `httpmw.ExtractAPIKey` fails when we are rendering an HTML page, the HTML output will be dirtied with the error repsonse and the HTTP status will also be wrong. The use of this function in the `renderHTMLWithState` is additive, and failure means we simply can't embed static data. To fix this, we can simply pass a `http.ResponseWriter` that is no-op. Fixes #8351
df0ce70
to
630a40a
Compare
site/site.go
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible to add a unit test that ossifies this behaviour? It would be sad if we had another regression on this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, I'm looking into this currently 👍🏻
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Covered in 54406d9
(#8450):
coder@w ~/src/coder/coder mafredri/fix-site-oauth*
❯ go test ./site -run=TestInjectionFailure -v
=== RUN TestInjectionFailureProducesCleanHTML
=== PAUSE TestInjectionFailureProducesCleanHTML
=== CONT TestInjectionFailureProducesCleanHTML
site_test.go:114:
Error Trace: /home/coder/src/coder/coder/site/site_test.go:114
Error: Not equal:
expected: 200
actual : 500
Test: TestInjectionFailureProducesCleanHTML
--- FAIL: TestInjectionFailureProducesCleanHTML (0.00s)
FAIL
FAIL github.com/coder/coder/site 0.042s
FAIL
coder@w ~/src/coder/coder mafredri/fix-site-oauth*
❯ go test ./site -run=TestInjectionFailure -v
=== RUN TestInjectionFailureProducesCleanHTML
=== PAUSE TestInjectionFailureProducesCleanHTML
=== CONT TestInjectionFailureProducesCleanHTML
site_test.go:116:
Error Trace: /home/coder/src/coder/coder/site/site_test.go:116
Error: Not equal:
expected: "<html></html>"
actual : "{\n\t\"message\": \"An internal error occurred. Please try again or contact the system administrator.\",\n\t\"detail\": \"Unable to refresh OAuth token for login type \\\"github\\\". No OAuth2Configs provided. Contact an administrator to configure this login type.\"\n}\n<html></html>"
Diff:
--- Expected
+++ Actual
@@ -1 +1,5 @@
+{
+ "message": "An internal error occurred. Please try again or contact the system administrator.",
+ "detail": "Unable to refresh OAuth token for login type \"github\". No OAuth2Configs provided. Contact an administrator to configure this login type."
+}
<html></html>
Test: TestInjectionFailureProducesCleanHTML
--- FAIL: TestInjectionFailureProducesCleanHTML (0.00s)
FAIL
FAIL github.com/coder/coder/site 0.031s
FAIL
coder@w ~/src/coder/coder mafredri/fix-site-oauth*
❯ go test ./site -run=TestInjectionFailure -v
=== RUN TestInjectionFailureProducesCleanHTML
=== PAUSE TestInjectionFailureProducesCleanHTML
=== CONT TestInjectionFailureProducesCleanHTML
--- PASS: TestInjectionFailureProducesCleanHTML (0.01s)
PASS
ok github.com/coder/coder/site 0.053s
siteFS := fstest.MapFS{ | ||
"index.html": &fstest.MapFile{ | ||
Data: []byte("<html>{{ .User }}</html>"), | ||
}, | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TIL about testing/fstest
❤️
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Neat test! 👍
noopRW := noopResponseWriter{} | ||
apiKey, actor, ok := httpmw.ExtractAPIKey(noopRW, r, httpmw.ExtractAPIKeyConfig{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes a lot of sense.
I think the FE has code where if a static value is not populated statically, it defaults to using the API. so there should never be an error, just an empty static field.
If
httpmw.ExtractAPIKey
fails when we are rendering an HTML page, theHTML output will be dirtied with the error repsonse and the HTTP status
will also be wrong.
The use of this function in the
renderHTMLWithState
is additive, andfailure means we simply can't embed static data. To fix this, we can
simply pass a
http.ResponseWriter
that is no-op.Fixes #8351