@wipcomputer/wip-ldm-os 0.4.84 → 0.4.85-alpha.10

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.
@@ -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
 
@@ -27,6 +27,31 @@ import { parse as parseUrlQs } from "node:querystring";
27
27
  // ── Settings ─────────────────────────────────────────────────────────
28
28
 
29
29
  const PORT = parseInt(process.env.MCP_PORT || "18800", 10);
30
+ // Dev mode: opt-in to JSON-file fallbacks for the data layer and to
31
+ // reading tokens/passkeys from local JSON files. Production must run
32
+ // without this flag set (production fails closed when Prisma is
33
+ // unavailable, and never reads/writes the local JSON token files).
34
+ // Tracked by ai/product/bugs/security/2026-04-28--cc-mini--vps-hosted-mcp-audit.md (F-002, F-005a).
35
+ const DEV_MODE = process.env.LDM_HOSTED_MCP_DEV_MODE === "1";
36
+ // WebSocket Origin allowlist (F-003 in the VPS hosted-mcp audit).
37
+ // Browser-borne web WS upgrades must present an Origin from this list.
38
+ // Comma-separated env var; default is the production origin.
39
+ // Daemon WS upgrades (CLI / agent connections) are NOT gated on Origin
40
+ // because they do not send a browser Origin header.
41
+ const WS_ORIGIN_ALLOWLIST = (process.env.LDM_HOSTED_MCP_WS_ORIGIN_ALLOWLIST || "https://wip.computer")
42
+ .split(",")
43
+ .map(s => s.trim())
44
+ .filter(Boolean);
45
+
46
+ function isWsOriginAllowed(origin) {
47
+ if (!origin) return false;
48
+ return WS_ORIGIN_ALLOWLIST.includes(origin);
49
+ }
50
+ // F-001: WS URL-token fallback (browser sends ?token=ck-... on upgrade).
51
+ // Default off in production. Set LDM_HOSTED_MCP_ALLOW_WS_URL_TOKEN=1 to
52
+ // allow the legacy back-compat path. Independent of any other dev flag
53
+ // so that this can be toggled without enabling other dev-mode behavior.
54
+ const ALLOW_WS_URL_TOKEN = process.env.LDM_HOSTED_MCP_ALLOW_WS_URL_TOKEN === "1";
30
55
  const SESSION_TIMEOUT_MS = 30 * 60 * 1000;
31
56
  const SESSION_CLEANUP_INTERVAL_MS = 5 * 60 * 1000;
32
57
  const OAUTH_CODE_EXPIRY_MS = 10 * 60 * 1000;
@@ -44,18 +69,19 @@ const RP_ORIGIN = "https://wip.computer";
44
69
 
45
70
  // ── Data layer ──────────────────────────────────────────────────────
46
71
  //
47
- // Primary: Postgres via Prisma (production).
48
- // Fallback: JSON files (if DATABASE_URL is not set, e.g. local dev without Postgres).
72
+ // Production: Postgres via Prisma is the canonical store. If Prisma
73
+ // cannot connect, the server refuses to start (F-005a).
49
74
  //
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.
75
+ // Dev mode (LDM_HOSTED_MCP_DEV_MODE=1): JSON files are used as a
76
+ // fallback for tokens and passkeys when Prisma is unavailable, and
77
+ // are also seeded into the in-memory cache on boot. Production must
78
+ // not set this flag.
52
79
 
53
80
  const __dirname = dirname(fileURLToPath(import.meta.url));
54
81
  const TOKEN_FILE = join(__dirname, "tokens.json");
55
82
  const PASSKEY_FILE = join(__dirname, "passkeys.json");
56
83
  const WALLET_FILE_LEGACY = join(__dirname, "wallets.json");
57
84
 
58
- // Initialize Prisma (may fail if DATABASE_URL not set)
59
85
  let prisma = null;
60
86
  let usePrisma = false;
61
87
  try {
@@ -64,30 +90,102 @@ try {
64
90
  usePrisma = true;
65
91
  console.log("Database: Postgres via Prisma");
66
92
  } catch (err) {
67
- console.log("Database: JSON files (Prisma not available: " + err.message + ")");
93
+ if (!DEV_MODE) {
94
+ console.error("FATAL: Prisma unavailable; refusing to start.");
95
+ console.error("Cause: " + err.message);
96
+ console.error("Set LDM_HOSTED_MCP_DEV_MODE=1 to allow the JSON fallback (dev only).");
97
+ process.exit(1);
98
+ }
99
+ console.warn("Database: JSON files (DEV MODE; Prisma not available: " + err.message + ")");
68
100
  }
69
101
 
70
102
  // ── API Keys ────────────────────────────────────────────────────────
103
+ //
104
+ // Hardcoded production defaults removed (F-002). Production keys live
105
+ // in the Postgres ApiKey table and are loaded on boot. In DEV_MODE,
106
+ // the local tokens.json file is also seeded into the in-memory cache.
71
107
 
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
- };
108
+ const API_KEYS = {};
109
+ const API_KEY_HANDLES = {};
110
+ const ACCOUNT_TENANT_PREFIX = "acct:";
111
+ const LEGACY_API_KEY_TENANT_PREFIX = "key:";
112
+ const OAUTH_API_KEY_TENANT_PREFIX = "oauth:";
113
+
114
+ function isInternalTenantId(id) {
115
+ return typeof id === "string"
116
+ && (id.startsWith(ACCOUNT_TENANT_PREFIX)
117
+ || id.startsWith(LEGACY_API_KEY_TENANT_PREFIX)
118
+ || id.startsWith(OAUTH_API_KEY_TENANT_PREFIX));
119
+ }
120
+
121
+ function accountTenantIdForUserId(userId) {
122
+ return ACCOUNT_TENANT_PREFIX + userId;
123
+ }
124
+
125
+ function legacyTenantIdForApiKey(key) {
126
+ return LEGACY_API_KEY_TENANT_PREFIX + createHash("sha256").update(key).digest("base64url").slice(0, 32);
127
+ }
128
+
129
+ function oauthTenantIdForApiKey(key) {
130
+ return OAUTH_API_KEY_TENANT_PREFIX + createHash("sha256").update(key).digest("base64url").slice(0, 32);
131
+ }
79
132
 
80
- // In-memory cache (populated from DB or JSON on boot)
81
- const API_KEYS = { ...DEFAULT_API_KEYS };
133
+ function rememberApiKeyInMemory(key, tenantId, handle = null) {
134
+ API_KEYS[key] = tenantId;
135
+ if (handle) API_KEY_HANDLES[key] = handle;
136
+ else delete API_KEY_HANDLES[key];
137
+ }
138
+
139
+ function rememberLoadedApiKey(key, storedAgentId) {
140
+ const tenantId = isInternalTenantId(storedAgentId) ? storedAgentId : legacyTenantIdForApiKey(key);
141
+ const handle = isInternalTenantId(storedAgentId) ? null : storedAgentId;
142
+ rememberApiKeyInMemory(key, tenantId, handle);
143
+ }
144
+
145
+ function identityForApiKey(key) {
146
+ const tenantId = API_KEYS[key];
147
+ if (!tenantId) return null;
148
+ return {
149
+ agentId: tenantId,
150
+ tenantId,
151
+ handle: API_KEY_HANDLES[key] || tenantId,
152
+ apiKey: key,
153
+ };
154
+ }
82
155
 
83
- // Load from JSON (fallback)
84
156
  function loadTokensFromFile() {
85
- try { return JSON.parse(readFileSync(TOKEN_FILE, "utf8")); } catch { return {}; }
157
+ let rows = {};
158
+ try { rows = JSON.parse(readFileSync(TOKEN_FILE, "utf8")); } catch { return; }
159
+ for (const [key, storedAgentId] of Object.entries(rows)) {
160
+ rememberLoadedApiKey(key, storedAgentId);
161
+ }
86
162
  }
87
- Object.assign(API_KEYS, loadTokensFromFile());
88
163
 
