Repository navigation
send -H cookies once on same-host redirects #2697
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
Open
Mzack9999
wants to merge
5
commits into
dev
Choose a base branch
from
redirect-credential-scope
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+230
−6
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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,151 @@ | ||
| package runner | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io" | ||
| "net" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| "sync" | ||
| "testing" | ||
|
|
||
| "github.com/projectdiscovery/goflags" | ||
| "github.com/projectdiscovery/httpx/common/customheader" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| type headerLog struct { | ||
| mu sync.Mutex | ||
| headers map[string]http.Header | ||
| } | ||
|
|
||
| func (l *headerLog) record(path string, h http.Header) { | ||
| l.mu.Lock() | ||
| defer l.mu.Unlock() | ||
| if l.headers == nil { | ||
| l.headers = make(map[string]http.Header) | ||
| } | ||
| l.headers[path] = h.Clone() | ||
| } | ||
|
|
||
| func (l *headerLog) get(path string) http.Header { | ||
| l.mu.Lock() | ||
| defer l.mu.Unlock() | ||
| return l.headers[path] | ||
| } | ||
|
|
||
| // TestRedirectCredentialsStayOnHost checks that -fr does not send -H cookies | ||
| // or secret-file headers to another host, and keeps them on the same host. | ||
| func TestRedirectCredentialsStayOnHost(t *testing.T) { | ||
| log := &headerLog{} | ||
|
|
||
| // [::1] is another host than the 127.0.0.1 target and needs no name resolution | ||
| ln, err := net.Listen("tcp6", "[::1]:0") | ||
| if err != nil { | ||
| t.Skipf("ipv6 loopback is not available: %v", err) | ||
| } | ||
| var targetURL string | ||
| third := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| if r.URL.Path == "/to-target" { | ||
| http.Redirect(w, r, targetURL+"/inbound", http.StatusFound) | ||
| return | ||
| } | ||
| log.record("third"+r.URL.Path, r.Header) | ||
| _, _ = io.WriteString(w, "third") | ||
| })) | ||
| _ = third.Listener.Close() | ||
| third.Listener = ln | ||
| third.Start() | ||
| defer third.Close() | ||
| thirdURL := third.URL | ||
|
|
||
| mux := http.NewServeMux() | ||
| mux.HandleFunc("/away", func(w http.ResponseWriter, r *http.Request) { | ||
| http.Redirect(w, r, thirdURL+"/landing", http.StatusFound) | ||
| }) | ||
| mux.HandleFunc("/stay", func(w http.ResponseWriter, r *http.Request) { | ||
| http.Redirect(w, r, "/final", http.StatusFound) | ||
| }) | ||
| mux.HandleFunc("/final", func(w http.ResponseWriter, r *http.Request) { | ||
| log.record("target/final", r.Header) | ||
| _, _ = io.WriteString(w, "final") | ||
| }) | ||
| mux.HandleFunc("/inbound", func(w http.ResponseWriter, r *http.Request) { | ||
| log.record("target/inbound", r.Header) | ||
| _, _ = io.WriteString(w, "inbound") | ||
| }) | ||
| target := httptest.NewServer(mux) | ||
| defer target.Close() | ||
| targetURL = target.URL | ||
|
|
||
| targetHost := strings.TrimPrefix(target.URL, "http://") | ||
| secrets := filepath.Join(t.TempDir(), "secrets.yaml") | ||
| require.NoError(t, os.WriteFile(secrets, []byte(fmt.Sprintf(`id: test | ||
| info: | ||
| name: test | ||
| static: | ||
| - type: Header | ||
| domains: | ||
| - %s | ||
| headers: | ||
| - key: X-Api-Key | ||
| value: SECRET-SF-KEY | ||
| - type: Cookie | ||
| domains: | ||
| - %s | ||
| cookies: | ||
| - key: sfsess | ||
| value: SECRET-SF-COOKIE | ||
| `, targetHost, targetHost)), 0o600)) | ||
|
|
||
| var mu sync.Mutex | ||
| var results []Result | ||
| options := &Options{ | ||
| Methods: "GET", | ||
| InputTargetHost: goflags.StringSlice{target.URL + "/away", target.URL + "/stay", thirdURL + "/to-target"}, | ||
| FollowRedirects: true, | ||
| MaxRedirects: 10, | ||
| SecretFile: secrets, | ||
| CustomHeaders: customheader.CustomHeaders{"Cookie: sess=SECRET-H-COOKIE"}, | ||
| Threads: 1, | ||
| Timeout: 5, | ||
| OnResult: func(r Result) { | ||
| mu.Lock() | ||
| results = append(results, r) | ||
| mu.Unlock() | ||
| }, | ||
| } | ||
| require.NoError(t, options.ValidateOptions()) | ||
| r, err := New(options) | ||
| require.NoError(t, err) | ||
| r.RunEnumeration() | ||
| r.Close() | ||
|
|
||
| for _, res := range results { | ||
| require.NoError(t, res.Err, res.Input) | ||
| } | ||
|
|
||
| got := log.get("third/landing") | ||
| require.NotNil(t, got, "redirect to the other host was not followed") | ||
| require.Empty(t, got.Get("Cookie"), "-H cookie sent to another host") | ||
| require.Empty(t, got.Get("X-Api-Key"), "secret-file header sent to a host it is not scoped to") | ||
|
|
||
| got = log.get("target/final") | ||
| require.NotNil(t, got, "same-host redirect was not followed") | ||
| require.Equal(t, "SECRET-SF-KEY", got.Get("X-Api-Key")) | ||
| cookies := (&http.Request{Header: got}).Cookies() | ||
| counts := map[string]int{} | ||
| for _, c := range cookies { | ||
| counts[c.Name]++ | ||
| } | ||
| require.Equal(t, 1, counts["sess"], "-H cookie on a same-host redirect: %v", got.Values("Cookie")) | ||
| require.Equal(t, 1, counts["sfsess"], "secret-file cookie on a same-host redirect: %v", got.Values("Cookie")) | ||
|
|
||
| // the first URL matches no secret, the redirect host does | ||
| got = log.get("target/inbound") | ||
| require.NotNil(t, got, "redirect into the secret host was not followed") | ||
| require.Equal(t, "SECRET-SF-KEY", got.Get("X-Api-Key"), "secret-file header missing after a redirect into its host") | ||
| } |
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.