@preventive/triage 1.0.0-alpha.2 → 1.0.0-alpha.20

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 (168) hide show
  1. package/api/reap.ts +17 -0
  2. package/cli.js +6 -0
  3. package/client/finding-link.js +305 -0
  4. package/client/linked-findings.d.ts +1 -0
  5. package/client/linked-findings.js +111 -0
  6. package/common/bundle-metadata.d.ts +10 -0
  7. package/common/bundle-metadata.js +177 -0
  8. package/common/bundle-reasons.d.ts +2 -0
  9. package/common/bundle-reasons.js +21 -0
  10. package/common/bundle-sources.d.ts +3 -0
  11. package/common/bundle-sources.js +284 -0
  12. package/common/bundle-stats.js +41 -0
  13. package/common/bundle-tabs.js +1 -0
  14. package/common/code-language.js +36 -0
  15. package/common/default-scan-models.ts +30 -0
  16. package/common/finding-id.js +47 -0
  17. package/common/github-pr.ts +56 -0
  18. package/common/managed/comments.ts +34 -0
  19. package/common/managed/permissions.ts +35 -0
  20. package/common/managed/report-content.ts +42 -0
  21. package/common/managed/report-filter.ts +108 -0
  22. package/common/managed/roles.ts +28 -0
  23. package/common/managed/routes.d.ts +2 -0
  24. package/common/managed/routes.js +121 -0
  25. package/common/managed/scan-models.ts +6 -0
  26. package/common/managed/triage.ts +83 -0
  27. package/common/save-error-reason.ts +20 -7
  28. package/common/scan-server.ts +13 -0
  29. package/common/server-info.ts +33 -0
  30. package/common/utf8.d.ts +3 -0
  31. package/common/utf8.js +45 -0
  32. package/out/brotli-fallback.js +3 -3
  33. package/out/client-managed-import.js +81 -0
  34. package/out/client-managed.js +110 -0
  35. package/out/client-sync.js +16 -13
  36. package/out/graph.js +30 -4
  37. package/out/index.html +55 -8
  38. package/out/prism.js +2 -2
  39. package/out/stasis.svg +45 -0
  40. package/out/terminal.js +273 -39
  41. package/out/view.css +1 -1
  42. package/out/view.js +198 -62
  43. package/package.json +179 -55
  44. package/report/index.js +254 -0
  45. package/report/src/finding-id.js +80 -0
  46. package/report/src/finding.js +312 -0
  47. package/report/src/labels.js +33 -0
  48. package/report/src/md-structure.js +471 -0
  49. package/report/src/md-text.js +167 -0
  50. package/report/src/meta.js +76 -0
  51. package/report/src/parse-codex.js +147 -0
  52. package/report/src/parse-deepsec.js +197 -0
  53. package/report/src/parse-deepview-fields.js +375 -0
  54. package/report/src/parse-deepview-md.js +185 -0
  55. package/report/src/parse-md-id.js +137 -0
  56. package/report/src/parse-md.js +322 -0
  57. package/report/src/parse-piolium-id.js +79 -0
  58. package/report/src/parse-piolium-rows.js +131 -0
  59. package/report/src/parse-piolium-tokens.js +175 -0
  60. package/report/src/parse-piolium.js +400 -0
  61. package/report/src/security.js +63 -0
  62. package/report/src/utf8.js +21 -0
  63. package/report/src/write-md-finding.js +273 -0
  64. package/report/src/write-md.js +291 -0
  65. package/server-common/database-config.ts +16 -0
  66. package/server-common/initialize.ts +18 -0
  67. package/server-common/npm-advisories.ts +101 -0
  68. package/{server → server-common}/origin.ts +5 -5
  69. package/server-common/reap.ts +48 -0
  70. package/server-common/scan-config.ts +19 -0
  71. package/server-common/standalone.ts +29 -0
  72. package/server-common/storage-log.ts +34 -0
  73. package/server-common/vercel-blob.ts +110 -0
  74. package/server-e2e/app.ts +485 -0
  75. package/{server → server-e2e}/auth.ts +5 -1
  76. package/{server → server-e2e}/bus-receiver.ts +9 -8
  77. package/{server → server-e2e}/cli.js +9 -4
  78. package/{server → server-e2e}/config.ts +54 -39
  79. package/{server → server-e2e}/db-neon.ts +2 -2
  80. package/{server → server-e2e}/db-revision-sql.ts +7 -10
  81. package/{server → server-e2e}/db-stmt.ts +2 -2
  82. package/{server → server-e2e}/db.ts +96 -135
  83. package/{server → server-e2e}/http.ts +110 -12
  84. package/{server → server-e2e}/hub.ts +44 -14
  85. package/server-e2e/index.ts +17 -0
  86. package/server-e2e/lifecycle.ts +95 -0
  87. package/{server → server-e2e}/neon-driver.ts +2 -2
  88. package/{server → server-e2e}/npm-proxy.ts +11 -144
  89. package/{server → server-e2e}/objstore/blob-fs.ts +6 -8
  90. package/{server → server-e2e}/objstore/blob-vercel.ts +49 -141
  91. package/{server → server-e2e}/objstore/blob.ts +24 -9
  92. package/server-e2e/objstore/fetch-mint-guard.ts +74 -0
  93. package/{server → server-e2e}/objstore/handlers.ts +19 -20
  94. package/{server → server-e2e}/objstore/init.ts +53 -27
  95. package/{server → server-e2e}/objstore/reaper.ts +31 -11
  96. package/server-e2e/objstore/rest-deny.ts +28 -0
  97. package/server-e2e/objstore/rest-mint.ts +224 -0
  98. package/{server → server-e2e}/objstore/rest.ts +110 -93
  99. package/{server → server-e2e}/objstore/sign.ts +105 -0
  100. package/{server → server-e2e}/objstore/store-neon.ts +10 -14
  101. package/{server → server-e2e}/objstore/store.ts +98 -118
  102. package/{server → server-e2e}/objstore/tokens.ts +9 -12
  103. package/{server → server-e2e}/peer.ts +7 -9
  104. package/{server → server-e2e}/pubsub.ts +29 -36
  105. package/{server → server-e2e}/sign.ts +12 -14
  106. package/{server → server-e2e}/sse-server.ts +105 -73
  107. package/{server → server-e2e}/sse-session.ts +30 -16
  108. package/{server → server-e2e}/static.ts +36 -29
  109. package/server-e2e/sync-handlers.ts +408 -0
  110. package/{server → server-e2e}/util.ts +9 -0
  111. package/{server → server-e2e}/ws-server.ts +29 -23
  112. package/server-managed/activity.ts +231 -0
  113. package/server-managed/avatar-store.ts +51 -0
  114. package/server-managed/blob-store.ts +66 -0
  115. package/server-managed/blob-vercel.ts +125 -0
  116. package/server-managed/brotli.ts +10 -0
  117. package/server-managed/bundle-cache.ts +185 -0
  118. package/server-managed/bundle-catalog.ts +29 -0
  119. package/server-managed/bundle-store.ts +28 -0
  120. package/server-managed/bundle-summary-cache.ts +97 -0
  121. package/server-managed/bundle.ts +39 -0
  122. package/server-managed/cache-storage.ts +40 -0
  123. package/server-managed/cli.js +13 -0
  124. package/server-managed/combined.ts +46 -0
  125. package/server-managed/comments.ts +151 -0
  126. package/server-managed/config.ts +144 -0
  127. package/server-managed/content-access.ts +15 -0
  128. package/server-managed/crypto.ts +25 -0
  129. package/server-managed/db-methods.ts +1330 -0
  130. package/server-managed/db-neon.ts +171 -0
  131. package/server-managed/db-schema.ts +203 -0
  132. package/server-managed/db-table-names.ts +22 -0
  133. package/server-managed/db.ts +109 -0
  134. package/server-managed/github-app.ts +332 -0
  135. package/server-managed/github-metadata.ts +65 -0
  136. package/server-managed/github-oauth.ts +215 -0
  137. package/server-managed/github-pulls.ts +115 -0
  138. package/server-managed/http-response.ts +18 -0
  139. package/server-managed/http.ts +2043 -0
  140. package/server-managed/import-triage.ts +48 -0
  141. package/server-managed/index.ts +135 -0
  142. package/server-managed/public-workspace.ts +150 -0
  143. package/server-managed/repo-path.ts +21 -0
  144. package/server-managed/report-migration.ts +35 -0
  145. package/server-managed/report-query.ts +4 -0
  146. package/server-managed/report-response.ts +16 -0
  147. package/server-managed/report-sources.ts +154 -0
  148. package/server-managed/repository-discovery.ts +82 -0
  149. package/server-managed/repository-policy.ts +25 -0
  150. package/server-managed/session.ts +78 -0
  151. package/server-managed/slugs.ts +38 -0
  152. package/server-managed/sql-postgres.ts +30 -0
  153. package/server-managed/sql.ts +61 -0
  154. package/server-managed/static.ts +28 -0
  155. package/server-managed/storage.ts +34 -0
  156. package/server-managed/team-catalog.ts +7 -0
  157. package/server-managed/team-feed.ts +128 -0
  158. package/server-managed/team-reports.ts +156 -0
  159. package/server-managed/triage-response.ts +16 -0
  160. package/server-managed/uploads.ts +47 -0
  161. package/server-managed/workspace-shares.ts +150 -0
  162. package/server.ts +50 -0
  163. package/server/index.ts +0 -481
  164. package/server/lifecycle.ts +0 -204
  165. package/server/sync-handlers.ts +0 -327
  166. /package/{server → server-e2e}/config.example.json +0 -0
  167. /package/{server → server-e2e}/objstore/fs.ts +0 -0
  168. /package/{server → server-e2e}/validation.ts +0 -0
