Skip to content
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

Eliminate // in paths #1286

Merged
merged 4 commits into from
Jan 5, 2020
Merged

Eliminate // in paths #1286

merged 4 commits into from
Jan 5, 2020

Conversation

edk0
Copy link
Contributor

@edk0 edk0 commented Apr 22, 2018

Incoming paths are redirected to remove repeated slashes upon matching. Rules containing repeated slashes collapse them to just one and send a warning.

I haven't included a way to turn this off; do people have reasons to?

closes #1132

@davidism
Copy link
Member

davidism commented Apr 23, 2018

The only problematic but somewhat valid case I can think of is /scrape/https://example.com, so it might be good to be a little conservative and only merge slashes that aren't preceded by :.

There should be a merge_slashes=True attribute on Rule and Map, similar to strict_slashes, to control this behavior.

Like strict_slashes, Rule.match should raise an exception that MapAdapater handles.

I was considering only merging slashes in static parts, not variable parts, of matched URLs, but that would get really complicated. I think it's OK to just merge everything.

@davidism
Copy link
Member

Also remember to add a changelog entry and a versionchanged note to the docstring.

@edk0
Copy link
Contributor Author

edk0 commented Apr 23, 2018

Like strict_slashes, Rule.match should raise an exception that MapAdapater handles.

I was considering only merging slashes in static parts, not variable parts, of matched URLs, but that would get really complicated. I think it's OK to just merge everything.

Doing the work in Rule.match would mean each rule had to repeat the transformation, which I didn't like (I thought it would be slow, but since measured, and it isn't really), or altering the generated regexp, which might be a pain but would make it fairly easy to do the "only merge in static parts" bit.

Oh, and probably if we want to go /a//b -> /a/b/ it should take only one redirect. Hmm.

edk0 added a commit to edk0/werkzeug that referenced this pull request Apr 24, 2018
@edk0
Copy link
Contributor Author

edk0 commented Apr 24, 2018

So I've done all that (I think). The top commit makes only static parts affected—as you can see it's not a whole lot of extra work, but I'm happy to remove it if you prefer the other thing. (Personally, I like that it eliminates the special case for ://)

One thing testing this threw up is that a <path> can't start with /. With merge_slashes on, any inital slashes are eaten by the merging. Without it I think it will always 404. Anyway, it's trivial to let paths start with a slash, if you want.

@RDProjekt
Copy link

One reason for supporting the switch to turn off collapsing slashes is that Werkzeug is used by moto (https://github.com/spulec/moto), a library emulating Amazon AWS infrastructure. Since double slashes are meaningful for Amazon's servers (see e.g. getmoto/moto#1637),
unconditionally collapsing slashes may make the full emulation impossible.

Incidentally, the problem with paths starting with slashes mentioned above is also a problem for moto, so it would be great to have it fixed as well.

@davidism
Copy link
Member

@edk0 I've rebased and update the code style for this, but I can't push. Would you enable that option?

@davidism davidism force-pushed the doubleslash branch 2 times, most recently from d2ff0e0 to bafe618 Compare October 15, 2019 20:43
@davidism
Copy link
Member

@edk0 would you review this and make sure I didn't bork anything during the rebase?

@edk0
Copy link
Contributor Author

edk0 commented Oct 18, 2019

Looks good to me.

edk0 and others added 3 commits January 5, 2020 08:09
*   don't merge slashes immediately after ':'
*   don't merge slashes on construction

    We don't know whether the Map wants us to yet, so there's no ideal
    place to do it at all. The associated warning and its corresponding
    test are removed. merge_slashes is now enforced at `Rule.compile`
    time for building as well as parsing.

* merge slashes only in static parts
fix error in merge_slashes=False code
rename paths in test for clarity
@davidism davidism merged commit 55182e7 into pallets:master Jan 5, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Groups of slashes in path should be normalized and redirected
4 participants