Skip to content

Commit

Permalink
martian(header): do not add X-Forwarded headers for CONNECT requests
Browse files Browse the repository at this point in the history
  • Loading branch information
mmatczuk committed Apr 30, 2024
1 parent 85c6261 commit 86c2928
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
4 changes: 4 additions & 0 deletions internal/martian/header/forwarded_modifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ import (
func NewForwardedModifier() martian.RequestModifier {
return martian.RequestModifierFunc(
func(req *http.Request) error {
if req.Method == http.MethodConnect {
return nil
}

if v := req.Header.Get("X-Forwarded-Proto"); v == "" {
req.Header.Set("X-Forwarded-Proto", req.URL.Scheme)
}
Expand Down
19 changes: 19 additions & 0 deletions internal/martian/header/forwarded_modifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,23 @@ func TestSetForwardHeaders(t *testing.T) {
if got, want := req.Header.Get(xfu), "https://preserved.host.com/foo?x=y"; got != want {
t.Errorf("req.Header.Get(%q): got %q, want %q", xfh, got, want)
}

// Test that the modifier does not set headers for CONNECT requests.
req, err = http.NewRequest(http.MethodConnect, "http://martian.local", http.NoBody)
if err != nil {
t.Fatalf("http.NewRequest(): got %v, want no error", err)
}

if got, want := req.Header.Get(xfp), ""; got != want {
t.Errorf("req.Header.Get(%q): got %q, want %q", xfp, got, want)
}
if got, want := req.Header.Get(xff), ""; got != want {
t.Errorf("req.Header.Get(%q): got %q, want %q", xff, got, want)
}
if got, want := req.Header.Get(xfh), ""; got != want {
t.Errorf("req.Header.Get(%q): got %q, want %q", xfh, got, want)
}
if got, want := req.Header.Get(xfu), ""; got != want {
t.Errorf("req.Header.Get(%q): got %q, want %q", xfh, got, want)
}
}

0 comments on commit 86c2928

Please sign in to comment.