feat(deploy): restrict the public perimeter and resolve the client address

This commit is contained in:
shira committed 2026-10-06 21:32:03 +03:00
1 parent e52ffe7bcb
commit 57174dffba
25 files changed
+1224 -25

No files matched your search

+125 -10
View File
@@ -1,11 +1,28 @@
# Public perimeter of the app host. Only three things are reachable here: the
# SPA (static files plus the index.html fallback), the versioned public API
# under /api/v1/, and the restricted same-origin /telemetry receiver. Backend
# /metrics (port 9090), /readyz, and every other service never pass through
# this server; the paths that look like them are answered with 404 so the SPA
# fallback does not turn them into a 200 page either.
#
# The perimeter is checked by deploy/scripts/test_perimeter.py
# (make -C deploy perimeter-test).
# Behind the TLS edge proxy the client's scheme arrives in X-Forwarded-Proto
# (deploy/edge). Keep it when it is https so the backend sees the original
# scheme; every other value falls back to the scheme of this server. The
# header is trustworthy only because the frontend port is published on
# loopback or on the tunnel address, never on a public one.
map $http_x_forwarded_proto $forwarded_proto {
default $scheme;
https https;
# The claim is believed only from the trusted edge peer, so any other peer
# cannot make the backend think the request was HTTPS.
geo $realip_remote_addr $trusted_edge {
default 0;
10.79.0.1 1;
}
map "$trusted_edge:$http_x_forwarded_proto" $forwarded_proto {
default $scheme;
"1:https" https;
}
server {
@@ -13,11 +30,46 @@ server {
server_name _;
root /usr/share/nginx/html;
index index.html;
# Redirects (for example /assets to /assets/) never use the Host header or
# the container port, so a forged Host cannot steer a redirect.
absolute_redirect off;
port_in_redirect off;
add_header X-Content-Type-Options nosniff always;
add_header Referrer-Policy strict-origin-when-cross-origin always;
add_header X-Frame-Options DENY always;
add_header Permissions-Policy "camera=(), microphone=(), geolocation=()" always;
# Client address. The only trusted proxy is the edge end of the tunnel
# (AWG_EDGE_IP in deploy/edge/edge.env.example; the perimeter test keeps
# the two equal). For a request from that peer, the client is the last
# X-Forwarded-For entry that is not a trusted proxy; for any other peer
# the header is ignored and $remote_addr stays the connecting address.
# The rate limits below and the X-Forwarded-For sent to the backend use
# this value, so a client cannot choose its own bucket. If the edge peer
# reaches this server under another address (for example a Docker
# gateway address), the header is ignored and all clients share one
# bucket: that fails closed, never open.
set_real_ip_from 10.79.0.1;
real_ip_header X-Forwarded-For;
real_ip_recursive on;
include /etc/nginx/snippets/security-headers.conf;
# Limit and rate responses use the problem format of the API contract.
# The status is set explicitly, so the error_page statuses do not change.
error_page 413 @payload_too_large;
error_page 429 @rate_limited;
location @payload_too_large {
include /etc/nginx/snippets/security-headers.conf;
default_type application/problem+json;
add_header X-Request-ID $request_id always;
return 413 '{"type":"urn:lincodex:problem:payload_too_large","title":"Request body too large","status":413,"code":"payload_too_large","detail":"Reduce the request body size and retry.","trace_id":"$request_id"}';
}
location @rate_limited {
include /etc/nginx/snippets/security-headers.conf;
add_header Retry-After 1 always;
add_header X-Request-ID $request_id always;
default_type application/problem+json;
return 429 '{"type":"urn:lincodex:problem:rate_limited","title":"Too many requests","status":429,"code":"rate_limited","detail":"Wait for the Retry-After interval and retry.","trace_id":"$request_id"}';
}
location = /healthz {
access_log off;
@@ -25,39 +77,102 @@ server {
return 200 "ok\n";
}
# Operational and administration paths of the services behind this
# server. None of them is proxied; 404 is the same answer as for an
# unknown API path, so the response does not confirm that the path exists.
location ~ ^/(metrics|readyz|debug|actuator)(/|$) {
return 404;
}
# Dotfiles are never served from the SPA directory.
location ~ /\. {
return 404;
}
# Without these, nginx would redirect /api/v1 to /api/v1/ and leak the
# difference; the public paths all have an operation name after the slash.
location = /api {
return 404;
}
location ^~ /api/ {
location = /api/v1 {
return 404;
}
# The public API. Same-origin only: no CORS header is added here or by the
# backend, so a browser refuses cross-origin reads. The backend still
# checks Origin on unsafe methods (403 csrf_check_failed).
location ^~ /api/v1/ {
limit_req zone=api_rate burst=60 nodelay;
limit_conn api_conn 40;
proxy_pass http://backend:8080;
proxy_http_version 1.1;
proxy_set_header Host $host;
# Replace, never append: the backend receives one entry, the client
# address resolved above, from a proxy it trusts (this container).
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Forwarded "";
proxy_set_header X-Forwarded-Proto $forwarded_proto;
proxy_set_header X-Request-ID $request_id;
proxy_set_header Connection "";
proxy_connect_timeout 3s;
proxy_send_timeout 30s;
# Above the longest backend dependency timeout (10 s for storage and
# mail) and below the edge read timeout (60 s), so the backend answers
# first and the edge never gives up before this server.
proxy_read_timeout 30s;
}
# Everything else under /api is not part of the public contract.
location ^~ /api/ {
return 404;
}
location = /telemetry {
if ($request_method != POST) {
return 405;
}
limit_req zone=telemetry_rate burst=10 nodelay;
client_max_body_size 1m;
set $faro_receiver faro-receiver:12347;
proxy_pass http://$faro_receiver/collect;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Forwarded "";
proxy_set_header X-Forwarded-Proto $forwarded_proto;
proxy_set_header Connection "";
proxy_connect_timeout 2s;
proxy_send_timeout 5s;
proxy_read_timeout 5s;
}
location ^~ /telemetry/ {
return 404;
}
# Hashed build files never change under their name, so they are cached
# for a year; a missing one is a 404, never the index.html fallback.
location ^~ /assets/ {
if ($request_method !~ ^(GET|HEAD)$) {
return 405;
}
include /etc/nginx/snippets/security-headers.conf;
add_header Cache-Control "public, max-age=31536000, immutable" always;
try_files $uri =404;
}
# index.html and the deep-link fallback must be revalidated on every
# load: a cached copy would name hashed assets that a release has removed.
location / {
if ($request_method !~ ^(GET|HEAD)$) {
return 405;
}
include /etc/nginx/snippets/security-headers.conf;
add_header Cache-Control "no-cache" always;
try_files $uri $uri/ /index.html;
}
}
+39 -2
View File
@@ -18,7 +18,15 @@ http {
tcp_nopush on;
keepalive_timeout 30s;
server_tokens off;
client_max_body_size 10m;
# Default request body limit: the JSON API bodies of the contract are
# small. The 5 MB resume upload has no operation in the contract yet; the
# route that gets one raises the limit in its own location in app.conf.
# The edge proxy allows 10 MB (EDGE_MAX_BODY) and must stay at or above
# the largest limit here.
client_max_body_size 1m;
client_header_timeout 10s;
client_body_timeout 30s;
send_timeout 30s;
client_body_temp_path /tmp/client_body;
proxy_temp_path /tmp/proxy;
@@ -26,7 +34,36 @@ http {
scgi_temp_path /tmp/scgi;
uwsgi_temp_path /tmp/uwsgi;
limit_req_zone $binary_remote_addr zone=telemetry_rate:10m rate=30r/m;
# Coarse per-client protection only. $remote_addr is the client address
# that app.conf resolved through the trusted edge (realip), never a value
# the client supplied. The values are far above normal browsing: the
# per-account and per-route quotas stay in the backend and are not
# replaced by these. The zones keep addresses in memory only; the access
# log does not record them.
#
# The key is the address, except that an IPv6 address counts as its /64
# network (as middleware.LimiterKey does in the backend): one subscriber
# controls a whole /64 and could otherwise take a new bucket per request.
# $remote_addr is canonical text here (inet_ntop from the edge), so the
# first four groups, or the part before "::", name the network. The two
# spellings of one /64 that differ in where "::" falls get two buckets, a
# harmless looseness for a coarse limit.
map $remote_addr $limit_key {
"~^(?<v6full>(?:[0-9a-f]{1,4}:){4})" $v6full;
"~^(?<v6short>(?:[0-9a-f]{1,4}(?::[0-9a-f]{1,4}){0,2})?::)" $v6short;
default $remote_addr;
}
limit_req_zone $limit_key zone=api_rate:10m rate=20r/s;
limit_conn_zone $limit_key zone=api_conn:10m;
limit_req_zone $limit_key zone=telemetry_rate:10m rate=30r/m;
limit_req_status 429;
limit_conn_status 429;
# A rejection line in the error log names the client address. Rejections
# are routine under a flood and the address must not be logged, so they
# are logged at notice, below the warn threshold of error_log above
# (the default, error, would write them with the address).
limit_req_log_level notice;
limit_conn_log_level notice;
resolver 127.0.0.11 valid=30s ipv6=off;
include /etc/nginx/conf.d/*.conf;
@@ -0,0 +1,18 @@
# Security headers of the app Nginx. Nginx drops every inherited add_header
# in a block that sets its own, so each block that adds a header (named
# error locations) includes this file again.
#
# CSP: the built SPA is one module script and one stylesheet from the same
# origin, fonts and logos are bundled files, and the API is same-origin, so no
# external host and no inline code is allowed. frame-ancestors is the modern
# form of X-Frame-Options and base-uri/form-action/object-src close the
# injection routes that default-src does not cover. When telemetry (Faro)
# is connected it posts to the same-origin /telemetry, which connect-src
# 'self' already allows.
add_header Content-Security-Policy "default-src 'none'; script-src 'self'; style-src 'self'; img-src 'self' data:; font-src 'self'; connect-src 'self'; manifest-src 'self'; base-uri 'none'; form-action 'self'; frame-ancestors 'none'; object-src 'none'" always;
add_header X-Content-Type-Options nosniff always;
add_header Referrer-Policy strict-origin-when-cross-origin always;
add_header X-Frame-Options DENY always;
add_header Cross-Origin-Opener-Policy same-origin always;
add_header Cross-Origin-Resource-Policy same-origin always;
add_header Permissions-Policy "camera=(), microphone=(), geolocation=(), payment=(), usb=(), interest-cohort=()" always;