Files

179 lines
7.0 KiB
Plaintext

# 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.
# 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 {
listen 8080;
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;
# 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;
default_type text/plain;
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/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 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 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;
}
}