-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
630a40a
fix(site): prevent ExtractAPIKey from dirtying the HTML output
mafredri 54406d9
test(site): add test for injection with oauth error
mafredri 3180dbc
improve comment
mafredri 68f3aa9
typo
mafredri a3c408e
be more explicit in test about skipped oauth configs
mafredri b70084d
s/require/assert/ to give more context
mafredri File filter
Filter by extension
8000 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 |
---|---|---|
|
@@ -26,6 +26,7 @@ import ( | |
"github.com/coder/coder/coderd/database/db2sdk" | ||
"github.com/coder/coder/coderd/database/dbfake" | ||
"github.com/coder/coder/coderd/database/dbgen" | ||
"github.com/coder/coder/coderd/httpmw" | ||
"github.com/coder/coder/codersdk" | ||
"github.com/coder/coder/site" | ||
"github.com/coder/coder/testutil" | ||
|
@@ -69,6 +70,59 @@ func TestInjection(t *testing.T) { | |
require.Equal(t, db2sdk.User(user, []uuid.UUID{}), got) | ||
} | ||
|
||
func TestInjectionFailureProducesCleanHTML(t *testing.T) { | ||
t.Parallel() | ||
|
||
db := dbfake.New() | ||
|
||
// Create an expired user with a refresh token, but provide no OAuth2 | ||
// configuration so that refresh is impossible, this should result in | ||
// an error when httpmw.ExtractAPIKey is called. | ||
user := dbgen.User(t, db, database.User{}) | ||
_, token := dbgen.APIKey(t, db, database.APIKey{ | ||
UserID: user.ID, | ||
LastUsed: database.Now().Add(-time.Hour), | ||
ExpiresAt: database.Now().Add(-time.Second), | ||
LoginType: database.LoginTypeGithub, | ||
}) | ||
_ = dbgen.UserLink(t, db, database.UserLink{ | ||
UserID: user.ID, | ||
LoginType: database.LoginTypeGithub, | ||
OAuthRefreshToken: "hello", | ||
OAuthExpiry: database.Now().Add(-time.Second), | ||
}) | ||
|
||
binFs := http.FS(fstest.MapFS{}) | ||
siteFS := fstest.MapFS{ | ||
"index.html": &fstest.MapFile{ | ||
Data: []byte("<html>{{ .User }}</html>"), | ||
}, | ||
} | ||
Comment on lines
+96
to
+100
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. TIL about |
||
handler := site.New(&site.Options{ | ||
BinFS: binFs, | ||
Database: db, | ||
SiteFS: siteFS, | ||
|
||
// No OAuth2 configs, refresh will fail. | ||
OAuth2Configs: &httpmw.OAuth2Configs{ | ||
Github: nil, | ||
OIDC: nil, | ||
}, | ||
}) | ||
|
||
r := httptest.NewRequest("GET", "/", nil) | ||
r.Header.Set(codersdk.SessionTokenHeader, token) | ||
rw := httptest.NewRecorder() | ||
|
||
handler.ServeHTTP(rw, r) | ||
|
||
// Ensure we get a clean HTML response with no user data or errors | ||
// from httpmw.ExtractAPIKey. | ||
assert.Equal(t, http.StatusOK, rw.Code) | ||
body := rw.Body.String() | ||
assert.Equal(t, "<html></html>", body) | ||
} | ||
|
||
func TestCaching(t *testing.T) { | ||
t.Parallel() | ||
|
||
|
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.
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.