89
- async function saveApiKey(key, agentId) {
90
- API_KEYS[key] = agentId;
164
+ async function loadApiKeysFromDb() {
165
+ if (!usePrisma) return;
166
+ try {
167
+ const rows = await prisma.apiKey.findMany();
168
+ for (const row of rows) rememberLoadedApiKey(row.key, row.agentId);
169
+ } catch (err) {
170
+ if (!DEV_MODE) {
171
+ console.error("FATAL: Prisma loadApiKeys failed; refusing to start.");
172
+ console.error("Cause: " + err.message);
173
+ process.exit(1);
174
+ }
175
+ console.error("Prisma loadApiKeys error (DEV_MODE):", err.message);
176
+ }
177
+ }
178
+
179
+ if (DEV_MODE) {
180
+ loadTokensFromFile();
181
+ }
182
+ await loadApiKeysFromDb();
183
+
184
+ async function saveApiKey(key, agentId, { handle = null } = {}) {
185
+ // Persist before advertising in memory: a newly issued key must not
186
+ // become valid in the in-memory cache if the canonical store did not
187
+ // accept it. Otherwise the key would work for the lifetime of the
188
+ // process and disappear on restart.
91
189
  if (usePrisma) {
92
190
  try {
93
191
  await prisma.apiKey.upsert({
@@ -97,10 +195,17 @@ async function saveApiKey(key, agentId) {
97
195
  });
98
196
  } catch (err) {
99
197
  console.error("Prisma saveApiKey error:", err.message);
198
+ if (!DEV_MODE) throw new Error("saveApiKey persistence failed: " + err.message);
100
199
  }
200
+ } else if (!DEV_MODE) {
201
+ // Production should never reach here (boot exits if Prisma is
202
+ // unavailable), but guard explicitly.
203
+ throw new Error("saveApiKey called without Prisma in production");
204
+ }
205
+ rememberApiKeyInMemory(key, agentId, handle);
206
+ if (DEV_MODE) {
207
+ try { writeFileSync(TOKEN_FILE, JSON.stringify(API_KEYS, null, 2) + "\n"); } catch {}
101
208
  }
102
- // Always write JSON as backup
103
- try { writeFileSync(TOKEN_FILE, JSON.stringify(API_KEYS, null, 2) + "\n"); } catch {}
104
209
  }
105
210
 
106
211
  // ── Passkeys ────────────────────────────────────────────────────────
@@ -113,33 +218,72 @@ function loadPasskeysFromFile() {
113
218
  }
114
219
 
115
220
  async function loadPasskeysFromDb() {
116
- if (!usePrisma) return loadPasskeysFromFile();
221
+ if (!usePrisma) {
222
+ return DEV_MODE ? loadPasskeysFromFile() : [];
223
+ }
117
224
  try {
118
225
  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
- }));
226
+ const handleUserIds = new Map();
227
+ for (const c of creds) {
228
+ const handle = c.user?.name || (c.userId ? "user-" + c.userId.slice(0, 8) : "unknown");
229
+ if (!handleUserIds.has(handle)) handleUserIds.set(handle, new Set());
230
+ handleUserIds.get(handle).add(c.userId);
231
+ }
232
+ const out = [];
233
+ for (const c of creds) {
234
+ const handle = c.user?.name || (c.userId ? "user-" + c.userId.slice(0, 8) : "unknown");
235
+ const agentId = accountTenantIdForUserId(c.userId);
236
+ let apiKey = null;
237
+ for (const [key, tenantId] of Object.entries(API_KEYS)) {
238
+ if (tenantId === agentId || (handleUserIds.get(handle)?.size === 1 && API_KEY_HANDLES[key] === handle)) {
239
+ apiKey = key;
240
+ break;
241
+ }
242
+ }
243
+ if (apiKey) {
244
+ API_KEY_HANDLES[apiKey] = handle;
245
+ if (API_KEYS[apiKey] !== agentId) {
246
+ try {
247
+ await saveApiKey(apiKey, agentId, { handle });
248
+ console.log("loadPasskeysFromDb: migrated API key tenant for handle '" + handle + "' to immutable account id");
249
+ } catch (err) {
250
+ console.error("loadPasskeysFromDb: failed to migrate API key tenant for handle '" + handle + "':", err.message);
251
+ if (!DEV_MODE) throw err;
252
+ }
253
+ }
254
+ } else if (handle !== "unknown") {
255
+ console.warn("loadPasskeysFromDb: no ApiKey row for account tenant '" + agentId + "'; auth-verify will mint on next successful login");
256
+ }
257
+ out.push({
258
+ credentialId: c.id,
259
+ publicKey: Buffer.from(c.publicKey).toString("base64url"),
260
+ counter: c.counter,
261
+ userId: c.userId,
262
+ agentId,
263
+ handle,
264
+ apiKey,
265
+ createdAt: c.createdAt.toISOString(),
266
+ transports: c.transports || [],
267
+ });
268
+ }
269
+ return out;
128
270
  } catch (err) {
129
271
  console.error("Prisma loadPasskeys error:", err.message);
130
- return loadPasskeysFromFile();
272
+ return DEV_MODE ? loadPasskeysFromFile() : [];
131
273
  }
132
274
  }
133
275
 
