-
Notifications
You must be signed in to change notification settings - Fork 887
fix: relax csrf to exclude path based apps #11430
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
12 commits
Select commit
Hold shift + click to select a range
658d7df
fix: relax csrf to exclude path based apps
Emyrk fe4e2ba
add unit test to verify path based apps are not CSRF blocked
Emyrk f2e615d
testing needs to be parallel
Emyrk a4bbc80
Linting
Emyrk 63133cf
Merge remote-tracking branch 'origin/main' into stevenmasley/relax_csrf
Emyrk 3828354
Merge remote-tracking branch 'origin/main' into stevenmasley/relax_csrf
Emyrk c6c4141
Try exempting first user
Emyrk 42a3021
Merge remote-tracking branch 'origin/main' into stevenmasley/relax_csrf
Emyrk 043c79d
we need to add csrf mw to set cookie
Emyrk 388e56e
add comment
Emyrk fee870b
add unit tests
Emyrk 6966f87
Linting
Emyrk 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package httpmw_test | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/justinas/nosurf" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/coder/coder/v2/coderd/httpmw" | ||
"github.com/coder/coder/v2/codersdk" | ||
) | ||
|
||
func TestCSRFExemptList(t *testing.T) { | ||
t.Parallel() | ||
|
||
cases := []struct { | ||
Name string | ||
URL string | ||
Exempt bool | ||
}{ | ||
{ | ||
Name: "Root", | ||
URL: "https://example.com", | ||
Exempt: true, | ||
}, | ||
{ | ||
Name: "WorkspacePage", | ||
URL: "https://coder.com/workspaces", | ||
Exempt: true, | ||
}, | ||
{ | ||
Name: "SubApp", | ||
URL: "https://app--dev--coder--user--apps.coder.com/", | ||
Exempt: true, | ||
}, | ||
{ | ||
Name: "PathApp", | ||
URL: "https://coder.com/@USER/test.instance/apps/app", | ||
Exempt: true, | ||
}, | ||
{ | ||
Name: "API", | ||
URL: "https://coder.com/api/v2", | ||
Exempt: false, | ||
}, | ||
{ | ||
Name: "APIMe", | ||
URL: "https://coder.com/api/v2/me", | ||
Exempt: false, | ||
}, | ||
} | ||
|
||
mw := httpmw.CSRF(false) | ||
csrfmw := mw(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})).(*nosurf.CSRFHandler) | ||
|
||
for _, c := range cases { | ||
c := c | ||
t.Run(c.Name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
r, err := http.NewRequestWithContext(context.Background(), http.MethodPost, c.URL, nil) | ||
require.NoError(t, err) | ||
|
||
r.AddCookie(&http.Cookie{Name: codersdk.SessionTokenCookie, Value: "test"}) | ||
exempt := csrfmw.IsExempt(r) | ||
require.Equal(t, c.Exempt, exempt) | ||
}) | ||
} | ||
} |
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
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.
Is it this one?
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.
Yup. All of our existing unit tests bypass CSRF since the codersdk uses the HEADER method to auth, not the cookie.