RedirectResponse call to `quote_plus` replaces spaces & `%20`'s with `+` character
See original GitHub issueChecklist
- The bug is reproducible against the latest release and/or
master
. - There are no similar issues or pull requests to fix it yet.
Describe the bug
RedirectResponse
replaces url paths with spaces or %20
with the +
character. This happens as the location
header is written after passing the url to quote_plus
: https://github.com/encode/starlette/blob/master/starlette/responses.py#L181
If part of the path was a path param (with a space in it), this then modified those path params.
We currently encounter this with trailing slash redirects, which return a RedirectResponse.
To reproduce
from starlette.applications import Starlette
from starlette.responses import JSONResponse
from starlette.routing import Route
async def homepage(request):
name = request.path_params["name"]
return JSONResponse({"hello": name})
app = Starlette(routes=[Route("/{name}/greeting", homepage)])
from starlette.testclient import TestClient
client = TestClient(app)
def test_redirect_homepage():
name = "bob%20ross"
url = f"{name}/greeting"
for u in [url, url + "/"]:
resp = client.get(u)
assert url in str(resp.url)
Expected behavior
Expect that the location header would contain the url to redirect to without modification. If needed, it could replace the actual space character with %20
, instead, but it’s not clear to me that is necessary, after looking through the http specs.
This could be accomplished with swapping urllib.quote_plus
for urllib.quote
in RedirectResponse
.
Actual behavior
Spaces are being replaced with the +
character.
Environment
- OS: macOS
- Python version: 3.8.8
- Starlette version: 0.13.6 (also separately tested 0.14.2)
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:5 (5 by maintainers)
Top GitHub Comments
@falkben a pull request would be appreciated
If you’d like I can submit a Pull Request to make the url quoting the same as Django/flask?