Skip to content

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 12, 2025
Merged

fix: handle urls with multiple slashes #16527

merged 6 commits into from
Feb 12, 2025

Conversation

mtojek
Copy link
Member

@mtojek mtojek commented Feb 11, 2025

Fixes: #9877

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

Testing:

curl http://localhost:8080//api///v2/buildinfo
curl http://localhost:8080/api//v2/buildinfo
curl http://localhost:3000//api/////v2
curl http://localhost:3000/api//v2

@mtojek mtojek self-assigned this Feb 11, 2025
@mtojek mtojek changed the title fix: rewrite urls with multiple slashes fix: handle urls with multiple slashes Feb 11, 2025
@mtojek mtojek requested review from johnstcn and mafredri February 11, 2025 16:11
@mtojek mtojek marked this pull request as ready for review February 11, 2025 16:11
Copy link
Member

@johnstcn johnstcn 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 is technically 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
Member Author

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!

coderd/coderd.go Outdated
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
Member Author

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 is technically 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 😄

@mtojek mtojek merged commit b396408 into main Feb 12, 2025
31 of 33 checks passed
@mtojek mtojek deleted the 9877-fix branch February 12, 2025 08:23
@github-actions github-actions bot locked and limited conversation to collaborators Feb 12, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
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