-
Notifications
You must be signed in to change notification settings - Fork 9.2k
fix(url): handle arbitrary number of levels for relative paths #10603
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
fix(url): handle arbitrary number of levels for relative paths #10603
Conversation
0adc633 to
c3bb466
Compare
|
Can someone please take a look at this? |
|
we need this too. |
|
We stumbled upon this issue as well in a very big project, it is a very annoying problem, and the original code looks so naive with hardcoded handling of just one level. Any reason this is not being picked up? |
|
Hi @DavidVogel - thanks for contribution, @sn0wcat, @mhussein, thanks for raising this and sharing the context. We’ve added this PR to our review list with high priority, given the impact and feedback from multiple projects. We’re currently reorganizing processes and working on major updates (including the new Swagger Editor), so reviews are taking a bit longer than usual - but this one is now at the top of the queue. Appreciate your patience and input! |
glowcloud
left a comment
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.
Hi @DavidVogel,
Thanks again for addressing this issue! I think the logic could be simplified a bit - we can use a regular expression to get the relative path directly from the trimmed URL. Let me know if you see any issues with this solution.
| return `..${urlObject.pathname}${urlObject.search}${urlObject.hash}` | ||
|
|
||
| // Handle relative paths (./path, ../path, ./../../path, etc.) | ||
| if (urlTrimmed.startsWith("./") || urlTrimmed.startsWith("../")) { |
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.
| if (urlTrimmed.startsWith("./") || urlTrimmed.startsWith("../")) { | |
| if (urlTrimmed.startsWith("./") || urlTrimmed.startsWith("../")) { | |
| const relativePath = urlTrimmed.match(/^(\.\.?\/)+/)[0] | |
| const remainingPath = urlObject.pathname.substring(1) | |
| return `${relativePath}${remainingPath}${urlObject.search}${urlObject.hash}` | |
| } |
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.
@glowcloud Yes, that approach works for me.
|
@DavidVogel Thanks for the confirmation for the improvement! I created a superseding PR, which includes your changes as well: #10640. |
This pull request enhances the
sanitizeUrlutility function to handle URLs with multiple levels of relative paths (such as../../file.json), ensuring correct path reconstruction. Unit tests have also been added to verify this new behavior.Description
Improvements to URL sanitization:
sanitizeUrlinsrc/core/utils/url.jsto correctly process and reconstruct URLs with multiple../segments, supporting cases like../../file.jsonand deeper relative paths.Motivation and Context
Current method only handles up to 1 parent level. This allows an arbitrary number of levels.
Fixes #4107
How Has This Been Tested?
test/unit/core/utils.jsto verify thatsanitizeUrlcorrectly handles URLs with two or more../segments.Screenshots (if appropriate):
Checklist
My PR contains...
src/is unmodified: changes to documentation, CI, metadata, etc.)package.json)My changes...
Documentation
Automated tests