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

Location returned by redirect_slashes should be relative instead of absolute #2748

Open
rijenkii opened this issue Nov 11, 2024 Discussed in #2651 · 0 comments
Open

Location returned by redirect_slashes should be relative instead of absolute #2748

rijenkii opened this issue Nov 11, 2024 Discussed in #2651 · 0 comments

Comments

@rijenkii
Copy link
Contributor

rijenkii commented Nov 11, 2024

Discussed in #2651

Originally posted by rijenkii July 23, 2024
I have a following setup:

Browser --(https)--> Nginx --(http)--> FastAPI/Starlette

When a browser accidentally calls an endpoint without an ending slash, Starlette responds with a 307 with Location: http://example.org/api/endpoint/, which browser rightfully rejects because of mixed content blocking.

If Starlette responded with Location: /api/endpoint/ instead, browser would then just slap that relative location to the current host.

Django does it like that, but they respond with 301s:

> GET /api/endpoint HTTP/1.1

< HTTP/1.1 301 Moved Permanently
< location: /api/endpoint/

Related code:

if scope["type"] == "http" and self.redirect_slashes and route_path != "/":
redirect_scope = dict(scope)
if route_path.endswith("/"):
redirect_scope["path"] = redirect_scope["path"].rstrip("/")
else:
redirect_scope["path"] = redirect_scope["path"] + "/"
for route in self.routes:
match, child_scope = route.matches(redirect_scope)
if match != Match.NONE:
redirect_url = URL(scope=redirect_scope)
response = RedirectResponse(url=str(redirect_url))
await response(scope, receive, send)
return

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant