|
1 | 1 | import { createServer, type Server } from 'node:http'; |
2 | 2 | import { type AddressInfo } from 'node:net'; |
| 3 | +import { randomBytes, timingSafeEqual } from 'node:crypto'; |
| 4 | +import { Buffer } from 'node:buffer'; |
3 | 5 | import WebSocket, { WebSocketServer } from 'ws'; |
4 | 6 |
|
5 | 7 | import { logger } from '../logger.js'; |
6 | 8 |
|
| 9 | +/** |
| 10 | + * Phase 134.4 (deepsec HIGH): generate a per-multiplexer capability |
| 11 | + * token that consumers must include in the WebSocket upgrade path. |
| 12 | + * 32 bytes of crypto.randomBytes → 43 char base64url string. Never |
| 13 | + * logged, never exposed in error messages. |
| 14 | + */ |
| 15 | +export function generateCapabilityToken(): string { |
| 16 | + return randomBytes(32).toString('base64url'); |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * Pure verification helper. Returns true iff the request path is |
| 21 | + * exactly `/<expectedToken>`. Uses timingSafeEqual on equal-length |
| 22 | + * buffers to avoid leaking the token via response-timing side |
| 23 | + * channels. Fails closed on missing/empty/non-string inputs and on |
| 24 | + * empty expectedToken (never accept an unauthenticated multiplexer). |
| 25 | + */ |
| 26 | +export function verifyConsumerPath(reqUrl: unknown, expectedToken: string): boolean { |
| 27 | + if (typeof expectedToken !== 'string' || expectedToken.length === 0) return false; |
| 28 | + if (typeof reqUrl !== 'string' || reqUrl.length === 0) return false; |
| 29 | + if (!reqUrl.startsWith('/')) return false; |
| 30 | + const submitted = reqUrl.slice(1); |
| 31 | + if (submitted.length !== expectedToken.length) return false; |
| 32 | + const a = Buffer.from(submitted); |
| 33 | + const b = Buffer.from(expectedToken); |
| 34 | + if (a.length !== b.length) return false; |
| 35 | + return timingSafeEqual(a, b); |
| 36 | +} |
| 37 | + |
7 | 38 | /** |
8 | 39 | * CDP Multiplexer proxy (M1 / Phase 90 Tier 1). |
9 | 40 | * |
@@ -53,6 +84,15 @@ export class CDPMultiplexer { |
53 | 84 | private hermesBuffer: string[] = []; |
54 | 85 | private state: 'stopped' | 'starting' | 'running' | 'stopping' = 'stopped'; |
55 | 86 | private boundPort: number | null = null; |
| 87 | + /** |
| 88 | + * Phase 134.4: per-instance capability token. Required in the |
| 89 | + * WebSocket upgrade path. Never logged. Exposed via `token` getter |
| 90 | + * so the caller can include it in the URL it hands to DevTools. |
| 91 | + */ |
| 92 | + private readonly capabilityToken: string = generateCapabilityToken(); |
| 93 | + |
| 94 | + /** Phase 134.4: the capability token for this multiplexer instance. */ |
| 95 | + get token(): string { return this.capabilityToken; } |
56 | 96 | private routingSweeper: NodeJS.Timeout | null = null; |
57 | 97 |
|
58 | 98 | constructor(opts: MultiplexerOptions) { |
@@ -120,7 +160,24 @@ export class CDPMultiplexer { |
120 | 160 | private startConsumerServer(): Promise<number> { |
121 | 161 | return new Promise((resolve, reject) => { |
122 | 162 | this.httpServer = createServer(); |
123 | | - this.wss = new WebSocketServer({ server: this.httpServer }); |
| 163 | + this.wss = new WebSocketServer({ |
| 164 | + server: this.httpServer, |
| 165 | + // Phase 134.4 (deepsec HIGH): reject any upgrade that doesn't |
| 166 | + // include the capability token in the path. Any sibling |
| 167 | + // process that learned the loopback port (or a browser tab |
| 168 | + // scanning local ports) is refused before reaching |
| 169 | + // onConsumerConnect / onConsumerMessage. Token comparison |
| 170 | + // uses timingSafeEqual to avoid leaking the secret via |
| 171 | + // timing side channels. |
| 172 | + verifyClient: (info, callback) => { |
| 173 | + if (verifyConsumerPath(info.req.url, this.capabilityToken)) { |
| 174 | + callback(true); |
| 175 | + return; |
| 176 | + } |
| 177 | + logger.warn(this.opts.logTag, 'rejected upgrade: missing or invalid capability token'); |
| 178 | + callback(false, 401, 'Unauthorized'); |
| 179 | + }, |
| 180 | + }); |
124 | 181 |
|
125 | 182 | this.wss.on('connection', (ws) => this.onConsumerConnect(ws)); |
126 | 183 | this.wss.on('error', (err) => { |
|
0 commit comments