Skip to content

fix(hosting): show the server the public host, scheme and client address behind every door - #1726

Draft
osama-rizk wants to merge 1 commit into
mainfrom
fix/forwarded-host-client-ip
Draft

osama-rizk wants to merge 1 commit into
mainfrom
fix/forwarded-host-client-ip

Conversation

@osama-rizk

Copy link
Copy Markdown
Contributor

Problem

Behind several front doors the server rendered with an internal view of the request instead of the public one:

  • Nuxt behind the CloudFront door: Host and useRequestURL() named the REST API origin's execute-api host. x-forwarded-host was right, but Nitro doesn't read it.
  • Astro behind the stacked door: x-forwarded-proto was http (the edge → router hop), and X-Forwarded-For listed the viewer twice.
  • Astro behind the HTTP API door: Astro.url was http://localhost:3000/… and Astro.clientAddress was 127.0.0.1.
  • SvelteKit behind the ALB: getClientAddress() was 127.0.0.1. On Web Functions it was 169.254.100.1. Hosting set HOST_HEADER and PROTOCOL_HEADER for adapter-node but not ADDRESS_HEADER.

So absolute URLs built from the request (canonical links, redirect and OAuth URLs, emails) pointed at an origin or at localhost, and IP-based logic (rate limits, audit) saw a proxy address. Trusting the forwarded headers as they arrive isn't safe: a client can send its own X-Forwarded-For and X-Forwarded-Host, and Astro and h3 take the left-most X-Forwarded-For entry, which the client controls.

Changes

  • New forwarded_headers.ts. It records which forwarded headers each built-in door sets or appends, and generates a runtime rewrite:
    • Host and x-forwarded-host both name the public host.
    • X-Forwarded-For holds only the client address that the door appended, counted from the right. Entries the client sent are dropped.
    • x-forwarded-proto is the viewer's scheme.
    • On a Lambda event, requestContext.identity.sourceIp / requestContext.http.sourceIp are set to the client address too.
  • What each door is trusted for:
    • CloudFront: the router-set x-forwarded-host, and the 2nd X-Forwarded-For entry from the right (the origin appends the CloudFront address).
    • Stacked: the edge-set x-forwarded-host, the 2nd entry from the right, and the scheme from CloudFront-Forwarded-Proto.
    • ALB and API Gateway: Host, and the right-most entry.
    • Bring-your-own door and 'none': no rewrite.
  • The rewrite ships in the SSR cache guard module, which Hosting already installs in front of every server shape. On a Node HTTP server (Lambda Web Adapter, Web Function) it patches http.Server#emit for request events. On a native handler (Nitro aws-lambda, OpenNext) the guard's handler wrapper rewrites the event. The trust is baked into the module at synth.
  • The SvelteKit adapter sets ADDRESS_HEADER=x-forwarded-for and XFF_DEPTH=1, on Lambda and on Web Functions. Without the rewrite (a bring-your-own door), depth 1 still reads the nearest proxy's entry, never the client's.
  • README "The host the server sees": the trust model per door, and what each framework reads. Astro deliberately trusts no forwarded header until security.allowedDomains names the host, so the README says to add it. With the rewrite, the address Astro then takes from X-Forwarded-For is the client's and can't be spoofed.

