question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RedirectResponse call to `quote_plus` replaces spaces & `%20`'s with `+` character

See original GitHub issue

Checklist

  • 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:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
JayH5commented, Apr 12, 2021

@falkben a pull request would be appreciated

0reactions
falkbencommented, Apr 9, 2021

If you’d like I can submit a Pull Request to make the url quoting the same as Django/flask?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I replace all the spaces with %20 in C#? - Stack ...
UrlEncode method replaces spaces with + instead of %20. ... The below code will replace repeating space with a single %20 character.
Read more >
00marshmallow.py - gists · GitHub
:param default: since the Schema is replacing the field's default, its first argument is used. to set the default, use ellipsis (``.
Read more >
funcref_python.xml - CMU School of Computer Science
It mainly exists so that you can replace it with another function that has a compatible interface, in order to change the semantics...
Read more >
Python 2.0
If you're running Windows 95, 98, ME, NT or 2000, all you need is the Windows installer. It includes Python, Tcl/Tk, and the...
Read more >
version-control-tools: changeset 7295 ... - Mercurial
It will be # replaced during the next _get_conn() call. clean_exit = False if isinstance(e, ... Check for redirect response - if (self.urlfetch_retries...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found