134
276
  async function savePasskey(entry) {
135
- passkeys.push(entry);
277
+ // Persist before pushing to in-memory: a passkey must not exist in
278
+ // memory if it was never persisted, or it would authenticate for the
279
+ // lifetime of the process and disappear on restart.
136
280
  if (usePrisma) {
137
281
  try {
138
282
  // Ensure user exists
139
283
  let user = await prisma.user.findUnique({ where: { id: entry.userId } });
140
284
  if (!user) {
141
285
  user = await prisma.user.create({
142
- data: { id: entry.userId, name: entry.agentId || "user" },
286
+ data: { id: entry.userId, name: entry.handle || "user" },
143
287
  });
144
288
  }
145
289
  await prisma.credential.create({
@@ -153,15 +297,22 @@ async function savePasskey(entry) {
153
297
  });
154
298
  } catch (err) {
155
299
  console.error("Prisma savePasskey error:", err.message);
300
+ if (!DEV_MODE) throw new Error("savePasskey persistence failed: " + err.message);
156
301
  }
302
+ } else if (!DEV_MODE) {
303
+ throw new Error("savePasskey called without Prisma in production");
304
+ }
305
+ passkeys.push(entry);
306
+ if (DEV_MODE) {
307
+ try { writeFileSync(PASSKEY_FILE, JSON.stringify(passkeys, null, 2) + "\n"); } catch {}
157
308
  }
158
- // Always write JSON as backup
159
- try { writeFileSync(PASSKEY_FILE, JSON.stringify(passkeys, null, 2) + "\n"); } catch {}
160
309
  }
161
310
 
162
311
  async function updatePasskeyCounter(credentialId, newCounter) {
163
- const entry = passkeys.find(p => p.credentialId === credentialId);
164
- if (entry) entry.counter = newCounter;
312
+ // Persist before updating in-memory. The counter is the WebAuthn
313
+ // replay-protection state; advancing it in memory while the DB row
314
+ // stays behind would let a replayed assertion validate after a
315
+ // restart re-loaded the stale counter.
165
316
  if (usePrisma) {
166
317
  try {
167
318
  await prisma.credential.update({
@@ -170,9 +321,16 @@ async function updatePasskeyCounter(credentialId, newCounter) {
170
321
  });
171
322
  } catch (err) {
172
323
  console.error("Prisma updateCounter error:", err.message);
324
+ if (!DEV_MODE) throw new Error("updatePasskeyCounter persistence failed: " + err.message);
173
325
  }
326
+ } else if (!DEV_MODE) {
327
+ throw new Error("updatePasskeyCounter called without Prisma in production");
328
+ }
329
+ const entry = passkeys.find(p => p.credentialId === credentialId);
330
+ if (entry) entry.counter = newCounter;
331
+ if (DEV_MODE) {
332
+ try { writeFileSync(PASSKEY_FILE, JSON.stringify(passkeys, null, 2) + "\n"); } catch {}
174
333
  }
175
- try { writeFileSync(PASSKEY_FILE, JSON.stringify(passkeys, null, 2) + "\n"); } catch {}
176
334
  }
177
335
 
178
336
  // Boot: load passkeys
@@ -219,7 +377,7 @@ function authenticate(req) {
219
377
  const auth = req.headers["authorization"];
220
378
  if (!auth?.startsWith("Bearer ")) return null;
221
379
  const key = auth.slice(7).trim();
222
- return API_KEYS[key] ? { agentId: API_KEYS[key], apiKey: key } : null;
380
+ return identityForApiKey(key);
223
381
  }
224
382
 
225
383
  function readBody(req) {
@@ -275,13 +433,92 @@ function parseUrl(reqUrl) {
275
433
  return new URL(reqUrl, "http://localhost");
276
434
  }
277
435
 
436
+ // ── Rate limiting (F-008 in the VPS hosted-mcp audit) ───────────────
437
+ //
438
+ // Per-IP, per-bucket fixed-window counter. In-process Map; resets on
439
+ // restart. nginx-side limit_req would be more durable but harder to
440
+ // scope per route; in-process keeps the policy with the code that
441
+ // mints/validates the auth tokens. Defaults are conservative; tune via
442
+ // env. Stale entries are pruned periodically so memory stays bounded.
443
+ //
444
+ // Buckets:
445
+ // mint ... endpoints that issue a credential or ticket
446
+ // validate ... endpoints that consume / verify a credential
447
+ // status ... poll-friendly endpoints (higher limit)
448
+
449
+ const RATE_LIMIT_BUCKETS = {
450
+ mint: { limit: parseInt(process.env.LDM_HOSTED_MCP_RL_MINT || "30", 10), windowMs: 60_000 },
451
+ validate: { limit: parseInt(process.env.LDM_HOSTED_MCP_RL_VALIDATE || "60", 10), windowMs: 60_000 },
452
+ status: { limit: parseInt(process.env.LDM_HOSTED_MCP_RL_STATUS || "120", 10), windowMs: 60_000 },
453
+ };
454
+
455
+ const rateLimitState = new Map(); // key: "<bucket>:<ip>" -> { count, windowStart }
456
+
457
+ function getClientIp(req) {
458
+ // Prefer X-Real-IP (nginx overwrites on proxy hop, harder to spoof
459
+ // through the proxy). Fall back to the LAST entry in X-Forwarded-For
460
+ // (nginx appends $remote_addr via proxy_add_x_forwarded_for, so the
461
+ // last entry is the real client IP from nginx's perspective; the
462
+ // first entries are attacker-controlled). Last fallback: socket.
463
+ const xRealIp = req.headers["x-real-ip"];
464
+ if (typeof xRealIp === "string" && xRealIp.length > 0) return xRealIp.trim();
465
+ const xff = req.headers["x-forwarded-for"];
466
+ if (typeof xff === "string" && xff.length > 0) {
467
+ const parts = xff.split(",").map(s => s.trim()).filter(Boolean);
468
+ if (parts.length > 0) return parts[parts.length - 1];
469
+ }
470
+ return req.socket?.remoteAddress || "unknown";
471
+ }
472
+
473
+ function rateLimitCheck(req, bucket) {
474
+ const config = RATE_LIMIT_BUCKETS[bucket];
475
+ if (!config) return { ok: true };
476
+ const ip = getClientIp(req);
477
+ const key = bucket + ":" + ip;
478
+ const now = Date.now();
479
+ const entry = rateLimitState.get(key);
480
+ if (!entry || now - entry.windowStart > config.windowMs) {
481
+ rateLimitState.set(key, { count: 1, windowStart: now });
482
+ return { ok: true };
483
+ }
484
+ entry.count += 1;
485
+ if (entry.count > config.limit) {
486
+ const retryAfterSec = Math.max(1, Math.ceil((config.windowMs - (now - entry.windowStart)) / 1000));
487
+ return { ok: false, retryAfterSec };
488
+ }
489
+ return { ok: true };
490
+ }
491
+
492
+ // Returns true if the request is allowed. If limited, writes 429 and
493
+ // returns false; the caller must `return` immediately on false.
494
+ function applyRateLimit(req, res, bucket) {
495
+ const result = rateLimitCheck(req, bucket);
496
+ if (!result.ok) {
497
+ res.setHeader("Retry-After", String(result.retryAfterSec));
498
+ json(res, 429, { error: "rate_limit_exceeded", error_description: "Too many requests. Retry after " + result.retryAfterSec + "s." });
499
+ console.warn("rate-limit hit:", bucket, getClientIp(req), req.method, req.url?.split("?")[0]);
500
+ return false;
501
+ }
502
+ return true;
503
+ }
504
+
505
+ // Keep memory bounded: drop entries older than 2 windows.
506
+ setInterval(() => {
507
+ const now = Date.now();
508
+ for (const [key, entry] of rateLimitState) {
509
+ if (now - entry.windowStart > 2 * 60_000) {
510
+ rateLimitState.delete(key);
511
+ }
512
+ }
513
+ }, 5 * 60_000).unref();
514
+
278
515
  function esc(s) {
279
516
  return s.replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
280
517
  }
281
518
 
282
- function sanitizeUsername(raw) {
519
+ function sanitizeDisplayLabel(raw) {
283
520
  if (!raw || typeof raw !== "string") return null;
284
- const cleaned = raw.toLowerCase().replace(/[^a-z0-9-]/g, "").slice(0, 30);
521
+ const cleaned = raw.replace(/[\u0000-\u001f\u007f]/g, "").replace(/\s+/g, " ").trim().slice(0, 64);
285
522
  return cleaned.length > 0 ? cleaned : null;
286
523
  }
287
524
 
@@ -406,14 +643,17 @@ async function handleRegisterOptions(req, res) {
406
643
  let body;
407
644
  try { body = await readBody(req); } catch { body = {}; }
408
645
 
409
- // Accept optional username from request body
410
- const username = sanitizeUsername(body?.username);
646
+ // Accept the existing `username` field for wire compatibility, but
647
+ // treat it only as a display label for the passkey prompt. It is not
648
+ // a public username, account handle, or relay tenant boundary.
649
+ // Duplicate display labels are allowed.
650
+ const displayLabel = sanitizeDisplayLabel(body?.displayName || body?.username);
411
651
 
412
652
  const userId = randomBytes(16);
413
653
  const userIdB64 = userId.toString("base64url");
414
654
 
415
- const userName = username || ("user-" + userIdB64.slice(0, 8));
416
- const displayName = username || "Memory Crystal User";
655
+ const userName = displayLabel || ("user-" + userIdB64.slice(0, 8));
656
+ const displayName = displayLabel || "Memory Crystal User";
417
657
 
418
658
  let options;
419
659
  try {
@@ -442,7 +682,7 @@ async function handleRegisterOptions(req, res) {
442
682
  challenge: options.challenge,
443
683
  type: "registration",
444
684
  userId: userIdB64,
445
- username: username,
685
+ displayLabel,
446
686
  expires: Date.now() + 120000,
447
687
  };
448
688
 
@@ -495,8 +735,16 @@ async function handleRegisterVerify(req, res) {
495
735
 
496
736
  const { credential: cred, credentialDeviceType, credentialBackedUp } = verification.registrationInfo;
497
737
 
498
- // Use provided username as agentId, or fall back to passkey-<id>
499
- const agentId = stored.username || ("passkey-" + stored.userId.slice(0, 12));
738
+ // Internal tenancy is the immutable WebAuthn user id. The user-entered
739
+ // display label is metadata only and never owns a relay namespace.
740
+ const agentId = accountTenantIdForUserId(stored.userId);
741
+ // credentialLabel matches the userName passed to
742
+ // generateRegistrationOptions in handleRegisterOptions, which is what
743
+ // iOS Passwords / 1Password show next to the saved passkey. The
744
+ // welcome view should display this, not agentId. Auth semantics are
745
+ // unchanged; only the user-facing label is aligned with the saved
746
+ // credential.
747
+ const credentialLabel = stored.displayLabel || ("user-" + stored.userId.slice(0, 8));
500
748
  const apiKey = generateApiKey();
501
749
 
502
750
  const entry = {
@@ -505,18 +753,25 @@ async function handleRegisterVerify(req, res) {
505
753
  counter: cred.counter,
506
754
  userId: stored.userId,
507
755
  agentId,
756
+ handle: credentialLabel,
508
757
  apiKey,
509
758
  deviceType: credentialDeviceType,
510
759
  backedUp: credentialBackedUp,
511
760
  transports: credential.response?.transports || [],
512
761
  createdAt: new Date().toISOString(),
513
762
  };
514
- await savePasskey(entry);
515
- await saveApiKey(apiKey, agentId);
763
+ try {
764
+ await savePasskey(entry);
765
+ await saveApiKey(apiKey, agentId, { handle: credentialLabel });
766
+ } catch (err) {
767
+ console.error("Persistence failure during passkey registration:", err.message);
768
+ json(res, 500, { error: "persistence_failure", error_description: "Could not persist credentials. Try again." });
769
+ return;
770
+ }
516
771
 
517
- console.log("WebAuthn: registered passkey for agent '" + agentId + "' (credId: " + cred.id.slice(0, 16) + "...)");
772
+ console.log("WebAuthn: registered passkey for tenant '" + agentId + "' handle '" + credentialLabel + "' (credId: " + cred.id.slice(0, 16) + "...)");
518
773
 
519
- json(res, 200, { success: true, agentId, apiKey });
774
+ json(res, 200, { success: true, agentId: credentialLabel, tenantId: agentId, apiKey, credentialLabel });
520
775
  }
521
776
 
522
777
  // POST /webauthn/auth-options
@@ -602,12 +857,53 @@ async function handleAuthVerify(req, res) {
602
857
  return;
603
858
  }
604
859
 
605
- entry.counter = verification.authenticationInfo.newCounter;
606
- await updatePasskeyCounter(entry.credentialId, entry.counter);
860
+ // Persist new counter before mutating in-memory entry. updatePasskeyCounter
861
+ // performs the in-memory update only on success, so the in-memory counter
862
+ // stays consistent with the DB and replay protection holds across restarts.
863
+ try {
864
+ await updatePasskeyCounter(entry.credentialId, verification.authenticationInfo.newCounter);
865
+ } catch (err) {
866
+ console.error("Persistence failure during passkey counter update:", err.message);
867
+ json(res, 500, { error: "persistence_failure", error_description: "Could not persist counter. Try again." });
868
+ return;
869
+ }
870
+
871
+ let credentialLabel = entry.handle;
872
+ if (!credentialLabel && entry.agentId && entry.agentId.startsWith("passkey-")) {
873
+ credentialLabel = (typeof entry.userId === "string" && entry.userId.length >= 8)
874
+ ? "user-" + entry.userId.slice(0, 8)
875
+ : entry.agentId;
876
+ } else if (!credentialLabel && !isInternalTenantId(entry.agentId)) {
877
+ credentialLabel = entry.agentId;
878
+ } else if (!credentialLabel && typeof entry.userId === "string" && entry.userId.length >= 8) {
879
+ credentialLabel = "user-" + entry.userId.slice(0, 8);
880
+ } else if (!credentialLabel) {
881
+ credentialLabel = "you";
882
+ }
883
+
884
+ // Recovery path: a passkey reloaded from Postgres after a restart may
885
+ // have entry.apiKey = null if no ApiKey row was found for its agent
886
+ // at boot. Mint a fresh ck- now so the login response always carries
887
+ // a usable token. Without this, the browser would store
888
+ // sessionStorage.wip_api_key = null and Remote Control would 401 on
889
+ // /bootstrap and /ws-ticket.
890
+ if (!entry.apiKey) {
891
+ const newKey = generateApiKey();
892
+ try {
893
+ await saveApiKey(newKey, entry.agentId, { handle: credentialLabel });
894
+ } catch (err) {
895
+ console.error("Persistence failure minting recovery key for tenant '" + entry.agentId + "':", err.message);
896
+ json(res, 500, { error: "persistence_failure", error_description: "Could not mint API key. Try again." });
897
+ return;
898
+ }
899
+ entry.apiKey = newKey;
900
+ entry.handle = credentialLabel;
901
+ console.log("WebAuthn: minted recovery key for tenant '" + entry.agentId + "' (key: " + newKey.slice(0, 10) + "...)");
902
+ }
607
903
 
608
- console.log("WebAuthn: authenticated agent '" + entry.agentId + "'");
904
+ console.log("WebAuthn: authenticated tenant '" + entry.agentId + "' handle '" + credentialLabel + "'");
609
905
 
610
- json(res, 200, { success: true, agentId: entry.agentId, apiKey: entry.apiKey });
906
+ json(res, 200, { success: true, agentId: credentialLabel, tenantId: entry.agentId, apiKey: entry.apiKey, credentialLabel });
611
907
  }
612
908
 
613
909
  // ---------- Page handlers ----------
@@ -1018,19 +1314,18 @@ async function handleOAuthToken(req, res) {
1018
1314
  }
1019
1315
  }
1020
1316
 
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);
1317
+ const agentHandle = stored.agent_name || "oauth-user";
1318
+ const apiKey = generateApiKey();
1319
+ const agentId = oauthTenantIdForApiKey(apiKey);
1320
+ try {
1321
+ await saveApiKey(apiKey, agentId, { handle: agentHandle });
1322
+ } catch (err) {
1323
+ console.error("Persistence failure during OAuth token issuance:", err.message);
1324
+ json(res, 500, { error: "server_error", error_description: "Could not issue token. Try again." });
1325
+ return;
1031
1326
  }
1032
1327
 
1033
- console.log("OAuth: issued token for agent '" + agentId + "' (key: " + apiKey.slice(0, 10) + "...)");
1328
+ console.log("OAuth: issued token for tenant '" + agentId + "' handle '" + agentHandle + "' (key: " + apiKey.slice(0, 10) + "...)");
1034
1329
 
1035
1330
  json(res, 200, {
1036
1331
  access_token: apiKey,
@@ -1383,12 +1678,53 @@ function handleAgentAuthApprove(req, res) {
1383
1678
 
1384
1679
  // ---------- QR Login (Chrome fallback) ----------
1385
1680
 
1681
+ // `next` whitelist for the QR login flow. Two shapes are allowed; both
1682
+ // land the user on a known phone-side surface after successful sign-in.
1683
+ // Anything else is silently dropped. `next` is NOT a general redirect
1684
+ // primitive.
1685
+ //
1686
+ // 1. PAIR_NEXT_REGEX: /pair/<CODE> using the daemon's real alphabet
1687
+ // (CODEX_PAIR_ALPHABET, length 6, L IS included; I/O/0/1 excluded).
1688
+ // See plan ai/product/plans-prds/codex-remote-control/
1689
+ // 2026-04-30--cc-mini--pair-via-login-qr-flow.md constraints C1,
1690
+ // C8, and round-5. Per C8 the URL fallback for this shape is
1691
+ // mobile-only (desktop must not become the pairing authority).
1692
+ //
1693
+ // 2. REMOTE_CONTROL_NEXT_REGEX: /codex-remote-control/<UUID> for the
1694
+ // Kaleidoscope phone-side remote-control thread surface. Standard
1695
+ // ?next semantics; allowed on both desktop and mobile (this is
1696
+ // navigation continuation, not authority transfer).
1697
+ const PAIR_NEXT_REGEX = /^\/pair\/[ABCDEFGHJKLMNPQRSTUVWXYZ23456789]{6}$/;
1698
+ 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;
1699
+
1700
+ function sanitizeCrcPairNext(raw) {
1701
+ if (typeof raw !== "string") return null;
1702
+ // Single decode; reject if a second decode would still differ.
1703
+ let decoded;
1704
+ try { decoded = decodeURIComponent(raw); } catch { return null; }
1705
+ // Catch double-encoded payloads.
1706
+ if (decoded !== raw && /%/.test(decoded)) return null;
1707
+ if (!PAIR_NEXT_REGEX.test(decoded) && !REMOTE_CONTROL_NEXT_REGEX.test(decoded)) return null;
1708
+ return decoded;
1709
+ }
1710
+
1386
1711
  // POST /api/qr-login ... create a QR login session
1387
1712
  async function handleQrLoginStart(req, res) {
1388
1713
  cleanupExpiredChallenges();
1389
1714
  const body = await readBody(req).catch(() => ({}));
1390
1715
  const handle = ((body && body.handle) || "").trim().toLowerCase().replace(/[^a-z0-9-]/g, "").slice(0, 30);
1391
1716
  const mode = ((body && body.mode) || "register") === "signin" ? "signin" : "register";
1717
+ // Validate `next` strictly. Invalid next is silently dropped, not
1718
+ // 400'd, so legacy callers still work.
1719
+ //
1720
+ // Only /pair/<CODE> next triggers pair-mode (C6 strip on desktop
1721
+ // status, C8 desktop-no-redirect, the "phone is the actor" model).
1722
+ // /codex-remote-control/<UUID> is a normal post-login continuation:
1723
+ // desktop status returns the full login response (apiKey, handle,
1724
+ // next) so the desktop poll can authenticate and redirect on its
1725
+ // own. The phone also gets next via approve, so both ends can act.
1726
+ const next = sanitizeCrcPairNext(body && body.next);
1727
+ const purpose = (next && PAIR_NEXT_REGEX.test(next)) ? "pair" : null;
1392
1728
  const sessionId = randomUUID();
1393
1729
  const loginUrl = ISSUER_URL + "/login?s=" + sessionId + "&m=" + mode + (handle ? "&h=" + encodeURIComponent(handle) : "");
1394
1730
  const qrBuffer = await QRCode.toBuffer(loginUrl, { type: "png", width: 400, margin: 2 });
@@ -1399,8 +1735,10 @@ async function handleQrLoginStart(req, res) {
1399
1735
  apiKey: null,
1400
1736
  handle: handle || null,
1401
1737
  expires: Date.now() + QR_LOGIN_EXPIRY_MS,
1738
+ purpose, // "pair" | null
1739
+ next: next || null, // sanitized `/pair/<CODE>` or null
1402
1740
  };
1403
- console.log("QR login: created session " + sessionId.slice(0, 8) + "...");
1741
+ console.log("QR login: created session " + sessionId.slice(0, 8) + "..." + (purpose === "pair" ? " (pair-mode)" : ""));
1404
1742
  json(res, 200, { sessionId, qrUrl: "/api/qr-login/qr?s=" + sessionId });
1405
1743
  }
1406
1744
 
@@ -1418,6 +1756,12 @@ function handleQrLoginQR(req, res) {
1418
1756
  }
1419
1757
 
1420
1758
  // GET /api/qr-login/status?s=XXX ... poll for completion
1759
+ //
1760
+ // Response shape depends on `purpose`:
1761
+ // - Pair-mode (purpose === "pair"): {status, agentId} only on approved.
1762
+ // NEVER returns apiKey or next to the desktop. Phone receives next via
1763
+ // /api/qr-login/approve. Per plan C6 round 4.
1764
+ // - Legacy login mode: {status, agentId, apiKey} on approved (unchanged).
1421
1765
  function handleQrLoginStatus(req, res) {
1422
1766
  const url = parseUrl(req.url);
1423
1767
  const s = url.searchParams.get("s");
@@ -1427,7 +1771,28 @@ function handleQrLoginStatus(req, res) {
1427
1771
  return;
1428
1772
  }
1429
1773
  if (entry.status === "approved") {
1430
- json(res, 200, { status: "approved", agentId: entry.agentId, apiKey: entry.apiKey });
1774
+ if (entry.purpose === "pair") {
1775
+ // Pair-mode (purpose === "pair", next === /pair/<CODE>):
1776
+ // desktop gets ONLY a display label. No apiKey. No next. Plan
1777
+ // C6 round 4. Desktop never becomes the pairing authority.
1778
+ json(res, 200, { status: "approved", agentId: entry.agentId });
1779
+ } else {
1780
+ // Legacy login mode OR codex-remote-control continuation
1781
+ // (purpose === null). Desktop gets full identity to render the
1782
+ // welcome view OR redirect to next on its own poll.
1783
+ // credentialLabel matches the saved-passkey label (see
1784
+ // register-verify / auth-verify). next is included only if a
1785
+ // sanitized non-pair-mode next was set on the session
1786
+ // (currently /codex-remote-control/<UUID>); legacy login
1787
+ // sessions without next get next === null.
1788
+ json(res, 200, {
1789
+ status: "approved",
1790
+ agentId: entry.agentId,
1791
+ apiKey: entry.apiKey,
1792
+ credentialLabel: entry.credentialLabel || null,
1793
+ next: entry.next || null,
1794
+ });
1795
+ }
1431
1796
  delete qrLoginSessions[s]; // one-time use
1432
1797
  } else {
1433
1798
  json(res, 200, { status: "pending" });
@@ -1435,9 +1800,13 @@ function handleQrLoginStatus(req, res) {
1435
1800
  }
1436
1801
 
1437
1802
  // POST /api/qr-login/approve ... phone calls after passkey created
1803
+ //
1804
+ // In pair-mode, the response includes the sanitized `next` so the phone
1805
+ // can location.replace(next) into /pair/<CODE>. Legacy login mode returns
1806
+ // {ok: true} unchanged.
1438
1807
  function handleQrLoginApprove(req, res) {
1439
1808
  readBody(req).then(function(body) {
1440
- const { sessionId, agentId, apiKey } = body || {};
1809
+ const { sessionId, agentId, apiKey, credentialLabel } = body || {};
1441
1810
  const entry = qrLoginSessions[sessionId];
1442
1811
  if (!entry || Date.now() > entry.expires) {
1443
1812
  json(res, 404, { error: "Session not found or expired" });
@@ -1450,8 +1819,21 @@ function handleQrLoginApprove(req, res) {
1450
1819
  entry.status = "approved";
1451
1820
  entry.agentId = agentId;
1452
1821
  entry.apiKey = apiKey;
1453
- console.log("QR login: approved session " + sessionId.slice(0, 8) + "... for '" + agentId + "'");
1454
- json(res, 200, { ok: true });
1822
+ // Phone-side passes the label it received from register-verify /
1823
+ // auth-verify so the desktop can show the same string the user
1824
+ // just saved on their phone. Optional for back-compat.
1825
+ entry.credentialLabel = (typeof credentialLabel === "string" && credentialLabel.length <= 64) ? credentialLabel : null;
1826
+ console.log("QR login: approved session " + sessionId.slice(0, 8) + "... for '" + agentId + "'" + (entry.purpose === "pair" ? " (pair-mode)" : (entry.next ? " (next=" + entry.next + ")" : "")));
1827
+ // Phone receives next on approve regardless of purpose, so the
1828
+ // phone can redirect to either /pair/<CODE> (pair-mode, phone is
1829
+ // the actor) or /codex-remote-control/<UUID> (continuation, phone
1830
+ // can act). Desktop's separate behavior (strip vs full response)
1831
+ // is handled in handleQrLoginStatus.
1832
+ if (entry.next) {
1833
+ json(res, 200, { ok: true, next: entry.next });
1834
+ } else {
1835
+ json(res, 200, { ok: true });
1836
+ }
1455
1837
  }).catch(function() {
1456
1838
  json(res, 400, { error: "Invalid request" });
1457
1839
  });
@@ -1876,13 +2258,28 @@ const httpServer = createServer(async (req, res) => {
1876
2258
  }
1877
2259
 
1878
2260
  if (req.method === "GET" && (path === "/login" || path === "/login/")) {
1879
- // Serve the new app/ login (two-path: this device or QR-from-phone).
2261
+ // Production /login owns its own file at app/kaleidoscope-login.html.
2262
+ //
2263
+ // Earlier this route served demo/login.html, which made production
2264
+ // auth depend on a file under demo/. That coupling is wrong: demo/
2265
+ // is the demo site's domain, not production. The canonical
2266
+ // Kaleidoscope login HTML now lives under app/, where production
2267
+ // owns it.
2268
+ //
2269
+ // Fallback chain (defense in depth):
2270
+ // 1. app/kaleidoscope-login.html ... canonical production file.
2271
+ // 2. demo/login.html ... legacy fallback during the
2272
+ // transition; will be removed in a follow-up once the
2273
+ // production file is verified live.
2274
+ // 3. handleLoginPage ... server-rendered last resort.
2275
+ //
2276
+ // /login/app continues to serve the developed app/login.html flow
2277
+ // (see the next handler).
1880
2278
  try {
1881
- const loginHtml = readFileSync(join(__dirname, "app", "login.html"), "utf8");
2279
+ const html = readFileSync(join(__dirname, "app", "kaleidoscope-login.html"), "utf8");
1882
2280
  res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
1883
- res.end(loginHtml);
2281
+ res.end(html);
1884
2282
  } catch {
1885
- // Fallback to legacy demo login, then server-rendered.
1886
2283
  try {
1887
2284
  const legacy = readFileSync(join(__dirname, "demo", "login.html"), "utf8");
1888
2285
  res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
@@ -1894,6 +2291,22 @@ const httpServer = createServer(async (req, res) => {
1894
2291
  return;
1895
2292
  }
1896
2293
 
2294
+ if (req.method === "GET" && (path === "/login/app" || path === "/login/app/")) {
2295
+ // Explicit non-primary route for the app/login.html flow (the
2296
+ // newer two-path "this device or QR-from-phone" copy). This
2297
+ // exists so the developed flow stays reachable without hijacking
2298
+ // /login. If app/login.html is not present, return 404 rather
2299
+ // than silently falling back to the canonical /login page.
2300
+ try {
2301
+ const loginHtml = readFileSync(join(__dirname, "app", "login.html"), "utf8");
2302
+ res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
2303
+ res.end(loginHtml);
2304
+ } catch {
2305
+ json(res, 404, { error: "Not found" });
2306
+ }
2307
+ return;
2308
+ }
2309
+
1897
2310
  // --- Legal pages ---
1898
2311
 
1899
2312
  if (req.method === "GET" && (path === "/legal/privacy/en-ww/" || path === "/legal/privacy/en-ww")) {
@@ -1917,21 +2330,25 @@ const httpServer = createServer(async (req, res) => {
1917
2330
  // --- WebAuthn API ---
1918
2331
 
1919
2332
  if (req.method === "POST" && path === "/webauthn/register-options") {
2333
+ if (!applyRateLimit(req, res, "mint")) return;
1920
2334
  await handleRegisterOptions(req, res);
1921
2335
  return;
1922
2336
  }
1923
2337
 
1924
2338
  if (req.method === "POST" && path === "/webauthn/register-verify") {
2339
+ if (!applyRateLimit(req, res, "validate")) return;
1925
2340
  await handleRegisterVerify(req, res);
1926
2341
  return;
1927
2342
  }
1928
2343
 
1929
2344
  if (req.method === "POST" && path === "/webauthn/auth-options") {
2345
+ if (!applyRateLimit(req, res, "mint")) return;
1930
2346
  await handleAuthOptions(req, res);
1931
2347
  return;
1932
2348
  }
1933
2349
 
1934
2350
  if (req.method === "POST" && path === "/webauthn/auth-verify") {
2351
+ if (!applyRateLimit(req, res, "validate")) return;
1935
2352
  await handleAuthVerify(req, res);
1936
2353
  return;
1937
2354
  }
@@ -1949,6 +2366,7 @@ const httpServer = createServer(async (req, res) => {
1949
2366
  }
1950
2367
 
1951
2368
  if (req.method === "POST" && path === "/oauth/register") {
2369
+ if (!applyRateLimit(req, res, "mint")) return;
1952
2370
  await handleOAuthRegister(req, res);
1953
2371
  return;
1954
2372
  }
@@ -1959,11 +2377,13 @@ const httpServer = createServer(async (req, res) => {
1959
2377
  }
1960
2378
 
1961
2379
  if (req.method === "POST" && path === "/oauth/authorize/submit") {
2380
+ if (!applyRateLimit(req, res, "validate")) return;
1962
2381
  await handleOAuthAuthorizeSubmit(req, res);
1963
2382
  return;
1964
2383
  }
1965
2384
 
1966
2385
  if (req.method === "POST" && path === "/oauth/token") {
2386
+ if (!applyRateLimit(req, res, "mint")) return;
1967
2387
  await handleOAuthToken(req, res);
1968
2388
  return;
1969
2389
  }
@@ -1971,21 +2391,25 @@ const httpServer = createServer(async (req, res) => {
1971
2391
  // --- Agent QR Auth ---
1972
2392
 
1973
2393
  if (req.method === "GET" && path === "/demo/api/agent-auth") {
2394
+ if (!applyRateLimit(req, res, "mint")) return;
1974
2395
  await handleAgentAuthStart(req, res);
1975
2396
  return;
1976
2397
  }
1977
2398
 
1978
2399
  if (req.method === "GET" && path === "/demo/api/agent-auth/qr") {
2400
+ if (!applyRateLimit(req, res, "status")) return;
1979
2401
  handleAgentAuthQR(req, res);
1980
2402
  return;
1981
2403
  }
1982
2404
 
1983
2405
  if (req.method === "GET" && path === "/demo/api/agent-auth/status") {
2406
+ if (!applyRateLimit(req, res, "status")) return;
1984
2407
  handleAgentAuthStatus(req, res);
1985
2408
  return;
1986
2409
  }
1987
2410
 
1988
2411
  if (req.method === "POST" && path === "/demo/api/agent-auth/approve") {
2412
+ if (!applyRateLimit(req, res, "validate")) return;
1989
2413
  handleAgentAuthApprove(req, res);
1990
2414
  return;
1991
2415
  }
@@ -2017,21 +2441,25 @@ const httpServer = createServer(async (req, res) => {
2017
2441
  // --- QR Login (Chrome fallback) ---
2018
2442
 
2019
2443
  if (req.method === "POST" && path === "/api/qr-login") {
2444
+ if (!applyRateLimit(req, res, "mint")) return;
2020
2445
  await handleQrLoginStart(req, res);
2021
2446
  return;
2022
2447
  }
2023
2448
 
2024
2449
  if (req.method === "GET" && path === "/api/qr-login/qr") {
2450
+ if (!applyRateLimit(req, res, "status")) return;
2025
2451
  handleQrLoginQR(req, res);
2026
2452
  return;
2027
2453
  }
2028
2454
 
2029
2455
  if (req.method === "GET" && path === "/api/qr-login/status") {
2456
+ if (!applyRateLimit(req, res, "status")) return;
2030
2457
  handleQrLoginStatus(req, res);
2031
2458
  return;
2032
2459
  }
2033
2460
 
2034
2461
  if (req.method === "POST" && path === "/api/qr-login/approve") {
2462
+ if (!applyRateLimit(req, res, "validate")) return;
2035
2463
  handleQrLoginApprove(req, res);
2036
2464
  return;
2037
2465
  }
@@ -2094,32 +2522,38 @@ const httpServer = createServer(async (req, res) => {
2094
2522
  // --- Codex Relay (codex-daemon ↔ phone) ---
2095
2523
 
2096
2524
  if (req.method === "POST" && path === "/api/codex-relay/pair-init") {
2525
+ if (!applyRateLimit(req, res, "mint")) return;
2097
2526
  await handleCodexPairInit(req, res);
2098
2527
  return;
2099
2528
  }
2100
2529
 
2101
2530
  if (req.method === "GET" && path.startsWith("/api/codex-relay/pair-status/")) {
2531
+ if (!applyRateLimit(req, res, "status")) return;
2102
2532
  handleCodexPairStatus(req, res, path.slice("/api/codex-relay/pair-status/".length));
2103
2533
  return;
2104
2534
  }
2105
2535
 
2106
2536
  if (req.method === "POST" && path === "/api/codex-relay/pair-complete") {
2537
+ if (!applyRateLimit(req, res, "validate")) return;
2107
2538
  await handleCodexPairComplete(req, res);
2108
2539
  return;
2109
2540
  }
2110
2541
 
2111
2542
  if (req.method === "GET" && path === "/api/codex-relay/state") {
2543
+ if (!applyRateLimit(req, res, "status")) return;
2112
2544
  handleCodexRelayState(req, res);
2113
2545
  return;
2114
2546
  }
2115
2547
 
2116
2548
  if (req.method === "GET" && path.startsWith("/api/codex-relay/bootstrap/")) {
2549
+ if (!applyRateLimit(req, res, "mint")) return;
2117
2550
  const tid = decodeURIComponent(path.slice("/api/codex-relay/bootstrap/".length));
2118
2551
  handleCodexBootstrap(req, res, tid);
2119
2552
  return;
2120
2553
  }
2121
2554
 
2122
2555
  if (req.method === "POST" && path === "/api/codex-relay/ws-ticket") {
2556
+ if (!applyRateLimit(req, res, "mint")) return;
2123
2557
  await handleCodexWsTicket(req, res);
2124
2558
  return;
2125
2559
  }
@@ -2131,6 +2565,13 @@ const httpServer = createServer(async (req, res) => {
2131
2565
  return;
2132
2566
  }
2133
2567
 
2568
+ // /pair/<CODE> ... URL-first pair flow. Per plan C1 + round 5: real daemon
2569
+ // alphabet [ABCDEFGHJKLMNPQRSTUVWXYZ23456789]{6}, length 6, L IS included.
2570
+ if (req.method === "GET" && /^\/pair\/[ABCDEFGHJKLMNPQRSTUVWXYZ23456789]{6}$/.test(path)) {
2571
+ serveAppFile(res, "pair.html");
2572
+ return;
2573
+ }
2574
+
2134
2575
  // /:handle/codex-remote-control/:threadId
2135
2576
  const remoteControlMatch = path.match(/^\/([^/]+)\/codex-remote-control\/([^/]+)\/?$/);
2136
2577
  if (req.method === "GET" && remoteControlMatch) {
@@ -2151,21 +2592,22 @@ const httpServer = createServer(async (req, res) => {
2151
2592
  // ---------- Codex Relay (codex-daemon ↔ phone) ----------
2152
2593
  //
2153
2594
  // 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
2595
+ // immutable tenant id (one daemon per tenant; new daemon kicks the old one). Web clients
2596
+ // indexed by `tenantId:threadId`. Server is a transparent passthrough between
2156
2597
  // the daemon and the matching web client(s); thread routing is enforced
2157
2598
  // purely client-side via session.send/sessionId payloads.
2158
2599
 
2159
2600
  const CODEX_PAIR_EXPIRY_MS = 5 * 60 * 1000;
2160
2601
  const CODEX_PAIR_ALPHABET = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
2161
- const codexPairings = {}; // pairing_id -> { code, status, expires, daemon_info, apiKey?, agentId?, daemon_public_key?, crypto_versions? }
2602
+ const codexPairings = {}; // pairing_id -> { code, status, expires, poll_token, poll_token_used?, daemon_info, apiKey?, agentId?, handle?, daemon_public_key?, crypto_versions? }
2162
2603
  const codexPairingByCode = {}; // code -> pairing_id (only while pending)
2163
2604
  const codexDaemons = new Map(); // agentId -> ws
2164
- const codexWebClients = new Map(); // `${agentId}:${threadId}` -> ws
2605
+ const codexWebClients = new Map(); // `${agentId}:${threadId}` -> Set<ws>
2606
+ const codexE2eeSessionRoutes = new Map(); // `${agentId}:${e2eeSession}` -> { threadId, webKey, ws }
2165
2607
 
2166
2608
  // E2EE substrate (Phase 2.5).
2167
2609
  //
2168
- // codexDaemonPubkeys: per agentId, the most recently paired daemon's
2610
+ // codexDaemonPubkeys: per tenant id, the most recently paired daemon's
2169
2611
  // public key (P-256 SPKI base64url) + supported crypto versions +
2170
2612
  // registration timestamp. This is what the browser fetches via
2171
2613
  // bootstrap before opening an encrypted session.
@@ -2174,10 +2616,150 @@ const codexWebClients = new Map(); // `${agentId}:${threadId}` -> ws
2174
2616
  // ?token=ck-... in the browser WebSocket URL. Bound to a specific
2175
2617
  // (agentId, threadId) so a leaked ticket cannot drive a different
2176
2618
  // route, even by the same authenticated user.
2177
- const codexDaemonPubkeys = new Map(); // agentId -> { pubkey, crypto_versions, registered_at }
2619
+ const codexDaemonPubkeys = new Map(); // tenantId -> { pubkey, crypto_versions, registered_at }
2178
2620
  const codexRelayTickets = new Map(); // ticket -> { agentId, threadId, expires, used }
2179
2621
  const CODEX_RELAY_TICKET_TTL_MS = 60 * 1000; // 60s; browser must connect immediately
2180
2622
 
2623
+ async function ensureCodexDaemonPubkeyStore() {
2624
+ if (!usePrisma) return;
2625
+ await prisma.$executeRawUnsafe(`
2626
+ CREATE TABLE IF NOT EXISTS codex_daemon_e2ee_keys (
2627
+ tenant_id TEXT PRIMARY KEY,
2628
+ pubkey TEXT NOT NULL,
2629
+ crypto_versions_json TEXT NOT NULL,
2630
+ registered_at TIMESTAMPTZ NOT NULL DEFAULT now()
2631
+ )
2632
+ `);
2633
+ }
2634
+
2635
+ function normalizeCodexCryptoVersions(versions) {
2636
+ const out = Array.isArray(versions)
2637
+ ? versions.filter((v) => typeof v === "string" && v.length > 0 && v.length <= 32).slice(0, 8)
2638
+ : [];
2639
+ return out.length ? out : ["e2ee-v1"];
2640
+ }
2641
+
2642
+ async function loadCodexDaemonPubkeysFromDb() {
2643
+ if (!usePrisma) return;
2644
+ try {
2645
+ await ensureCodexDaemonPubkeyStore();
2646
+ const rows = await prisma.$queryRawUnsafe(`
2647
+ SELECT tenant_id, pubkey, crypto_versions_json, registered_at
2648
+ FROM codex_daemon_e2ee_keys
2649
+ `);
2650
+ for (const row of rows) {
2651
+ let cryptoVersions = ["e2ee-v1"];
2652
+ try { cryptoVersions = normalizeCodexCryptoVersions(JSON.parse(row.crypto_versions_json)); } catch {}
2653
+ codexDaemonPubkeys.set(row.tenant_id, {
2654
+ pubkey: row.pubkey,
2655
+ crypto_versions: cryptoVersions,
2656
+ registered_at: row.registered_at instanceof Date ? row.registered_at.toISOString() : String(row.registered_at),
2657
+ });
2658
+ }
2659
+ console.log("codex-relay: loaded " + rows.length + " persisted E2EE daemon pubkey(s)");
2660
+ } catch (err) {
2661
+ console.error("codex-relay: failed to load persisted E2EE daemon pubkeys:", err.message);
2662
+ if (!DEV_MODE) process.exit(1);
2663
+ }
2664
+ }
2665
+
2666
+ async function persistCodexDaemonPubkey(agentId, pubkey, cryptoVersions) {
2667
+ if (!usePrisma) return;
2668
+ await ensureCodexDaemonPubkeyStore();
2669
+ await prisma.$executeRawUnsafe(
2670
+ `INSERT INTO codex_daemon_e2ee_keys
2671
+ (tenant_id, pubkey, crypto_versions_json, registered_at)
2672
+ VALUES ($1, $2, $3, now())
2673
+ ON CONFLICT (tenant_id)
2674
+ DO UPDATE SET
2675
+ pubkey = EXCLUDED.pubkey,
2676
+ crypto_versions_json = EXCLUDED.crypto_versions_json,
2677
+ registered_at = EXCLUDED.registered_at`,
2678
+ agentId,
2679
+ pubkey,
2680
+ JSON.stringify(cryptoVersions),
2681
+ );
2682
+ }
2683
+
2684
+ function registerCodexDaemonPubkey(agentId, pubkey, cryptoVersions, source) {
2685
+ if (typeof agentId !== "string" || !agentId) return Promise.resolve(false);
2686
+ if (typeof pubkey !== "string" || !pubkey || pubkey.length > 1024) return Promise.resolve(false);
2687
+ const normalizedVersions = normalizeCodexCryptoVersions(cryptoVersions);
2688
+ const registeredAt = new Date().toISOString();
2689
+ codexDaemonPubkeys.set(agentId, {
2690
+ pubkey,
2691
+ crypto_versions: normalizedVersions,
2692
+ registered_at: registeredAt,
2693
+ });
2694
+ console.log("codex-relay: registered E2EE pubkey for " + agentId + " via " + source);
2695
+ return persistCodexDaemonPubkey(agentId, pubkey, normalizedVersions)
2696
+ .then(() => true)
2697
+ .catch((err) => {
2698
+ console.error("codex-relay: failed to persist E2EE pubkey for " + agentId + ":", err.message);
2699
+ if (!DEV_MODE) throw err;
2700
+ return false;
2701
+ });
2702
+ }
2703
+
2704
+ await loadCodexDaemonPubkeysFromDb();
2705
+
2706
+ function codexRelayKey(agentId, id) {
2707
+ return agentId + ":" + id;
2708
+ }
2709
+
2710
+ function isCodexE2eeEnvelope(envelope) {
2711
+ return !!(envelope && typeof envelope.type === "string" && envelope.type.startsWith("e2ee."));
2712
+ }
2713
+
2714
+ function registerCodexE2eeSessionRoute(agentId, e2eeSession, threadId, ws) {
2715
+ if (typeof e2eeSession !== "string" || !e2eeSession) return;
2716
+ if (typeof threadId !== "string" || !threadId) return;
2717
+ const webKey = codexRelayKey(agentId, threadId);
2718
+ codexE2eeSessionRoutes.set(codexRelayKey(agentId, e2eeSession), { threadId, webKey, ws });
2719
+ }
2720
+
2721
+ function addCodexWebClient(webKey, ws) {
2722
+ let clients = codexWebClients.get(webKey);
2723
+ if (!clients) {
2724
+ clients = new Set();
2725
+ codexWebClients.set(webKey, clients);
2726
+ }
2727
+ clients.add(ws);
2728
+ return clients.size;
2729
+ }
2730
+
2731
+ function removeCodexWebClient(webKey, ws) {
2732
+ const clients = codexWebClients.get(webKey);
2733
+ if (!clients) return 0;
2734
+ clients.delete(ws);
2735
+ if (clients.size === 0) {
2736
+ codexWebClients.delete(webKey);
2737
+ return 0;
2738
+ }
2739
+ return clients.size;
2740
+ }
2741
+
2742
+ function openCodexWebClientsForKey(webKey) {
2743
+ const clients = codexWebClients.get(webKey);
2744
+ if (!clients) return [];
2745
+ return [...clients].filter((webWs) => webWs.readyState === webWs.OPEN);
2746
+ }
2747
+
2748
+ function resolveCodexWebClientsForDaemonFrame(agentId, routeId) {
2749
+ const routed = codexE2eeSessionRoutes.get(codexRelayKey(agentId, routeId));
2750
+ if (routed && routed.ws && routed.ws.readyState === routed.ws.OPEN) return [routed.ws];
2751
+ return openCodexWebClientsForKey(codexRelayKey(agentId, routeId));
2752
+ }
2753
+
2754
+ function removeCodexE2eeRoutesForWeb(agentId, threadId, ws) {
2755
+ const webKey = codexRelayKey(agentId, threadId);
2756
+ for (const [routeKey, route] of codexE2eeSessionRoutes) {
2757
+ if (route.webKey === webKey && (!ws || route.ws === ws)) {
2758
+ codexE2eeSessionRoutes.delete(routeKey);
2759
+ }
2760
+ }
2761
+ }
2762
+
2181
2763
  function generateCodexPairingCode() {
2182
2764
  for (let attempt = 0; attempt < 100; attempt += 1) {
2183
2765
  let code = "";
@@ -2190,16 +2772,30 @@ function generateCodexPairingCode() {
2190
2772
  throw new Error("Could not generate unique codex-relay pairing code");
2191
2773
  }
2192
2774
 
2775
+ function generateCodexPairPollToken() {
2776
+ return "ppt_" + randomBytes(32).toString("base64url");
2777
+ }
2778
+
2779
+ function getBearerToken(req) {
2780
+ const auth = req.headers["authorization"];
2781
+ if (typeof auth !== "string" || !auth.startsWith("Bearer ")) return null;
2782
+ const token = auth.slice(7).trim();
2783
+ return token || null;
2784
+ }
2785
+
2193
2786
  async function handleCodexPairInit(req, res) {
2194
2787
  let body = {};
2195
2788
  try { body = (await readBody(req)) || {}; } catch {}
2196
2789
  const code = generateCodexPairingCode();
2197
2790
  const pairingId = randomUUID();
2791
+ const pollToken = generateCodexPairPollToken();
2198
2792
  const expires = Date.now() + CODEX_PAIR_EXPIRY_MS;
2199
2793
  codexPairings[pairingId] = {
2200
2794
  code,
2201
2795
  status: "pending",
2202
2796
  expires,
2797
+ poll_token: pollToken,
2798
+ poll_token_used: false,
2203
2799
  daemon_info: {
2204
2800
  hostname: typeof body.hostname === "string" ? body.hostname.slice(0, 64) : null,
2205
2801
  platform: typeof body.platform === "string" ? body.platform.slice(0, 32) : null,
@@ -2216,10 +2812,14 @@ async function handleCodexPairInit(req, res) {
2216
2812
  : null,
2217
2813
  };
2218
2814
  codexPairingByCode[code] = pairingId;
2815
+ // Per plan: web_url goes through /login first so the existing Kaleidoscope
2816
+ // QR + phone-passkey ceremony handles auth. After phone passkey, phone
2817
+ // (not desktop) redirects to /pair/<CODE> and completes pair-complete.
2219
2818
  json(res, 200, {
2220
2819
  code,
2221
2820
  pairing_id: pairingId,
2222
- web_url: ISSUER_URL + "/pair",
2821
+ pair_poll_token: pollToken,
2822
+ web_url: ISSUER_URL + "/login?next=" + encodeURIComponent("/pair/" + code),
2223
2823
  expires_at: new Date(expires).toISOString(),
2224
2824
  });
2225
2825
  }
@@ -2227,12 +2827,20 @@ async function handleCodexPairInit(req, res) {
2227
2827
  function handleCodexPairStatus(req, res, pairingId) {
2228
2828
  const p = codexPairings[pairingId];
2229
2829
  if (!p) { json(res, 404, { error: "pairing not found" }); return; }
2230
- if (p.status === "pending" && Date.now() > p.expires) {
2830
+ if (Date.now() > p.expires) {
2231
2831
  p.status = "expired";
2232
2832
  if (codexPairingByCode[p.code] === pairingId) delete codexPairingByCode[p.code];
2833
+ json(res, 401, { error: "pair_poll_token_expired" });
2834
+ return;
2835
+ }
2836
+ const pollToken = getBearerToken(req);
2837
+ if (!pollToken || pollToken !== p.poll_token || p.poll_token_used) {
2838
+ json(res, 401, { error: "invalid_pair_poll_token" });
2839
+ return;
2233
2840
  }
2234
2841
  if (p.status === "completed") {
2235
- json(res, 200, { status: "completed", api_key: p.apiKey, handle: p.agentId });
2842
+ p.poll_token_used = true;
2843
+ json(res, 200, { status: "completed", api_key: p.apiKey, handle: p.handle || p.agentId });
2236
2844
  } else {
2237
2845
  json(res, 200, { status: p.status });
2238
2846
  }
@@ -2245,6 +2853,14 @@ async function handleCodexPairComplete(req, res) {
2245
2853
  try { body = await readBody(req); } catch { json(res, 400, { error: "bad request" }); return; }
2246
2854
  const code = (body && typeof body.code === "string") ? body.code.trim().toUpperCase() : "";
2247
2855
  if (!code) { json(res, 400, { error: "missing code" }); return; }
2856
+ // Defensive: reject codes that don't match the daemon's alphabet up front,
2857
+ // before the map lookup, so probe attempts get one uniform reject path
2858
+ // instead of leaking timing/shape info between "wrong-alphabet" and "valid
2859
+ // shape but unknown code." Per plan C3 + round 5.
2860
+ if (!/^[ABCDEFGHJKLMNPQRSTUVWXYZ23456789]{6}$/.test(code)) {
2861
+ json(res, 404, { error: "invalid or already-used code" });
2862
+ return;
2863
+ }
2248
2864
  const pairingId = codexPairingByCode[code];
2249
2865
  if (!pairingId) { json(res, 404, { error: "invalid or already-used code" }); return; }
2250
2866
  const p = codexPairings[pairingId];
@@ -2255,27 +2871,23 @@ async function handleCodexPairComplete(req, res) {
2255
2871
  p.status = "completed";
2256
2872
  p.apiKey = identity.apiKey;
2257
2873
  p.agentId = identity.agentId;
2874
+ p.handle = identity.handle;
2258
2875
  // 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).
2876
+ // authenticated immutable tenant id. The display handle is returned
2877
+ // as metadata only.
2261
2878
  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);
2879
+ await registerCodexDaemonPubkey(identity.agentId, p.daemon_public_key, p.crypto_versions, "pair-complete");
2268
2880
  }
2269
2881
  delete codexPairingByCode[code];
2270
- console.log("codex-relay: paired daemon for " + identity.agentId);
2271
- json(res, 200, { ok: true, handle: identity.agentId });
2882
+ console.log("codex-relay: paired daemon for tenant " + identity.agentId + " handle " + identity.handle);
2883
+ json(res, 200, { ok: true, handle: identity.handle });
2272
2884
  }
2273
2885
 
2274
2886
  function handleCodexRelayState(req, res) {
2275
2887
  const identity = authenticate(req);
2276
2888
  if (!identity) { json(res, 401, { error: "Unauthorized" }); return; }
2277
2889
  json(res, 200, {
2278
- handle: identity.agentId,
2890
+ handle: identity.handle,
2279
2891
  daemon_online: codexDaemons.has(identity.agentId),
2280
2892
  });
2281
2893
  }
@@ -2291,7 +2903,7 @@ function handleCodexBootstrap(req, res, threadId) {
2291
2903
  const daemonOnline = codexDaemons.has(identity.agentId);
2292
2904
  const daemonKey = codexDaemonPubkeys.get(identity.agentId) || null;
2293
2905
  json(res, 200, {
2294
- handle: identity.agentId,
2906
+ handle: identity.handle,
2295
2907
  thread_id: threadId,
2296
2908
  daemon_online: daemonOnline,
2297
2909
  daemon_public_key: daemonKey ? daemonKey.pubkey : null,
@@ -2318,6 +2930,7 @@ async function handleCodexWsTicket(req, res) {
2318
2930
  const expires = Date.now() + CODEX_RELAY_TICKET_TTL_MS;
2319
2931
  codexRelayTickets.set(ticket, {
2320
2932
  agentId: identity.agentId,
2933
+ handle: identity.handle,
2321
2934
  threadId,
2322
2935
  expires,
2323
2936
  used: false,
@@ -2342,7 +2955,7 @@ function consumeCodexRelayTicket(ticket, threadId) {
2342
2955
  if (Date.now() > entry.expires) { codexRelayTickets.delete(ticket); return null; }
2343
2956
  if (entry.threadId !== threadId) return null; // bound to specific route
2344
2957
  entry.used = true;
2345
- return { agentId: entry.agentId };
2958
+ return { agentId: entry.agentId, handle: entry.handle || entry.agentId };
2346
2959
  }
2347
2960
 
2348
2961
  function serveAppFile(res, relPath) {
@@ -2368,23 +2981,64 @@ function serveAppFile(res, relPath) {
2368
2981
  }
2369
2982
  }
2370
2983
 
2371
- function authenticateWs(req) {
2984
+ // authenticateWs verifies a WS upgrade request against the API_KEYS
2985
+ // map. Default behavior is HEADER ONLY: Authorization: Bearer ck-...
2986
+ // is accepted; ?token=ck-... in the URL is ignored.
2987
+ //
2988
+ // Set { allowQueryToken: true } only on the explicit web-side
2989
+ // back-compat branch that runs inside ALLOW_WS_URL_TOKEN. Daemon
2990
+ // connections never enable this; CLI clients can always set
2991
+ // Authorization, and a daemon accepting URL-token would be a needless
2992
+ // attack surface (URL leaks via referrer / log scrape are not relevant
2993
+ // for daemons, but the asymmetry of "header-only on the daemon path"
2994
+ // keeps the policy clean and auditable).
2995
+ function authenticateWs(req, { allowQueryToken = false } = {}) {
2372
2996
  const auth = req.headers["authorization"];
2373
2997
  if (auth && auth.startsWith("Bearer ")) {
2374
2998
  const key = auth.slice(7).trim();
2375
- if (API_KEYS[key]) return { agentId: API_KEYS[key], apiKey: key };
2999
+ const identity = identityForApiKey(key);
3000
+ if (identity) return identity;
2376
3001
  }
2377
- // Browsers can't set Authorization on WebSocket(): accept ?token= fallback.
3002
+ if (!allowQueryToken) return null;
3003
+
3004
+ // Web back-compat path only. Browsers cannot set Authorization on a
3005
+ // WebSocket() handshake, so legacy clients put ck- in the URL.
3006
+ // parseUrl returns a WHATWG URL, which has no .query getter (only
3007
+ // .search/.searchParams). Strip the leading "?" off .search and
3008
+ // parse with querystring so the array/string handling below works.
2378
3009
  const u = parseUrl(req.url);
2379
- const qs = u.query ? parseUrlQs(u.query) : {};
3010
+ const qs = u.search ? parseUrlQs(u.search.slice(1)) : {};
2380
3011
  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 };
3012
+ if (typeof tokenParam === "string") {
3013
+ return identityForApiKey(tokenParam);
2383
3014
  }
2384
3015
  return null;
2385
3016
  }
2386
3017
 
2387
- const codexRelayWss = new WebSocketServer({ noServer: true });
3018
+ // F-001: subprotocol-based WS auth for the codex-relay surface. The web
3019
+ // client sends Sec-WebSocket-Protocol: ldm-codex-relay.v1, ticket.<v>.
3020
+ // Server echoes only the protocol name (not the ticket-bearing entry).
3021
+ // Daemon connections do not use a subprotocol, so empty Sets pass.
3022
+ const codexRelayWss = new WebSocketServer({
3023
+ noServer: true,
3024
+ handleProtocols: (protocols /*, request */) => {
3025
+ if (!protocols || protocols.size === 0) return undefined;
3026
+ if (protocols.has("ldm-codex-relay.v1")) return "ldm-codex-relay.v1";
3027
+ return false;
3028
+ },
3029
+ });
3030
+
3031
+ function getTicketFromSubprotocol(req) {
3032
+ const header = req.headers["sec-websocket-protocol"];
3033
+ if (!header) return null;
3034
+ const tokens = header.split(",").map(s => s.trim()).filter(Boolean);
3035
+ for (const t of tokens) {
3036
+ if (t.startsWith("ticket.")) {
3037
+ return t.slice("ticket.".length);
3038
+ }
3039
+ }
3040
+ return null;
3041
+ }
2388
3042
 
2389
3043
  httpServer.on("upgrade", (req, socket, head) => {
2390
3044
  const u = parseUrl(req.url);
@@ -2393,21 +3047,64 @@ httpServer.on("upgrade", (req, socket, head) => {
2393
3047
  const isWeb = path.startsWith("/api/codex-relay/web/");
2394
3048
  if (!isDaemon && !isWeb) return; // let other listeners (or default) handle it
2395
3049
 
3050
+ // F-003: enforce Origin allowlist for browser-borne web upgrades.
3051
+ // Runs BEFORE ticket consumption / authenticateWs so a request from
3052
+ // a disallowed origin cannot burn a valid one-time ticket or trigger
3053
+ // an auth check side effect. Daemon path is exempt because CLI
3054
+ // clients do not send a browser Origin header. Requires nginx to
3055
+ // pass the Origin header through unchanged on the upgrade hop;
3056
+ // verified in Lane B B2 of the audit doc.
3057
+ if (isWeb) {
3058
+ const origin = req.headers["origin"];
3059
+ if (!isWsOriginAllowed(origin)) {
3060
+ console.warn("WS upgrade rejected: bad origin (" + (origin || "<none>") + ") for " + path);
3061
+ socket.write("HTTP/1.1 403 Forbidden\r\nConnection: close\r\n\r\n");
3062
+ socket.destroy();
3063
+ return;
3064
+ }
3065
+ }
3066
+
2396
3067
  // 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.
3068
+ // Web side: prefer ticket via Sec-WebSocket-Protocol (F-001), fall
3069
+ // back to ?ticket= query string. The legacy ?token=ck- URL fallback
3070
+ // is gated behind LDM_HOSTED_MCP_ALLOW_WS_URL_TOKEN=1; production
3071
+ // does not accept it.
2399
3072
  let identity = null;
2400
3073
  if (isDaemon) {
2401
- identity = authenticateWs(req);
3074
+ // Daemon connections must use Authorization: Bearer ck-. URL-token
3075
+ // is never accepted on the daemon path (allowQueryToken: false).
3076
+ identity = authenticateWs(req, { allowQueryToken: false });
2402
3077
  } else {
2403
3078
  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 };
3079
+
3080
+ // Preferred path: ticket carried in Sec-WebSocket-Protocol. Avoids
3081
+ // URL/log/referrer exposure of the ticket value.
3082
+ const subTicket = getTicketFromSubprotocol(req);
3083
+ if (subTicket) {
3084
+ const consumed = consumeCodexRelayTicket(subTicket, threadId);
3085
+ if (consumed) identity = { agentId: consumed.agentId, handle: consumed.handle, viaTicket: true };
3086
+ }
3087
+
3088
+ // Back-compat: ?ticket= query string. Same single-use binding.
3089
+ if (!identity) {
3090
+ // parseUrl returns a WHATWG URL with .search/.searchParams, no
3091
+ // .query. Strip the leading "?" off .search and parse with
3092
+ // querystring so the array/string handling below still works.
3093
+ const qs = u.search ? parseUrlQs(u.search.slice(1)) : {};
3094
+ const ticketParam = Array.isArray(qs.ticket) ? qs.ticket[0] : qs.ticket;
3095
+ if (typeof ticketParam === "string" && ticketParam) {
3096
+ const consumed = consumeCodexRelayTicket(ticketParam, threadId);
3097
+ if (consumed) identity = { agentId: consumed.agentId, handle: consumed.handle, viaTicket: true };
3098
+ }
3099
+ }
3100
+
3101
+ // Legacy ?token=ck- URL fallback: dev/back-compat only. Production
3102
+ // refuses long-lived bearer in WS URLs (gate condition 2). The
3103
+ // URL-token branch in authenticateWs is gated by allowQueryToken,
3104
+ // and we only set it true here, only when ALLOW_WS_URL_TOKEN is on.
3105
+ if (!identity && ALLOW_WS_URL_TOKEN) {
3106
+ identity = authenticateWs(req, { allowQueryToken: true });
2409
3107
  }
2410
- if (!identity) identity = authenticateWs(req);
2411
3108
  }
2412
3109
 
2413
3110
  if (!identity) {
@@ -2422,14 +3119,69 @@ httpServer.on("upgrade", (req, socket, head) => {
2422
3119
  if (previous && previous !== ws) try { previous.close(4000, "replaced"); } catch {}
2423
3120
  codexDaemons.set(identity.agentId, ws);
2424
3121
  console.log("codex-relay: daemon online for " + identity.agentId);
3122
+ // F-001 per-thread isolation. Daemon -> web routing must NOT
3123
+ // fan out every frame to every same-agent web socket; that
3124
+ // breaks isolation when one user has multiple threads open.
3125
+ // Parse the OUTER envelope only to read the routing field
3126
+ // (session/sessionId). The encrypted ciphertext (or any inner
3127
+ // session.* payload) is never inspected, so gate 3a still
3128
+ // holds: the relay sees only routing metadata on the envelope.
3129
+ // No-session frames are an explicit allowlist (control/presence
3130
+ // types) or are dropped with a redacted warning. We never
3131
+ // broadcast unknown frames.
3132
+ const BROADCAST_TYPES = new Set([
3133
+ "presence",
3134
+ "presence.web",
3135
+ "presence.daemon",
3136
+ "daemon.online",
3137
+ "daemon.offline",
3138
+ ]);
2425
3139
  ws.on("message", (data) => {
2426
3140
  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);
3141
+ let envelope = null;
3142
+ try { envelope = JSON.parse(text); } catch {}
3143
+ if (envelope?.type === "daemon.identity") {
3144
+ void registerCodexDaemonPubkey(
3145
+ identity.agentId,
3146
+ envelope.daemon_public_key,
3147
+ envelope.crypto_versions,
3148
+ "daemon-reconnect",
3149
+ ).catch(() => {
3150
+ try { ws.close(1011, "daemon identity persistence failed"); } catch {}
3151
+ });
3152
+ return;
3153
+ }
3154
+ const sessionId = envelope?.session || envelope?.sessionId || envelope?.threadId;
3155
+ if (sessionId) {
3156
+ const targets = resolveCodexWebClientsForDaemonFrame(identity.agentId, sessionId);
3157
+ for (const target of targets) {
3158
+ target.send(text);
2431
3159
  }
3160
+ // No matching web client: drop silently. Daemon-emitted
3161
+ // frames for a thread the user has not opened in any browser
3162
+ // are not interesting to fan out elsewhere.
3163
+ return;
2432
3164
  }
3165
+ const type = envelope?.type;
3166
+ if (type && BROADCAST_TYPES.has(type)) {
3167
+ // Allowlisted agent-level frame (presence, online status).
3168
+ // Fan out within agent, never across agents.
3169
+ const prefix = identity.agentId + ":";
3170
+ for (const [key, webClients] of codexWebClients) {
3171
+ if (key.startsWith(prefix)) {
3172
+ for (const webWs of webClients) {
3173
+ if (webWs.readyState === webWs.OPEN) webWs.send(text);
3174
+ }
3175
+ }
3176
+ }
3177
+ return;
3178
+ }
3179
+ // Parse failed, missing session, or unknown type: drop. Log a
3180
+ // redacted notice so the operator can see if a daemon is
3181
+ // emitting unrouteable frames. We never log envelope/payload
3182
+ // bytes; only the agent and the type (or "no-type").
3183
+ const typeMarker = type ? String(type).slice(0, 32) : "no-type";
3184
+ console.warn("codex-relay: dropped unroutable daemon frame for " + identity.agentId + " (type=" + typeMarker + ")");
2433
3185
  });
2434
3186
  ws.on("close", () => {
2435
3187
  if (codexDaemons.get(identity.agentId) === ws) {
@@ -2452,21 +3204,26 @@ httpServer.on("upgrade", (req, socket, head) => {
2452
3204
  return;
2453
3205
  }
2454
3206
  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);
3207
+ const key = codexRelayKey(identity.agentId, threadId);
3208
+ const clientCount = addCodexWebClient(key, ws);
3209
+ console.log("codex-relay: web online " + key + " clients=" + clientCount);
2460
3210
  ws.on("message", (data) => {
3211
+ const text = data.toString();
3212
+ let envelope = null;
3213
+ try { envelope = JSON.parse(text); } catch {}
3214
+ if (isCodexE2eeEnvelope(envelope) && envelope.session) {
3215
+ registerCodexE2eeSessionRoute(identity.agentId, envelope.session, threadId, ws);
3216
+ }
2461
3217
  const daemonWs = codexDaemons.get(identity.agentId);
2462
3218
  if (daemonWs && daemonWs.readyState === daemonWs.OPEN) {
2463
- daemonWs.send(data.toString());
3219
+ daemonWs.send(text);
2464
3220
  } else {
2465
3221
  try { ws.send(JSON.stringify({ type: "error", message: "daemon offline" })); } catch {}
2466
3222
  }
2467
3223
  });
2468
3224
  ws.on("close", () => {
2469
- if (codexWebClients.get(key) === ws) codexWebClients.delete(key);
3225
+ removeCodexE2eeRoutesForWeb(identity.agentId, threadId, ws);
3226
+ removeCodexWebClient(key, ws);
2470
3227
  });
2471
3228
  ws.on("error", (err) => {
2472
3229
  console.error("codex-relay web ws error:", err.message);
@@ -2476,6 +3233,7 @@ httpServer.on("upgrade", (req, socket, head) => {
2476
3233
 
2477
3234
  httpServer.listen(PORT, SERVER_BIND, () => {
2478
3235
  console.log(SERVER_NAME + " v" + SERVER_VERSION + " listening on " + SERVER_BIND + ":" + PORT);
3236
+ console.log("WS origin allowlist: " + WS_ORIGIN_ALLOWLIST.join(", "));
2479
3237
  console.log("Health: http://localhost:" + PORT + "/health");
2480
3238
  console.log("MCP: http://localhost:" + PORT + "/mcp");
2481
3239
  console.log("OAuth: http://localhost:" + PORT + "/.well-known/oauth-authorization-server");