@wipcomputer/wip-ldm-os 0.4.85-alpha.3 → 0.4.85-alpha.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/README.md +22 -2
  2. package/SKILL.md +136 -14
  3. package/bin/ldm.js +422 -75
  4. package/docs/universal-installer/SPEC.md +16 -3
  5. package/docs/universal-installer/TECHNICAL.md +4 -4
  6. package/lib/deploy.mjs +104 -20
  7. package/lib/detect.mjs +35 -4
  8. package/lib/registry-migrations.mjs +296 -0
  9. package/package.json +17 -2
  10. package/scripts/test-crc-agentid-tenant-boundary.mjs +80 -0
  11. package/scripts/test-crc-e2ee-key-persistence.mjs +150 -0
  12. package/scripts/test-crc-e2ee-session-route.mjs +129 -0
  13. package/scripts/test-crc-pair-login-flow.mjs +40 -0
  14. package/scripts/test-crc-pair-relink-audit-and-rotation.mjs +164 -0
  15. package/scripts/test-crc-pair-status-poll-token.mjs +73 -0
  16. package/scripts/test-crc-websocket-abuse-limits.mjs +128 -0
  17. package/scripts/test-install-prompt-policy.mjs +84 -0
  18. package/scripts/test-installer-skill-directory.mjs +55 -0
  19. package/scripts/test-installer-skill-dry-run-destinations.mjs +100 -0
  20. package/scripts/test-installer-target-self-update.mjs +131 -0
  21. package/scripts/test-ldm-status-concurrency.mjs +118 -0
  22. package/scripts/test-ldm-status-timeout.mjs +96 -0
  23. package/scripts/test-legacy-npm-sources-migration.mjs +460 -0
  24. package/scripts/test-readme-install-prompt.mjs +66 -0
  25. package/shared/templates/install-prompt.md +20 -2
  26. package/src/hosted-mcp/README.md +37 -0
  27. package/src/hosted-mcp/app/footer.js +74 -0
  28. package/src/hosted-mcp/app/kaleidoscope-login.html +846 -0
  29. package/src/hosted-mcp/app/pair.html +165 -57
  30. package/src/hosted-mcp/app/sprites.png +0 -0
  31. package/src/hosted-mcp/codex-relay-e2ee-registry.mjs +208 -0
  32. package/src/hosted-mcp/codex-relay-ws-abuse-limits.mjs +140 -0
  33. package/src/hosted-mcp/demo/index.html +3 -7
  34. package/src/hosted-mcp/demo/login.html +318 -20
  35. package/src/hosted-mcp/deploy.sh +308 -56
  36. package/src/hosted-mcp/docs/self-host.md +268 -0
  37. package/src/hosted-mcp/nginx/codex-relay.conf +25 -0
  38. package/src/hosted-mcp/nginx/conf.d/redact-logs.conf +60 -0
  39. package/src/hosted-mcp/nginx/mcp-oauth.conf +58 -0
  40. package/src/hosted-mcp/nginx/wip.computer.conf +25 -1
  41. package/src/hosted-mcp/scripts/audit-logs.sh +205 -0
  42. package/src/hosted-mcp/scripts/verify-deploy.sh +102 -0
  43. package/src/hosted-mcp/server.mjs +1034 -146
@@ -1,6 +1,6 @@
1
1
  // server.mjs: Hosted MCP server for wip.computer
2
2
  // MCP Streamable HTTP transport at /mcp, health check at /health.
3
- // Auth: Bearer ck-... API key maps to an agent ID.
3
+ // Auth: Bearer ck-... API key maps to an immutable tenant ID.
4
4
  // OAuth 2.0: Minimal flow for Claude iOS custom connector.
5
5
  // WebAuthn: Passkey-based signup/login (replaces agent name text form).
6
6
 
