Repository navigation
fix(cli): refuse cross-origin redirects so credentials stay with the configured server #137
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
5 commits
Select commit
Hold shift + click to select a range
1ec4edc
fix(cli): refuse cross-origin redirects so credentials stay with the …
TristanSpeakEasy 9809f79
fix(cli): treat explicit default ports as the same redirect origin
TristanSpeakEasy 14793e0
fix(cli): refuse redirects that change the request method and classif…
TristanSpeakEasy 8c620c1
Merge remote-tracking branch 'origin/main' into fix/cli-same-origin-r…
TristanSpeakEasy ebe1163
fix(cli): compare redirect hosts with ASCII-only case folding and red…
TristanSpeakEasy 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| id: 1792715944-523a54e4 | ||
| features: | ||
| - core | ||
| targets: | ||
| - cli | ||
| type: fix | ||
| bump: patch | ||
| description: refuse cross-origin redirects so credentials are only sent to the configured server | ||
| author: TristanSpeakEasy | ||
| date: "2026-10-23" |
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
203 changes: 203 additions & 0 deletions
203
templates/templates/cli/auxiliary/internal/client/client_test.go.stmpl
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,203 @@ | ||
| package client | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "io" | ||
| "net" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/spf13/cobra" | ||
|
|
||
| "{{getGolangPackage}}/internal/output" | ||
| ) | ||
|
|
||
| func TestSameOriginRedirect(t *testing.T) { | ||
| var leakedKey, landedMethod, landedBody string | ||
| other := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| leakedKey = r.Header.Get("x-api-key") | ||
| w.WriteHeader(http.StatusOK) | ||
| })) | ||
| defer other.Close() | ||
|
|
||
| origin := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| switch r.URL.Path { | ||
| case "/cross": | ||
| http.Redirect(w, r, other.URL+"/landed", http.StatusTemporaryRedirect) | ||
|
TristanSpeakEasy marked this conversation as resolved.
|
||
| case "/same": | ||
| http.Redirect(w, r, "/landed", http.StatusTemporaryRedirect) | ||
| case "/convert": | ||
| http.Redirect(w, r, "/landed", http.StatusSeeOther) | ||
| default: | ||
| body, _ := io.ReadAll(r.Body) | ||
| landedMethod, landedBody = r.Method, string(body) | ||
| w.WriteHeader(http.StatusOK) | ||
| } | ||
| })) | ||
| defer origin.Close() | ||
|
|
||
| httpClient := &http.Client{CheckRedirect: SameOriginRedirect} | ||
| do := func(path string) (*http.Response, error) { | ||
| req, err := http.NewRequest(http.MethodPost, origin.URL+path, strings.NewReader("{}")) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| req.Header.Set("x-api-key", "secret") | ||
| return httpClient.Do(req) | ||
| } | ||
|
|
||
| res, err := do("/same") | ||
| if err != nil { | ||
| t.Fatalf("same-origin redirect: %v", err) | ||
| } | ||
| _ = res.Body.Close() | ||
| if res.StatusCode != http.StatusOK { | ||
| t.Fatalf("same-origin redirect status = %d", res.StatusCode) | ||
| } | ||
| if landedMethod != http.MethodPost || landedBody != "{}" { | ||
| t.Fatalf("307 redirect landed as %s with body %q", landedMethod, landedBody) | ||
| } | ||
|
|
||
| landedMethod = "" | ||
| _, err = do("/convert") | ||
| if err == nil || !strings.Contains(err.Error(), "the server tried to convert the request") { | ||
| t.Fatalf("method-changing redirect error = %v", err) | ||
| } | ||
| if landedMethod != "" { | ||
| t.Fatalf("method-changing redirect reached the target as %s", landedMethod) | ||
| } | ||
|
|
||
| _, err = do("/cross") | ||
| if err == nil || !strings.Contains(err.Error(), "refusing to follow a redirect") { | ||
| t.Fatalf("cross-origin redirect error = %v", err) | ||
| } | ||
| if leakedKey != "" { | ||
| t.Fatalf("credential header reached the redirect target: %q", leakedKey) | ||
| } | ||
| var refused *RedirectRefusedError | ||
| if !errors.As(err, &refused) { | ||
| t.Fatalf("cross-origin redirect error is %T, want *RedirectRefusedError", err) | ||
| } | ||
|
|
||
| c := output.Classify(&cobra.Command{}, err) | ||
| if c.Type != output.ErrorTypeProtocol { | ||
| t.Fatalf("refused redirect classified as %s", c.Type) | ||
| } | ||
| for _, hint := range c.Hints { | ||
| if strings.Contains(hint, "network connectivity") { | ||
| t.Fatalf("refused redirect carries connection hints: %v", c.Hints) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestSameOriginRedirectOrigins(t *testing.T) { | ||
| for _, tc := range []struct { | ||
| from, to string | ||
| allowed bool | ||
| }{ | ||
| {"https://api.example.com/a", "https://api.example.com/b", true}, | ||
| {"https://api.example.com/a", "https://API.example.com:443/b", true}, | ||
| {"http://api.example.com:80/a", "http://api.example.com/b", true}, | ||
| {"https://api.example.com/a", "http://api.example.com/b", false}, | ||
| {"https://api.example.com/a", "https://api.example.com:8443/b", false}, | ||
| {"https://api.example.com/a", "https://other.example.com/b", false}, | ||
| {"http://i.example/a", "http://\u0130.example/b", false}, | ||
| {"http://\u0130.example/a", "http://\u0130.example/b", true}, | ||
| } { | ||
| from, err := http.NewRequest(http.MethodGet, tc.from, nil) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| to, err := http.NewRequest(http.MethodGet, tc.to, nil) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| err = SameOriginRedirect(to, []*http.Request{from}) | ||
| if tc.allowed && err != nil { | ||
| t.Errorf("%s -> %s: unexpected error %v", tc.from, tc.to, err) | ||
| } | ||
| if !tc.allowed && err == nil { | ||
| t.Errorf("%s -> %s: redirect was allowed", tc.from, tc.to) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestSameOriginRedirectNeverDialsIDNAVariant(t *testing.T) { | ||
| server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| if r.URL.Path == "/start" { | ||
| w.Header()["Location"] = []string{"http://\u0130.example/landed"} | ||
| w.WriteHeader(http.StatusTemporaryRedirect) | ||
| return | ||
| } | ||
| w.WriteHeader(http.StatusOK) | ||
| })) | ||
| defer server.Close() | ||
|
|
||
| var dialed []string | ||
| dialer := &net.Dialer{} | ||
| transport := &http.Transport{DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { | ||
| dialed = append(dialed, addr) | ||
| return dialer.DialContext(ctx, network, server.Listener.Addr().String()) | ||
| }} | ||
| defer transport.CloseIdleConnections() | ||
| httpClient := &redirectGuardClient{&http.Client{Transport: transport, CheckRedirect: SameOriginRedirect}} | ||
|
|
||
| req, err := http.NewRequest(http.MethodPost, "http://i.example/start", strings.NewReader("{}")) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| req.Header.Set("x-api-key", "secret") | ||
| _, err = httpClient.Do(req) | ||
| var refused *RedirectRefusedError | ||
| if !errors.As(err, &refused) { | ||
| t.Fatalf("redirect to an IDNA variant host was not refused: %v", err) | ||
| } | ||
| if len(dialed) != 1 || dialed[0] != "i.example:80" { | ||
| t.Fatalf("dialed %v, want only i.example:80", dialed) | ||
| } | ||
| } | ||
|
|
||
| func TestRefusedRedirectRedactsLocation(t *testing.T) { | ||
| const secret = "synthetic-secret" | ||
| server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| if r.URL.Path == "/convert" { | ||
| http.Redirect(w, r, "https://user:"+secret+"@"+r.Host+"/landed?access_token="+secret+"#"+secret, http.StatusSeeOther) | ||
| return | ||
| } | ||
| http.Redirect(w, r, "/landed?access_token="+secret+"#"+secret, http.StatusSeeOther) | ||
| })) | ||
| defer server.Close() | ||
|
|
||
| for _, path := range []string{"/same-origin", "/convert"} { | ||
| var debug bytes.Buffer | ||
| httpClient := &DebugClient{ | ||
| Inner: &redirectGuardClient{&http.Client{CheckRedirect: SameOriginRedirect}}, | ||
| Stderr: &debug, | ||
| } | ||
| req, err := http.NewRequest(http.MethodPost, server.URL+path, strings.NewReader("{}")) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| _, err = httpClient.Do(req) | ||
| var refused *RedirectRefusedError | ||
| if !errors.As(err, &refused) { | ||
| t.Fatalf("%s: redirect was not refused: %v", path, err) | ||
| } | ||
| wrapped := fmt.Errorf("error sending request: %w", err) | ||
| c := output.Classify(&cobra.Command{}, wrapped) | ||
| for name, text := range map[string]string{ | ||
| "error": wrapped.Error(), | ||
| "classification": fmt.Sprintf("%+v", c), | ||
| "debug": debug.String(), | ||
| } { | ||
| if strings.Contains(text, secret) { | ||
| t.Errorf("%s: %s output contains the redirect secret: %s", path, name, text) | ||
| } | ||
| } | ||
| } | ||
| } | ||
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
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.