-
Notifications
You must be signed in to change notification settings - Fork 0
Plumb CSRF token to admin templates so POST forms work #26
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
4 commits
Select commit
Hold shift + click to select a range
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
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
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,90 @@ | ||
| package web_test | ||
|
|
||
| // Regression tests for the end-to-end CSRF token plumbing on admin | ||
| // POST forms. Background: connections form templates (gitlab, | ||
| // http_proxy, slack, etc.) historically didn't include a csrf_token | ||
| // hidden input and the render path didn't expose the plaintext token | ||
| // to nav.html, so every basic Add-Connection POST failed at the | ||
| // middleware with "csrf token missing or invalid". | ||
| // | ||
| // This file pins two properties: | ||
| // | ||
| // - TestPOSTConnectionsAddPassesCSRF: a POST to /connections/add | ||
| // carrying the csrf_token form field (the value the nav.html | ||
| // submit handler echoes back from window.SIEVE_CSRF) is accepted | ||
| // by the middleware and reaches the handler. We assert the | ||
| // documented 303 redirect on success. | ||
| // - TestPOSTConnectionsAddMissingCSRFRejected: the negative control | ||
| // — the same POST without any CSRF token still 403s with a | ||
| // csrf-themed body. Pins that the new plumbing didn't accidentally | ||
| // relax the gate. | ||
| // | ||
| // The new sieve_csrf cookie's set/clear lifecycle is covered alongside | ||
| // the session cookie in auth_test.go (TestLogin_HappyPath, | ||
| // TestLogin_WrongCredential, TestLogout_DeletesCookieAndSession). | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "net/url" | ||
| "strings" | ||
| "testing" | ||
| ) | ||
|
|
||
| // TestPOSTConnectionsAddPassesCSRF asserts that an admin POST with the | ||
| // session cookie + csrf_token form field passes the middleware. Uses | ||
| // the connector_type=mock seed (mock is registered by newTestWebServer) | ||
| // so the handler runs to completion and we exercise the real success | ||
| // path rather than a 400-on-unknown-connector path. | ||
| func TestPOSTConnectionsAddPassesCSRF(t *testing.T) { | ||
| handler, env := newTestWebServer(t) | ||
|
|
||
| form := url.Values{} | ||
| form.Set("connector_type", "mock") | ||
| form.Set("id", "smoke-csrf") | ||
| form.Set("display_name", "CSRF smoke") | ||
| form.Set("csrf_token", env.CSRFToken()) | ||
|
|
||
| req := httptest.NewRequest(http.MethodPost, "/connections/add", strings.NewReader(form.Encode())) | ||
| req.Header.Set("Content-Type", "application/x-www-form-urlencoded") | ||
| if c := env.SessionCookie(); c != nil { | ||
| req.AddCookie(c) | ||
| } | ||
| rec := httptest.NewRecorder() | ||
| handler.ServeHTTP(rec, req) | ||
|
|
||
| if rec.Code == http.StatusForbidden && strings.Contains(rec.Body.String(), "csrf") { | ||
| t.Fatalf("CSRF middleware rejected request that carried the form-field token (body: %s)", rec.Body.String()) | ||
| } | ||
| if rec.Code != http.StatusSeeOther { | ||
| t.Fatalf("expected 303 redirect after Add, got %d (body: %s)", rec.Code, rec.Body.String()) | ||
| } | ||
| } | ||
|
|
||
| // TestPOSTConnectionsAddMissingCSRFRejected is the negative control: | ||
| // without the csrf_token form field (and without the X-CSRF-Token | ||
| // header), the middleware MUST reject with 403. This pins the security | ||
| // property that the new plumbing didn't accidentally relax. | ||
| func TestPOSTConnectionsAddMissingCSRFRejected(t *testing.T) { | ||
| handler, env := newTestWebServer(t) | ||
|
|
||
| form := url.Values{} | ||
| form.Set("connector_type", "mock") | ||
| form.Set("id", "smoke-no-csrf") | ||
| form.Set("display_name", "CSRF negative control") | ||
|
|
||
| req := httptest.NewRequest(http.MethodPost, "/connections/add", strings.NewReader(form.Encode())) | ||
| req.Header.Set("Content-Type", "application/x-www-form-urlencoded") | ||
| if c := env.SessionCookie(); c != nil { | ||
| req.AddCookie(c) | ||
| } | ||
| rec := httptest.NewRecorder() | ||
| handler.ServeHTTP(rec, req) | ||
|
|
||
| if rec.Code != http.StatusForbidden { | ||
| t.Fatalf("expected 403 for missing CSRF token, got %d (body: %s)", rec.Code, rec.Body.String()) | ||
| } | ||
| if !strings.Contains(rec.Body.String(), "csrf") { | ||
| t.Errorf("expected csrf-related error body, got: %s", rec.Body.String()) | ||
| } | ||
| } |
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
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.