-
Notifications
You must be signed in to change notification settings - Fork 1.5k
fix(coderd): harden oauth2 redirect validation #27274
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
04066c0
fix(coderd/httpmw): harden oauth2 redirect url validation
aslilac ccd7573
refactor(coderd/httpmw): keep redirect helper unexported, test intern…
aslilac 16d7808
refactor(coderd): consolidate redirect url validation into one helper
aslilac f546689
refactor(coderd/httpapi): move redirect sanitizer out of httpmw
aslilac fc23006
Update coderd/httpapi/redirect.go
aslilac 5b5d122
Update coderd/httpapi/redirect.go
aslilac 38d9a6a
feedback
aslilac 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,31 @@ | ||
| package httpapi | ||
|
|
||
| import ( | ||
| "net/url" | ||
| "strings" | ||
| ) | ||
|
|
||
| // SafeRedirectPath reduces a redirect URL down to a safe, relative path. The | ||
| // scheme and host are dropped to prevent redirecting to another origin. Opaque | ||
| // URLs (e.g. `javascript:`, `data:`) are rejected outright and default to /. | ||
| func SafeRedirectPath(u string) string { | ||
| uri, err := url.Parse(u) | ||
| if err != nil || uri.Opaque != "" { | ||
| return "/" | ||
| } | ||
|
|
||
| // A path with 2 or more leading slashes (e.g. "//evil.com") is interpreted as | ||
| // protocol-relative, so make sure there is exactly one. | ||
| path := "/" + strings.TrimLeft(uri.EscapedPath(), "/") | ||
| if uri.RawQuery != "" { | ||
| path += "?" + uri.RawQuery | ||
| } | ||
| // We're specifically checking Fragment instead of RawFragment here because | ||
| // RawFragment is only populated when the parser needs to preserve a | ||
| // non-default escaping, so it is empty for plain-alphanumeric fragments like | ||
| // "#wooble". EscapedFragment handles escaping correctly in either case. | ||
| if uri.Fragment != "" { | ||
| path += "#" + uri.EscapedFragment() | ||
| } | ||
| return path | ||
| } | ||
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,48 @@ | ||
| package httpapi_test | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/coder/coder/v2/coderd/httpapi" | ||
| ) | ||
|
|
||
| func TestSafeRedirectPath(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| in string | ||
| want string | ||
| }{ | ||
| {"empty", "", "/"}, | ||
| {"simple path", "/foo/bar", "/foo/bar"}, | ||
| {"path with query", "/foo/bar?baz=qux", "/foo/bar?baz=qux"}, | ||
| {"path with fragment", "/foo/bar#wooble", "/foo/bar#wooble"}, | ||
| {"path with query+fragment", "/foo/bar?wibble=wobble#wooble", "/foo/bar?wibble=wobble#wooble"}, | ||
| {"no leading slash", "foo/bar", "/foo/bar"}, | ||
| {"malformed", "http://[::1]:namedport", "/"}, | ||
| // Ensure backslashes aren't a blindspot. | ||
| {"backslash after slash", `/\evil.example.com`, "/%5Cevil.example.com"}, | ||
| {"leading double backslash", `\\evil.example.com`, "/%5C%5Cevil.example.com"}, | ||
| {"backslash then slash", `\/evil.example.com`, "/%5C/evil.example.com"}, | ||
| {"mixed slash backslash", `/\/evil.example.com`, "/%5C/evil.example.com"}, | ||
| {"scheme with backslash", `https:/\evil.example.com`, "/%5Cevil.example.com"}, | ||
| // Cure53 CDM-02-009: triple-slash open redirect. | ||
| {"protocol relative triple slash", "///evil.example.com", "/evil.example.com"}, | ||
| {"protocol relative double slash", "//evil.example.com", "/"}, | ||
| {"absolute url with host", "http://evil.example.com/path", "/path"}, | ||
| {"absolute url with host and query", "https://evil.example.com/path?a=b", "/path?a=b"}, | ||
| // Cure53 CDM-02-009: javascript: scheme bypassing CSP. | ||
| {"javascript scheme", "javascript:alert(origin)", "/"}, | ||
| {"nested javascript scheme", "javascript:javascript:javascript:alert(origin)", "/"}, | ||
| {"data scheme", "data:text/html,<script>alert(origin)</script>", "/"}, | ||
|
aslilac marked this conversation as resolved.
|
||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| t.Parallel() | ||
| if got := httpapi.SafeRedirectPath(tt.in); got != tt.want { | ||
| t.Errorf("SafeRedirectPath(%q) = %q, want %q", tt.in, got, tt.want) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
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
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.