You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add support for the X-Forwarded-Prefix for improved support for reverse proxies. Redirects should always be relative to X-Forwarded-Prefix, not only in RedirectTrailingSlash.
Description
Redirects (and maybe other response path related functions) do not respect the X-Forwarded-Prefix header. This non-standard HTTP header allows applications to be proxied under a sub-URL. For example a user-facing application may be composed of multiple services (e.g. payment, order) using gin. Requests are proxied by a reverse proxy. Both services respond to API-calls like POST /api/v1/order or GET /api/v1/payment/.... The reverse proxy delegates the requests to the respective service:
/static/pic.png -> serve static/pic.png
/orders/api/v1/order -> redirect request to order service: /api/v1/order, X-Forwarded-Prefix: /orders
/payments/api/v1/payment -> redirect request to payment service: /api/v1/payment, X-Forwarded-Prefix: /payments
The header should be respected in HTTP responses containing the path. A redirect should be relative to the X-Forwarded-Prefix header.
How to reproduce
The endpoints /apple and /banana redirect to /orange. /apple does not respect the X-Forwarded-Prefix header, /banana does.
TL;DR
Add support for the
X-Forwarded-Prefix
for improved support for reverse proxies. Redirects should always be relative toX-Forwarded-Prefix
, not only inRedirectTrailingSlash
.Description
Redirects (and maybe other response path related functions) do not respect the
X-Forwarded-Prefix
header. This non-standard HTTP header allows applications to be proxied under a sub-URL. For example a user-facing application may be composed of multiple services (e.g.payment
,order
) using gin. Requests are proxied by a reverse proxy. Both services respond to API-calls likePOST /api/v1/order
orGET /api/v1/payment/...
. The reverse proxy delegates the requests to the respective service:/static/pic.png
-> servestatic/pic.png
/orders/api/v1/order
-> redirect request to order service:/api/v1/order
,X-Forwarded-Prefix: /orders
/payments/api/v1/payment
-> redirect request to payment service:/api/v1/payment
,X-Forwarded-Prefix: /payments
The header should be respected in HTTP responses containing the path. A redirect should be relative to the
X-Forwarded-Prefix
header.How to reproduce
The endpoints
/apple
and/banana
redirect to/orange
./apple
does not respect theX-Forwarded-Prefix
header,/banana
does.Expectations (Banana)
Redirect correctly is
/foo/orange
.Actual result (Apple)
Redirect should be
/foo/orange
, but is/orange
.Related Issues / PRs / Code-Snippets:
gin/gin.go
Lines 606 to 617 in eb75ce0
Environment
go version go1.17.1 linux/amd64
v1.7.4
Arch Linux
The text was updated successfully, but these errors were encountered: