-
Notifications
You must be signed in to change notification settings - Fork 3.2k
fix(httpclientpool): rebuild malformed Location URL #5902
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
ehsandeep
merged 2 commits into
dev
from
dwisiswant0/fix/httpclientpool/rebuild-malformed-location-URL
Dec 19, 2024
Merged
Changes from all commits
Commits
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 |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ package httpclientpool | |
| import ( | ||
| "context" | ||
| "crypto/tls" | ||
| "fmt" | ||
| "net" | ||
| "net/http" | ||
| "net/http/cookiejar" | ||
|
|
@@ -25,6 +26,7 @@ import ( | |
| "github.com/projectdiscovery/rawhttp" | ||
| "github.com/projectdiscovery/retryablehttp-go" | ||
| mapsutil "github.com/projectdiscovery/utils/maps" | ||
| urlutil "github.com/projectdiscovery/utils/url" | ||
| ) | ||
|
|
||
| var ( | ||
|
|
@@ -377,7 +379,7 @@ func makeCheckRedirectFunc(redirectType RedirectFlow, maxRedirects int) checkRed | |
| } | ||
| } | ||
|
|
||
| func checkMaxRedirects(_ *http.Request, via []*http.Request, maxRedirects int) error { | ||
| func checkMaxRedirects(req *http.Request, via []*http.Request, maxRedirects int) error { | ||
| if maxRedirects == 0 { | ||
| if len(via) > defaultMaxRedirects { | ||
| return http.ErrUseLastResponse | ||
|
|
@@ -388,5 +390,29 @@ func checkMaxRedirects(_ *http.Request, via []*http.Request, maxRedirects int) e | |
| if len(via) > maxRedirects { | ||
| return http.ErrUseLastResponse | ||
| } | ||
|
|
||
| // NOTE(dwisiswant0): rebuild request URL. See #5900. | ||
| if u := req.URL.String(); !isURLEncoded(u) { | ||
| parsed, err := urlutil.Parse(u) | ||
| if err != nil { | ||
| return fmt.Errorf("%w: %w", ErrRebuildURL, err) | ||
| } | ||
|
|
||
| req.URL = parsed.URL | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // isURLEncoded is an helper function to check if the URL is already encoded | ||
| // | ||
| // NOTE(dwisiswant0): shall we move this under `projectdiscovery/utils/urlutil`? | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ++ |
||
| func isURLEncoded(s string) bool { | ||
| decoded, err := url.QueryUnescape(s) | ||
| if err != nil { | ||
| // If decoding fails, it may indicate a malformed URL/invalid encoding. | ||
| return false | ||
| } | ||
|
|
||
| return decoded != s | ||
| } | ||
dwisiswant0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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,7 @@ | ||
| package httpclientpool | ||
|
|
||
| import "errors" | ||
|
|
||
| var ( | ||
| ErrRebuildURL = errors.New("could not rebuild request URL") | ||
| ) |
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.
🛠️ Refactor suggestion
Enhance error handling and URL validation
The URL rebuilding logic could be improved in several ways:
Consider this enhanced implementation:
// NOTE(dwisiswant0): rebuild request URL. See #5900. if u := req.URL.String(); !isURLEncoded(u) { parsed, err := urlutil.Parse(u) if err != nil { - return fmt.Errorf("%w: %w", ErrRebuildURL, err) + return fmt.Errorf("failed to rebuild URL %q: %w", u, ErrRebuildURL) } + + // Validate the rebuilt URL + if parsed.URL.Host == "" || parsed.URL.Scheme == "" { + return fmt.Errorf("invalid URL after rebuild %q: missing host or scheme", parsed.URL) + } req.URL = parsed.URL }📝 Committable suggestion