@@ -55,15 +55,6 @@ export type SubscribeMsg = {
55
55
  // `Uint8Array<ArrayBuffer>` (not `<ArrayBufferLike>`) so the bytes
56
56
  // thread directly into `crypto.subtle.digest` — `BufferSource`
57
57
  // rejects SharedArrayBuffer-backed views.
58
- //
59
- // NOTE: `verifySaveSigAndCanonical` is a test-friendly composition
60
- // helper. Production `handleSave` in `server/index.ts` does NOT
61
- // call it — it composes `canonicalSave` +
62
- // `computeRevisionIdFromCanonical` + `verifyEd25519` separately so
63
- // the dup-precheck (`revisionExists`) can fire BEFORE the Ed25519
64
- // verify, closing the round-9 H1 CPU-DoS vector where a passive
65
- // observer floods captured saves. The wrapper exists for unit
66
- // tests that don't need the precheck ordering.
67
58
  export type VerifyResult =
68
59
  | { ok: true; canonical: Uint8Array<ArrayBuffer> }
69
60
  | { ok: false; canonical: null }
@@ -101,7 +92,7 @@ export function canonicalSave(
101
92
  // confusing diff in the canonical bytes rather than a clean drop.
102
93
  // Mirrors the `isSafeNonNegativeInt` rigor that the objstore
103
94
  // canonical builders apply. Input-validation audit
104
- // `server/sign.ts:88`.
95
+ // `server-e2e/sign.ts:88`.
105
96
  if (typeof workspaceTag !== 'string') throw new TypeError('canonicalSave: workspaceTag must be string')
106
97
  if (typeof nonce !== 'string') throw new TypeError('canonicalSave: nonce must be string')
107
98
  if (typeof ciphertext !== 'string') throw new TypeError('canonicalSave: ciphertext must be string')
@@ -120,11 +111,18 @@ function canonicalSubscribe(
120
111
  { workspaceTag, from }: SubscribeMsg,
121
112
  connectionNonce: string,
122
113
  ): Uint8Array<ArrayBuffer> {
123
- const fromStr = from == null ? '' : String(from)
114
+ // Strict `from` typing, mirroring canonicalSave's `base` check: `from`
115
+ // is `string | null`. A non-string non-null value would otherwise
116
+ // coerce via `String(...)` to canonical bytes the client could never
117
+ // reproduce (123 → "123", {} → "[object Object]"), so a signature
118
+ // computed over that coercion would verify against a malformed wire
119
+ // shape. verifySubscribeSig wraps this call in try/catch → clean drop.
120
+ if (from != null && typeof from !== 'string') throw new TypeError('canonicalSubscribe: from must be string or null')
121
+ const fromStr = from == null ? '' : from
124
122
  return encodeUtf8([SUBSCRIBE_DOMAIN, workspaceTag as string, fromStr, connectionNonce].join('\n'))
125
123
  }
126
124
 
127
- // Exported because the v1.objstore signing module (server/objstore/sign.ts)
125
+ // Exported because the v1.objstore signing module (server-e2e/objstore/sign.ts)
128
126
  // reuses it for its four verifiers — same workspaceTag-as-pubkey contract,
129
127
  // same domain-separated canonical bytes. Keeping the WebCrypto plumbing
130
128
  // in one place avoids drift between the triage-sync and objstore verify
@@ -159,7 +157,7 @@ export async function verifyEd25519(
159
157
  // `{ ok: false, canonical: null }` on bad shape / bad sig;
160
158
  // `{ ok: true, canonical: <bytes> }` on success.
161
159
  //
162
- // NOT used by `server/index.ts handleSave`. Production composes
160
+ // NOT used by `server-e2e/index.ts handleSave`. Production composes
163
161
  // the smaller helpers (`canonicalSave`, `computeRevisionIdFromCanonical`,
164
162
  // `verifyEd25519`) separately so the dup-precheck via
165
163
  // `revisionExists` can fire BEFORE Ed25519-verify and skip the
@@ -206,7 +204,7 @@ export async function computeRevisionIdFromCanonical(canonical: Uint8Array<Array
206
204
 
207
205
  // `connectionNonce` is the per-socket challenge the server issued
208
206
  // to the originating connection (see `Peer.challenge` in
209
- // server/peer.ts). The client signs a canonical that includes the
207
+ // server-e2e/peer.ts). The client signs a canonical that includes the
210
208
  // nonce; verifying against the SAME nonce here is what blocks
211
209
  // cross-connection replay of a captured subscribe frame. Audit
212
210
  // round-9 H2.
@@ -8,9 +8,10 @@
8
8
  //
9
9
  // One route, POSTs only:
10
10
  //
11
- // POST /api/sync/sse[?id=<sid>]
11
+ // POST /api/sync/sse
12
12
  // Request body:
13
- // { password?: string, — cached client password
13
+ // { id?: string, — session continuation token
14
+ // password?: string, — cached client password
14
15
  // frames?: Array<protocol-frame> — WS-style JSON frames
15
16
  // }
16
17
  // Response:
@@ -25,14 +26,23 @@
25
26
  // carrying the WS protocol's JSON envelopes.
26
27
  //
27
28
  // Continuation: each POST replaces the previous POST's response as the
28
- // session's downstream channel. If the `?id=<sid>` in the URL matches
29
- // a session this replica knows, the session continues (new outbound
30
- // stream attached, old one end()ed). If the id is unknown (different
31
- // replica picked up the POST, or session expired), a fresh session
32
- // with a new id is created and announced via the first `session`
33
- // event; the client uses the new id on all subsequent POSTs and re-
34
- // sends its subscribe frames on the next POST (its `frames` carry the
35
- // signed subscribes — they always do, see client/sync/sse-transport.ts).
29
+ // session's downstream channel. If the body's `id` matches a session
30
+ // this replica knows, the session continues (new outbound stream
31
+ // attached, old one end()ed). If the id is unknown (different replica
32
+ // picked up the POST, or session expired), a fresh session with a new
33
+ // id is created and announced via the first `session` event; the
34
+ // client uses the new id on all subsequent POSTs and re-sends its
35
+ // subscribe frames on the next POST (its `frames` carry the signed
36
+ // subscribes — they always do, see client/sync/sse-transport.ts).
37
+ //
38
+ // The id rides the JSON body, NOT the URL: the sid is a live bearer
39
+ // capability (whoever presents it attaches to the session's downstream
40
+ // and inherits its operator-auth flag), and a query-string token leaks
41
+ // into proxy / LB access logs — the same reason the objstore bearer
42
+ // tokens ride the Authorization header, never the URL. A legacy
43
+ // `?id=<sid>` query form is still ACCEPTED (older client bundles sent
44
+ // it; rejecting would churn them through a fresh session per POST),
45
+ // but current clients never emit it.
36
46
  //
37
47
  // Why POSTs only: a long-lived GET pins the client to one replica via
38
48
  // TCP affinity, which would mean POSTs from the same client must be
@@ -47,17 +57,35 @@ import { type PeerConnectionDeps, setupPeerConnection } from './ws-server.ts'
47
57
  import { SseSession } from './sse-session.ts'
48
58
  import { errMsg, randomId } from './util.ts'
49
59
 
50
- // Same prefix the WS upgrade lives under (server/http.ts WS_UPGRADE_PATH
60
+ // Same prefix the WS upgrade lives under (server-e2e/http.ts WS_UPGRADE_PATH
51
61
  // = '/api/sync'); subroute keeps the SSE plane sibling to the upgrade
52
62
  // path so the same `location` block routes both.
53
63
  export const SSE_OPEN_PATH = '/api/sync/sse'
54
64
 
65
+ // Cadence of the server-driven keepalive sweep: every tick we write a `:`
66
+ // comment to each open session's downstream so intermediary proxies don't
67
+ // idle-close it (nginx et al. default to a ~60s read timeout). This is the
68
+ // server's own liveness upkeep — the client no longer POSTs a periodic ping
69
+ // (which forced a stream takeover every tick).
70
+ //
71
+ // Reaping model (replaces the old POST-driven idle timer): a session is
72
+ // dropped on its downstream response `close` — clean disconnect, or a
73
+ // half-open socket the per-session TCP keepalive forces closed (see
74
+ // SseSession.SOCKET_KEEPALIVE_MS) — NOT by this sweep. We intentionally
75
+ // trust connection-level liveness. The one topology this can't see is a
76
+ // buffering / TLS-terminating proxy that holds the upstream open after the
77
+ // real client vanished (keepalive then probes the proxy hop, not the
78
+ // client); such a session lingers until `maxSessions`, the hard backstop.
79
+ // This is the same exposure the WS heartbeat already has (its ping only
80
+ // proves the proxy↔server hop too), not a new class of leak.
81
+ const KEEPALIVE_SWEEP_MS = 30_000
82
+
55
83
  export type SseServerDeps = {
56
84
  // The WS dispatch is the cohesive unit; SSE just provides another
57
85
  // transport into it. Closure over the same handler / hub / objstore
58
86
  // / track / debug surface the WS path uses.
59
87
  peerDeps: PeerConnectionDeps
60
- // Shutdown gate. Mirror of the REST + WS branches in server/http.ts:
88
+ // Shutdown gate. Mirror of the REST + WS branches in server-e2e/http.ts:
61
89
  // an SSE POST arriving on an existing keep-alive socket after
62
90
  // SIGTERM should be rejected, not dispatched against a draining DB.
63
91
  isShuttingDown: () => boolean
@@ -65,17 +93,11 @@ export type SseServerDeps = {
65
93
  // 503 the open request. Caps the SSE-side equivalent of `wss.clients`.
66
94
  maxSessions: number
67
95
  // Max body size for one POST. Matches the WS `maxPayload` in
68
- // server/index.ts so the SSE plane can't accept frames the WS plane
96
+ // server-e2e/index.ts so the SSE plane can't accept frames the WS plane
69
97
  // would reject. The POST body envelope can hold multiple frames so
70
98
  // the per-frame budget is the same as the WS plane after the
71
99
  // dispatcher splits them.
72
100
  maxBodyBytes: number
73
- // Idle timeout for a session with no inbound POSTs. Detects the
74
- // wandered-off browser tab the WS heartbeat sweep handles via
75
- // ping/pong on real sockets. The client's JSON ping/pong heartbeat
76
- // (every 15s) is the steady-state liveness signal; this is the
77
- // hard ceiling.
78
- sessionIdleMs: number
79
101
  debug: boolean
80
102
  }
81
103
 
@@ -86,48 +108,37 @@ export type SseServer = {
86
108
  // Iterates active sessions. Lifecycle's graceful-shutdown loop
87
109
  // reads this to close SSE sessions alongside WS clients.
88
110
  sessions: () => Iterable<SseSession>
111
+ // The keepalive-sweep timer. Lifecycle clears it on shutdown (parity
112
+ // with the WS heartbeat timer) so a tick can't fire mid-teardown.
113
+ keepaliveTimer: ReturnType<typeof setInterval>
89
114
  }
90
115
 
91
116
  // Inbound POST body. Every field optional — an empty-body POST is a
92
117
  // valid "wake the session" probe (the response stream rides on every
93
118
  // POST), and a body that carries only `password` or only `frames` is
94
- // a normal partial update.
119
+ // a normal partial update. `id` is the session continuation token
120
+ // (see the header's "Continuation" note for why it rides the body).
95
121
  type SseBody = {
122
+ id?: unknown
96
123
  password?: unknown
97
124
  frames?: unknown
98
125
  }
99
126
 
100
127
  export function installSseServer(deps: SseServerDeps): SseServer {
101
- const { peerDeps, isShuttingDown, maxSessions, maxBodyBytes, sessionIdleMs, debug } = deps
128
+ const { peerDeps, isShuttingDown, maxSessions, maxBodyBytes, debug } = deps
102
129
 
103
130
  // Active SSE sessions, keyed by the random session id `createSession`
104
- // mints on the first POST that lacks a `?id=` (or whose id this
105
- // replica doesn't recognise) and that subsequent POSTs echo back to
106
- // continue the session. Bounded by `maxSessions` — over the cap, new
107
- // POSTs get a 503. POSTs against an unknown id are NOT 404'd — they
108
- // mint a fresh session instead, so a multi-replica deployment doesn't
109
- // require sticky LB routing to recover.
131
+ // mints on the first POST that carries no continuation id (or whose
132
+ // id this replica doesn't recognise) and that subsequent POSTs echo
133
+ // back (body `id` field) to continue the session. Bounded by
134
+ // `maxSessions` — over the cap, new POSTs get a 503. POSTs against an
135
+ // unknown id are NOT 404'd — they mint a fresh session instead, so a
136
+ // multi-replica deployment doesn't require sticky LB routing to
137
+ // recover.
110
138
  const sessions = new Map<string, SseSession>()
111
- // Per-session idle timer handle. Reset on every inbound POST; fires
112
- // after `sessionIdleMs` of silence to close a stranded session.
113
- const idleTimers = new Map<string, ReturnType<typeof setTimeout>>()
114
-
115
- function armIdleTimer(sid: string, session: SseSession): void {
116
- clearTimeout(idleTimers.get(sid))
117
- if (sessionIdleMs <= 0) return
118
- const t = setTimeout(() => {
119
- if (debug) console.warn(`sse: session ${sid.slice(0, 8)}… idle ${sessionIdleMs}ms → close`)
120
- try { session.terminate() } catch {}
121
- }, sessionIdleMs)
122
- t.unref?.()
123
- idleTimers.set(sid, t)
124
- }
125
139
 
126
140
  function dropSession(sid: string): void {
127
141
  sessions.delete(sid)
128
- const t = idleTimers.get(sid)
129
- if (t) clearTimeout(t)
130
- idleTimers.delete(sid)
131
142
  }
132
143
 
133
144
  function writeSseHeaders(res: ServerResponse): void {
@@ -149,7 +160,7 @@ export function installSseServer(deps: SseServerDeps): SseServer {
149
160
  res.write('retry: 1000\n\n')
150
161
  }
151
162
 
152
- function createSession(res: ServerResponse, req: HttpRequest): { sid: string; session: SseSession } | null {
163
+ function createSession(res: ServerResponse, req: HttpRequest): SseSession | null {
153
164
  if (sessions.size >= maxSessions) {
154
165
  if (debug) console.warn(`sse: refused open — sessions ${sessions.size} >= ${maxSessions}`)
155
166
  return null
@@ -158,7 +169,6 @@ export function installSseServer(deps: SseServerDeps): SseServer {
158
169
  const sid = randomId()
159
170
  const session = new SseSession(res)
160
171
  sessions.set(sid, session)
161
- armIdleTimer(sid, session)
162
172
  session.on('close', () => { dropSession(sid) })
163
173
  // Announce the continuation token BEFORE the dispatcher emits its
164
174
  // `challenge` frame so the client latches the id first and the
@@ -168,11 +178,10 @@ export function installSseServer(deps: SseServerDeps): SseServer {
168
178
  session.writeEvent('session', sid)
169
179
  // Hand the session to the shared WS connection setup so it joins
170
180
  // the same Peer / dispatcher / hub lifecycle as a real WebSocket.
171
- // `setupPeerConnection` sends the protocol `challenge` frame as
172
- // its first action; that re-uses the normal default-named SSE
173
- // message channel.
181
+ // `setupPeerConnection`'s first action is the protocol `challenge`
182
+ // frame, on the default-named SSE channel.
174
183
  setupPeerConnection(session as unknown as WebSocket, req, peerDeps)
175
- return { sid, session }
184
+ return session
176
185
  }
177
186
 
178
187
  // Drives a POST body's `password` and `frames` through the shared
@@ -255,6 +264,12 @@ export function installSseServer(deps: SseServerDeps): SseServer {
255
264
  return
256
265
  }
257
266
  }
267
+ // Resolve the continuation token: the JSON body's `id` is the
268
+ // canonical carrier; the `?id=` query form is the legacy
269
+ // fallback for older client bundles (see the header note — a
270
+ // query sid leaks into access logs). Body wins when both are
271
+ // present (current clients only ever send one).
272
+ const sid = parseSid(body.id) ?? sidFromUrl
258
273
  // Look up the session by id (if the client sent one). If found
259
274
  // and alive, attach the new response and dispatch. If not (or
260
275
  // session is closed), mint a fresh one — the response stream
@@ -269,19 +284,16 @@ export function installSseServer(deps: SseServerDeps): SseServer {
269
284
  // through to createSession with `res` still header-virgin, so
270
285
  // the new session can writeSseHeaders without ERR_HTTP_HEADERS_SENT.
271
286
  let session: SseSession | null = null
272
- let sid: string | null = null
273
- if (sidFromUrl) {
274
- const existing = sessions.get(sidFromUrl)
287
+ if (sid) {
288
+ const existing = sessions.get(sid)
275
289
  if (existing && existing.readyState === existing.OPEN && existing.attachResponse(res)) {
276
290
  writeSseHeaders(res)
277
291
  session = existing
278
- sid = sidFromUrl
279
- armIdleTimer(sid, session)
280
292
  }
281
293
  }
282
294
  if (!session) {
283
- const created = createSession(res, req)
284
- if (!created) {
295
+ session = createSession(res, req)
296
+ if (!session) {
285
297
  // Cap exceeded; createSession already logged. Response
286
298
  // headers not yet written by writeSseHeaders, so send a
287
299
  // 503 JSON instead.
@@ -289,8 +301,6 @@ export function installSseServer(deps: SseServerDeps): SseServer {
289
301
  res.end(JSON.stringify({ error: 'too-many-sessions' }))
290
302
  return
291
303
  }
292
- session = created.session
293
- sid = created.sid
294
304
  }
295
305
  dispatchBody(session, body)
296
306
  // Do NOT res.end() — the response stays open as the session's
@@ -324,29 +334,51 @@ export function installSseServer(deps: SseServerDeps): SseServer {
324
334
  return true
325
335
  }
326
336
 
327
- return { handle, sessions: () => sessions.values() }
337
+ // Server-driven keepalive sweep. Writes a `:` comment to every open
338
+ // session's downstream so proxies don't idle-close it. `unref` so it can't
339
+ // by itself hold the event loop open (parity with the WS heartbeat timer);
340
+ // skipped during shutdown so a tick can't write to a session the close
341
+ // loop is tearing down. Dead-session reaping is the response `close` event
342
+ // (see SseSession.wireResponse + the per-session TCP keepalive), NOT this
343
+ // sweep — so a half-open client is dropped without ever POSTing.
344
+ const keepaliveTimer = setInterval(() => {
345
+ if (isShuttingDown()) return
346
+ for (const session of sessions.values()) {
347
+ try { session.ping() } catch {}
348
+ }
349
+ }, KEEPALIVE_SWEEP_MS)
350
+ keepaliveTimer.unref?.()
351
+
352
+ return { handle, sessions: () => sessions.values(), keepaliveTimer }
328
353
  }
329
354
 
330
- // Bare-bones query parse for `id=<base64url>`. Avoids URLSearchParams
331
- // (which decodes percent-escapes) — `randomId()` mints a 22-char
332
- // base64url string, and the client echoes it back unchanged, so no
333
- // escapes are possible on the legitimate path. The {1,64} bound is a
355
+ // Shape gate for a continuation token from either carrier (body `id`
356
+ // field or the legacy `?id=` query). The {1,64} bound is a
334
357
  // deliberately lenient sanity gate: anything outside the base64url
335
- // alphabet is rejected (and a missing id is treated as "no id"); a
336
- // client that sent a sid this regex doesn't recognise just gets a
337
- // fresh session minted by createSession, no failure mode. The wide
338
- // length window means a future randomId-length change here doesn't
339
- // silently break old clients that round-trip a longer or shorter
340
- // token. Returns null on missing / malformed.
358
+ // alphabet is rejected; an unrecognised sid just gets a fresh session
359
+ // from createSession (no failure mode), and the wide length window
360
+ // keeps a future randomId-length change from silently breaking old
361
+ // clients that round-trip a longer/shorter token. Returns null on
362
+ // missing / non-string / malformed.
363
+ function parseSid(v: unknown): string | null {
364
+ if (typeof v !== 'string') return null
365
+ if (!/^[A-Za-z0-9_-]{1,64}$/u.test(v)) return null
366
+ return v
367
+ }
368
+
369
+ // Bare-bones query parse for the LEGACY `id=<base64url>` carrier
370
+ // (older client bundles; current clients send the sid in the POST
371
+ // body — see the header's "Continuation" note). Avoids URLSearchParams
372
+ // (which decodes percent-escapes) — `randomId()` mints a 22-char
373
+ // base64url string echoed back unchanged, so no escapes are possible
374
+ // on the legitimate path.
341
375
  function parseSidQuery(query: string | undefined): string | null {
342
376
  if (typeof query !== 'string') return null
343
377
  for (const part of query.split('&')) {
344
378
  const eq = part.indexOf('=')
345
379
  if (eq <= 0) continue
346
380
  if (part.slice(0, eq) !== 'id') continue
347
- const v = part.slice(eq + 1)
348
- if (!/^[A-Za-z0-9_-]{1,64}$/u.test(v)) return null
349
- return v
381
+ return parseSid(part.slice(eq + 1))
350
382
  }
351
383
  return null
352
384
  }
@@ -28,6 +28,20 @@ import { EventEmitter } from 'node:events'
28
28
  import type { Buffer } from 'node:buffer'
29
29
  import type { ServerResponse } from 'node:http'
30
30
 
31
+ // TCP keepalive idle delay for the downstream socket. With the client no
32
+ // longer POSTing a periodic ping (see client/sync/socket-transport.ts), a
33
+ // QUIET session whose client vanished without a FIN (crash, NAT/idle drop)
34
+ // has no application-level liveness signal — so we lean on the kernel:
35
+ // after this much idle the kernel starts probing, and a dead half-open
36
+ // socket surfaces as a `close`/`error` here. Node's `setKeepAlive` sets
37
+ // only TCP_KEEPIDLE (the delay to the FIRST probe), not the probe
38
+ // interval/count — so full teardown is this delay PLUS the OS's
39
+ // TCP_KEEPINTVL × TCP_KEEPCNT (~minutes on Linux defaults), still bounded
40
+ // and far below the kernel's hours-long default-off behaviour. `maxSessions`
41
+ // is the hard backstop. Behind a TLS-terminating / buffering proxy this
42
+ // probes the proxy hop, not the client — see the reaping note in sse-server.ts.
43
+ const SOCKET_KEEPALIVE_MS = 30_000
44
+
31
45
  export class SseSession extends EventEmitter {
32
46
  static readonly CONNECTING = 0
33
47
  static readonly OPEN = 1
@@ -62,21 +76,19 @@ export class SseSession extends EventEmitter {
62
76
  // spurious session 'error' that operators read as a real transport
63
77
  // failure on a healthy session.
64
78
  private wireResponse(res: ServerResponse): void {
79
+ // Probe half-open downstreams at the kernel level (see SOCKET_KEEPALIVE_MS).
80
+ // A failed probe trips the `close`/`error` handlers below, which is how a
81
+ // dead-but-quiet SSE client is reaped now that there's no client ping.
82
+ try { res.socket?.setKeepAlive(true, SOCKET_KEEPALIVE_MS) } catch {}
65
83
  res.on('close', () => {
66
- // Only the *current* response's close terminates the session. A
67
- // swapped-out previous response closes naturally during takeover
68
- // and must not knock the session offline.
69
- if (res !== this.currentRes) return
84
+ if (res !== this.currentRes) return // current-response guard (see above)
70
85
  if (this.readyState === SseSession.CLOSED) return
71
86
  this.readyState = SseSession.CLOSED
72
87
  this.currentRes = null
73
88
  this.emit('close')
74
89
  })
75
90
  res.on('error', (err: Error) => {
76
- // Same identity guard as close: drained-out previous responses
77
- // may emit RST/EPIPE during flush and we don't want those to
78
- // pseudo-fail the healthy session that's now on a new response.
79
- if (res !== this.currentRes) return
91
+ if (res !== this.currentRes) return // current-response guard (see above)
80
92
  this.emit('error', err)
81
93
  })
82
94
  }
@@ -148,9 +160,8 @@ export class SseSession extends EventEmitter {
148
160
  // double-emit), which means without the explicit emit here neither
149
161
  // sse-server's dropSession cleanup nor setupPeerConnection's
150
162
  // unsubscribeAll/peers.delete would run on any server-initiated
151
- // teardown — sessions / hub.subscribers / idleTimers would leak per
152
- // close. The wireResponse guard then ensures the later async fire
153
- // is a no-op.
163
+ // teardown — the sessions map / hub.subscribers would leak per close.
164
+ // The wireResponse guard then ensures the later async fire is a no-op.
154
165
  close(code?: number, reason?: string): void {
155
166
  if (this.readyState === SseSession.CLOSED) return
156
167
  const res = this.currentRes
@@ -179,11 +190,14 @@ export class SseSession extends EventEmitter {
179
190
  this.emit('close')
180
191
  }
181
192
 
182
- // The heartbeat sweep ping()s every WS client to detect dead sockets
183
- // via the unanswered-pong path. SSE has no `pong` equivalent, so we
184
- // write a comment line that keeps the channel alive across proxies
185
- // without expecting a reply. The per-session idle timeout in
186
- // sse-server.ts owns the dead-client detection.
193
+ // Server-driven keepalive, called on the SSE keepalive sweep in
194
+ // sse-server.ts. SSE has no `pong` equivalent, so we write a `:` comment
195
+ // line — ignored by the client parser — that keeps the downstream from
196
+ // being idle-closed by intermediary proxies (nginx et al. default to a
197
+ // ~60s read timeout). Dead-client detection is the response `close` event
198
+ // (clean disconnect, or a half-open socket the TCP keepalive in
199
+ // `wireResponse` forces closed), NOT this write: a `:`-comment write to a
200
+ // half-open socket just buffers, it doesn't synchronously throw.
187
201
  ping(): void {
188
202
  if (this.readyState !== SseSession.OPEN) return
189
203
  const res = this.currentRes
@@ -6,8 +6,8 @@
6
6
  // `join(staticDir, dirent.name)` from `readdirSync` at boot — the
7
7
  // request URL is only used as a Map key, never joined with the
8
8
  // filesystem — so the handler has no path-traversal surface at all:
9
- // `..`, percent-encoded slashes, nested subpaths and absolute-form
10
- // URIs all just produce a key that isn't in the map and 404.
9
+ // `..`, percent-encoded slashes and absolute-form URIs all just
10
+ // produce a key that isn't in the map and 404.
11
11
  //
12
12
  // Compression: every entry whose extension is in COMPRESSIBLE gets
13
13
  // pre-computed brotli + gzip at boot. The handler picks brotli over
@@ -38,6 +38,7 @@ import { readFileSync, readdirSync } from 'node:fs'
38
38
  import { createHash } from 'node:crypto'
39
39
  import { brotliCompressSync, gzipSync, constants as zlibConstants } from 'node:zlib'
40
40
  import { extname, join } from 'node:path'
41
+ import { scanServerHtml } from '../server-common/scan-config.ts'
41
42
 
42
43
  // MIME types for extensions the production UI bundle currently
43
44
  // emits. Anything else falls through to `application/octet-stream`
@@ -64,15 +65,24 @@ const COMPRESSIBLE = new Set(['.html', '.css', '.js', '.svg', '.webmanifest'])
64
65
  // navigated `image/svg+xml` icon into something script-executable.
65
66
  // `no-referrer` keeps URLs of this (potentially sensitive) viewer out
66
67
  // of outbound Referer headers, and `same-origin` CORP stops other
67
- // origins embedding these bytes. CSP is deliberately NOT here: the
68
- // HTML documents carry their own per-page `<meta http-equiv>` policy
69
- // (and the three pages differ), so a blanket header CSP would just AND
70
- // against the meta one and risk breaking a page — non-document assets
71
- // get their own flat CSP via `StaticEntry.csp` instead.
68
+ // origins embedding these bytes. COOP `same-origin` severs the
69
+ // window.opener link to any cross-origin opener/popup (so an external
70
+ // `target=_blank` GitHub/Claude link can't reach back into this context),
71
+ // and COEP `require-corp` bars the document from loading any cross-origin
72
+ // subresource that doesn't opt in via CORP/CORS. Together they also make
73
+ // the page cross-origin-isolated. Safe here because the bundle is fully
74
+ // same-origin (no external scripts/images/fonts/iframes) — switch COEP to
75
+ // `credentialless` if a cross-origin resource is ever added. CSP is
76
+ // deliberately NOT here: the HTML documents carry their own per-page
77
+ // `<meta http-equiv>` policy (and the three pages differ), so a blanket
78
+ // header CSP would just AND against the meta one and risk breaking a page
79
+ // — non-document assets get their own flat CSP via `StaticEntry.csp`.
72
80
  const SECURITY_HEADERS = {
73
81
  'x-content-type-options': 'nosniff',
74
82
  'referrer-policy': 'no-referrer',
75
83
  'cross-origin-resource-policy': 'same-origin',
84
+ 'cross-origin-opener-policy': 'same-origin',
85
+ 'cross-origin-embedder-policy': 'require-corp',
76
86
  } as const
77
87
 
78
88
  type StaticEntry = {
@@ -93,8 +103,10 @@ export type StaticHandler = (req: HttpRequest, res: ServerResponse) => boolean
93
103
  // caller falls through to its next route. Missing `staticDir`
94
104
  // (pre-build case) logs a warning and returns a handler that always
95
105
  // answers false; the API/WS planes are unaffected.
96
- export function loadStatic(staticDir: string): StaticHandler {
97
- const files = readStaticFiles(staticDir)
106
+ type StaticOptions = { transformIndex?: (html: string) => string, indexOnly?: boolean }
107
+
108
+ export function loadStatic(staticDir: string, deepviewScanServer: string | null = null, options: StaticOptions = {}): StaticHandler {
109
+ const files = readStaticFiles(staticDir, deepviewScanServer, options)
98
110
  return function handleStatic(req: HttpRequest, res: ServerResponse): boolean {
99
111
  if (req.method !== 'GET' && req.method !== 'HEAD') return false
100
112
  if (typeof req.url !== 'string') return false
@@ -142,7 +154,7 @@ export function loadStatic(staticDir: string): StaticHandler {
142
154
  }
143
155
  }
144
156
 
145
- function readStaticFiles(staticDir: string): ReadonlyMap<string, StaticEntry> {
157
+ function readStaticFiles(staticDir: string, deepviewScanServer: string | null, options: StaticOptions): ReadonlyMap<string, StaticEntry> {
146
158
  const files = new Map<string, StaticEntry>()
147
159
  let entries
148
160
  try { entries = readdirSync(staticDir, { withFileTypes: true }) } catch (err) {
@@ -156,24 +168,21 @@ function readStaticFiles(staticDir: string): ReadonlyMap<string, StaticEntry> {
156
168
  throw err
157
169
  }
158
170
  for (const entry of entries) {
159
- // `isFile()` filters subdirectories, symlinks, FIFOs etc. The
160
- // build emits a flat tree; dropping anything else keeps the
161
- // surface minimal even if a stray non-file sneaks in.
162
- if (!entry.isFile()) continue
163
- files.set(entry.name, buildEntry(staticDir, entry.name))
171
+ // Exclude subdirectories, symlinks and non-file entries.
172
+ if (!entry.isFile() || (options.indexOnly && entry.name !== 'index.html')) continue
173
+ files.set(entry.name, buildEntry(staticDir, entry.name, deepviewScanServer, options.transformIndex))
164
174
  }
165
175
  return files
166
176
  }
167
177
 
168
- function buildEntry(staticDir: string, name: string): StaticEntry {
178
+ function buildEntry(staticDir: string, name: string, deepviewScanServer: string | null = null, transformIndex: (html: string) => string = html => html): StaticEntry {
169
179
  const ext = extname(name)
170
- const raw = readFileSync(join(staticDir, name))
180
+ const source = readFileSync(join(staticDir, name))
181
+ const raw = name === 'index.html' ? Buffer.from(transformIndex(scanServerHtml(source.toString('utf8'), deepviewScanServer))) : source
171
182
  const type = CONTENT_TYPE[ext] ?? 'application/octet-stream'
172
- // HTML: lift `<link rel="(module)preload" …>` into a Link header
173
- // and drop the tags from the served body so the bytes ship without
174
- // the now-redundant in-body hint. ETag + compression run against
175
- // the stripped body — the on-disk file and the served body diverge
176
- // by exactly the lifted tags.
183
+ // HTML: lift `<link rel="(module)preload" …>` into a Link header and
184
+ // drop the tags from the served body. ETag + compression run against
185
+ // the stripped body (see the header note on the divergence).
177
186
  let identity = raw
178
187
  let link: string | null = null
179
188
  if (ext === '.html') {
@@ -273,13 +282,11 @@ function isUnsafeAttr(s: string): boolean {
273
282
  return /[\r\n",;<>]/u.test(s)
274
283
  }
275
284
 
276
- // Find character ranges in `html` whose contents are NOT real HTML
277
- // content: comment bodies and script / noscript raw-text bodies. A
278
- // `<link>` whose match offset falls inside any range is preserved
279
- // verbatim (no header lift). Heuristic: a real HTML parser is
280
- // overkill here — the build emits clean, well-formed markup; this is
281
- // defence against the build (or a hand edit) accidentally mentioning
282
- // `<link rel="preload">` somewhere it isn't meant to fire.
285
+ // Char ranges in `html` whose contents are NOT real HTML content:
286
+ // comment bodies and script / noscript raw-text bodies. A `<link>`
287
+ // whose match offset falls in any range is preserved verbatim (no
288
+ // header lift — see the call site for why). Heuristic, not a real
289
+ // parser: the build emits clean, well-formed markup.
283
290
  function findSkipRanges(html: string): Array<[number, number]> {
284
291
  const ranges: Array<[number, number]> = []
285
292
  const patterns = [