Commit 78525b1
authored
fix: Ensure x-forwarded-port header is used in Forwarded header (#115)
## What I'm Changing
This PR ensures that a `x-forwarded-port` header is used when
constructing the `forwarded` header.
### Backstory
We have been experiencing an issue in a Kubernetes environment where
Links were being written with `href` values with incorrect origins. To
be specific, they would point to `localhost:8001` rather than
`localhost`, where `localhost` is the public-facing host and `:8001` is
the port of the internal upstream STAC API. The network layout was such
that we had a Traefik proxy in front of the STAC Auth Proxy. That proxy
sets the `x-forwarded-port` header on requests that were sent to the
STAC Auth Proxy. However, the request did _not_ contain a `forwarded`
header, so we would create one. The problem was that our current logic
would _not_ make use of the `x-forwarded-port` when created the
`forwarded` header. Despite us forwarding the `x-forwarded-port`, the
upstream STAC API would defer to the `forwarded` header and thus ignore
the provided `x-forwarded-port`. This meant that the upstream STAC API
would generate links for `http://localhost:8001`. Digging through
`stac-fastapi` code, we can see that the port first is read from the
`host` header and then possibly overridden by the value in `forwarded`:
https://github.com/stac-utils/stac-fastapi/blob/3b24f86bc538b8c1d6b86008845d5541f4f481e8/stac_fastapi/api/stac_fastapi/api/middleware.py#L87-L106
The current problematic flow looks something like this:
```mermaid
sequenceDiagram
autonumber
participant Client as Client
participant Traefik as Traefik Proxy
participant Auth as STAC Auth Proxy
participant STAC as STAC API
Client->>Traefik: HTTP request
Note left of Traefik: host: http://localhost
Traefik->>Auth: Forward request
Note left of Auth: host: http://localhost
Note left of Auth: x-forwarded-for: 192.168.65.1
Note left of Auth: x-forwarded-host: localhost
Note left of Auth: x-forwarded-port: 80
Note left of Auth: x-forwarded-proto: http
Note left of Auth: x-forwarded-server: 26a4cc50fa1a
Auth->>STAC: Forward request (proxied)
Note left of STAC: host: http://stac:8001
Note left of STAC: x-forwarded-for: 192.168.65.1
Note left of STAC: x-forwarded-host: localhost
Note left of STAC: x-forwarded-port: 80
Note left of STAC: x-forwarded-proto: http
Note left of STAC: x-forwarded-server: 26a4cc50fa1a
Note left of STAC: via: 1.1 stac-auth-proxy
Note left of STAC: forwarded: for=192.168.65.1 host=localhost proto=http path=/stac
STAC-->>Auth: Response document
Note left of Auth: links point to http://localhost:8001/...
Auth-->>Client: Response (unchanged body)
```
Unfortunately, our link rewriting middleware would miss these links as
it was looking for the internal upstream url `stac:8001` and not
`localhost:8001`. This is maybe besides the point, as the link rewriting
middleware is really about cleaning up paths and in an ideal world we
expect the upstream STAC API to properly make use of the `forwarded`
header to properly construct links (which stac-fastapi-pgstac does
decently well).
<details>
<summary>some more thoughts about
<code>x-forwarded-port</code></summary>
Reading the MDN docs on the `forwarded` header[^1], we see:
> The alternative and de-facto standard versions of this header are the
[X-forwarded-For](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/X-forwarded-For),
[X-forwarded-Host](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/X-forwarded-Host)
and
[X-forwarded-Proto](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/X-forwarded-Proto)
headers.
The lack of mention of `x-forwarded-port` led me to believe that the
`X-forwarded-Host` included the port. However, that doesn't not seem
accurate when reviewing Traefik behavior.
</details>
## How I did it
The fix was pretty simple: ensure that the `host` within the `forwarded`
header contained the x-forwarded port.
## How you can test it
Docker image of this build available here:
https://github.com/developmentseed/stac-auth-proxy/releases/tag/v0.10.2-rc21 parent 6396563 commit 78525b1
File tree
2 files changed
+47
-2
lines changed- src/stac_auth_proxy/handlers
- tests
2 files changed
+47
-2
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
35 | 35 | | |
36 | 36 | | |
37 | 37 | | |
38 | | - | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
39 | 45 | | |
40 | 46 | | |
41 | 47 | | |
| |||
44 | 50 | | |
45 | 51 | | |
46 | 52 | | |
| 53 | + | |
47 | 54 | | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
48 | 63 | | |
49 | 64 | | |
50 | | - | |
| 65 | + | |
51 | 66 | | |
52 | 67 | | |
53 | 68 | | |
| 69 | + | |
54 | 70 | | |
55 | 71 | | |
56 | 72 | | |
57 | 73 | | |
58 | 74 | | |
| 75 | + | |
59 | 76 | | |
60 | 77 | | |
61 | 78 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
282 | 282 | | |
283 | 283 | | |
284 | 284 | | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
0 commit comments