Behaviour change: behind the built-in doors the server now sees Host as the public host (on the CloudFront and stacked doors it used to be the origin's host) and a single-entry X-Forwarded-For. Code that read the full X-Forwarded-For chain, or expected the origin's host in Host, now sees the public values.

Testing

Deployed verification

Both apps were redeployed as production stacks with packages packed from this branch. The new build id was confirmed live before the checks. Each check requested /dynamic with spoofed X-Forwarded-For: 6.6.6.6, 7.7.7.7, X-Forwarded-Host: evil.example and X-Forwarded-Proto: gopher.

  • SvelteKit 2 (adapter-node) on a Web Function behind the CloudFront door. getClientAddress() is the client's own address over IPv4 and over IPv6. It was 169.254.100.1. X-Forwarded-For holds only that address, and the host, URL and scheme are the distribution's. The end-to-end suite passed 34/34. The SSR matrix shows the same results as before this change, except P1: the matrix's P1 check only accepts IPv4, and this client reached CloudFront over IPv6. The value it saw is the client's real address.
  • Astro 7 (@astrojs/node) behind the API Gateway HTTP API door, with security.allowedDomains set to the API's host. Astro.url is https://<api-host>/dynamic (it was http://localhost:3000/dynamic), and Astro.clientAddress is the client's address (it was 127.0.0.1). The spoofed values reached neither. The end-to-end suite passed 34/34, and the SSR matrix P1 check passes. For this deploy the app's one rest-parameter redirect (/blog/[...slug]) was removed: the HTTP API door rejects its route on main, independently of this change (see fix(hosting): translate Astro dynamic redirects for every front door #1717).
  • Nuxt 4 (Nitro aws-lambda) behind the CloudFront door, checked locally. The real built server bundle was invoked through the generated handler wrapper with a CloudFront-shaped REST event. Without the rewrite, useRequestURL() was https://<execute-api-host>/dynamic. With it, it was https://<distribution-host>/dynamic, and X-Forwarded-For held only the viewer.
  • The stacked door is covered at unit level: the construct test for each door, and the live http.Server test with the stacked door's duplicated X-Forwarded-For and plain-HTTP x-forwarded-proto. It was not redeployed.

@github-actions

Copy link
Copy Markdown
Contributor
📖 Glossary & notes — scoring, indicators, per-metric thresholds (click to expand)
  • N = 1 — one rep per cell, so a small delta may be model variance, not a real change; re-run for certainty.
  • Indicators (change vs baseline, per metric): ✨ noticeably improved · ✅ OK / no significant change (within noise band) · ⚠️ slight regression (beyond 1× threshold) · ❌ serious regression (beyond 2× threshold) · 🆕 new — no baseline value yet (current value shown, tagged (new)) · — nothing to show this run · 🗑️ cell gone since the baseline. Each cell shows <indicator> <value> (<Δ vs main>) inline.
  • Columns: Tests (pass/denom) · Judge (overall 0-10 score with signed delta; per-dimension breakdown lives in the judge artifact JSON) · Cost · Turns (agent cycles) · Score · Stop reason.
  • Threshold definitions (per metric, DELTA_THRESHOLDS in overview.mjs):
    • composite/score: ±5 points — a change ≤5 pts is ✅; >5 improving is ✨; >5 worsening is ⚠️; >10 worsening is ❌.
    • judge (overall): ±0.3 — a shift ≤0.3 is ✅; >0.3 improving is ✨; >0.3 worsening is ⚠️; >0.6 worsening is ❌.
    • tests: ±1 pass — a ≤1 pass change is ✅; >1 improving is ✨; >1 worsening is ⚠️; >2 worsening is ❌.
    • cost: ±10% of baseline (floor $0.02) — within 10% is ✅; >10% cheaper is ✨; >10% more expensive is ⚠️; >20% more expensive is ❌.
    • turns: ±3 cycles — a ≤3 turn change is ✅; >3 fewer is ✨; >3 more is ⚠️; >6 more is ❌.
  • Directions: higher is better for tests, judge, and score; lower is better for cost and turns.
  • Composite (0-100) = round(60·test_rate + 4·judge·min(1, 4·test_rate), 1) — 60% objective pass-rate + 40% judge, the judge term gated below a 25% pass-rate.
  • Cost = builder token spend at Bedrock Claude Opus 4.8 rates ($5 in / $25 out per 1M tokens; BUILDER_PRICING in scoring.mjs).
  • SCORE = composite ÷ cost — composite points per dollar (higher = better). Cost, not raw token volume, is the denominator, so tokens can't dominate; a broken (composite 0) cell scores 0 no matter how cheap. Flip SCORE_PER_DOLLAR in scoring.mjs for cost-per-point (lower = better).
  • Baseline = the most recent main-branch bench (bench/runs/latest-main.json), NOT the PR base commit — the PR always diffs against the current state of main.
  • Excluded from the mean: harness_error cells (infra failures) and gradeable cells that ran no tests. agent_fail (agent produced no app in budget) and dead_server (built app never served / crashed) are BOTH included, as composite 0.
  • Run artifacts — per-cell source + full agent traces · 🕒 Last run: 2026-11-20T13:24:52Z · run #44736454681

Results — PR vs main baseline

Each metric shows its current value colored by the change vs the latest main baseline 4d09d05. ✨ improved · ✅ within noise · ⚠️ slight regression · ❌ serious regression · 🆕 no baseline yet (value shown, tagged (new)).

  • Mean composite 85.6/100 across 9 scored cell(s) — ✅ -0.8 vs main · judge mean 9.13/10.
  • Verdicts: 8 pass · 0 partial · 1 fail · 1 harness_error.
  • Totals: cost $131.74 · tokens 25.4M in / 181.4K out · 393 turns.
  • Biggest gains: async-word-counter/bare (+4), file-gallery/bare (+3.2), collab-presence-board/default (+0.8).
  • Biggest drops: cognito-profile/auth (-4), oidc-dsql-notes/react (-3.2).
  • Config: baseline 4d09d05.
  • ✅ Composite band 🟢 across 9 scored cell(s). Threshold 65 — pass.
Task Template Tests Judge Cost Turns Score Stop reason
async-word-counter bare ✅ 13/13 (0) ✨ 9.4 (+1) ✅ $6.51 (+$0.56) ✅ 30 (+1) ✅ 15 (-0.7) endTurn
auth-notes demo ✅ 0/11 (0) ✨ 9.6 (+0.8) ✅ $17.04 (+$0.72) ❌ 50 (+9) ✅ 0 (0) endTurn
cognito-profile auth ✅ 10/10 (0) ❌ 8.4 (-1) ✨ $13.13 (-$15.56) ✨ 44 (-29) ✅ 7.1 (+3.7) endTurn
collab-presence-board default ✅ 11/11 (0) ✅ 9.4 (+0.2) ❌ $4.09 (+$1.77) ❌ 21 (+7) ❌ 23.9 (-17.9) endTurn
email-digest demo ✅ 12/12 (0) ✅ 9.4 (+0.2) ✅ $3.49 (+$0.14) ✅ 16 (0) ✅ 27.9 (-1) endTurn
file-gallery bare ✅ 12/12 (0) ✨ 9.6 (+0.8) ✨ $3.06 (-$0.81) ✅ 14 (-3) ✨ 32.2 (+7.6) endTurn
kb-chat-agent demo ✅ 9/9 (0) ✅ 9 (0) ✨ $20.5 (-$9.71) ✨ 46 (-17) ✅ 4.7 (+1.5) endTurn
observability-api backend ✅ 18/18 (0) ✅ 9 (0) ✅ $6.45 (-$0.32) ✅ 37 (+1) ✅ 14.9 (+0.7) endTurn
oidc-dsql-notes react ✅ 11/11 (0) ❌ 8.4 (-0.8) ✨ $22.68 (-$16.33) ✨ 51 (-27) ✅ 4.1 (+1.6) endTurn
sql-kb-catalog nextjs — — ❌ $34.79 (+$25.32) ❌ 84 (+53) — in_progress

Merge verdict

✅ This bench run does not block the PR.

  • ✅ Absolute floor — mean composite 85.6 >= floor 65.
  • ✅ Regression vs main — mean composite delta -0.8 pts vs main is within the 10-pt limit.

The Δ vs base ball above is a fixed ±5-pt display band; the regression gate uses BENCH_MAX_REGRESSION. The ball is cosmetic — the gate threshold is what blocks.

Caveats & exclusions

  • 🧰 Excluded (harness_error, not scored) — 1: 1× wall_clock_timeout. Cells: sql-kb-catalog/nextjs

This branch had an error being deployed

1 failed and 1 inactive deployments
agent-bench — 3a037528 Deployed Nov 20, 2026 by osama-rizk via bench / summary #6675
e2e — 3a037528 Deployed Nov 20, 2026 by osama-rizk via E2E Teardown Verify #7647
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant