Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix: handle urls with multiple slashes#16527

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
mtojek merged 6 commits intomainfrom9877-fix
Feb 12, 2025
Merged

fix: handle urls with multiple slashes#16527

mtojek merged 6 commits intomainfrom9877-fix
Feb 12, 2025

Conversation

mtojek
Copy link
Member

@mtojekmtojek commentedFeb 11, 2025
edited
Loading

Fixes:#9877

This PR introduces another middleware to rewrite URLs when multiple slashes are used.

Testing:

curl http://localhost:8080//api///v2/buildinfocurl http://localhost:8080/api//v2/buildinfocurl http://localhost:3000//api/////v2curl http://localhost:3000/api//v2

@mtojekmtojek self-assigned thisFeb 11, 2025
@mtojekmtojek changed the titlefix: rewrite urls with multiple slashesfix: handle urls with multiple slashesFeb 11, 2025
@mtojekmtojek marked this pull request as ready for reviewFebruary 11, 2025 16:11
Copy link
Member

@johnstcnjohnstcn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

LGTM, but I'd like to get an OK from@mafredri as well. Multiple slashes istechnically valid in the case of/api/v2/something/$FOO/bar if$FOO can be an empty string. I don't see us needing this in the future, but I'm also no soothsayer.

}

// Normalize multiple slashes to a single slash
newPath := multipleSlashesRe.ReplaceAllString(path, "/")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

nit, non-blocking: I wonder what the cost of a regex replace is versus iterating over the string once?

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I had that thought to fiddle with chars, but blindly assumed that regexp is safer and easier for devs to comprehend. Thanks for the comment anyway!

if rctx != nil {
rctx.RoutePath = newPath
}
r.URL.Path = newPath
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

In some cases we may overwrite RoutePath with Path, and vice-versa. Depending on which is missing. Is there any risk with this approach?

Looking at existing middleware, they seem to take a conditional approach for the assignment of both values:https://github.com/go-chi/chi/blob/e846b8304c769c4f1a51c9de06bebfaa4576bd88/middleware/strip.go#L24-L28

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I don't think it is a great deal, but let's keep it consistent :) Thanks for raising this

@mafredri
Copy link
Member

Multiple slashes istechnically valid in the case of/api/v2/something/$FOO/bar if$FOO can be an empty string. I don't see us needing this in the future, but I'm also no soothsayer.

This is a good point@johnstcn, I strongly hope we will never end up there as it's a use-case that's better not being supported IMO 😄

@mtojekmtojek merged commitb396408 intomainFeb 12, 2025
31 of 33 checks passed
@mtojekmtojek deleted the 9877-fix branchFebruary 12, 2025 08:23
@github-actionsgithub-actionsbot locked and limited conversation to collaboratorsFeb 12, 2025
Sign up for freeto subscribe to this conversation on GitHub. Already have an account?Sign in.
Reviewers

@mafredrimafredrimafredri approved these changes

@johnstcnjohnstcnjohnstcn approved these changes

Assignees

@mtojekmtojek

Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

Using double forward slash in URL returns HTML instead of performing an API request
3 participants
@mtojek@mafredri@johnstcn

[8]ページ先頭

©2009-2025 Movatter.jp