Skip to content

fix: admin endpoint security hardening — CSRF, PID recycle, fsync perf - #81

Closed
ranxianglei wants to merge 1 commit into
masterfrom
fix/admin-security-hardening
Closed

ranxianglei wants to merge 1 commit into
masterfrom
fix/admin-security-hardening

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Addresses PR#73 review items #5/#6/#7 (the medium-priority items deferred from the initial merge).

#5 DNS-rebinding CSRF defense (server.ts)

isTrustedAdminOrigin now:

  • Pins Host hostname to loopback (127.0.0.1 / localhost / ::1). Without this, a rebinding domain (evil.com → 127.0.0.1) passed the Origin===Host check because both carried the attacker domain.
  • Checks Sec-Fetch-Site: rejects anything that is not same-origin. This is a fetch metadata header that browser JS cannot forge.

Non-browser clients (curl, etc.) send neither Origin nor Sec-Fetch-Site — unaffected.

#6 PID-recycling defense (codex-takeover.ts)

  • Stores pidStartTick (Linux /proc/<pid>/stat field 22) alongside pid in route state.
  • pidAlive now compares the start tick — a dead bili's recycled PID is no longer mistaken for the live owner.
  • Non-Linux falls back to process.kill(pid, 0) (unchanged).

#7 Per-request fsync overhead (codex-takeover.ts)

  • atomicWrite / writeState gain a durable param (default true).
  • recordCodexRouteRequest writes with durable=false (skips fsync) — the last-seen timestamp is non-critical; atomic rename still guarantees consistency. Critical state (claim/disable) keeps fsync.

Tests

  • DNS rebinding: non-loopback Host → 403, cross-site Sec-Fetch-Site → 403
  • pidStartTick presence after enableCodexTakeover

226 tests pass, typecheck clean, build OK.

…ints

Rework of PR #81 onto current master. The original PR also hardened
codex-takeover.ts (PID-recycling defense + fsync perf), but that file was
removed from master, so only the server.ts CSRF defense survives here.

- isTrustedAdminOrigin: pin Host hostname to loopback (127.0.0.1/localhost/::1)
  so a rebinding domain (evil.com -> 127.0.0.1) can no longer pass Origin===Host.
- reject Sec-Fetch-Site other than same-origin (fetch metadata, unforgeable by
  browser JS); non-browser clients send neither header and are unaffected.
- test: admin endpoints return 403 for non-loopback Host and cross-site
  Sec-Fetch-Site.

371/371 tests pass, typecheck clean, build OK.
@ranxianglei
ranxianglei force-pushed the fix/admin-security-hardening branch from faec567 to 7540e60 Compare August 13, 2026 13:17
cskwork pushed a commit to cskwork/billion-context that referenced this pull request Oct 21, 2026
…ints

Rework of PR ranxianglei#81 onto current master. The original PR also hardened
codex-takeover.ts (PID-recycling defense + fsync perf), but that file was
removed from master, so only the server.ts CSRF defense survives here.

- isTrustedAdminOrigin: pin Host hostname to loopback (127.0.0.1/localhost/::1)
  so a rebinding domain (evil.com -> 127.0.0.1) can no longer pass Origin===Host.
- reject Sec-Fetch-Site other than same-origin (fetch metadata, unforgeable by
  browser JS); non-browser clients send neither header and are unaffected.
- test: admin endpoints return 403 for non-loopback Host and cross-site
  Sec-Fetch-Site.

371/371 tests pass, typecheck clean, build OK.
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