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
{{ message }}
Repository navigation
Commit fb44ceb
Browse filesBrowse the repository at this point in the historyBrowse files
AgentC Consulting
committed
Merge pull request #1415 from crimson-knight/feature/trusted-proxy-https
Decide HTTPS and client IP through trusted proxies (server.trusted_proxies)
| `"x-forwarded"` (default) | the last `X-Forwarded-Proto` value | `X-Forwarded-For` | Caddy, nginx, HAProxy, cloud load balancers, Cloudflare |
84
+
| `"forwarded"` | the `proto=` of the last `Forwarded` element (RFC 7239) | `Forwarded: for=` | proxies configured to write RFC 7239 `Forwarded` |
85
+
86
+
A proxy rewrites only the family it manages and passes the other one
87
+
through from the client unchanged, so believing both would let a client
88
+
choose its own scheme and address. Each proxy appends its value to the end
89
+
of its header, so Amber reads the last one, which is the value the trusted
90
+
hop wrote. A missing header, or a value other than `http` or `https`, keeps
91
+
the connection's own scheme. Amber reads no other header for the scheme,
92
+
including the CGI-style `HTTPS: on`. Requests from any other peer keep their
93
+
real scheme, whatever headers they carry.
94
+
95
+
Entries are IP addresses or CIDR blocks of your proxies. Pick the list for
96
+
your deployment:
97
+
98
+
| Deployment | `trusted_proxies` |
99
+
|------------|-------------------|
100
+
| Caddy or nginx on the same host, forwarding to `127.0.0.1` | default (loopback) |
101
+
| Proxy in another container or host on a private network | that network, e.g. `"10.0.0.0/8"` |
102
+
| Platform router that is the only route to the app (e.g. DigitalOcean App Platform) | the private network the router connects from; log `request.remote_address` once to see it |
103
+
| Cloudflare in front of Caddy on the host | default; configure Caddy as shown below |
104
+
105
+
The loopback default trusts every process on the host, not only the proxy:
106
+
any local program that connects to Amber's port directly can set its own
107
+
scheme and client address. That is sound on a host or container that runs
108
+
only your application and its proxy. On a host shared with other users or
109
+
untrusted programs, set `server.unix_socket` instead, so only the proxy's
110
+
user or group can connect, or set `trusted_proxies` to `[]` and send the
111
+
proxy's traffic over an address only it can reach.
112
+
113
+
List only addresses your proxies use. An entry that covers every address
114
+
(`"0.0.0.0/0"`, `"::/0"`) is rejected: every client would become a trusted
115
+
proxy. A block that clients share with your proxies has the same effect for
116
+
those clients, so keep it as narrow as the proxies' own addresses.
117
+
118
+
The same rule decides the client's IP address, `request.client_ip` (which
119
+
the `Amber::Pipe::ClientIp` pipe stores in `context.client_ip`). From an
120
+
untrusted peer it is the peer's own address, and forwarding headers are
121
+
ignored. From a trusted proxy Amber reads the hops in the configured header
122
+
(`X-Forwarded-For`, or `Forwarded: for=`) from the right, skips every hop
123
+
that is itself a trusted proxy, and returns the first one that is not; a
124
+
spoofed leftmost entry is never reached. A hop that is not exactly an IP
125
+
address, with an optional port, ends the walk, and the nearest verified
126
+
address is used. When every hop is a trusted proxy, the farthest one is the
127
+
client. An empty hop also ends the walk.
128
+
129
+
When a CDN such as Cloudflare sits in front of Caddy, Caddy is Amber's only
130
+
peer, so the CDN's own headers reach Amber only as Caddy passes them on.
131
+
Let Caddy decide who the client is, and keep Amber's loopback default:
132
+
133
+
```caddyfile
134
+
{
135
+
servers {
136
+
trusted_proxies static <Cloudflare's published IP ranges>
137
+
client_ip_headers Cf-Connecting-Ip
138
+
}
139
+
}
140
+
141
+
example.com {
142
+
reverse_proxy 127.0.0.1:3000 {
143
+
header_up X-Forwarded-For {client_ip}
144
+
}
145
+
}
146
+
```
147
+
148
+
With Cloudflare's ranges trusted, Caddy resolves `{client_ip}` to the
149
+
visitor and keeps the `X-Forwarded-Proto` Cloudflare sent; a client that
150
+
reaches Caddy directly gets its own address and scheme. Use Cloudflare's Full
151
+
(strict) TLS mode so the hop from Cloudflare to Caddy is HTTPS too.
152
+
153
+
`ClientIp.new(["X-Real-IP"])`reads a named header instead, under the same
154
+
rule. Name a header only when your proxy sets it on every request, which
155
+
overwrites any value a client sent (for Caddy, `header_up X-Real-IP
156
+
{client_ip}`); a proxy passes other headers through from the client
157
+
unchanged.
158
+
159
+
To put the proxy on a UNIX socket instead of loopback TCP, set
160
+
`server.unix_socket`(for Caddy, `reverse_proxy unix//run/app.sock`). While
161
+
it is set, every UNIX socket peer counts as a trusted proxy: the connection
162
+
is local by construction and the socket file's permissions decide who may
163
+
open it. Amber creates the socket with mode `0660`, so only its own user and
164
+
group can connect: run the proxy as the application's user, or add the
165
+
proxy's user to the application's group (for nginx on Debian, `www-data`).
166
+
Put the socket in a directory the application owns, such as `/run/app/`,
167
+
rather than a shared one like `/tmp`. The `<socket>.lock` file beside it is
168
+
created at mode `0600`; a lock file another user owns or other users may
169
+
open, or a lock another process holds for more than 10 seconds, fails the start with
170
+
`Socket::BindError`. UNIX sockets are not supported on Windows, and these
171
+
three `server` settings need the V2 settings format: a flat V1 file that
172
+
sets one, directly or through an `AMBER_SERVER_*` variable, fails the boot. A
173
+
socket file left behind by a stopped
174
+
server is replaced at boot;
175
+
a socket another running server still accepts connections on is not, and the
176
+
start fails with `Socket::BindError`. It cannot be combined with
177
+
`server.ssl`or a `process_count` above 1; the server checks this before it
178
+
forks or binds.
179
+
180
+
Set the list in YAML, in `config/settings.cr`
181
+
(`settings.server.trusted_proxies = ["127.0.0.1", "10.0.0.0/8"]`), or with
182
+
the comma-separated `AMBER_SERVER_TRUSTED_PROXIES` environment variable. An
183
+
empty value trusts no proxy. The list is replaced, not edited in place:
184
+
`trusted_proxies`returns a copy, so assign a new array to change it. Each
185
+
entry is checked when the settings load or the list is assigned.
186
+
60
187
Validation rules:
61
188
- `port`must be between 1 and 65535
62
189
- `process_count`must be at least 1
63
190
- `secret_key_base`must be set in production
64
191
- `secret_key_base`should be at least 32 characters when set
65
192
- SSL files must exist when SSL is enabled
193
+
- every `trusted_proxies` entry must be an IP address or CIDR block that does
194
+
not cover every address, checked when the settings load or the list is
195
+
assigned
196
+
- `trusted_proxy_headers`must be `"x-forwarded"` or `"forwarded"`, checked
197
+
the same way
198
+
- `unix_socket`cannot be combined with `ssl` or a `process_count` above 1,
199
+
checked by `validate!` and again when the server starts
66
200
67
201
### SessionConfig
68
202
@@ -170,6 +304,9 @@ The naming convention is `AMBER_{SECTION}_{KEY}`:
| HTTPS decided by trusted proxies | Apps behind a TLS-terminating proxy | Low |
18
19
19
20
## 1. CLI Extracted from the Framework
20
21
@@ -493,6 +494,68 @@ V2 updates the default session configuration for improved security:
493
494
494
495
Review your session configuration and update as needed.
495
496
497
+
## 10. HTTPS Behind a Reverse Proxy
498
+
499
+
Production sessions use `Secure` cookies, and Amber emits a `Secure` cookie
500
+
only on a request it considers HTTPS. Earlier V2 betas considered a request
501
+
HTTPS only when it carried the header `HTTPS: on`, which TLS-terminating
502
+
proxies (Caddy, nginx, DigitalOcean App Platform, Cloudflare) do not send.
503
+
They send `X-Forwarded-Proto: https` or `Forwarded: proto=https`. Behind such
504
+
a proxy the session cookie never arrived, so login and CSRF failed.
505
+
506
+
Amber now answers "is this request HTTPS" in one place, `request.https?`,
507
+
using the new `server.trusted_proxies` setting: a list of IP addresses and
508
+
CIDR blocks, loopback only by default. When the immediate peer is in the
509
+
list, Amber believes the scheme that proxy appended to `X-Forwarded-Proto`
510
+
(or, with `server.trusted_proxy_headers: "forwarded"`, to RFC 7239
511
+
`Forwarded`). Requests from any other peer keep their real scheme.
512
+
513
+
What to change:
514
+
515
+
1. **Proxy on the same host** (Caddy or nginx forwarding to `127.0.0.1`):
516
+
nothing to configure; the default trusts loopback.
517
+
2. **Proxy elsewhere** (App Platform, a load balancer, a proxy container):
518
+
list its addresses.
519
+
520
+
```yaml
521
+
# config/environments/production.yml
522
+
server:
523
+
trusted_proxies:
524
+
- "10.0.0.0/8"
525
+
```
526
+
527
+
or set `AMBER_SERVER_TRUSTED_PROXIES=10.0.0.0/8` in the deployment. List
528
+
only the network your proxies or platform router connect from; an entry
529
+
that covers every address (`"0.0.0.0/0"`, `"::/0"`) is rejected.
530
+
3. **Proxy that writes RFC 7239 `Forwarded`** instead of `X-Forwarded-*`:
531
+
set `server.trusted_proxy_headers: "forwarded"` (or
532
+
`AMBER_SERVER_TRUSTED_PROXY_HEADERS=forwarded`). Amber reads only the
533
+
family you name, because a proxy passes the other one through from the
534
+
client.
535
+
4. **Delete any workaround pipe** that writes `HTTPS: on` into the request
536
+
(for example one that sets it unconditionally in production). Amber no
537
+
longer reads `HTTPS: on` at all; it reads the proxy's own
538
+
`X-Forwarded-Proto`. If your proxy sends only `HTTPS: on`, configure it
539
+
to send `X-Forwarded-Proto` instead.
540
+
541
+
The same setting decides the client's IP address. `request.client_ip` and
542
+
the `ClientIp` pipe now return the peer's own address for an untrusted peer,
543
+
and for a trusted proxy the nearest untrusted hop in `X-Forwarded-For` (or
544
+
`Forwarded: for=`), read from the right. Earlier versions took the leftmost
545
+
`X-Forwarded-For`entry from any peer, which a client controls; if your app
546
+
logged or rate-limited on that value, expect the real address now.
547
+
548
+
A proxy can also reach Amber over a UNIX socket: set `server.unix_socket`,
549
+
and UNIX socket peers are trusted while it is set.
550
+
551
+
One behavior changes: `HTTPS: on`no longer marks a request as HTTPS, from
552
+
any peer, because a proxy passes it through from the client unchanged.
553
+
Behind Caddy, nginx or a cloud load balancer nothing else changes, since
554
+
they send `X-Forwarded-Proto`.
555
+
556
+
See the [configuration guide](guides/configuration.md#running-behind-a-tls-terminating-proxy)
557
+
for the full rules.
558
+
496
559
## Dependency Changes Summary
497
560
498
561
### Remove from shard.yml
@@ -540,5 +603,6 @@ is published.
540
603
- [ ] Update session configuration to use adapter pattern
541
604
- [ ] Remove any direct `require "redis"`, `require "amber_router"`, or `require "kilt"` statements
542
605
- [ ] Add database drivers explicitly to shard.yml if needed
606
+
- [ ] Behind a TLS-terminating proxy, set `server.trusted_proxies` to the proxy's addresses, make sure it sends `X-Forwarded-Proto`, and delete any pipe that writes `HTTPS: on`
543
607
- [ ] Test all routes and templates
544
608
- [ ] (Optional) Start migrating controller actions to use Schema API
0 commit comments