|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, expect, it, vi } from 'vitest'; |
| 4 | +import { createChromiumDriver, type ChromiumLauncher } from './chromium-driver.js'; |
| 5 | +import type { PrintSessionCookie } from './principal-session.js'; |
| 6 | + |
| 7 | +/** |
| 8 | + * The driver with an injected launcher — no browser, so this file runs on every |
| 9 | + * host. What a real Chromium cannot be made to do on demand is staged here: an |
| 10 | + * engine error that echoes the session cookie, and a browser that stalls. |
| 11 | + */ |
| 12 | + |
| 13 | +const COOKIE: PrintSessionCookie = { |
| 14 | + name: 'os.session_token', |
| 15 | + value: 'minted-cookie-value-9f2c.signature', |
| 16 | + path: '/', |
| 17 | + httpOnly: true, |
| 18 | + secure: false, |
| 19 | + sameSite: 'lax', |
| 20 | +}; |
| 21 | +const URL_ = 'http://127.0.0.1:3000/_console/apps/showcase/invoice/record/inv_1'; |
| 22 | +const ENDPOINT_TOKEN = 'endpoint-token-7d1a'; |
| 23 | + |
| 24 | +/** A step that never settles: a stalled browser. */ |
| 25 | +const never = <T>(): Promise<T> => new Promise<T>(() => undefined); |
| 26 | + |
| 27 | +interface Stage { |
| 28 | + goto?: () => Promise<unknown>; |
| 29 | + newContext?: () => Promise<unknown>; |
| 30 | + pdf?: () => Promise<Buffer>; |
| 31 | + connect?: () => Promise<unknown>; |
| 32 | +} |
| 33 | + |
| 34 | +function stagedLauncher(stage: Stage) { |
| 35 | + const closed = { context: 0, browser: 0 }; |
| 36 | + const page = { |
| 37 | + goto: vi.fn(stage.goto ?? (async () => undefined)), |
| 38 | + waitForFunction: vi.fn(async () => undefined), |
| 39 | + evaluate: vi.fn(async () => true), |
| 40 | + pdf: vi.fn(stage.pdf ?? (async () => Buffer.from('%PDF-1.4'))), |
| 41 | + }; |
| 42 | + const context = { |
| 43 | + addCookies: vi.fn(async () => undefined), |
| 44 | + newPage: vi.fn(async () => page), |
| 45 | + close: vi.fn(async () => { |
| 46 | + closed.context++; |
| 47 | + }), |
| 48 | + }; |
| 49 | + const browser = { |
| 50 | + version: () => '141.0.0.0', |
| 51 | + newContext: vi.fn(stage.newContext ?? (async () => context)), |
| 52 | + close: vi.fn(async () => { |
| 53 | + closed.browser++; |
| 54 | + }), |
| 55 | + }; |
| 56 | + const launcher = { |
| 57 | + launch: vi.fn(async () => browser), |
| 58 | + connectOverCDP: vi.fn(stage.connect ?? (async () => browser)), |
| 59 | + } as unknown as ChromiumLauncher; |
| 60 | + return { launcher: async () => launcher, closed, page }; |
| 61 | +} |
| 62 | + |
| 63 | +async function refusalOf(p: Promise<unknown>): Promise<{ code?: unknown; status?: unknown; message: string }> { |
| 64 | + try { |
| 65 | + await p; |
| 66 | + } catch (err) { |
| 67 | + const e = err as { code?: unknown; status?: unknown; message: string }; |
| 68 | + return { code: e.code, status: e.status, message: e.message }; |
| 69 | + } |
| 70 | + throw new Error('expected a refusal, the call resolved'); |
| 71 | +} |
| 72 | + |
| 73 | +describe('the driver redacts the session cookie from an engine error', () => { |
| 74 | + it('an engine error carrying the cookie value answers INTERNAL_ERROR / 500 with it replaced by [redacted]', async () => { |
| 75 | + const { launcher, closed } = stagedLauncher({ |
| 76 | + goto: async () => { |
| 77 | + throw new Error(`net::ERR_ABORTED loading ${URL_} with cookie ${COOKIE.name}=${COOKIE.value}`); |
| 78 | + }, |
| 79 | + }); |
| 80 | + const driver = createChromiumDriver({ placement: 'local', target: '/usr/bin/chromium', consoleUrl: 'x' }, launcher); |
| 81 | + const r = await refusalOf(driver.renderPdf({ url: URL_, cookie: COOKIE, timeoutMs: 5_000 })); |
| 82 | + expect([r.code, r.status]).toEqual(['INTERNAL_ERROR', 500]); |
| 83 | + expect(r.message).toContain('[redacted]'); |
| 84 | + expect(r.message).not.toContain(COOKIE.value); |
| 85 | + expect(closed).toEqual({ context: 1, browser: 1 }); |
| 86 | + }); |
| 87 | + |
| 88 | + it('a remote endpoint carrying a token is redacted from an engine error, and never named beyond scheme and host', async () => { |
| 89 | + const endpoint = `wss://browser.example.com/devtools?token=${ENDPOINT_TOKEN}`; |
| 90 | + const { launcher } = stagedLauncher({ |
| 91 | + connect: async () => { |
| 92 | + throw new Error(`WebSocket error: ${endpoint} 403 Forbidden`); |
| 93 | + }, |
| 94 | + }); |
| 95 | + const driver = createChromiumDriver({ placement: 'remote', target: endpoint, consoleUrl: 'x' }, launcher); |
| 96 | + const r = await refusalOf(driver.renderPdf({ url: URL_, cookie: COOKIE, timeoutMs: 5_000 })); |
| 97 | + expect([r.code, r.status]).toEqual(['INTERNAL_ERROR', 500]); |
| 98 | + expect(r.message).toContain('wss://browser.example.com'); |
| 99 | + expect(r.message).not.toContain(ENDPOINT_TOKEN); |
| 100 | + }); |
| 101 | +}); |
| 102 | + |
| 103 | +describe('one budget bounds the whole render', () => { |
| 104 | + for (const [step, stage] of [ |
| 105 | + ['opening a context', { newContext: () => never() }], |
| 106 | + ['printing the PDF', { pdf: () => never<Buffer>() }], |
| 107 | + ] as const) { |
| 108 | + it(`a browser that stalls ${step} answers TIMEOUT / 504 within the budget, and what was opened is closed`, async () => { |
| 109 | + const { launcher, closed } = stagedLauncher(stage); |
| 110 | + const driver = createChromiumDriver({ placement: 'remote', target: 'ws://127.0.0.1:9222/x', consoleUrl: 'x' }, launcher); |
| 111 | + const started = Date.now(); |
| 112 | + const r = await refusalOf(driver.renderPdf({ url: URL_, cookie: COOKIE, timeoutMs: 300 })); |
| 113 | + const elapsed = Date.now() - started; |
| 114 | + expect([r.code, r.status]).toEqual(['TIMEOUT', 504]); |
| 115 | + expect(elapsed).toBeGreaterThanOrEqual(290); |
| 116 | + expect(elapsed).toBeLessThan(2_000); |
| 117 | + expect(closed.browser).toBe(1); |
| 118 | + expect(r.message).not.toContain(COOKIE.value); |
| 119 | + }); |
| 120 | + } |
| 121 | +}); |
0 commit comments