Commit 83fa9a0
fix(auth): close PAT mint chain + redirect-open + cookie session (P0)
Auth P0 chain found by QA 2026-05-29. Six findings; all gated on the
same login surface, ship as one PR.
Findings closed in this PR:
AUTH-001 (P0): PATs could mint child PATs (live returned 201; OpenAPI
contract says 403). A leaked PAT could spawn additional keys that
outlived the parent's revocation, defeating rotation/detection.
Fix: handlers/api_keys.go branches on middleware.IsAuthedViaAPIKey
and rejects with 403 pat_cannot_mint_pat regardless of scope.
AUTH-002 (P0): read-only PAT minted admin-scope child (201 with
scopes:["admin"]). Complete privilege escalation from a leaked
read-only key. Fix: the AUTH-001 structural gate catches this as
a subset — no PAT can mint a PAT at all, so subset-of-parent is
trivially satisfied.
AUTH-090 (P0): session JWT could mint admin-scope PAT with no re-auth.
Last hop in the phishing → session cookie → admin PAT chain. Fix:
minting an admin-scope PAT from a session JWT requires
X-Confirm-Reauth: 1 (set by the dashboard after a fresh
password/MFA prompt). Without the header → 403 reauth_required.
AUTH-164 (P1): scopes:[] and scopes:null silently defaulted to
["read","write"]. Caller asked for "no scopes" and got broad write.
Fix: distinguish absent (→ safe default ["read"]) from explicit
empty/null (→ 400 invalid_scopes).
AUTH-016 / AUTH-017 (P0): return_to=javascript:alert(1) and
return_to=data:text/html,<script>... accepted with 202 on
/auth/email/start. Fix: fail-closed scheme allow-list = [https, http]
(http only when ALLOW_RETURN_LOCALHOST is set). javascript:, data:,
file:, etc. → 400 invalid_return_to. Same gate added to
/auth/github/start and /auth/google/start.
AUTH-004 (P0): session_token leaked in 302 Location URL of
/auth/email/callback (also github/google browser callbacks). Full
24h-TTL JWT landed in browser history, ingress logs, CDN logs,
Referer on every subsequent navigation. Fix: JWT now rides in a
Secure; HttpOnly; SameSite=Lax cookie scoped to .instanode.dev
(production) or current host (dev). Redirect URL carries only a
non-secret ?signed_in=1 marker so the dashboard SPA knows to call
/auth/me. middleware/RequireAuth accepts the cookie as a fallback
when no Authorization: Bearer header is present.
Regression tests (each test reproduces the original exploit and
asserts the fix blocks it):
TestPAT_CannotMintChild
TestPAT_ChildScopesSubsetOfParent
TestPAT_SessionJWTCannotMintAdminScope_RequiresReauth
TestPAT_EmptyScopesRejected
TestAuthStart_RejectsJavascriptReturnTo
TestAuthStart_RejectsDataReturnTo
TestAuthStart_AllowsValidHTTPS_LegitimateFlow (guardrail — UI flow)
TestAuthCallback_DoesNotPutJWTInLocation
Existing redirect tests updated to assert the new "JWT in cookie, not
Location" behaviour. existing TestAPIKeys_Create_ValidationArms/
valid_admin_scope renamed to valid_admin_scope_without_reauth and
asserts 403 (the new gate). Admin-scope happy path is covered by the
new TestPAT_SessionJWTCannotMintAdminScope_RequiresReauth (header path
returns 201).
Rule-22 surface checklist:
- api/internal/handlers/api_keys.go (handler)
- api/internal/handlers/magic_link.go (handler)
- api/internal/handlers/auth.go (handlers + helpers)
- api/internal/middleware/auth.go (cookie fallback)
- OpenAPI: handlers/openapi.go not updated (contract
already documented 403 on AUTH-001; the others are stricter
versions of existing 400 envelopes — no new error keywords land
outside the existing error_code map). Follow-up: surface
reauth_required + invalid_return_to + invalid_scopes in
codeToAgentAction (handlers/agent_action.go) as a polish PR.
- dashboard upgradeCopy.ts not impacted (no tier
boundary change)
- content/llms.txt not impacted (no public
contract change beyond stricter validation)
- instanode-web pricing not impacted
Live-verify (rule 14) and curl evidence (per finding) attached in PR
body.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>1 parent f2fb140 commit 83fa9a0
11 files changed
Lines changed: 844 additions & 13 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
12 | 23 | | |
13 | 24 | | |
| 25 | + | |
14 | 26 | | |
| 27 | + | |
15 | 28 | | |
16 | 29 | | |
17 | 30 | | |
| |||
36 | 49 | | |
37 | 50 | | |
38 | 51 | | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
39 | 60 | | |
40 | 61 | | |
41 | 62 | | |
| |||
51 | 72 | | |
52 | 73 | | |
53 | 74 | | |
54 | | - | |
55 | | - | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
56 | 89 | | |
57 | 90 | | |
58 | 91 | | |
59 | 92 | | |
60 | 93 | | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
61 | 103 | | |
62 | 104 | | |
63 | 105 | | |
| |||
73 | 115 | | |
74 | 116 | | |
75 | 117 | | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
76 | 123 | | |
77 | 124 | | |
78 | 125 | | |
| |||
84 | 131 | | |
85 | 132 | | |
86 | 133 | | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
87 | 157 | | |
88 | 158 | | |
89 | 159 | | |
| |||
161 | 231 | | |
162 | 232 | | |
163 | 233 | | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
0 commit comments