-
Notifications
You must be signed in to change notification settings - Fork 4.6k
fix path cleaning of proxied urls #22671
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
sanikachavan5
merged 5 commits into
main
from
sanikachavan5/SECVULN-25362-fix-path-cleaning-of-proxied-urls
Sep 22, 2025
+267
−10
Merged
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| ```release-note:security | ||
| security: Fixed proxied URL path validation to prevent path traversal. | ||
| ``` |
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 |
|---|---|---|
|
|
@@ -813,20 +813,34 @@ func (s *HTTPHandlers) UIMetricsProxy(resp http.ResponseWriter, req *http.Reques | |
| // Replace prefix in the path | ||
| subPath := strings.TrimPrefix(req.URL.Path, "/v1/internal/ui/metrics-proxy") | ||
|
|
||
| // Append that to the BaseURL (which might contain a path prefix component) | ||
| newURL := cfg.BaseURL + subPath | ||
| // This prevents path traversal while preserving URL structure | ||
| cleanedSubPath := path.Clean(subPath) | ||
|
|
||
| // Clean the base URL for security in logging and comparisons | ||
| cleanedBaseURL := cfg.BaseURL | ||
| if parsedBase, err := url.Parse(cfg.BaseURL); err == nil && parsedBase.Path != "" { | ||
| parsedBase.Path = path.Clean(parsedBase.Path) | ||
| cleanedBaseURL = parsedBase.String() | ||
| } | ||
|
|
||
| // Parse the base URL to get its components | ||
| baseURL, err := url.Parse(cfg.BaseURL) | ||
| if err != nil { | ||
| log.Error("couldn't parse base URL", "base_url", cleanedBaseURL) | ||
| return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: "Invalid base URL."} | ||
| } | ||
|
|
||
| // Join the base path with the cleaned subpath using proper URL path joining | ||
| baseURL.Path = path.Join(baseURL.Path, cleanedSubPath) | ||
| newURL := baseURL.String() | ||
|
|
||
| // Parse it into a new URL | ||
| u, err := url.Parse(newURL) | ||
| if err != nil { | ||
| log.Error("couldn't parse target URL", "base_url", cfg.BaseURL, "path", subPath) | ||
| log.Error("couldn't parse target URL", "base_url", cleanedBaseURL, "path", subPath) | ||
| return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: "Invalid path."} | ||
| } | ||
|
|
||
| // Clean the new URL path to prevent path traversal attacks and remove any | ||
| // double slashes etc. | ||
| u.Path = path.Clean(u.Path) | ||
|
|
||
| if len(cfg.PathAllowlist) > 0 { | ||
| // This could be done better with a map, but for the prometheus default | ||
| // integration this list has two items in it, so the straight iteration | ||
|
|
@@ -840,7 +854,7 @@ func (s *HTTPHandlers) UIMetricsProxy(resp http.ResponseWriter, req *http.Reques | |
| } | ||
| if denied { | ||
| log.Error("target URL path is not allowed", | ||
| "base_url", cfg.BaseURL, | ||
| "base_url", cleanedBaseURL, | ||
| "path", subPath, | ||
| "target_url", u.String(), | ||
| "path_allowlist", cfg.PathAllowlist, | ||
|
|
@@ -863,9 +877,18 @@ func (s *HTTPHandlers) UIMetricsProxy(resp http.ResponseWriter, req *http.Reques | |
| // hit this handler. Any /../ that are far enough into the path to hit this | ||
| // handler, can't backtrack far enough to eat into the BaseURL either. But we | ||
| // leave this in anyway in case something changes in the future. | ||
| if !strings.HasPrefix(u.String(), cfg.BaseURL) { | ||
|
|
||
|
Contributor
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. can we add UT for this |
||
| // Security fix: Ensure BaseURL has trailing "/" for proper prefix checking | ||
dduzgun-security marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| baseURLForPrefix := cfg.BaseURL | ||
| if !strings.HasSuffix(baseURLForPrefix, "/") { | ||
| baseURLForPrefix += "/" | ||
| } | ||
|
|
||
| targetURL := u.String() | ||
| // Allow exact match of BaseURL (without trailing slash) or proper prefix match | ||
| if targetURL != cfg.BaseURL && !strings.HasPrefix(targetURL, baseURLForPrefix) { | ||
dduzgun-security marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| log.Error("target URL escaped from base path", | ||
| "base_url", cfg.BaseURL, | ||
| "base_url", cleanedBaseURL, | ||
| "path", subPath, | ||
| "target_url", u.String(), | ||
| ) | ||
|
|
||
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.