Routes without trailing slash cause 307
redirect which breaks reverse proxies via paths
#327
Closed
3 tasks done
Labels
bug:functional
Functional defects resulting from feature changes.
released
This issue/pull request has been released.
type:bug
Defects in shipped code and fixes for those defects
Context
Depending on whether a route in FastAPI is defined with or without a trailing slash, FastAPI by default tries to automatically redirect the alternative to the URL with the
/
added or removed (see examples below).Right now:
curl -L
):307
)307
)Some consequences:
my-neurobagel-node.org/api
->localhost:8000
, when we send a request tomy-neurobagel-node.org/api/query
, the redirect is followed leading tomy-neurobagel-node.org/query
, which does not exist from the perspective of the proxyhttps://
, this can become lost and turn intohttp://
during the redirect (e.g., Trailing backslash reruns makes a redirect that looses https. fastapi/fastapi#8514)curl -v -L https://qpn.neurobagel.org/query
), possibly because (1) the Dockerized NGINX config which forwards headers by default, and (2) the NGINX container is on the same Docker network as the Uvicorn server, there might be an implicit trust mechanism between IPs on the same networkThis hasn't been an issue for us in the past because:
-L
(or, we have been using it pretty liberally)Rather than the current redirect behaviour (which is also less efficient due to multiple round trips), we probably want one of the following outcomes:
Option 1:
(can be achieved via two route decorators)
Option 2:
(can be achieved w/ the
redirect_slashes
parameter of the FastAPI and/or APIRouter class)Decisions
redirect_slashes
globally (option 2) for more predictable behaviour--forwarded-allow-ips=*
flag as recommended in Why FastAPI redirects url with a trailing slash instead of resolving it internally? fastapi/fastapi#9328 (comment) to ensure we don't lose HTTPS behind a proxy (this shouldn't happen anymore now that we're getting rid of redirects, but might be a good safeguard regardless)Relevant issues:
The text was updated successfully, but these errors were encountered: