Skip to content

chore: fix csrf error message on empty session header #14018

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 3 commits into from
Jul 25, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add comments
  • Loading branch information
Emyrk committed Jul 25, 2024
commit 44724f1773d913a44f539735f90a4301c2685efe
6 changes: 5 additions & 1 deletion coderd/httpmw/csrf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,21 @@
}
}

// TestCSRFError verifies the error message returned to a user when CSRF
// checks fail.
func TestCSRFError(t *testing.T) {
t.Parallel()

// Hard coded matching CSRF values
const csrfCookieValue = "JXm9hOUdZctWt0ZZGAy9xiS/gxMKYOThdxjjMnMUyn4="
const csrfHeaderValue = "KNKvagCBEHZK7ihe2t7fj6VeJ0UyTDco1yVUJE8N06oNqxLu5Zx1vRxZbgfC0mJJgeGkVjgs08mgPbcWPBkZ1A=="
// Use a url with "/api" as the root, other routes bypass CSRF.
const urlPath = "https://coder.com/api/v2/hello"

var handler http.Handler = http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
writer.WriteHeader(http.StatusOK)
})
handler = httpmw.CSRF(false)(handler)
const urlPath = "https://coder.com/api/v2/hello"

// Not testing the error case, just providing the example of things working
// to base the failure tests off of.
Expand All @@ -98,10 +101,11 @@

rec := httptest.NewRecorder()
handler.ServeHTTP(rec, req)
resp := rec.Result()

Check failure on line 104 in coderd/httpmw/csrf_test.go

View workflow job for this annotation

GitHub Actions / lint

response body must be closed (bodyclose)
require.Equal(t, http.StatusOK, resp.StatusCode)
})

// The classic CSRF failure returns the generic error.
t.Run("MissingCSRFHeader", func(t *testing.T) {
t.Parallel()

Expand All @@ -113,7 +117,7 @@

rec := httptest.NewRecorder()
handler.ServeHTTP(rec, req)
resp := rec.Result()

Check failure on line 120 in coderd/httpmw/csrf_test.go

View workflow job for this annotation

GitHub Actions / lint

response body must be closed (bodyclose)
require.Equal(t, http.StatusBadRequest, resp.StatusCode)
require.Contains(t, rec.Body.String(), "Something is wrong with your CSRF token.")
})
Expand All @@ -133,7 +137,7 @@

rec := httptest.NewRecorder()
handler.ServeHTTP(rec, req)
resp := rec.Result()

Check failure on line 140 in coderd/httpmw/csrf_test.go

View workflow job for this annotation

GitHub Actions / lint

response body must be closed (bodyclose)
require.Equal(t, http.StatusBadRequest, resp.StatusCode)
require.Contains(t, rec.Body.String(), "CSRF error encountered. Authentication via")
})
Expand Down
Loading