@@ -23,10 +23,49 @@ import {
23
23
  import QRCode from "qrcode";
24
24
  import { WebSocketServer } from "ws";
25
25
  import { parse as parseUrlQs } from "node:querystring";
26
+ import {
27
+ buildCodexBootstrapPayload,
28
+ codexDaemonPubkeyFingerprint,
29
+ createCodexDaemonPubkeyRegistry,
30
+ evaluateCodexDaemonReconnectPubkey,
31
+ } from "./codex-relay-e2ee-registry.mjs";
32
+ import {
33
+ codexWsFrameByteLength,
34
+ createCodexWsAbuseLimitConfig,
35
+ createCodexWsConnectionGuard,
36
+ formatCodexWsLimitLog,
37
+ isCodexWsAgentDisabled,
38
+ } from "./codex-relay-ws-abuse-limits.mjs";
26
39
 
27
40
  // ── Settings ─────────────────────────────────────────────────────────
28
41
 
29
42
  const PORT = parseInt(process.env.MCP_PORT || "18800", 10);
43
+ // Dev mode: opt-in to JSON-file fallbacks for the data layer and to
44
+ // reading tokens/passkeys from local JSON files. Production must run
45
+ // without this flag set (production fails closed when Prisma is
46
+ // unavailable, and never reads/writes the local JSON token files).
47
+ // Tracked by ai/product/bugs/security/2026-04-28--cc-mini--vps-hosted-mcp-audit.md (F-002, F-005a).
48
+ const DEV_MODE = process.env.LDM_HOSTED_MCP_DEV_MODE === "1";
49
+ // WebSocket Origin allowlist (F-003 in the VPS hosted-mcp audit).
50
+ // Browser-borne web WS upgrades must present an Origin from this list.
51
+ // Comma-separated env var; default is the production origin.
52
+ // Daemon WS upgrades (CLI / agent connections) are NOT gated on Origin
53
+ // because they do not send a browser Origin header.
54
+ const WS_ORIGIN_ALLOWLIST = (process.env.LDM_HOSTED_MCP_WS_ORIGIN_ALLOWLIST || "https://wip.computer")
55
+ .split(",")
56
+ .map(s => s.trim())
57
+ .filter(Boolean);
58
+ const CODEX_WS_ABUSE_LIMITS = createCodexWsAbuseLimitConfig(process.env);
59
+
60
+ function isWsOriginAllowed(origin) {
61
+ if (!origin) return false;
62
+ return WS_ORIGIN_ALLOWLIST.includes(origin);
63
+ }
64
+ // F-001: WS URL-token fallback (browser sends ?token=ck-... on upgrade).
65
+ // Default off in production. Set LDM_HOSTED_MCP_ALLOW_WS_URL_TOKEN=1 to
66
+ // allow the legacy back-compat path. Independent of any other dev flag
67
+ // so that this can be toggled without enabling other dev-mode behavior.
68
+ const ALLOW_WS_URL_TOKEN = process.env.LDM_HOSTED_MCP_ALLOW_WS_URL_TOKEN === "1";
30
69
  const SESSION_TIMEOUT_MS = 30 * 60 * 1000;
31
70
  const SESSION_CLEANUP_INTERVAL_MS = 5 * 60 * 1000;
32
71
  const OAUTH_CODE_EXPIRY_MS = 10 * 60 * 1000;
@@ -44,18 +83,19 @@ const RP_ORIGIN = "https://wip.computer";
44
83
 
45
84
  // ── Data layer ──────────────────────────────────────────────────────
46
85
  //
47
- // Primary: Postgres via Prisma (production).
48
- // Fallback: JSON files (if DATABASE_URL is not set, e.g. local dev without Postgres).
86
+ // Production: Postgres via Prisma is the canonical store. If Prisma
87
+ // cannot connect, the server refuses to start (F-005a).
49
88
  //
50
- // The demo and all API endpoints use the db.* functions below.
51
- // They try Prisma first, fall back to JSON if Prisma isn't available.
89
+ // Dev mode (LDM_HOSTED_MCP_DEV_MODE=1): JSON files are used as a
90
+ // fallback for tokens and passkeys when Prisma is unavailable, and
91
+ // are also seeded into the in-memory cache on boot. Production must
92
+ // not set this flag.
52
93
 
53
94
  const __dirname = dirname(fileURLToPath(import.meta.url));
54
95
  const TOKEN_FILE = join(__dirname, "tokens.json");
55
96
  const PASSKEY_FILE = join(__dirname, "passkeys.json");
56
97
  const WALLET_FILE_LEGACY = join(__dirname, "wallets.json");
57
98
 
58
- // Initialize Prisma (may fail if DATABASE_URL not set)
59
99
  let prisma = null;
60
100
  let usePrisma = false;
61
101
  try {
@@ -64,30 +104,102 @@ try {
64
104
  usePrisma = true;
65
105
  console.log("Database: Postgres via Prisma");
66
106
  } catch (err) {
67
- console.log("Database: JSON files (Prisma not available: " + err.message + ")");
107
+ if (!DEV_MODE) {
108
+ console.error("FATAL: Prisma unavailable; refusing to start.");
109
+ console.error("Cause: " + err.message);
110
+ console.error("Set LDM_HOSTED_MCP_DEV_MODE=1 to allow the JSON fallback (dev only).");
111
+ process.exit(1);
112
+ }
113
+ console.warn("Database: JSON files (DEV MODE; Prisma not available: " + err.message + ")");
68
114
  }
69
115
 
70
116
  // ── API Keys ────────────────────────────────────────────────────────
117
+ //
118
+ // Hardcoded production defaults removed (F-002). Production keys live
119
+ // in the Postgres ApiKey table and are loaded on boot. In DEV_MODE,
120
+ // the local tokens.json file is also seeded into the in-memory cache.
71
121
 
72
- // Hardcoded defaults (always available, even without DB)
73
- const DEFAULT_API_KEYS = {
74
- "ck-test-001": "test-agent",
75
- "ck-e04df46877aa3672e21c4e33149bacc4": "cc-mini",
76
- "ck-f1986e957e21cbb40dc100bc05dc78ec": "lesa",
77
- "ck-c2849eef903407c877bc6e79bf8794aa": "parker",
78
- };
122
+ const API_KEYS = {};
123
+ const API_KEY_HANDLES = {};
124
+ const ACCOUNT_TENANT_PREFIX = "acct:";
125
+ const LEGACY_API_KEY_TENANT_PREFIX = "key:";
126
+ const OAUTH_API_KEY_TENANT_PREFIX = "oauth:";
127
+
128
+ function isInternalTenantId(id) {
129
+ return typeof id === "string"
130
+ && (id.startsWith(ACCOUNT_TENANT_PREFIX)
131
+ || id.startsWith(LEGACY_API_KEY_TENANT_PREFIX)
132
+ || id.startsWith(OAUTH_API_KEY_TENANT_PREFIX));
133
+ }
134
+
135
+ function accountTenantIdForUserId(userId) {
136
+ return ACCOUNT_TENANT_PREFIX + userId;
137
+ }
138
+
139
+ function legacyTenantIdForApiKey(key) {
140
+ return LEGACY_API_KEY_TENANT_PREFIX + createHash("sha256").update(key).digest("base64url").slice(0, 32);
141
+ }
142
+
143
+ function oauthTenantIdForApiKey(key) {
144
+ return OAUTH_API_KEY_TENANT_PREFIX + createHash("sha256").update(key).digest("base64url").slice(0, 32);
145
+ }
146
+
147
+ function rememberApiKeyInMemory(key, tenantId, handle = null) {
148
+ API_KEYS[key] = tenantId;
149
+ if (handle) API_KEY_HANDLES[key] = handle;
150
+ else delete API_KEY_HANDLES[key];
151
+ }
79
152
 
80
- // In-memory cache (populated from DB or JSON on boot)
81
- const API_KEYS = { ...DEFAULT_API_KEYS };
153
+ function rememberLoadedApiKey(key, storedAgentId) {
154
+ const tenantId = isInternalTenantId(storedAgentId) ? storedAgentId : legacyTenantIdForApiKey(key);
155
+ const handle = isInternalTenantId(storedAgentId) ? null : storedAgentId;
156
+ rememberApiKeyInMemory(key, tenantId, handle);
157
+ }
158
+
159
+ function identityForApiKey(key) {
160
+ const tenantId = API_KEYS[key];
161
+ if (!tenantId) return null;
162
+ return {
163
+ agentId: tenantId,
164
+ tenantId,
165
+ handle: API_KEY_HANDLES[key] || tenantId,
166
+ apiKey: key,
167
+ };
168
+ }
82
169
 
83
- // Load from JSON (fallback)
84
170
  function loadTokensFromFile() {
85
- try { return JSON.parse(readFileSync(TOKEN_FILE, "utf8")); } catch { return {}; }
171
+ let rows = {};
172
+ try { rows = JSON.parse(readFileSync(TOKEN_FILE, "utf8")); } catch { return; }
173
+ for (const [key, storedAgentId] of Object.entries(rows)) {
174
+ rememberLoadedApiKey(key, storedAgentId);
175
+ }
176
+ }
177
+
178
+ async function loadApiKeysFromDb() {
179
+ if (!usePrisma) return;
180
+ try {
181
+ const rows = await prisma.apiKey.findMany();
182
+ for (const row of rows) rememberLoadedApiKey(row.key, row.agentId);
183
+ } catch (err) {
184
+ if (!DEV_MODE) {
185
+ console.error("FATAL: Prisma loadApiKeys failed; refusing to start.");
186
+ console.error("Cause: " + err.message);
187
+ process.exit(1);
188
+ }
189
+ console.error("Prisma loadApiKeys error (DEV_MODE):", err.message);
190
+ }
191
+ }
192
+
193
+ if (DEV_MODE) {
194
+ loadTokensFromFile();
86
195
  }
87
- Object.assign(API_KEYS, loadTokensFromFile());
196
+ await loadApiKeysFromDb();
88
197
 
89
- async function saveApiKey(key, agentId) {
90
- API_KEYS[key] = agentId;
198
+ async function saveApiKey(key, agentId, { handle = null } = {}) {
199
+ // Persist before advertising in memory: a newly issued key must not
200
+ // become valid in the in-memory cache if the canonical store did not
201
+ // accept it. Otherwise the key would work for the lifetime of the
202
+ // process and disappear on restart.
91
203
  if (usePrisma) {
92
204
  try {
93
205
  await prisma.apiKey.upsert({
@@ -97,10 +209,17 @@ async function saveApiKey(key, agentId) {
97
209
  });
98
210
  } catch (err) {
99
211
  console.error("Prisma saveApiKey error:", err.message);
212
+ if (!DEV_MODE) throw new Error("saveApiKey persistence failed: " + err.message);
100
213
  }
214
+ } else if (!DEV_MODE) {
215
+ // Production should never reach here (boot exits if Prisma is
216
+ // unavailable), but guard explicitly.
217
+ throw new Error("saveApiKey called without Prisma in production");
218
+ }
219
+ rememberApiKeyInMemory(key, agentId, handle);
220
+ if (DEV_MODE) {
221
+ try { writeFileSync(TOKEN_FILE, JSON.stringify(API_KEYS, null, 2) + "\n"); } catch {}
101
222
  }
102
- // Always write JSON as backup
103
- try { writeFileSync(TOKEN_FILE, JSON.stringify(API_KEYS, null, 2) + "\n"); } catch {}
104
223
  }
105
224
 
106
225
  // ── Passkeys ────────────────────────────────────────────────────────
@@ -113,33 +232,72 @@ function loadPasskeysFromFile() {
113
232
  }
114
233
 
115
234
  async function loadPasskeysFromDb() {
116
- if (!usePrisma) return loadPasskeysFromFile();
235
+ if (!usePrisma) {
236
+ return DEV_MODE ? loadPasskeysFromFile() : [];
237
+ }
117
238
  try {
118
239
  const creds = await prisma.credential.findMany({ include: { user: true } });
119
- return creds.map(c => ({
120
- credentialId: c.id,
121
- publicKey: Buffer.from(c.publicKey).toString("base64url"),
122
- counter: c.counter,
123
- userId: c.userId,
124
- agentId: c.user?.name ? "passkey-" + c.user.name.slice(0, 12) : "unknown",
125
- createdAt: c.createdAt.toISOString(),
126
- transports: c.transports || [],
127
- }));
240
+ const handleUserIds = new Map();
241
+ for (const c of creds) {
242
+ const handle = c.user?.name || (c.userId ? "user-" + c.userId.slice(0, 8) : "unknown");
243
+ if (!handleUserIds.has(handle)) handleUserIds.set(handle, new Set());
244
+ handleUserIds.get(handle).add(c.userId);
245
+ }
246
+ const out = [];
247
+ for (const c of creds) {
248
+ const handle = c.user?.name || (c.userId ? "user-" + c.userId.slice(0, 8) : "unknown");
249
+ const agentId = accountTenantIdForUserId(c.userId);
250
+ let apiKey = null;
251
+ for (const [key, tenantId] of Object.entries(API_KEYS)) {
252
+ if (tenantId === agentId || (handleUserIds.get(handle)?.size === 1 && API_KEY_HANDLES[key] === handle)) {
253
+ apiKey = key;
254
+ break;
255
+ }
256
+ }
257
+ if (apiKey) {
258
+ API_KEY_HANDLES[apiKey] = handle;
259
+ if (API_KEYS[apiKey] !== agentId) {
260
+ try {
261
+ await saveApiKey(apiKey, agentId, { handle });
262
+ console.log("loadPasskeysFromDb: migrated API key tenant for handle '" + handle + "' to immutable account id");
263
+ } catch (err) {
264
+ console.error("loadPasskeysFromDb: failed to migrate API key tenant for handle '" + handle + "':", err.message);
265
+ if (!DEV_MODE) throw err;
266
+ }
267
+ }
268
+ } else if (handle !== "unknown") {
269
+ console.warn("loadPasskeysFromDb: no ApiKey row for account tenant '" + agentId + "'; auth-verify will mint on next successful login");
270
+ }
271
+ out.push({
272
+ credentialId: c.id,
273
+ publicKey: Buffer.from(c.publicKey).toString("base64url"),
274
+ counter: c.counter,
275
+ userId: c.userId,
276
+ agentId,
277
+ handle,
278
+ apiKey,
279
+ createdAt: c.createdAt.toISOString(),
280
+ transports: c.transports || [],
281
+ });
282
+ }
283
+ return out;
128
284
  } catch (err) {
129
285
  console.error("Prisma loadPasskeys error:", err.message);
130
- return loadPasskeysFromFile();
286
+ return DEV_MODE ? loadPasskeysFromFile() : [];
131
287
  }
132
288
  }
133
289
 
134
290
  async function savePasskey(entry) {
135
- passkeys.push(entry);
291
+ // Persist before pushing to in-memory: a passkey must not exist in
292
+ // memory if it was never persisted, or it would authenticate for the
293
+ // lifetime of the process and disappear on restart.
136
294
  if (usePrisma) {
137
295
  try {
138
296
  // Ensure user exists
139
297
  let user = await prisma.user.findUnique({ where: { id: entry.userId } });
140
298
  if (!user) {
141
299
  user = await prisma.user.create({
142
- data: { id: entry.userId, name: entry.agentId || "user" },
300
+ data: { id: entry.userId, name: entry.handle || "user" },
143
301
  });
144
302
  }
145
303
  await prisma.credential.create({
@@ -153,15 +311,22 @@ async function savePasskey(entry) {
153
311
  });
154
312
  } catch (err) {
155
313
  console.error("Prisma savePasskey error:", err.message);
314
+ if (!DEV_MODE) throw new Error("savePasskey persistence failed: " + err.message);
156
315
  }
316
+ } else if (!DEV_MODE) {
317
+ throw new Error("savePasskey called without Prisma in production");
318
+ }
319
+ passkeys.push(entry);
320
+ if (DEV_MODE) {
321
+ try { writeFileSync(PASSKEY_FILE, JSON.stringify(passkeys, null, 2) + "\n"); } catch {}
157
322
  }
158
- // Always write JSON as backup
159
- try { writeFileSync(PASSKEY_FILE, JSON.stringify(passkeys, null, 2) + "\n"); } catch {}
160
323
  }
161
324
 
162
325
  async function updatePasskeyCounter(credentialId, newCounter) {
163
- const entry = passkeys.find(p => p.credentialId === credentialId);
164
- if (entry) entry.counter = newCounter;
326
+ // Persist before updating in-memory. The counter is the WebAuthn
327
+ // replay-protection state; advancing it in memory while the DB row
328
+ // stays behind would let a replayed assertion validate after a
329
+ // restart re-loaded the stale counter.
165
330
  if (usePrisma) {
166
331
  try {
167
332
  await prisma.credential.update({
@@ -170,9 +335,16 @@ async function updatePasskeyCounter(credentialId, newCounter) {
170
335
  });
171
336
  } catch (err) {
172
337
  console.error("Prisma updateCounter error:", err.message);
338
+ if (!DEV_MODE) throw new Error("updatePasskeyCounter persistence failed: " + err.message);
173
339
  }
340
+ } else if (!DEV_MODE) {
341
+ throw new Error("updatePasskeyCounter called without Prisma in production");
342
+ }
343
+ const entry = passkeys.find(p => p.credentialId === credentialId);
344
+ if (entry) entry.counter = newCounter;
345
+ if (DEV_MODE) {
346
+ try { writeFileSync(PASSKEY_FILE, JSON.stringify(passkeys, null, 2) + "\n"); } catch {}
174
347
  }
175
- try { writeFileSync(PASSKEY_FILE, JSON.stringify(passkeys, null, 2) + "\n"); } catch {}
176
348
  }
177
349
 
178
350
  // Boot: load passkeys
@@ -219,7 +391,7 @@ function authenticate(req) {
219
391
  const auth = req.headers["authorization"];
220
392
  if (!auth?.startsWith("Bearer ")) return null;
221
393
  const key = auth.slice(7).trim();
222
- return API_KEYS[key] ? { agentId: API_KEYS[key], apiKey: key } : null;
394
+ return identityForApiKey(key);
223
395
  }
224
396
 
225
397
  function readBody(req) {
@@ -275,13 +447,92 @@ function parseUrl(reqUrl) {
275
447
  return new URL(reqUrl, "http://localhost");
276
448
  }
277
449
 
450
+ // ── Rate limiting (F-008 in the VPS hosted-mcp audit) ───────────────
451
+ //
452
+ // Per-IP, per-bucket fixed-window counter. In-process Map; resets on
453
+ // restart. nginx-side limit_req would be more durable but harder to
454
+ // scope per route; in-process keeps the policy with the code that
455
+ // mints/validates the auth tokens. Defaults are conservative; tune via
456
+ // env. Stale entries are pruned periodically so memory stays bounded.
457
+ //
458
+ // Buckets:
459
+ // mint ... endpoints that issue a credential or ticket
460
+ // validate ... endpoints that consume / verify a credential
461
+ // status ... poll-friendly endpoints (higher limit)
462
+
463
+ const RATE_LIMIT_BUCKETS = {
464
+ mint: { limit: parseInt(process.env.LDM_HOSTED_MCP_RL_MINT || "30", 10), windowMs: 60_000 },
465
+ validate: { limit: parseInt(process.env.LDM_HOSTED_MCP_RL_VALIDATE || "60", 10), windowMs: 60_000 },
466
+ status: { limit: parseInt(process.env.LDM_HOSTED_MCP_RL_STATUS || "120", 10), windowMs: 60_000 },
467
+ };
468
+
469
+ const rateLimitState = new Map(); // key: "<bucket>:<ip>" -> { count, windowStart }
470
+
471
+ function getClientIp(req) {
472
+ // Prefer X-Real-IP (nginx overwrites on proxy hop, harder to spoof
473
+ // through the proxy). Fall back to the LAST entry in X-Forwarded-For
474
+ // (nginx appends $remote_addr via proxy_add_x_forwarded_for, so the
475
+ // last entry is the real client IP from nginx's perspective; the
476
+ // first entries are attacker-controlled). Last fallback: socket.
477
+ const xRealIp = req.headers["x-real-ip"];
478
+ if (typeof xRealIp === "string" && xRealIp.length > 0) return xRealIp.trim();
479
+ const xff = req.headers["x-forwarded-for"];
480
+ if (typeof xff === "string" && xff.length > 0) {
481
+ const parts = xff.split(",").map(s => s.trim()).filter(Boolean);
482
+ if (parts.length > 0) return parts[parts.length - 1];
483
+ }
484
+ return req.socket?.remoteAddress || "unknown";
485
+ }
486
+
487
+ function rateLimitCheck(req, bucket) {
488
+ const config = RATE_LIMIT_BUCKETS[bucket];
489
+ if (!config) return { ok: true };
490
+ const ip = getClientIp(req);
491
+ const key = bucket + ":" + ip;
492
+ const now = Date.now();
493
+ const entry = rateLimitState.get(key);
494
+ if (!entry || now - entry.windowStart > config.windowMs) {
495
+ rateLimitState.set(key, { count: 1, windowStart: now });
496
+ return { ok: true };
497
+ }
498
+ entry.count += 1;
499
+ if (entry.count > config.limit) {
500
+ const retryAfterSec = Math.max(1, Math.ceil((config.windowMs - (now - entry.windowStart)) / 1000));
501
+ return { ok: false, retryAfterSec };
502
+ }
503
+ return { ok: true };
504
+ }
505
+
506
+ // Returns true if the request is allowed. If limited, writes 429 and
507
+ // returns false; the caller must `return` immediately on false.
508
+ function applyRateLimit(req, res, bucket) {
509
+ const result = rateLimitCheck(req, bucket);
510
+ if (!result.ok) {
511
+ res.setHeader("Retry-After", String(result.retryAfterSec));
512
+ json(res, 429, { error: "rate_limit_exceeded", error_description: "Too many requests. Retry after " + result.retryAfterSec + "s." });
513
+ console.warn("rate-limit hit:", bucket, getClientIp(req), req.method, req.url?.split("?")[0]);
514
+ return false;
515
+ }
516
+ return true;
517
+ }
518
+
519
+ // Keep memory bounded: drop entries older than 2 windows.
520
+ setInterval(() => {
521
+ const now = Date.now();
522
+ for (const [key, entry] of rateLimitState) {
523
+ if (now - entry.windowStart > 2 * 60_000) {
524
+ rateLimitState.delete(key);
525
+ }
526
+ }
527
+ }, 5 * 60_000).unref();
528
+
278
529
  function esc(s) {
279
530
  return s.replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
280
531
  }
281
532
 
282
- function sanitizeUsername(raw) {
533
+ function sanitizeDisplayLabel(raw) {
283
534
  if (!raw || typeof raw !== "string") return null;
284
- const cleaned = raw.toLowerCase().replace(/[^a-z0-9-]/g, "").slice(0, 30);
535
+ const cleaned = raw.replace(/[\u0000-\u001f\u007f]/g, "").replace(/\s+/g, " ").trim().slice(0, 64);
285
536
  return cleaned.length > 0 ? cleaned : null;
286
537
  }
287
538
 
@@ -406,14 +657,17 @@ async function handleRegisterOptions(req, res) {
406
657
  let body;
407
658
  try { body = await readBody(req); } catch { body = {}; }
408
659
 
409
- // Accept optional username from request body
410
- const username = sanitizeUsername(body?.username);
660
+ // Accept the existing `username` field for wire compatibility, but
661
+ // treat it only as a display label for the passkey prompt. It is not
662
+ // a public username, account handle, or relay tenant boundary.
663
+ // Duplicate display labels are allowed.
664
+ const displayLabel = sanitizeDisplayLabel(body?.displayName || body?.username);
411
665
 
412
666
  const userId = randomBytes(16);
413
667
  const userIdB64 = userId.toString("base64url");
414
668
 
415
- const userName = username || ("user-" + userIdB64.slice(0, 8));
416
- const displayName = username || "Memory Crystal User";
669
+ const userName = displayLabel || ("user-" + userIdB64.slice(0, 8));
670
+ const displayName = displayLabel || "Memory Crystal User";
417
671
 
418
672
  let options;
419
673
  try {
@@ -442,7 +696,7 @@ async function handleRegisterOptions(req, res) {
442
696
  challenge: options.challenge,
443
697
  type: "registration",
444
698
  userId: userIdB64,
445
- username: username,
699
+ displayLabel,
446
700
  expires: Date.now() + 120000,
447
701
  };
448
702
 
@@ -495,8 +749,16 @@ async function handleRegisterVerify(req, res) {
495
749
 
496
750
  const { credential: cred, credentialDeviceType, credentialBackedUp } = verification.registrationInfo;
497
751
 
498
- // Use provided username as agentId, or fall back to passkey-<id>
499
- const agentId = stored.username || ("passkey-" + stored.userId.slice(0, 12));
752
+ // Internal tenancy is the immutable WebAuthn user id. The user-entered
753
+ // display label is metadata only and never owns a relay namespace.
754
+ const agentId = accountTenantIdForUserId(stored.userId);
755
+ // credentialLabel matches the userName passed to
756
+ // generateRegistrationOptions in handleRegisterOptions, which is what
757
+ // iOS Passwords / 1Password show next to the saved passkey. The
758
+ // welcome view should display this, not agentId. Auth semantics are
759
+ // unchanged; only the user-facing label is aligned with the saved
760
+ // credential.
761
+ const credentialLabel = stored.displayLabel || ("user-" + stored.userId.slice(0, 8));
500
762
  const apiKey = generateApiKey();
501
763
 
502
764
  const entry = {
@@ -505,18 +767,32 @@ async function handleRegisterVerify(req, res) {
505
767
  counter: cred.counter,
506
768
  userId: stored.userId,
507
769
  agentId,
770
+ handle: credentialLabel,
508
771
  apiKey,
509
772
  deviceType: credentialDeviceType,
510
773
  backedUp: credentialBackedUp,
511
774
  transports: credential.response?.transports || [],
512
775
  createdAt: new Date().toISOString(),
513
776
  };
514
- await savePasskey(entry);
515
- await saveApiKey(apiKey, agentId);
777
+ try {
778
+ await savePasskey(entry);
779
+ await saveApiKey(apiKey, agentId, { handle: credentialLabel });
780
+ } catch (err) {
781
+ console.error("Persistence failure during passkey registration:", err.message);
782
+ json(res, 500, { error: "persistence_failure", error_description: "Could not persist credentials. Try again." });
783
+ return;
784
+ }
516
785
 
517
- console.log("WebAuthn: registered passkey for agent '" + agentId + "' (credId: " + cred.id.slice(0, 16) + "...)");
786
+ console.log("WebAuthn: registered passkey for tenant '" + agentId + "' handle '" + credentialLabel + "' (credId: " + cred.id.slice(0, 16) + "...)");
518
787
 
519
- json(res, 200, { success: true, agentId, apiKey });
788
+ json(res, 200, {
789
+ success: true,
790
+ agentId: credentialLabel,
791
+ tenantId: agentId,
792
+ apiKey,
793
+ credentialLabel,
794
+ codex_pair_presence_token: generateCodexPairPresenceToken(agentId),
795
+ });
520
796
  }
521
797
 
522
798
  // POST /webauthn/auth-options
@@ -602,12 +878,60 @@ async function handleAuthVerify(req, res) {
602
878
  return;
603
879
  }
604
880
 
605
- entry.counter = verification.authenticationInfo.newCounter;
606
- await updatePasskeyCounter(entry.credentialId, entry.counter);
881
+ // Persist new counter before mutating in-memory entry. updatePasskeyCounter
882
+ // performs the in-memory update only on success, so the in-memory counter
883
+ // stays consistent with the DB and replay protection holds across restarts.
884
+ try {
885
+ await updatePasskeyCounter(entry.credentialId, verification.authenticationInfo.newCounter);
886
+ } catch (err) {
887
+ console.error("Persistence failure during passkey counter update:", err.message);
888
+ json(res, 500, { error: "persistence_failure", error_description: "Could not persist counter. Try again." });
889
+ return;
890
+ }
891
+
892
+ let credentialLabel = entry.handle;
893
+ if (!credentialLabel && entry.agentId && entry.agentId.startsWith("passkey-")) {
894
+ credentialLabel = (typeof entry.userId === "string" && entry.userId.length >= 8)
895
+ ? "user-" + entry.userId.slice(0, 8)
896
+ : entry.agentId;
897
+ } else if (!credentialLabel && !isInternalTenantId(entry.agentId)) {
898
+ credentialLabel = entry.agentId;
899
+ } else if (!credentialLabel && typeof entry.userId === "string" && entry.userId.length >= 8) {
900
+ credentialLabel = "user-" + entry.userId.slice(0, 8);
901
+ } else if (!credentialLabel) {
902
+ credentialLabel = "you";
903
+ }
904
+
905
+ // Recovery path: a passkey reloaded from Postgres after a restart may
906
+ // have entry.apiKey = null if no ApiKey row was found for its agent
907
+ // at boot. Mint a fresh ck- now so the login response always carries
908
+ // a usable token. Without this, the browser would store
909
+ // sessionStorage.wip_api_key = null and Remote Control would 401 on
910
+ // /bootstrap and /ws-ticket.
911
+ if (!entry.apiKey) {
912
+ const newKey = generateApiKey();
913
+ try {
914
+ await saveApiKey(newKey, entry.agentId, { handle: credentialLabel });
915
+ } catch (err) {
916
+ console.error("Persistence failure minting recovery key for tenant '" + entry.agentId + "':", err.message);
917
+ json(res, 500, { error: "persistence_failure", error_description: "Could not mint API key. Try again." });
918
+ return;
919
+ }
920
+ entry.apiKey = newKey;
921
+ entry.handle = credentialLabel;
922
+ console.log("WebAuthn: minted recovery key for tenant '" + entry.agentId + "' (key: " + newKey.slice(0, 6) + "...)");
923
+ }
607
924
 
608
- console.log("WebAuthn: authenticated agent '" + entry.agentId + "'");
925
+ console.log("WebAuthn: authenticated tenant '" + entry.agentId + "' handle '" + credentialLabel + "'");
609
926
 
610
- json(res, 200, { success: true, agentId: entry.agentId, apiKey: entry.apiKey });
927
+ json(res, 200, {
928
+ success: true,
929
+ agentId: credentialLabel,
930
+ tenantId: entry.agentId,
931
+ apiKey: entry.apiKey,
932
+ credentialLabel,
933
+ codex_pair_presence_token: generateCodexPairPresenceToken(entry.agentId),
934
+ });
611
935
  }
612
936
 
613
937
  // ---------- Page handlers ----------
@@ -1018,19 +1342,18 @@ async function handleOAuthToken(req, res) {
1018
1342
  }
1019
1343
  }
1020
1344
 
1021
- // Check if agent already has an API key (from passkey registration)
1022
- const agentId = stored.agent_name || "oauth-user";
1023
- let apiKey;
1024
-
1025
- const existingKey = Object.entries(API_KEYS).find(([k, v]) => v === agentId);
1026
- if (existingKey) {
1027
- apiKey = existingKey[0];
1028
- } else {
1029
- apiKey = generateApiKey();
1030
- await saveApiKey(apiKey, agentId);
1345
+ const agentHandle = stored.agent_name || "oauth-user";
1346
+ const apiKey = generateApiKey();
1347
+ const agentId = oauthTenantIdForApiKey(apiKey);
1348
+ try {
1349
+ await saveApiKey(apiKey, agentId, { handle: agentHandle });
1350
+ } catch (err) {
1351
+ console.error("Persistence failure during OAuth token issuance:", err.message);
1352
+ json(res, 500, { error: "server_error", error_description: "Could not issue token. Try again." });
1353
+ return;
1031
1354
  }
1032
1355
 
1033
- console.log("OAuth: issued token for agent '" + agentId + "' (key: " + apiKey.slice(0, 10) + "...)");
1356
+ console.log("OAuth: issued token for tenant '" + agentId + "' handle '" + agentHandle + "' (key: " + apiKey.slice(0, 10) + "...)");
1034
1357
 
1035
1358
  json(res, 200, {
1036
1359
  access_token: apiKey,
@@ -1383,12 +1706,53 @@ function handleAgentAuthApprove(req, res) {
1383
1706
 
1384
1707
  // ---------- QR Login (Chrome fallback) ----------
1385
1708
 
1709
+ // `next` whitelist for the QR login flow. Two shapes are allowed; both
1710
+ // land the user on a known phone-side surface after successful sign-in.
1711
+ // Anything else is silently dropped. `next` is NOT a general redirect
1712
+ // primitive.
1713
+ //
1714
+ // 1. PAIR_NEXT_REGEX: /pair/<CODE> using the daemon's real alphabet
1715
+ // (CODEX_PAIR_ALPHABET, length 6, L IS included; I/O/0/1 excluded).
1716
+ // See plan ai/product/plans-prds/codex-remote-control/
1717
+ // 2026-04-30--cc-mini--pair-via-login-qr-flow.md constraints C1,
1718
+ // C8, and round-5. Per C8 the URL fallback for this shape is
1719
+ // mobile-only (desktop must not become the pairing authority).
1720
+ //
1721
+ // 2. REMOTE_CONTROL_NEXT_REGEX: /codex-remote-control/<UUID> for the
1722
+ // Kaleidoscope phone-side remote-control thread surface. Standard
1723
+ // ?next semantics; allowed on both desktop and mobile (this is
1724
+ // navigation continuation, not authority transfer).
1725
+ const PAIR_NEXT_REGEX = /^\/pair\/[ABCDEFGHJKLMNPQRSTUVWXYZ23456789]{6}$/;
1726
+ const REMOTE_CONTROL_NEXT_REGEX = /^\/codex-remote-control\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
1727
+
1728
+ function sanitizeCrcPairNext(raw) {
1729
+ if (typeof raw !== "string") return null;
1730
+ // Single decode; reject if a second decode would still differ.
1731
+ let decoded;
1732
+ try { decoded = decodeURIComponent(raw); } catch { return null; }
1733
+ // Catch double-encoded payloads.
1734
+ if (decoded !== raw && /%/.test(decoded)) return null;
1735
+ if (!PAIR_NEXT_REGEX.test(decoded) && !REMOTE_CONTROL_NEXT_REGEX.test(decoded)) return null;
1736
+ return decoded;
1737
+ }
1738
+
1386
1739
  // POST /api/qr-login ... create a QR login session
1387
1740
  async function handleQrLoginStart(req, res) {
1388
1741
  cleanupExpiredChallenges();
1389
1742
  const body = await readBody(req).catch(() => ({}));
1390
1743
  const handle = ((body && body.handle) || "").trim().toLowerCase().replace(/[^a-z0-9-]/g, "").slice(0, 30);
1391
1744
  const mode = ((body && body.mode) || "register") === "signin" ? "signin" : "register";
1745
+ // Validate `next` strictly. Invalid next is silently dropped, not
1746
+ // 400'd, so legacy callers still work.
1747
+ //
1748
+ // Only /pair/<CODE> next triggers pair-mode (C6 strip on desktop
1749
+ // status, C8 desktop-no-redirect, the "phone is the actor" model).
1750
+ // /codex-remote-control/<UUID> is a normal post-login continuation:
1751
+ // desktop status returns the full login response (apiKey, handle,
1752
+ // next) so the desktop poll can authenticate and redirect on its
1753
+ // own. The phone also gets next via approve, so both ends can act.
1754
+ const next = sanitizeCrcPairNext(body && body.next);
1755
+ const purpose = (next && PAIR_NEXT_REGEX.test(next)) ? "pair" : null;
1392
1756
  const sessionId = randomUUID();
1393
1757
  const loginUrl = ISSUER_URL + "/login?s=" + sessionId + "&m=" + mode + (handle ? "&h=" + encodeURIComponent(handle) : "");
1394
1758
  const qrBuffer = await QRCode.toBuffer(loginUrl, { type: "png", width: 400, margin: 2 });
@@ -1399,8 +1763,10 @@ async function handleQrLoginStart(req, res) {
1399
1763
  apiKey: null,
1400
1764
  handle: handle || null,
1401
1765
  expires: Date.now() + QR_LOGIN_EXPIRY_MS,
1766
+ purpose, // "pair" | null
1767
+ next: next || null, // sanitized `/pair/<CODE>` or null
1402
1768
  };
1403
- console.log("QR login: created session " + sessionId.slice(0, 8) + "...");
1769
+ console.log("QR login: created session " + sessionId.slice(0, 8) + "..." + (purpose === "pair" ? " (pair-mode)" : ""));
1404
1770
  json(res, 200, { sessionId, qrUrl: "/api/qr-login/qr?s=" + sessionId });
1405
1771
  }
1406
1772
 
@@ -1418,6 +1784,12 @@ function handleQrLoginQR(req, res) {
1418
1784
  }
1419
1785
 
1420
1786
  // GET /api/qr-login/status?s=XXX ... poll for completion
1787
+ //
1788
+ // Response shape depends on `purpose`:
1789
+ // - Pair-mode (purpose === "pair"): {status, agentId} only on approved.
1790
+ // NEVER returns apiKey or next to the desktop. Phone receives next via
1791
+ // /api/qr-login/approve. Per plan C6 round 4.
1792
+ // - Legacy login mode: {status, agentId, apiKey} on approved (unchanged).
1421
1793
  function handleQrLoginStatus(req, res) {
1422
1794
  const url = parseUrl(req.url);
1423
1795
  const s = url.searchParams.get("s");
@@ -1427,7 +1799,28 @@ function handleQrLoginStatus(req, res) {
1427
1799
  return;
1428
1800
  }
1429
1801
  if (entry.status === "approved") {
1430
- json(res, 200, { status: "approved", agentId: entry.agentId, apiKey: entry.apiKey });
1802
+ if (entry.purpose === "pair") {
1803
+ // Pair-mode (purpose === "pair", next === /pair/<CODE>):
1804
+ // desktop gets ONLY a display label. No apiKey. No next. Plan
1805
+ // C6 round 4. Desktop never becomes the pairing authority.
1806
+ json(res, 200, { status: "approved", agentId: entry.agentId });
1807
+ } else {
1808
+ // Legacy login mode OR codex-remote-control continuation
1809
+ // (purpose === null). Desktop gets full identity to render the
1810
+ // welcome view OR redirect to next on its own poll.
1811
+ // credentialLabel matches the saved-passkey label (see
1812
+ // register-verify / auth-verify). next is included only if a
1813
+ // sanitized non-pair-mode next was set on the session
1814
+ // (currently /codex-remote-control/<UUID>); legacy login
1815
+ // sessions without next get next === null.
1816
+ json(res, 200, {
1817
+ status: "approved",
1818
+ agentId: entry.agentId,
1819
+ apiKey: entry.apiKey,
1820
+ credentialLabel: entry.credentialLabel || null,
1821
+ next: entry.next || null,
1822
+ });
1823
+ }
1431
1824
  delete qrLoginSessions[s]; // one-time use
1432
1825
  } else {
1433
1826
  json(res, 200, { status: "pending" });
@@ -1435,9 +1828,13 @@ function handleQrLoginStatus(req, res) {
1435
1828
  }
1436
1829
 
1437
1830
  // POST /api/qr-login/approve ... phone calls after passkey created
1831
+ //
1832
+ // In pair-mode, the response includes the sanitized `next` so the phone
1833
+ // can location.replace(next) into /pair/<CODE>. Legacy login mode returns
1834
+ // {ok: true} unchanged.
1438
1835
  function handleQrLoginApprove(req, res) {
1439
1836
  readBody(req).then(function(body) {
1440
- const { sessionId, agentId, apiKey } = body || {};
1837
+ const { sessionId, agentId, apiKey, credentialLabel } = body || {};
1441
1838
  const entry = qrLoginSessions[sessionId];
1442
1839
  if (!entry || Date.now() > entry.expires) {
1443
1840
  json(res, 404, { error: "Session not found or expired" });
@@ -1450,8 +1847,21 @@ function handleQrLoginApprove(req, res) {
1450
1847
  entry.status = "approved";
1451
1848
  entry.agentId = agentId;
1452
1849
  entry.apiKey = apiKey;
1453
- console.log("QR login: approved session " + sessionId.slice(0, 8) + "... for '" + agentId + "'");
1454
- json(res, 200, { ok: true });
1850
+ // Phone-side passes the label it received from register-verify /
1851
+ // auth-verify so the desktop can show the same string the user
1852
+ // just saved on their phone. Optional for back-compat.
1853
+ entry.credentialLabel = (typeof credentialLabel === "string" && credentialLabel.length <= 64) ? credentialLabel : null;
1854
+ console.log("QR login: approved session " + sessionId.slice(0, 8) + "... for '" + agentId + "'" + (entry.purpose === "pair" ? " (pair-mode)" : (entry.next ? " (next=" + entry.next + ")" : "")));
1855
+ // Phone receives next on approve regardless of purpose, so the
1856
+ // phone can redirect to either /pair/<CODE> (pair-mode, phone is
1857
+ // the actor) or /codex-remote-control/<UUID> (continuation, phone
1858
+ // can act). Desktop's separate behavior (strip vs full response)
1859
+ // is handled in handleQrLoginStatus.
1860
+ if (entry.next) {
1861
+ json(res, 200, { ok: true, next: entry.next });
1862
+ } else {
1863
+ json(res, 200, { ok: true });
1864
+ }
1455
1865
  }).catch(function() {
1456
1866
  json(res, 400, { error: "Invalid request" });
1457
1867
  });
@@ -1876,13 +2286,28 @@ const httpServer = createServer(async (req, res) => {
1876
2286
  }
1877
2287
 
1878
2288
  if (req.method === "GET" && (path === "/login" || path === "/login/")) {
1879
- // Serve the new app/ login (two-path: this device or QR-from-phone).
2289
+ // Production /login owns its own file at app/kaleidoscope-login.html.
2290
+ //
2291
+ // Earlier this route served demo/login.html, which made production
2292
+ // auth depend on a file under demo/. That coupling is wrong: demo/
2293
+ // is the demo site's domain, not production. The canonical
2294
+ // Kaleidoscope login HTML now lives under app/, where production
2295
+ // owns it.
2296
+ //
2297
+ // Fallback chain (defense in depth):
2298
+ // 1. app/kaleidoscope-login.html ... canonical production file.
2299
+ // 2. demo/login.html ... legacy fallback during the
2300
+ // transition; will be removed in a follow-up once the
2301
+ // production file is verified live.
2302
+ // 3. handleLoginPage ... server-rendered last resort.
2303
+ //
2304
+ // /login/app continues to serve the developed app/login.html flow
2305
+ // (see the next handler).
1880
2306
  try {
1881
- const loginHtml = readFileSync(join(__dirname, "app", "login.html"), "utf8");
2307
+ const html = readFileSync(join(__dirname, "app", "kaleidoscope-login.html"), "utf8");
1882
2308
  res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
1883
- res.end(loginHtml);
2309
+ res.end(html);
1884
2310
  } catch {
1885
- // Fallback to legacy demo login, then server-rendered.
1886
2311
  try {
1887
2312
  const legacy = readFileSync(join(__dirname, "demo", "login.html"), "utf8");
1888
2313
  res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
@@ -1894,6 +2319,22 @@ const httpServer = createServer(async (req, res) => {
1894
2319
  return;
1895
2320
  }
1896
2321
 
2322
+ if (req.method === "GET" && (path === "/login/app" || path === "/login/app/")) {
2323
+ // Explicit non-primary route for the app/login.html flow (the
2324
+ // newer two-path "this device or QR-from-phone" copy). This
2325
+ // exists so the developed flow stays reachable without hijacking
2326
+ // /login. If app/login.html is not present, return 404 rather
2327
+ // than silently falling back to the canonical /login page.
2328
+ try {
2329
+ const loginHtml = readFileSync(join(__dirname, "app", "login.html"), "utf8");
2330
+ res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
2331
+ res.end(loginHtml);
2332
+ } catch {
2333
+ json(res, 404, { error: "Not found" });
2334
+ }
2335
+ return;
2336
+ }
2337
+
1897
2338
  // --- Legal pages ---
1898
2339
 
1899
2340
  if (req.method === "GET" && (path === "/legal/privacy/en-ww/" || path === "/legal/privacy/en-ww")) {
@@ -1917,21 +2358,25 @@ const httpServer = createServer(async (req, res) => {
1917
2358
  // --- WebAuthn API ---
1918
2359
 
1919
2360
  if (req.method === "POST" && path === "/webauthn/register-options") {
2361
+ if (!applyRateLimit(req, res, "mint")) return;
1920
2362
  await handleRegisterOptions(req, res);
1921
2363
  return;
1922
2364
  }
1923
2365
 
1924
2366
  if (req.method === "POST" && path === "/webauthn/register-verify") {
2367
+ if (!applyRateLimit(req, res, "validate")) return;
1925
2368
  await handleRegisterVerify(req, res);
1926
2369
  return;
1927
2370
  }
1928
2371
 
1929
2372
  if (req.method === "POST" && path === "/webauthn/auth-options") {
2373
+ if (!applyRateLimit(req, res, "mint")) return;
1930
2374
  await handleAuthOptions(req, res);
1931
2375
  return;
1932
2376
  }
1933
2377
 
1934
2378
  if (req.method === "POST" && path === "/webauthn/auth-verify") {
2379
+ if (!applyRateLimit(req, res, "validate")) return;
1935
2380
  await handleAuthVerify(req, res);
1936
2381
  return;
1937
2382
  }
@@ -1949,6 +2394,7 @@ const httpServer = createServer(async (req, res) => {
1949
2394
  }
1950
2395
 
1951
2396
  if (req.method === "POST" && path === "/oauth/register") {
2397
+ if (!applyRateLimit(req, res, "mint")) return;
1952
2398
  await handleOAuthRegister(req, res);
1953
2399
  return;
1954
2400
  }
@@ -1959,11 +2405,13 @@ const httpServer = createServer(async (req, res) => {
1959
2405
  }
1960
2406
 
1961
2407
  if (req.method === "POST" && path === "/oauth/authorize/submit") {
2408
+ if (!applyRateLimit(req, res, "validate")) return;
1962
2409
  await handleOAuthAuthorizeSubmit(req, res);
1963
2410
  return;
1964
2411
  }
1965
2412
 
1966
2413
  if (req.method === "POST" && path === "/oauth/token") {
2414
+ if (!applyRateLimit(req, res, "mint")) return;
1967
2415
  await handleOAuthToken(req, res);
1968
2416
  return;
1969
2417
  }
@@ -1971,21 +2419,25 @@ const httpServer = createServer(async (req, res) => {
1971
2419
  // --- Agent QR Auth ---
1972
2420
 
1973
2421
  if (req.method === "GET" && path === "/demo/api/agent-auth") {
2422
+ if (!applyRateLimit(req, res, "mint")) return;
1974
2423
  await handleAgentAuthStart(req, res);
1975
2424
  return;
1976
2425
  }
1977
2426
 
1978
2427
  if (req.method === "GET" && path === "/demo/api/agent-auth/qr") {
2428
+ if (!applyRateLimit(req, res, "status")) return;
1979
2429
  handleAgentAuthQR(req, res);
1980
2430
  return;
1981
2431
  }
1982
2432
 
1983
2433
  if (req.method === "GET" && path === "/demo/api/agent-auth/status") {
2434
+ if (!applyRateLimit(req, res, "status")) return;
1984
2435
  handleAgentAuthStatus(req, res);
1985
2436
  return;
1986
2437
  }
1987
2438
 
1988
2439
  if (req.method === "POST" && path === "/demo/api/agent-auth/approve") {
2440
+ if (!applyRateLimit(req, res, "validate")) return;
1989
2441
  handleAgentAuthApprove(req, res);
1990
2442
  return;
1991
2443
  }
@@ -2017,21 +2469,25 @@ const httpServer = createServer(async (req, res) => {
2017
2469
  // --- QR Login (Chrome fallback) ---
2018
2470
 
2019
2471
  if (req.method === "POST" && path === "/api/qr-login") {
2472
+ if (!applyRateLimit(req, res, "mint")) return;
2020
2473
  await handleQrLoginStart(req, res);
2021
2474
  return;
2022
2475
  }
2023
2476
 
2024
2477
  if (req.method === "GET" && path === "/api/qr-login/qr") {
2478
+ if (!applyRateLimit(req, res, "status")) return;
2025
2479
  handleQrLoginQR(req, res);
2026
2480
  return;
2027
2481
  }
2028
2482
 
2029
2483
  if (req.method === "GET" && path === "/api/qr-login/status") {
2484
+ if (!applyRateLimit(req, res, "status")) return;
2030
2485
  handleQrLoginStatus(req, res);
2031
2486
  return;
2032
2487
  }
2033
2488
 
2034
2489
  if (req.method === "POST" && path === "/api/qr-login/approve") {
2490
+ if (!applyRateLimit(req, res, "validate")) return;
2035
2491
  handleQrLoginApprove(req, res);
2036
2492
  return;
2037
2493
  }
@@ -2094,32 +2550,38 @@ const httpServer = createServer(async (req, res) => {
2094
2550
  // --- Codex Relay (codex-daemon ↔ phone) ---
2095
2551
 
2096
2552
  if (req.method === "POST" && path === "/api/codex-relay/pair-init") {
2553
+ if (!applyRateLimit(req, res, "mint")) return;
2097
2554
  await handleCodexPairInit(req, res);
2098
2555
  return;
2099
2556
  }
2100
2557
 
2101
2558
  if (req.method === "GET" && path.startsWith("/api/codex-relay/pair-status/")) {
2559
+ if (!applyRateLimit(req, res, "status")) return;
2102
2560
  handleCodexPairStatus(req, res, path.slice("/api/codex-relay/pair-status/".length));
2103
2561
  return;
2104
2562
  }
2105
2563
 
2106
2564
  if (req.method === "POST" && path === "/api/codex-relay/pair-complete") {
2565
+ if (!applyRateLimit(req, res, "validate")) return;
2107
2566
  await handleCodexPairComplete(req, res);
2108
2567
  return;
2109
2568
  }
2110
2569
 
2111
2570
  if (req.method === "GET" && path === "/api/codex-relay/state") {
2571
+ if (!applyRateLimit(req, res, "status")) return;
2112
2572
  handleCodexRelayState(req, res);
2113
2573
  return;
2114
2574
  }
2115
2575
 
2116
2576
  if (req.method === "GET" && path.startsWith("/api/codex-relay/bootstrap/")) {
2577
+ if (!applyRateLimit(req, res, "mint")) return;
2117
2578
  const tid = decodeURIComponent(path.slice("/api/codex-relay/bootstrap/".length));
2118
2579
  handleCodexBootstrap(req, res, tid);
2119
2580
  return;
2120
2581
  }
2121
2582
 
2122
2583
  if (req.method === "POST" && path === "/api/codex-relay/ws-ticket") {
2584
+ if (!applyRateLimit(req, res, "mint")) return;
2123
2585
  await handleCodexWsTicket(req, res);
2124
2586
  return;
2125
2587
  }
@@ -2131,6 +2593,13 @@ const httpServer = createServer(async (req, res) => {
2131
2593
  return;
2132
2594
  }
2133
2595
 
2596
+ // /pair/<CODE> ... URL-first pair flow. Per plan C1 + round 5: real daemon
2597
+ // alphabet [ABCDEFGHJKLMNPQRSTUVWXYZ23456789]{6}, length 6, L IS included.
2598
+ if (req.method === "GET" && /^\/pair\/[ABCDEFGHJKLMNPQRSTUVWXYZ23456789]{6}$/.test(path)) {
2599
+ serveAppFile(res, "pair.html");
2600
+ return;
2601
+ }
2602
+
2134
2603
  // /:handle/codex-remote-control/:threadId
2135
2604
  const remoteControlMatch = path.match(/^\/([^/]+)\/codex-remote-control\/([^/]+)\/?$/);
2136
2605
  if (req.method === "GET" && remoteControlMatch) {
@@ -2151,21 +2620,25 @@ const httpServer = createServer(async (req, res) => {
2151
2620
  // ---------- Codex Relay (codex-daemon ↔ phone) ----------
2152
2621
  //
2153
2622
  // In-memory state. Pairing codes: 6-char, 5-min TTL. Daemons indexed by
2154
- // agentId (one daemon per agentId; new daemon kicks the old one). Web clients
2155
- // indexed by `agentId:threadId`. Server is a transparent passthrough between
2156
- // the daemon and the matching web client(s); thread routing is enforced
2157
- // purely client-side via session.send/sessionId payloads.
2623
+ // immutable tenant id (one daemon per tenant; new daemon kicks the old one). Web clients
2624
+ // indexed by `tenantId:threadId`. The server is a transport relay between
2625
+ // the daemon and matching web client(s). The relay injects the route thread
2626
+ // into the E2EE handshake, and the daemon enforces that bound route after
2627
+ // decrypting session commands.
2158
2628
 
2159
2629
  const CODEX_PAIR_EXPIRY_MS = 5 * 60 * 1000;
2630
+ const CODEX_PAIR_PRESENCE_TTL_MS = 2 * 60 * 1000;
2160
2631
  const CODEX_PAIR_ALPHABET = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
2161
- const codexPairings = {}; // pairing_id -> { code, status, expires, daemon_info, apiKey?, agentId?, daemon_public_key?, crypto_versions? }
2632
+ const codexPairings = {}; // pairing_id -> { code, status, expires, poll_token, poll_token_used?, daemon_info, apiKey?, agentId?, handle?, daemon_public_key?, crypto_versions? }
2162
2633
  const codexPairingByCode = {}; // code -> pairing_id (only while pending)
2634
+ const codexPairPresenceTokens = new Map(); // token -> { agentId, expires, used }
2163
2635
  const codexDaemons = new Map(); // agentId -> ws
2164
- const codexWebClients = new Map(); // `${agentId}:${threadId}` -> ws
2636
+ const codexWebClients = new Map(); // `${agentId}:${threadId}` -> Set<ws>
2637
+ const codexE2eeSessionRoutes = new Map(); // `${agentId}:${e2eeSession}` -> { threadId, webKey, ws }
2165
2638
 
2166
2639
  // E2EE substrate (Phase 2.5).
2167
2640
  //
2168
- // codexDaemonPubkeys: per agentId, the most recently paired daemon's
2641
+ // codexDaemonPubkeyRegistry: per tenant id, the most recently paired daemon's
2169
2642
  // public key (P-256 SPKI base64url) + supported crypto versions +
2170
2643
  // registration timestamp. This is what the browser fetches via
2171
2644
  // bootstrap before opening an encrypted session.
@@ -2174,10 +2647,103 @@ const codexWebClients = new Map(); // `${agentId}:${threadId}` -> ws
2174
2647
  // ?token=ck-... in the browser WebSocket URL. Bound to a specific
2175
2648
  // (agentId, threadId) so a leaked ticket cannot drive a different
2176
2649
  // route, even by the same authenticated user.
2177
- const codexDaemonPubkeys = new Map(); // agentId -> { pubkey, crypto_versions, registered_at }
2178
2650
  const codexRelayTickets = new Map(); // ticket -> { agentId, threadId, expires, used }
2179
2651
  const CODEX_RELAY_TICKET_TTL_MS = 60 * 1000; // 60s; browser must connect immediately
2180
2652
 
2653
+ const codexDaemonPubkeyRegistry = createCodexDaemonPubkeyRegistry({
2654
+ usePrisma,
2655
+ prisma,
2656
+ devMode: DEV_MODE,
2657
+ logger: console,
2658
+ });
2659
+
2660
+ await codexDaemonPubkeyRegistry.loadFromDb();
2661
+
2662
+ function codexRelayKey(agentId, id) {
2663
+ return agentId + ":" + id;
2664
+ }
2665
+
2666
+ function isCodexE2eeEnvelope(envelope) {
2667
+ return !!(envelope && typeof envelope.type === "string" && envelope.type.startsWith("e2ee."));
2668
+ }
2669
+
2670
+ function registerCodexE2eeSessionRoute(agentId, e2eeSession, threadId, ws) {
2671
+ if (typeof e2eeSession !== "string" || !e2eeSession) return;
2672
+ if (typeof threadId !== "string" || !threadId) return;
2673
+ const webKey = codexRelayKey(agentId, threadId);
2674
+ codexE2eeSessionRoutes.set(codexRelayKey(agentId, e2eeSession), { threadId, webKey, ws });
2675
+ }
2676
+
2677
+ function addCodexWebClient(webKey, ws) {
2678
+ let clients = codexWebClients.get(webKey);
2679
+ if (!clients) {
2680
+ clients = new Set();
2681
+ codexWebClients.set(webKey, clients);
2682
+ }
2683
+ clients.add(ws);
2684
+ return clients.size;
2685
+ }
2686
+
2687
+ function removeCodexWebClient(webKey, ws) {
2688
+ const clients = codexWebClients.get(webKey);
2689
+ if (!clients) return 0;
2690
+ clients.delete(ws);
2691
+ if (clients.size === 0) {
2692
+ codexWebClients.delete(webKey);
2693
+ return 0;
2694
+ }
2695
+ return clients.size;
2696
+ }
2697
+
2698
+ function openCodexWebClientsForKey(webKey) {
2699
+ const clients = codexWebClients.get(webKey);
2700
+ if (!clients) return [];
2701
+ return [...clients].filter((webWs) => webWs.readyState === webWs.OPEN);
2702
+ }
2703
+
2704
+ function resolveCodexWebClientsForDaemonFrame(agentId, routeId) {
2705
+ const routed = codexE2eeSessionRoutes.get(codexRelayKey(agentId, routeId));
2706
+ if (routed && routed.ws && routed.ws.readyState === routed.ws.OPEN) return [routed.ws];
2707
+ return openCodexWebClientsForKey(codexRelayKey(agentId, routeId));
2708
+ }
2709
+
2710
+ function removeCodexE2eeRoutesForWeb(agentId, threadId, ws) {
2711
+ const webKey = codexRelayKey(agentId, threadId);
2712
+ for (const [routeKey, route] of codexE2eeSessionRoutes) {
2713
+ if (route.webKey === webKey && (!ws || route.ws === ws)) {
2714
+ codexE2eeSessionRoutes.delete(routeKey);
2715
+ }
2716
+ }
2717
+ }
2718
+
2719
+ function invalidateCodexBrowserSessionsForAgent(agentId, reason) {
2720
+ const prefix = agentId + ":";
2721
+ let closed = 0;
2722
+ for (const routeKey of [...codexE2eeSessionRoutes.keys()]) {
2723
+ if (routeKey.startsWith(prefix)) codexE2eeSessionRoutes.delete(routeKey);
2724
+ }
2725
+ for (const [webKey, clients] of [...codexWebClients]) {
2726
+ if (!webKey.startsWith(prefix)) continue;
2727
+ for (const webWs of clients) {
2728
+ if (webWs.readyState === webWs.OPEN) {
2729
+ closed += 1;
2730
+ try { webWs.close(4001, reason); } catch {}
2731
+ }
2732
+ }
2733
+ codexWebClients.delete(webKey);
2734
+ }
2735
+ return closed;
2736
+ }
2737
+
2738
+ function logCodexWsLimit({ agentId, threadId, connectionId, reason }) {
2739
+ console.warn(formatCodexWsLimitLog({ agentId, threadId, connectionId, reason }));
2740
+ }
2741
+
2742
+ function closeCodexWsForLimit(ws, { agentId, threadId, connectionId }, decision) {
2743
+ logCodexWsLimit({ agentId, threadId, connectionId, reason: decision.reason });
2744
+ try { ws.close(decision.code, decision.reason); } catch {}
2745
+ }
2746
+
2181
2747
  function generateCodexPairingCode() {
2182
2748
  for (let attempt = 0; attempt < 100; attempt += 1) {
2183
2749
  let code = "";
@@ -2190,16 +2756,58 @@ function generateCodexPairingCode() {
2190
2756
  throw new Error("Could not generate unique codex-relay pairing code");
2191
2757
  }
2192
2758
 
2759
+ function generateCodexPairPollToken() {
2760
+ return "ppt_" + randomBytes(32).toString("base64url");
2761
+ }
2762
+
2763
+ function cleanupCodexPairPresenceTokens() {
2764
+ const now = Date.now();
2765
+ for (const [token, entry] of codexPairPresenceTokens) {
2766
+ if (!entry || entry.used || now > entry.expires) codexPairPresenceTokens.delete(token);
2767
+ }
2768
+ }
2769
+
2770
+ function generateCodexPairPresenceToken(agentId) {
2771
+ cleanupCodexPairPresenceTokens();
2772
+ if (typeof agentId !== "string" || !agentId) return null;
2773
+ const token = "cpt_" + randomBytes(32).toString("base64url");
2774
+ codexPairPresenceTokens.set(token, {
2775
+ agentId,
2776
+ expires: Date.now() + CODEX_PAIR_PRESENCE_TTL_MS,
2777
+ used: false,
2778
+ });
2779
+ return token;
2780
+ }
2781
+
2782
+ function consumeCodexPairPresenceToken(token, agentId) {
2783
+ cleanupCodexPairPresenceTokens();
2784
+ const entry = codexPairPresenceTokens.get(token);
2785
+ if (!entry || entry.used || entry.agentId !== agentId || Date.now() > entry.expires) return false;
2786
+ entry.used = true;
2787
+ codexPairPresenceTokens.delete(token);
2788
+ return true;
2789
+ }
2790
+
2791
+ function getBearerToken(req) {
2792
+ const auth = req.headers["authorization"];
2793
+ if (typeof auth !== "string" || !auth.startsWith("Bearer ")) return null;
2794
+ const token = auth.slice(7).trim();
2795
+ return token || null;
2796
+ }
2797
+
2193
2798
  async function handleCodexPairInit(req, res) {
2194
2799
  let body = {};
2195
2800
  try { body = (await readBody(req)) || {}; } catch {}
2196
2801
  const code = generateCodexPairingCode();
2197
2802
  const pairingId = randomUUID();
2803
+ const pollToken = generateCodexPairPollToken();
2198
2804
  const expires = Date.now() + CODEX_PAIR_EXPIRY_MS;
2199
2805
  codexPairings[pairingId] = {
2200
2806
  code,
2201
2807
  status: "pending",
2202
2808
  expires,
2809
+ poll_token: pollToken,
2810
+ poll_token_used: false,
2203
2811
  daemon_info: {
2204
2812
  hostname: typeof body.hostname === "string" ? body.hostname.slice(0, 64) : null,
2205
2813
  platform: typeof body.platform === "string" ? body.platform.slice(0, 32) : null,
@@ -2216,10 +2824,14 @@ async function handleCodexPairInit(req, res) {
2216
2824
  : null,
2217
2825
  };
2218
2826
  codexPairingByCode[code] = pairingId;
2827
+ // Per plan: web_url goes through /login first so the existing Kaleidoscope
2828
+ // QR + phone-passkey ceremony handles auth. After phone passkey, phone
2829
+ // (not desktop) redirects to /pair/<CODE> and completes pair-complete.
2219
2830
  json(res, 200, {
2220
2831
  code,
2221
2832
  pairing_id: pairingId,
2222
- web_url: ISSUER_URL + "/pair",
2833
+ pair_poll_token: pollToken,
2834
+ web_url: ISSUER_URL + "/login?next=" + encodeURIComponent("/pair/" + code),
2223
2835
  expires_at: new Date(expires).toISOString(),
2224
2836
  });
2225
2837
  }
@@ -2227,12 +2839,25 @@ async function handleCodexPairInit(req, res) {
2227
2839
  function handleCodexPairStatus(req, res, pairingId) {
2228
2840
  const p = codexPairings[pairingId];
2229
2841
  if (!p) { json(res, 404, { error: "pairing not found" }); return; }
2230
- if (p.status === "pending" && Date.now() > p.expires) {
2842
+ if (Date.now() > p.expires) {
2231
2843
  p.status = "expired";
2232
2844
  if (codexPairingByCode[p.code] === pairingId) delete codexPairingByCode[p.code];
2845
+ json(res, 401, { error: "pair_poll_token_expired" });
2846
+ return;
2847
+ }
2848
+ const pollToken = getBearerToken(req);
2849
+ if (!pollToken || pollToken !== p.poll_token || p.poll_token_used) {
2850
+ json(res, 401, { error: "invalid_pair_poll_token" });
2851
+ return;
2233
2852
  }
2234
2853
  if (p.status === "completed") {
2235
- json(res, 200, { status: "completed", api_key: p.apiKey, handle: p.agentId });
2854
+ p.poll_token_used = true;
2855
+ json(res, 200, {
2856
+ status: "completed",
2857
+ api_key: p.apiKey,
2858
+ handle: p.handle || p.agentId,
2859
+ replaced_daemon_key: !!p.replaced_daemon_key,
2860
+ });
2236
2861
  } else {
2237
2862
  json(res, 200, { status: p.status });
2238
2863
  }
@@ -2245,6 +2870,14 @@ async function handleCodexPairComplete(req, res) {
2245
2870
  try { body = await readBody(req); } catch { json(res, 400, { error: "bad request" }); return; }
2246
2871
  const code = (body && typeof body.code === "string") ? body.code.trim().toUpperCase() : "";
2247
2872
  if (!code) { json(res, 400, { error: "missing code" }); return; }
2873
+ // Defensive: reject codes that don't match the daemon's alphabet up front,
2874
+ // before the map lookup, so probe attempts get one uniform reject path
2875
+ // instead of leaking timing/shape info between "wrong-alphabet" and "valid
2876
+ // shape but unknown code." Per plan C3 + round 5.
2877
+ if (!/^[ABCDEFGHJKLMNPQRSTUVWXYZ23456789]{6}$/.test(code)) {
2878
+ json(res, 404, { error: "invalid or already-used code" });
2879
+ return;
2880
+ }
2248
2881
  const pairingId = codexPairingByCode[code];
2249
2882
  if (!pairingId) { json(res, 404, { error: "invalid or already-used code" }); return; }
2250
2883
  const p = codexPairings[pairingId];
@@ -2252,30 +2885,51 @@ async function handleCodexPairComplete(req, res) {
2252
2885
  json(res, 410, { error: "code expired or already used" });
2253
2886
  return;
2254
2887
  }
2255
- p.status = "completed";
2256
- p.apiKey = identity.apiKey;
2257
- p.agentId = identity.agentId;
2888
+ const pairPresenceToken = body && typeof body.codex_pair_presence_token === "string"
2889
+ ? body.codex_pair_presence_token
2890
+ : "";
2891
+ if (p.daemon_public_key && !consumeCodexPairPresenceToken(pairPresenceToken, identity.agentId)) {
2892
+ json(res, 403, {
2893
+ error: "fresh_presence_required",
2894
+ error_description: "Pairing this daemon requires a fresh passkey confirmation. Sign in again from the pair page.",
2895
+ });
2896
+ return;
2897
+ }
2258
2898
  // Phase 2.5: register the daemon's E2EE public key against the
2259
- // authenticated handle. Replaces any previous key for this handle
2260
- // (rotate-key implicitly happens here on a re-pair).
2899
+ // authenticated immutable tenant id. The display handle is returned
2900
+ // as metadata only.
2901
+ let daemonKeyResult = null;
2261
2902
  if (p.daemon_public_key) {
2262
- codexDaemonPubkeys.set(identity.agentId, {
2263
- pubkey: p.daemon_public_key,
2264
- crypto_versions: p.crypto_versions && p.crypto_versions.length ? p.crypto_versions : ["e2ee-v1"],
2265
- registered_at: new Date().toISOString(),
2266
- });
2267
- console.log("codex-relay: registered E2EE pubkey for " + identity.agentId);
2903
+ daemonKeyResult = await codexDaemonPubkeyRegistry.register(identity.agentId, p.daemon_public_key, p.crypto_versions, "pair-complete");
2904
+ if (daemonKeyResult?.replaced) {
2905
+ const closed = invalidateCodexBrowserSessionsForAgent(identity.agentId, "daemon key replaced");
2906
+ console.log(
2907
+ "codex-relay: replaced daemon E2EE key for tenant " + identity.agentId
2908
+ + " old=" + daemonKeyResult.old_fingerprint
2909
+ + " new=" + daemonKeyResult.new_fingerprint
2910
+ + " closed_browser_sessions=" + closed
2911
+ );
2912
+ }
2268
2913
  }
2914
+ p.status = "completed";
2915
+ p.apiKey = identity.apiKey;
2916
+ p.agentId = identity.agentId;
2917
+ p.handle = identity.handle;
2918
+ p.replaced_daemon_key = !!daemonKeyResult?.replaced;
2269
2919
  delete codexPairingByCode[code];
2270
- console.log("codex-relay: paired daemon for " + identity.agentId);
2271
- json(res, 200, { ok: true, handle: identity.agentId });
2920
+ console.log("codex-relay: paired daemon for tenant " + identity.agentId + " handle " + identity.handle);
2921
+ json(res, 200, {
2922
+ ok: true,
2923
+ handle: identity.handle,
2924
+ replaced_daemon_key: !!daemonKeyResult?.replaced,
2925
+ });
2272
2926
  }
2273
2927
 
2274
2928
  function handleCodexRelayState(req, res) {
2275
2929
  const identity = authenticate(req);
2276
2930
  if (!identity) { json(res, 401, { error: "Unauthorized" }); return; }
2277
2931
  json(res, 200, {
2278
- handle: identity.agentId,
2932
+ handle: identity.handle,
2279
2933
  daemon_online: codexDaemons.has(identity.agentId),
2280
2934
  });
2281
2935
  }
@@ -2289,16 +2943,8 @@ function handleCodexBootstrap(req, res, threadId) {
2289
2943
  if (!identity) { json(res, 401, { error: "Unauthorized" }); return; }
2290
2944
  if (!threadId) { json(res, 400, { error: "missing threadId" }); return; }
2291
2945
  const daemonOnline = codexDaemons.has(identity.agentId);
2292
- const daemonKey = codexDaemonPubkeys.get(identity.agentId) || null;
2293
- json(res, 200, {
2294
- handle: identity.agentId,
2295
- thread_id: threadId,
2296
- daemon_online: daemonOnline,
2297
- daemon_public_key: daemonKey ? daemonKey.pubkey : null,
2298
- daemon_crypto_versions: daemonKey ? daemonKey.crypto_versions : null,
2299
- supported_crypto_versions: ["e2ee-v1"],
2300
- e2ee_available: !!daemonKey,
2301
- });
2946
+ const daemonKey = codexDaemonPubkeyRegistry.get(identity.agentId);
2947
+ json(res, 200, buildCodexBootstrapPayload({ identity, threadId, daemonOnline, daemonKey }));
2302
2948
  }
2303
2949
 
2304
2950
  // POST /api/codex-relay/ws-ticket
@@ -2318,6 +2964,7 @@ async function handleCodexWsTicket(req, res) {
2318
2964
  const expires = Date.now() + CODEX_RELAY_TICKET_TTL_MS;
2319
2965
  codexRelayTickets.set(ticket, {
2320
2966
  agentId: identity.agentId,
2967
+ handle: identity.handle,
2321
2968
  threadId,
2322
2969
  expires,
2323
2970
  used: false,
@@ -2342,7 +2989,7 @@ function consumeCodexRelayTicket(ticket, threadId) {
2342
2989
  if (Date.now() > entry.expires) { codexRelayTickets.delete(ticket); return null; }
2343
2990
  if (entry.threadId !== threadId) return null; // bound to specific route
2344
2991
  entry.used = true;
2345
- return { agentId: entry.agentId };
2992
+ return { agentId: entry.agentId, handle: entry.handle || entry.agentId };
2346
2993
  }
2347
2994
 
2348
2995
  function serveAppFile(res, relPath) {
@@ -2368,23 +3015,64 @@ function serveAppFile(res, relPath) {
2368
3015
  }
2369
3016
  }
2370
3017
 
2371
- function authenticateWs(req) {
3018
+ // authenticateWs verifies a WS upgrade request against the API_KEYS
3019
+ // map. Default behavior is HEADER ONLY: Authorization: Bearer ck-...
3020
+ // is accepted; ?token=ck-... in the URL is ignored.
3021
+ //
3022
+ // Set { allowQueryToken: true } only on the explicit web-side
3023
+ // back-compat branch that runs inside ALLOW_WS_URL_TOKEN. Daemon
3024
+ // connections never enable this; CLI clients can always set
3025
+ // Authorization, and a daemon accepting URL-token would be a needless
3026
+ // attack surface (URL leaks via referrer / log scrape are not relevant
3027
+ // for daemons, but the asymmetry of "header-only on the daemon path"
3028
+ // keeps the policy clean and auditable).
3029
+ function authenticateWs(req, { allowQueryToken = false } = {}) {
2372
3030
  const auth = req.headers["authorization"];
2373
3031
  if (auth && auth.startsWith("Bearer ")) {
2374
3032
  const key = auth.slice(7).trim();
2375
- if (API_KEYS[key]) return { agentId: API_KEYS[key], apiKey: key };
3033
+ const identity = identityForApiKey(key);
3034
+ if (identity) return identity;
2376
3035
  }
2377
- // Browsers can't set Authorization on WebSocket(): accept ?token= fallback.
3036
+ if (!allowQueryToken) return null;
3037
+
3038
+ // Web back-compat path only. Browsers cannot set Authorization on a
3039
+ // WebSocket() handshake, so legacy clients put ck- in the URL.
3040
+ // parseUrl returns a WHATWG URL, which has no .query getter (only
3041
+ // .search/.searchParams). Strip the leading "?" off .search and
3042
+ // parse with querystring so the array/string handling below works.
2378
3043
  const u = parseUrl(req.url);
2379
- const qs = u.query ? parseUrlQs(u.query) : {};
3044
+ const qs = u.search ? parseUrlQs(u.search.slice(1)) : {};
2380
3045
  const tokenParam = Array.isArray(qs.token) ? qs.token[0] : qs.token;
2381
- if (typeof tokenParam === "string" && API_KEYS[tokenParam]) {
2382
- return { agentId: API_KEYS[tokenParam], apiKey: tokenParam };
3046
+ if (typeof tokenParam === "string") {
3047
+ return identityForApiKey(tokenParam);
2383
3048
  }
2384
3049
  return null;
2385
3050
  }
2386
3051
 
2387
- const codexRelayWss = new WebSocketServer({ noServer: true });
3052
+ // F-001: subprotocol-based WS auth for the codex-relay surface. The web
3053
+ // client sends Sec-WebSocket-Protocol: ldm-codex-relay.v1, ticket.<v>.
3054
+ // Server echoes only the protocol name (not the ticket-bearing entry).
3055
+ // Daemon connections do not use a subprotocol, so empty Sets pass.
3056
+ const codexRelayWss = new WebSocketServer({
3057
+ noServer: true,
3058
+ handleProtocols: (protocols /*, request */) => {
3059
+ if (!protocols || protocols.size === 0) return undefined;
3060
+ if (protocols.has("ldm-codex-relay.v1")) return "ldm-codex-relay.v1";
3061
+ return false;
3062
+ },
3063
+ });
3064
+
3065
+ function getTicketFromSubprotocol(req) {
3066
+ const header = req.headers["sec-websocket-protocol"];
3067
+ if (!header) return null;
3068
+ const tokens = header.split(",").map(s => s.trim()).filter(Boolean);
3069
+ for (const t of tokens) {
3070
+ if (t.startsWith("ticket.")) {
3071
+ return t.slice("ticket.".length);
3072
+ }
3073
+ }
3074
+ return null;
3075
+ }
2388
3076
 
2389
3077
  httpServer.on("upgrade", (req, socket, head) => {
2390
3078
  const u = parseUrl(req.url);
@@ -2393,21 +3081,64 @@ httpServer.on("upgrade", (req, socket, head) => {
2393
3081
  const isWeb = path.startsWith("/api/codex-relay/web/");
2394
3082
  if (!isDaemon && !isWeb) return; // let other listeners (or default) handle it
2395
3083
 
3084
+ // F-003: enforce Origin allowlist for browser-borne web upgrades.
3085
+ // Runs BEFORE ticket consumption / authenticateWs so a request from
3086
+ // a disallowed origin cannot burn a valid one-time ticket or trigger
3087
+ // an auth check side effect. Daemon path is exempt because CLI
3088
+ // clients do not send a browser Origin header. Requires nginx to
3089
+ // pass the Origin header through unchanged on the upgrade hop;
3090
+ // verified in Lane B B2 of the audit doc.
3091
+ if (isWeb) {
3092
+ const origin = req.headers["origin"];
3093
+ if (!isWsOriginAllowed(origin)) {
3094
+ console.warn("WS upgrade rejected: bad origin (" + (origin || "<none>") + ") for " + path);
3095
+ socket.write("HTTP/1.1 403 Forbidden\r\nConnection: close\r\n\r\n");
3096
+ socket.destroy();
3097
+ return;
3098
+ }
3099
+ }
3100
+
2396
3101
  // Daemon side keeps the existing Bearer ck- token auth.
2397
- // Web side: prefer single-use ?ticket= bound to the route; fall back
2398
- // to ?token=ck- for back-compat with the pre-2.5 alpha.
3102
+ // Web side: prefer ticket via Sec-WebSocket-Protocol (F-001), fall
3103
+ // back to ?ticket= query string. The legacy ?token=ck- URL fallback
3104
+ // is gated behind LDM_HOSTED_MCP_ALLOW_WS_URL_TOKEN=1; production
3105
+ // does not accept it.
2399
3106
  let identity = null;
2400
3107
  if (isDaemon) {
2401
- identity = authenticateWs(req);
3108
+ // Daemon connections must use Authorization: Bearer ck-. URL-token
3109
+ // is never accepted on the daemon path (allowQueryToken: false).
3110
+ identity = authenticateWs(req, { allowQueryToken: false });
2402
3111
  } else {
2403
3112
  const threadId = decodeURIComponent(path.slice("/api/codex-relay/web/".length));
2404
- const qs = u.query ? parseUrlQs(u.query) : {};
2405
- const ticketParam = Array.isArray(qs.ticket) ? qs.ticket[0] : qs.ticket;
2406
- if (typeof ticketParam === "string" && ticketParam) {
2407
- const consumed = consumeCodexRelayTicket(ticketParam, threadId);
2408
- if (consumed) identity = { agentId: consumed.agentId, viaTicket: true };
3113
+
3114
+ // Preferred path: ticket carried in Sec-WebSocket-Protocol. Avoids
3115
+ // URL/log/referrer exposure of the ticket value.
3116
+ const subTicket = getTicketFromSubprotocol(req);
3117
+ if (subTicket) {
3118
+ const consumed = consumeCodexRelayTicket(subTicket, threadId);
3119
+ if (consumed) identity = { agentId: consumed.agentId, handle: consumed.handle, viaTicket: true };
3120
+ }
3121
+
3122
+ // Back-compat: ?ticket= query string. Same single-use binding.
3123
+ if (!identity) {
3124
+ // parseUrl returns a WHATWG URL with .search/.searchParams, no
3125
+ // .query. Strip the leading "?" off .search and parse with
3126
+ // querystring so the array/string handling below still works.
3127
+ const qs = u.search ? parseUrlQs(u.search.slice(1)) : {};
3128
+ const ticketParam = Array.isArray(qs.ticket) ? qs.ticket[0] : qs.ticket;
3129
+ if (typeof ticketParam === "string" && ticketParam) {
3130
+ const consumed = consumeCodexRelayTicket(ticketParam, threadId);
3131
+ if (consumed) identity = { agentId: consumed.agentId, handle: consumed.handle, viaTicket: true };
3132
+ }
3133
+ }
3134
+
3135
+ // Legacy ?token=ck- URL fallback: dev/back-compat only. Production
3136
+ // refuses long-lived bearer in WS URLs (gate condition 2). The
3137
+ // URL-token branch in authenticateWs is gated by allowQueryToken,
3138
+ // and we only set it true here, only when ALLOW_WS_URL_TOKEN is on.
3139
+ if (!identity && ALLOW_WS_URL_TOKEN) {
3140
+ identity = authenticateWs(req, { allowQueryToken: true });
2409
3141
  }
2410
- if (!identity) identity = authenticateWs(req);
2411
3142
  }
2412
3143
 
2413
3144
  if (!identity) {
@@ -2418,18 +3149,109 @@ httpServer.on("upgrade", (req, socket, head) => {
2418
3149
 
2419
3150
  if (isDaemon) {
2420
3151
  codexRelayWss.handleUpgrade(req, socket, head, (ws) => {
2421
- const previous = codexDaemons.get(identity.agentId);
2422
- if (previous && previous !== ws) try { previous.close(4000, "replaced"); } catch {}
2423
- codexDaemons.set(identity.agentId, ws);
2424
- console.log("codex-relay: daemon online for " + identity.agentId);
3152
+ let daemonIdentityAccepted = false;
3153
+ function activateCodexDaemonWs() {
3154
+ const previous = codexDaemons.get(identity.agentId);
3155
+ if (previous && previous !== ws && previous.readyState === previous.OPEN) {
3156
+ console.warn("codex-relay: rejected duplicate daemon reconnect for online tenant " + identity.agentId);
3157
+ try { ws.close(4004, "daemon already online"); } catch {}
3158
+ return false;
3159
+ }
3160
+ if (previous && previous !== ws) try { previous.close(4000, "replaced"); } catch {}
3161
+ codexDaemons.set(identity.agentId, ws);
3162
+ console.log("codex-relay: daemon online for " + identity.agentId);
3163
+ return true;
3164
+ }
3165
+ // F-001 per-thread isolation. Daemon -> web routing must NOT
3166
+ // fan out every frame to every same-agent web socket; that
3167
+ // breaks isolation when one user has multiple threads open.
3168
+ // Parse the OUTER envelope only to read the routing field
3169
+ // (session/sessionId). The encrypted ciphertext (or any inner
3170
+ // session.* payload) is never inspected, so gate 3a still
3171
+ // holds: the relay sees only routing metadata on the envelope.
3172
+ // No-session frames are an explicit allowlist (control/presence
3173
+ // types) or are dropped with a redacted warning. We never
3174
+ // broadcast unknown frames.
3175
+ const BROADCAST_TYPES = new Set([
3176
+ "presence",
3177
+ "presence.web",
3178
+ "presence.daemon",
3179
+ "daemon.online",
3180
+ "daemon.offline",
3181
+ ]);
2425
3182
  ws.on("message", (data) => {
2426
3183
  const text = data.toString();
2427
- const prefix = identity.agentId + ":";
2428
- for (const [key, webWs] of codexWebClients) {
2429
- if (key.startsWith(prefix) && webWs.readyState === webWs.OPEN) {
2430
- webWs.send(text);
3184
+ let envelope = null;
3185
+ try { envelope = JSON.parse(text); } catch {}
3186
+ if (envelope?.type === "daemon.identity") {
3187
+ const reconnectPolicy = evaluateCodexDaemonReconnectPubkey(
3188
+ codexDaemonPubkeyRegistry.get(identity.agentId),
3189
+ envelope.daemon_public_key,
3190
+ );
3191
+ if (!reconnectPolicy.allowed) {
3192
+ console.warn(
3193
+ "codex-relay: rejected daemon reconnect E2EE key for tenant " + identity.agentId
3194
+ + " reason=" + reconnectPolicy.reason
3195
+ + " old=" + (reconnectPolicy.old_fingerprint || "<none>")
3196
+ + " new=" + (reconnectPolicy.new_fingerprint || codexDaemonPubkeyFingerprint(envelope.daemon_public_key) || "<none>"),
3197
+ );
3198
+ const closeReason = reconnectPolicy.replaced
3199
+ ? "daemon key change requires fresh pair"
3200
+ : "invalid daemon identity";
3201
+ try { ws.close(reconnectPolicy.replaced ? 4003 : 1008, closeReason); } catch {}
3202
+ return;
3203
+ }
3204
+ void codexDaemonPubkeyRegistry.register(
3205
+ identity.agentId,
3206
+ envelope.daemon_public_key,
3207
+ envelope.crypto_versions,
3208
+ "daemon-reconnect",
3209
+ ).then((result) => {
3210
+ if (!result?.registered) {
3211
+ try { ws.close(1011, "daemon identity persistence failed"); } catch {}
3212
+ return;
3213
+ }
3214
+ daemonIdentityAccepted = activateCodexDaemonWs();
3215
+ }).catch(() => {
3216
+ try { ws.close(1011, "daemon identity persistence failed"); } catch {}
3217
+ });
3218
+ return;
3219
+ }
3220
+ if (!daemonIdentityAccepted) {
3221
+ try { ws.close(1008, "daemon identity required"); } catch {}
3222
+ return;
3223
+ }
3224
+ const sessionId = envelope?.session || envelope?.sessionId || envelope?.threadId;
3225
+ if (sessionId) {
3226
+ const targets = resolveCodexWebClientsForDaemonFrame(identity.agentId, sessionId);
3227
+ for (const target of targets) {
3228
+ target.send(text);
2431
3229
  }
3230
+ // No matching web client: drop silently. Daemon-emitted
3231
+ // frames for a thread the user has not opened in any browser
3232
+ // are not interesting to fan out elsewhere.
3233
+ return;
2432
3234
  }
3235
+ const type = envelope?.type;
3236
+ if (type && BROADCAST_TYPES.has(type)) {
3237
+ // Allowlisted agent-level frame (presence, online status).
3238
+ // Fan out within agent, never across agents.
3239
+ const prefix = identity.agentId + ":";
3240
+ for (const [key, webClients] of codexWebClients) {
3241
+ if (key.startsWith(prefix)) {
3242
+ for (const webWs of webClients) {
3243
+ if (webWs.readyState === webWs.OPEN) webWs.send(text);
3244
+ }
3245
+ }
3246
+ }
3247
+ return;
3248
+ }
3249
+ // Parse failed, missing session, or unknown type: drop. Log a
3250
+ // redacted notice so the operator can see if a daemon is
3251
+ // emitting unrouteable frames. We never log envelope/payload
3252
+ // bytes; only the agent and the type (or "no-type").
3253
+ const typeMarker = type ? String(type).slice(0, 32) : "no-type";
3254
+ console.warn("codex-relay: dropped unroutable daemon frame for " + identity.agentId + " (type=" + typeMarker + ")");
2433
3255
  });
2434
3256
  ws.on("close", () => {
2435
3257
  if (codexDaemons.get(identity.agentId) === ws) {
@@ -2451,22 +3273,87 @@ httpServer.on("upgrade", (req, socket, head) => {
2451
3273
  socket.destroy();
2452
3274
  return;
2453
3275
  }
3276
+ const webKey = codexRelayKey(identity.agentId, threadId);
3277
+ if (isCodexWsAgentDisabled(CODEX_WS_ABUSE_LIMITS, identity.agentId)) {
3278
+ console.warn(formatCodexWsLimitLog({
3279
+ agentId: identity.agentId,
3280
+ threadId,
3281
+ connectionId: "upgrade",
3282
+ reason: "operator disabled",
3283
+ }));
3284
+ socket.write("HTTP/1.1 503 Service Unavailable\r\nConnection: close\r\n\r\n");
3285
+ socket.destroy();
3286
+ return;
3287
+ }
3288
+ const openBrowserSockets = openCodexWebClientsForKey(webKey).length;
3289
+ if (openBrowserSockets >= CODEX_WS_ABUSE_LIMITS.maxBrowserSocketsPerThread) {
3290
+ console.warn(formatCodexWsLimitLog({
3291
+ agentId: identity.agentId,
3292
+ threadId,
3293
+ connectionId: "upgrade",
3294
+ reason: "too many browser sockets",
3295
+ }));
3296
+ socket.write("HTTP/1.1 429 Too Many Requests\r\nConnection: close\r\n\r\n");
3297
+ socket.destroy();
3298
+ return;
3299
+ }
2454
3300
  codexRelayWss.handleUpgrade(req, socket, head, (ws) => {
2455
- const key = identity.agentId + ":" + threadId;
2456
- const previous = codexWebClients.get(key);
2457
- if (previous && previous !== ws) try { previous.close(4000, "replaced"); } catch {}
2458
- codexWebClients.set(key, ws);
2459
- console.log("codex-relay: web online " + key);
3301
+ const connectionId = randomUUID();
3302
+ const guard = createCodexWsConnectionGuard({
3303
+ config: CODEX_WS_ABUSE_LIMITS,
3304
+ agentId: identity.agentId,
3305
+ });
3306
+ const guardContext = { agentId: identity.agentId, threadId, connectionId };
3307
+ const idleIntervalMs = Math.max(1000, Math.min(60_000, Math.floor(CODEX_WS_ABUSE_LIMITS.idleTtlMs / 2)));
3308
+ const idleTimer = setInterval(() => {
3309
+ const decision = guard.observeIdle();
3310
+ if (!decision.ok && ws.readyState === ws.OPEN) {
3311
+ closeCodexWsForLimit(ws, guardContext, decision);
3312
+ }
3313
+ }, idleIntervalMs);
3314
+ const clientCount = addCodexWebClient(webKey, ws);
3315
+ console.log("codex-relay: web online " + webKey + " clients=" + clientCount + " conn=" + connectionId);
2460
3316
  ws.on("message", (data) => {
3317
+ const frameDecision = guard.observeFrame(codexWsFrameByteLength(data));
3318
+ if (!frameDecision.ok) {
3319
+ closeCodexWsForLimit(ws, guardContext, frameDecision);
3320
+ return;
3321
+ }
3322
+ let text = data.toString();
3323
+ let envelope = null;
3324
+ try { envelope = JSON.parse(text); } catch {}
3325
+ if (!envelope || typeof envelope !== "object" || Array.isArray(envelope)) {
3326
+ const malformedDecision = guard.observeMalformed();
3327
+ if (!malformedDecision.ok) {
3328
+ closeCodexWsForLimit(ws, guardContext, malformedDecision);
3329
+ }
3330
+ return;
3331
+ }
3332
+ if (isCodexE2eeEnvelope(envelope) && envelope.session) {
3333
+ // The browser cannot be allowed to choose this value. The relay
3334
+ // owns the route because it consumed the ticket for this URL
3335
+ // thread. The daemon uses this metadata to bind the encrypted
3336
+ // session before it decrypts any session.* command.
3337
+ envelope.route_thread_id = threadId;
3338
+ text = JSON.stringify(envelope);
3339
+ registerCodexE2eeSessionRoute(identity.agentId, envelope.session, threadId, ws);
3340
+ }
2461
3341
  const daemonWs = codexDaemons.get(identity.agentId);
2462
3342
  if (daemonWs && daemonWs.readyState === daemonWs.OPEN) {
2463
- daemonWs.send(data.toString());
3343
+ const pendingDecision = guard.observePendingBytes(daemonWs.bufferedAmount || 0);
3344
+ if (!pendingDecision.ok) {
3345
+ closeCodexWsForLimit(ws, guardContext, pendingDecision);
3346
+ return;
3347
+ }
3348
+ daemonWs.send(text);
2464
3349
  } else {
2465
3350
  try { ws.send(JSON.stringify({ type: "error", message: "daemon offline" })); } catch {}
2466
3351
  }
2467
3352
  });
2468
3353
  ws.on("close", () => {
2469
- if (codexWebClients.get(key) === ws) codexWebClients.delete(key);
3354
+ clearInterval(idleTimer);
3355
+ removeCodexE2eeRoutesForWeb(identity.agentId, threadId, ws);
3356
+ removeCodexWebClient(webKey, ws);
2470
3357
  });
2471
3358
  ws.on("error", (err) => {
2472
3359
  console.error("codex-relay web ws error:", err.message);
@@ -2476,6 +3363,7 @@ httpServer.on("upgrade", (req, socket, head) => {
2476
3363
 
2477
3364
  httpServer.listen(PORT, SERVER_BIND, () => {
2478
3365
  console.log(SERVER_NAME + " v" + SERVER_VERSION + " listening on " + SERVER_BIND + ":" + PORT);
3366
+ console.log("WS origin allowlist: " + WS_ORIGIN_ALLOWLIST.join(", "));
2479
3367
  console.log("Health: http://localhost:" + PORT + "/health");
2480
3368
  console.log("MCP: http://localhost:" + PORT + "/mcp");
2481
3369
  console.log("OAuth: http://localhost:" + PORT + "/.well-known/oauth-authorization-server");