-
Notifications
You must be signed in to change notification settings - Fork 1.5k
fix(coderd): set Cache-Control: no-store on OAuth2 responses #28143
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
98efb88
fix(coderd): set Cache-Control: no-store on OAuth2 responses
BobbyHo 97cea43
fix(coderd): extend no-store to the oauth2-provider tree
BobbyHo e9c1cc2
test(coderd): tighten the no-store test comments
BobbyHo c4b1342
test(coderd/oauth2provider): widen the metadata cacheability assertion
BobbyHo d66a181
Merge branch 'main' into coder-plat-448-oauth-cache-control
BobbyHo 19e15d9
test(coderd): drop TestNoStoreAfterExperimentGate
BobbyHo f3dac88
docs(coderd/httpmw): scope the NoStore coverage claim to what pins it
BobbyHo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package httpmw | ||
|
|
||
| import "net/http" | ||
|
|
||
| // NoStore sets the response caching headers that OAuth2 requires on any | ||
| // response that may contain a credential. RFC 6749 §5.1 makes both headers a | ||
| // MUST for the authorization server; OAuth 2.1 §3.2.3 keeps only no-store, | ||
| // because RFC 9111 §5.4 deprecates Pragma as a request-only field. Both are | ||
| // sent so that a client or auditor reading either specification sees a | ||
| // conformant response. | ||
| // | ||
| // The headers are set before the wrapped handler runs, so a handler that | ||
| // writes its own Cache-Control would win. None does today; the integration | ||
| // tests pin that across the /oauth2 tree and spot-check | ||
| // /api/v2/oauth2-provider. Pragma is written unconditionally, so such a | ||
| // handler's Cache-Control ships alongside Pragma: no-cache. | ||
| // | ||
| // chi's middleware.NoCache is not used, though that package is already | ||
| // imported at the mount site. It strips the ETag-family headers from the | ||
| // request, which an authorization server has no business doing, and it sends | ||
| // directives neither specification asks for, where OAuth 2.1 narrows the | ||
| // requirement rather than widening it. | ||
| func NoStore(next http.Handler) http.Handler { | ||
|
BobbyHo marked this conversation as resolved.
BobbyHo marked this conversation as resolved.
|
||
| return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { | ||
| rw.Header().Set("Cache-Control", "no-store") | ||
| rw.Header().Set("Pragma", "no-cache") | ||
| next.ServeHTTP(rw, r) | ||
| }) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| package httpmw_test | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/coder/coder/v2/coderd/httpmw" | ||
| ) | ||
|
|
||
| func TestNoStore(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| tests := []struct { | ||
| Name string | ||
| Handler http.HandlerFunc | ||
|
|
||
| expectStatus int | ||
| expectCacheControl string | ||
| expectPragma string | ||
| assert func(t *testing.T, res *httptest.ResponseRecorder) | ||
| }{ | ||
| { | ||
| // The POST /oauth2/tokens shape. | ||
| Name: "OK", | ||
| Handler: func(rw http.ResponseWriter, _ *http.Request) { | ||
| rw.WriteHeader(http.StatusOK) | ||
| _, _ = rw.Write([]byte(`{"access_token":"secret"}`)) | ||
| }, | ||
| expectStatus: http.StatusOK, | ||
| expectCacheControl: "no-store", | ||
| expectPragma: "no-cache", | ||
| }, | ||
| { | ||
| // The DELETE /oauth2/clients/{client_id} shape: headers on a | ||
| // response with no body. | ||
| Name: "NoContent", | ||
| Handler: func(rw http.ResponseWriter, _ *http.Request) { | ||
| rw.WriteHeader(http.StatusNoContent) | ||
| }, | ||
| expectStatus: http.StatusNoContent, | ||
| expectCacheControl: "no-store", | ||
| expectPragma: "no-cache", | ||
| }, | ||
| { | ||
| // The POST /oauth2/authorize shape: http.Redirect writes its own | ||
| // headers without clearing the map. | ||
| Name: "Redirect", | ||
| Handler: func(rw http.ResponseWriter, r *http.Request) { | ||
| http.Redirect(rw, r, "https://example.com/callback?code=abc", http.StatusFound) | ||
| }, | ||
| expectStatus: http.StatusFound, | ||
| expectCacheControl: "no-store", | ||
| expectPragma: "no-cache", | ||
| assert: func(t *testing.T, res *httptest.ResponseRecorder) { | ||
| require.Equal(t, "https://example.com/callback?code=abc", res.Header().Get("Location")) | ||
| }, | ||
| }, | ||
| { | ||
| // The revoke.go and registration.go shape: a bare WriteHeader, | ||
| // never httpapi.Write. | ||
| Name: "BareWriteHeaderError", | ||
| Handler: func(rw http.ResponseWriter, _ *http.Request) { | ||
| rw.WriteHeader(http.StatusBadRequest) | ||
| }, | ||
| expectStatus: http.StatusBadRequest, | ||
| expectCacheControl: "no-store", | ||
| expectPragma: "no-cache", | ||
| }, | ||
| { | ||
| // The headers are advisory: a handler that writes its own | ||
| // Cache-Control wins, and Pragma survives alongside it. | ||
| Name: "HandlerOverwrites", | ||
| Handler: func(rw http.ResponseWriter, _ *http.Request) { | ||
| rw.Header().Set("Cache-Control", "private") | ||
| rw.WriteHeader(http.StatusOK) | ||
| }, | ||
| expectStatus: http.StatusOK, | ||
| expectCacheControl: "private", | ||
| expectPragma: "no-cache", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.Name, func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| req := httptest.NewRequest(http.MethodGet, "/", nil) | ||
| res := httptest.NewRecorder() | ||
| httpmw.NoStore(tt.Handler).ServeHTTP(res, req) | ||
|
|
||
| require.Equal(t, tt.expectStatus, res.Code) | ||
| require.Equal(t, tt.expectCacheControl, res.Header().Get("Cache-Control")) | ||
| require.Equal(t, tt.expectPragma, res.Header().Get("Pragma")) | ||
| if tt.assert != nil { | ||
| tt.assert(t, res) | ||
| } | ||
| }) | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.