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

Path matching with URLs ending in // #1688

Closed
h3kker opened this issue Nov 1, 2019 · 4 comments · Fixed by #1694
Closed

Path matching with URLs ending in // #1688

h3kker opened this issue Nov 1, 2019 · 4 comments · Fixed by #1694
Labels
Milestone

Comments

@h3kker
Copy link

h3kker commented Nov 1, 2019

I can't seem to get flask to match URLs ending in double slashes. I have a client app (that I don't control) that under some circumstances sends requests like this: /v1/collections//

Expected Behavior

Output "found double"

from flask import Flask
app = Flask(__name__)

@app.route('/test//')
def double(other):
    return 'found double'

Actual Behavior

curl localhost:5000/test//
# 404 not found

I have also tried with paths:

@app.route('/test/<path:other>')
def double(other):
    return 'found double'

but no luck either.

Environment

$ flask --version
Python 3.7.4
Flask 1.1.1
Werkzeug 0.16.0
@bolivierjr
Copy link

I can't seem to find a way, even with a catch-all, to hit a double slashed ended uri. Seems like you typically need a rewrite rule to get rid of the trailing slashes. One solution would be to put an nginx reverse proxy in front of the flask app with a rewrite in the config. e.g. rewrite ^/test/(.*)$ /$1 break; or ^test(/.*)$

@h3kker
Copy link
Author

h3kker commented Nov 8, 2019

Wouldn't you consider this a bug? After all I want my route to match a certain pattern and something is preventing that. At the least I think it is unexpected behaviour.

Especially since internal double slashes work (@app.route('/test//hase'))

Rewriting in an upstream proxy is some pain. Then my development environment behaves differently and I cannot unit-test those particular routes.

If there's really no way to match that, can I somehow rewrite the URL in flask before the path matching happens?

Thanks,
Heinz

@qdzzyb2014
Copy link

qdzzyb2014 commented Nov 29, 2019

try this

from flask import Flask
from werkzeug.routing import PathConverter

app = Flask(__name__)


class EverythingConverter(PathConverter):
    regex = '.*?'


app.url_map.converters['everything'] = EverythingConverter


@app.route('/test/<everything:other>')
def double(other):
    print(f'other: {other}')
    return 'found double'

https://stackoverflow.com/questions/24000729/flask-route-using-path-with-leading-slash

@davidism davidism transferred this issue from pallets/flask Dec 29, 2019
@davidism
Copy link
Member

davidism commented Jan 7, 2020

We just added a merge_slashes feature that merges consecutive slashes in URLs and redirects to the canonical location. I've extended that to include trailing slashes.

You still might have issues that Werkzeug can't address. Consecutive slashes in URLs aren't usually correct, and you should open a bug report against the client producing those URLs. Since merge_slashes issues a redirect rather than silently matching, the client will still need to be good enough to follow redirects when making requests. Many HTTP servers will merge slashes before they even get to the application, so you will probably not see this issue in production anyway.

@davidism davidism added this to the 1.0.0 milestone Jan 7, 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 a pull request may close this issue.

4 participants