Files
gstack/browse/test/server-auth.test.ts
T
Garry Tan 943105f109 v1.91.8.0 test: smaller suite, derived paid selection, retired never-green evals (#2994)
* test: delete test-infrastructure dead code (G)

- exit-propagation drives the runner's real strict verdict
  (BunTestOutputClassifier + strictTestExitCode); delete the unused
  shardRunLooksTruncated predicate.
- delete skill-coverage-matrix registry + its gate (nothing reads it; the
  floor already iterates skillCensus()).
- delete touchfiles-facade export-parity tests (Bun fails missing imports
  at link time) and the duplicated E2E_TIERS tier-value test.
- delete brain-cache-spec TRANSPORT_DEFAULT_POLICY, SKILL_RUN_RETENTION_DAYS
  and the now-unused BrainTrustPolicy type with their literal tests.
  AUTOPLAN_PREFLIGHT_BUDGET_BYTES stays: skill-preflight-budget enforces it
  against real resolver output.
- delete audit-compliance's JSDoc-comment grep.

* test: replace product tests that fake the product with real-boundary tests (F)

- design: serve.test.ts drove an inline mirror server; now two tests run the
  real serve() on an ephemeral port (reload confinement, submit exit 0).
- setup-gbrain: rollback + voyage tests execute the template-extracted init
  blocks (3 sites) instead of drifted local bash copies.
- terminal-agent: internalHandler source greps replaced by a behavioral
  /internal/grant + /internal/revoke auth matrix (no/wrong/valid token).
- /health: server-security-surface and the server-auth / security-audit-r2 /
  sidebar-tabs source greps fold into one liveness-only check on the real
  body; the L4 sidecar wiring gets a behavioral /pty-inject-scan test.
- delete tautologies (browser-manager onDisconnect, memory-command #12),
  ios swiftui tap fixture self-check, memory-ingest put_page grep, detach
  source greps, sidebar-agent absence pins, dead-CSS pins + the dead CSS,
  security-audit-r2 Task 1 + the test-only meta-commands re-export,
  duplicate generated-SKILL.md checks.
- make-pdf coverage-gaps cases move into their owner test files.

* test: delete tests of dead eval code (A)

- A1: the retired Eng lexical oracle (evaluateEngSeedCoverage,
  isEngSeedDecisionAUQ), the completion-handoff detector and the retained
  corpus had no paid caller since v1.87.6; delete their 26 replay files,
  ~2.6k helper LOC and fixtures, and the dead blocks in 8 mixed files
  (live hasNativePlanTerminal / batching assertions stay).
- A2: dead viewport approvers in autoplan-artifact-permission and their 11
  replay files + fixtures; recorder/launcher cases stay.
- A3: never-wired oracles and seeders (autoplan-phase-order,
  eng-finding-fixture, ceo-paired-fixture, design-ui-scope,
  plan-skill-completion, pty-current-screen, required-reads,
  transcript-section-logger); plan-seed-submission now decodes through the
  production createPtyScreen; section manifests name their actual guard.
- A4: zero-reference helper exports, plus execGit and invokeAndObserve
  found by the reachability pass.
- 52 fixtures orphaned by the deletions; touchfile and selection-table
  entries for every deleted path.

* test: clean up the paid eval lane (B1-B4, B6, B7)

- B1: delete paid files that assert nothing or cannot pass meaningfully:
  skill-llm-eval-spec and skill-e2e-spec-execute (test.todo), gemini-e2e
  (+ gemini-session-runner; no gemini CLI in CI), ship-idempotency (red
  since v1.63), the two opus-4-7 *-sonnet overlay wrappers, conductor-prose
  (+ its source-evaluation replay), codex-e2e-plan-format; drop their keys,
  scripts and census rows.
- B2: skill-llm-eval grades browse/sections/command-list.md with one union
  judge that also carries the baseline score pin; regression-vs-baseline
  deleted (paid run: pass, c4/c4/a4).
- B3: memory-pipeline, ios-qa, ios-qa-swift-build and plan-tune-cathedral
  make no model calls; renamed out of the paid glob so they run on every
  PR. Swift builds need GSTACK_TEST_SWIFT=1; device stub deleted.
- B4: codex-e2e*, outside-voice, aside and ios-device cannot run in the CI
  image; excluded from the weekly lane with a tracked re-entry condition.
- B6: fold opus-47's negative routing controls into skill-routing-e2e
  journey-negatives (paid run: 3/3 unrouted) and delete the file.
- B7: delete the never-green brain-privacy-gate eval; a free
  gstack-skill-start test now proves consent precedes artifacts egress.

* test: retire the finding-count cluster and trim its helpers (C)

- C0/C1: the five never-green evals (skill-e2e-autoplan-chain and
  skill-e2e-plan-{ceo,eng,design,devex}-finding-count) failed on harness and
  budget, never on skill behavior; delete them, their touchfile/tier ids,
  AUTOPLAN_CHAIN_BUDGET and the dedicated eighth periodic slice (--slices 7).
- C2: delete the helper groups whose only paid consumers were those files
  (11 modules), trim claude-pty-runner and eng-seeded-coverage to the paid
  closure, and delete the free replay tests whose assertions exercised only
  that dead code (89 files, 135 orphaned fixtures). Blocks that used dead code
  only as input for a live subject keep their assertions: the multiSelect
  default moved to plan-review-decisions, runner PTY tests use inline caller
  policies, and the timer-safe budget checks moved to eng-finding-retry-budget.
- The eight production-touching files stay except ceo-current-decision-record
  (its template read only feeds the retired counter).
- CARVE_GUARDS.autoplan is behavioral 'none'; TODOS records the lost chain
  and per-finding cadence coverage with their re-entry tests.

* test: fold per-incident replay series into their detector owners (D)

Twelve detector families move into one owner test each: 73 incident files
become describe blocks in ceo-section-loading-fixture (stale-fill race),
model-overlays, coverage-audit-evidence, autoplan-phase-observer,
native-auto-decide, outside-voice-evidence, eng-first-review,
plan-count-completion, plan-count-file-permission, ceo-mode-option,
plan-scope-selection and plan-count-prerequisite. Each block keeps its original
code and fixture, so every case still runs; only tests asserting the incident
file's own touchfile registration are dropped (41). Touchfile lists that named
an incident now name its owner.

* test: start the plan-count history PTY on its readiness marker (H)

The fake CLI prints a startup marker and the runner waits for it instead of the
fixed 8 s startup sleep (8.6 s -> 0.9 s locally). eng-semantic-terminal's
sleeping registration cases went with C; plan-count-timeout keeps the fixed wait
because it asserts deadline behavior.

* test: derive paid touchfiles from each eval's static closure (E)

touchfiles.test.ts now checks, per key, that the paid file's static
test/helpers and test/fixtures closure (plus fixture paths it names in string
literals) is covered, and names the file, path, chain and key to fix when it is
not. Free *.test.ts files are no longer touchfiles, so editing a free replay
test stops selecting paid evals: 950 entries removed, 653 real closure paths
added. The hand-copied inventories go: periodic-fixture-selection,
fake-impeccable-touchfiles and 45 per-file selection examples. Selection for
the sample edits (plan-eng-review template, claude-pty-runner,
plan-count-fixture, gstack-config) loses no case under either profile.
CONTRIBUTING documents the rule and its lower bound.

* test: skip hollow tier shards and census judges in the paid planner (B5)

A paid file is now skipped for a tier lane only when every E2E id it registers
is known statically and none has that tier; ids come from the touchfile
registrations and literal testName/*IfSelected arguments, so a comment or
skill path that quotes another id cannot unschedule it, and computed names
keep today's scheduling. --list and the manifest show each skip as
"skipped: no E2E_TIERS id has tier <tier>". The weekly gate census drops the
LLM judges (--skip-judges); they still run in the periodic census and PR gate
lanes. Gate lane 52 -> 42 files, census 41; periodic 77 -> 69.

* test: run seven paid evals on the current default capture model (B8)

skill-e2e-{auq-matrix,plan-format,qa-bugs,retro,workflow} pinned
claude-opus-4-7 and skill-e2e-office-hours plus -brain-writeback pinned
claude-sonnet-4-6; none tests a historical model, so they now capture with
resolveEvalModel('capture'), and the free harness tests that execute these
registrations receive the same resolver. The paid re-pin run passed all of
them. skill-e2e-{design,office-hours-phase4,plan-prosons,plan} keep
claude-opus-4-7: six of their cases failed on the default model (three
timeouts, a missing report file, a format miss and a posture score of 3), so
per the plan's fallback they keep their pins with a TODOS entry. The pre-spend
estimate and drop threshold are in docs/test-audit-2026-09.md.

* test: guard the reduced suite against new test-of-test files

- test/test-of-test-ratchet.test.ts records the 228 free tests that import only
  test/ code and fails on a new one, naming the owner test to extend instead;
  a stale baseline entry fails with the remove instruction.
- test/helpers/resolve-repo-path.ts is the one specifier/literal resolver for
  the ratchet and the touchfile closure invariant, with its own unit tests.
- CONTRIBUTING "Test tiers" describes the paid-failure workflow (fix, then one
  row in the detector's owner test) and the ratchet; TEST_PORTFOLIO gains the
  detector -> owner-test table and no longer claims an Autoplan chain eval.
- TODOS: automatic exclusion policy for chronically red periodic files (P3),
  the deferred native-completion table collapse, the unused CEO payment
  seeder; the PTY readiness item is narrowed to the paid runner.
- docs/test-audit-2026-09.md collects the triage, security mapping, inventories,
  selection proof, behavior-commit decisions and retained false positives.

* v1.91.8.0 test: smaller suite, derived paid selection, retired never-green evals

Release metadata for the test-reduction branch: VERSION 1.91.8.0 (1.91.7.0 is
claimed by #2983), CHANGELOG with the measured before/after table and a
contributor section, durations re-recorded on Ubicloud standard-16 (857 files,
0 failures), the agents digest, CONTRIBUTING's after-measurement row, the B8
fallback TODOS entry, and the after metrics, kept-vs-plan notes, B8 run and
census estimate in docs/test-audit-2026-09.md.

* fix(ubicloud): skip retrieval globs that match nothing instead of reporting a failed pull

* test: count issue-numbered Design findings in the UI-scope gate eval
2026-09-29 08:58:49 -07:00

425 lines
23 KiB
TypeScript

/**
* Server auth security tests — verify security remediation in server.ts
*
* Tests are source-level: they read server.ts and verify that auth checks,
* CORS restrictions, and token removal are correctly in place.
*/
import { describe, test, expect } from 'bun:test';
import * as fs from 'fs';
import * as path from 'path';
const SERVER_SRC = fs.readFileSync(path.join(import.meta.dir, '../src/server.ts'), 'utf-8');
const CLI_SRC = fs.readFileSync(path.join(import.meta.dir, '../src/cli.ts'), 'utf-8');
// Helper: extract a block of source between two markers
function sliceBetween(source: string, startMarker: string, endMarker: string): string {
const startIdx = source.indexOf(startMarker);
if (startIdx === -1) throw new Error(`Marker not found: ${startMarker}`);
const endIdx = source.indexOf(endMarker, startIdx + startMarker.length);
if (endIdx === -1) throw new Error(`End marker not found: ${endMarker}`);
return source.slice(startIdx, endIdx);
}
describe('Server auth security', () => {
// Test 1a: the pinned-origin bootstrap endpoint exists and gates on both
// the exact extension Origin and a loopback Host.
test('POST /extension-token gates on pinned Origin and loopback Host', () => {
const tokenBlock = sliceBetween(SERVER_SRC, "url.pathname === '/extension-token'", "url.pathname === '/health'");
expect(tokenBlock).toContain('GSTACK_EXTENSION_ID');
expect(tokenBlock).toContain('token: authToken');
// Host is parsed to a hostname (arrives as '127.0.0.1:34567'), never
// compared literally against the raw header.
expect(tokenBlock).toContain('.hostname');
expect(tokenBlock).toContain("'127.0.0.1'");
expect(tokenBlock).toContain("'localhost'");
expect(tokenBlock).toContain('403');
});
// Test 1c: newtab must check domain restrictions (CSO finding #5)
// Domain check for newtab is now unified with goto in the scope check section:
// (command === 'goto' || command === 'newtab') && args[0] → checkDomain
test('newtab enforces domain restrictions', () => {
const scopeBlock = sliceBetween(SERVER_SRC, "Scope check (for scoped tokens)", "Pin to a specific tab");
expect(scopeBlock).toContain("command === 'newtab'");
expect(scopeBlock).toContain('checkDomain');
expect(scopeBlock).toContain('Domain not allowed');
});
// Test 1d: validateAuth compares the bearer token in CONSTANT TIME with a
// length gate. A revert to `header === \`Bearer ${authToken}\`` keeps
// accept/reject behavior identical (functional tests still pass) but silently
// reintroduces the byte-by-byte timing side-channel; dropping the length gate
// makes timingSafeEqual throw RangeError (500 instead of 401) on a wrong-length
// token. Pin both properties, mirroring the token-registry sibling guard.
test('validateAuth uses constant-time comparison with a length gate', () => {
const authBlock = sliceBetween(SERVER_SRC, 'function validateAuth(req: Request): boolean {', '// Factory-scoped shutdown');
expect(authBlock).toContain('crypto.timingSafeEqual');
expect(authBlock).toContain('got.length === want.length');
// The null-header guard must remain (Buffer.from(null) would otherwise throw).
expect(authBlock).toContain('header === null');
// The raw === comparison of the header against the bearer string must be gone.
expect(authBlock).not.toContain('header === `Bearer ${authToken}`');
});
// Test 2: /refs endpoint requires auth via validateAuth
test('/refs endpoint requires authentication', () => {
const refsBlock = sliceBetween(SERVER_SRC, "url.pathname === '/refs'", "url.pathname === '/activity/stream'");
expect(refsBlock).toContain('validateAuth');
});
// Test 3: /refs has no wildcard CORS header
test('/refs has no wildcard CORS header', () => {
const refsBlock = sliceBetween(SERVER_SRC, "url.pathname === '/refs'", "url.pathname === '/activity/stream'");
expect(refsBlock).not.toContain("'*'");
});
// Test 4: /activity/history requires auth via validateAuth
test('/activity/history requires authentication', () => {
const historyBlock = sliceBetween(SERVER_SRC, "url.pathname === '/activity/history'", 'Batch endpoint');
expect(historyBlock).toContain('validateAuth');
});
// Test 5: /activity/history has no wildcard CORS header
test('/activity/history has no wildcard CORS header', () => {
const historyBlock = sliceBetween(SERVER_SRC, "url.pathname === '/activity/history'", 'Batch endpoint');
expect(historyBlock).not.toContain("'*'");
});
// Test 6: /activity/stream requires auth via Bearer OR view-only session cookie
// (N1: ?token= query param was dropped in v1.6.0.0 — URLs leak to logs/referer)
test('/activity/stream requires authentication with inline token check', () => {
const streamBlock = sliceBetween(SERVER_SRC, "url.pathname === '/activity/stream'", "url.pathname === '/activity/history'");
expect(streamBlock).toContain('validateAuth');
expect(streamBlock).toContain('validateSseSessionToken');
// Should not have wildcard CORS for the SSE stream
expect(streamBlock).not.toContain("Access-Control-Allow-Origin': '*'");
// ?token= query param must NOT be accepted anymore
expect(streamBlock).not.toContain("searchParams.get('token')");
});
// Test 7: /command accepts scoped tokens (not just root)
// This was the Wintermute bug — /command was BELOW the blanket validateAuth gate
// which only accepts root tokens. Scoped tokens got 401'd before reaching getTokenInfo.
test('/command endpoint sits ABOVE the blanket root-only auth gate', () => {
const commandIdx = SERVER_SRC.indexOf("url.pathname === '/command'");
const blanketGateIdx = SERVER_SRC.indexOf("Auth-required endpoints (root token only)");
// /command must appear BEFORE the blanket gate in source order
expect(commandIdx).toBeGreaterThan(0);
expect(blanketGateIdx).toBeGreaterThan(0);
expect(commandIdx).toBeLessThan(blanketGateIdx);
});
// Test 7b: /command uses getTokenInfo (accepts scoped tokens), not validateAuth (root-only)
test('/command uses getTokenInfo for auth, not validateAuth', () => {
const commandBlock = sliceBetween(SERVER_SRC, "url.pathname === '/command'", "Auth-required endpoints");
expect(commandBlock).toContain('getTokenInfo');
expect(commandBlock).not.toContain('validateAuth');
});
// Test 8: /tunnel/start requires root token
test('/tunnel/start requires root token', () => {
const tunnelBlock = sliceBetween(SERVER_SRC, "/tunnel/start", "Refs endpoint");
expect(tunnelBlock).toContain('isRootRequest');
expect(tunnelBlock).toContain('Root token required');
});
// Test 8b: /tunnel/start checks ngrok native config paths
test('/tunnel/start reads ngrok native config files', () => {
const tunnelBlock = sliceBetween(SERVER_SRC, "/tunnel/start", "Refs endpoint");
expect(tunnelBlock).toContain("'ngrok.yml'");
expect(tunnelBlock).toContain('authtoken');
});
// Test 8c: /tunnel/start returns already_active if tunnel is running
test('/tunnel/start returns already_active when tunnel exists', () => {
const tunnelBlock = sliceBetween(SERVER_SRC, "/tunnel/start", "Refs endpoint");
expect(tunnelBlock).toContain('already_active');
expect(tunnelBlock).toContain('tunnelActive');
});
// Test 9: /pair requires root token
test('/pair requires root token', () => {
const pairBlock = sliceBetween(SERVER_SRC, "url.pathname === '/pair'", "/tunnel/start");
expect(pairBlock).toContain('isRootRequest');
expect(pairBlock).toContain('Root token required');
});
// Test 9b: /pair calls createSetupKey (not createToken)
test('/pair creates setup keys, not session tokens', () => {
const pairBlock = sliceBetween(SERVER_SRC, "url.pathname === '/pair'", "/tunnel/start");
expect(pairBlock).toContain('createSetupKey');
expect(pairBlock).not.toContain('createToken');
});
// Test 10: tab ownership check happens before command dispatch
test('tab ownership check runs before command dispatch for scoped tokens', () => {
const handleBlock = sliceBetween(SERVER_SRC, "async function handleCommand", "Block mutation commands while watching");
expect(handleBlock).toContain('checkTabAccess');
expect(handleBlock).toContain('Tab not owned by your agent');
});
// Test 10a: tab gate is gated on own-only, not on isWrite
// Regression test for v1.20.0.0 footgun fix. Pre-fix the gate fired for
// any write command from any non-root token, which 403'd local skill
// spawns trying to drive the user's natural (unowned) tabs. The bundled
// hackernews-frontpage skill failed identically. The fix narrows the
// gate to `tabPolicy === 'own-only'` so pair-agent tunnel tokens stay
// strict while local shared-policy tokens (skill spawns) get unblocked.
test('tab gate predicate is own-only-scoped, not write-scoped', () => {
const handleBlock = sliceBetween(SERVER_SRC, "async function handleCommand", "Block mutation commands while watching");
// The gate condition must include the own-only check.
expect(handleBlock).toContain("tabPolicy === 'own-only'");
// It must NOT depend on WRITE_COMMANDS in the gate predicate (only inside
// the checkTabAccess call's isWrite arg, which is informational). The
// surrounding `if (...) {` for the gate must use `tabPolicy === 'own-only'`
// as the trigger, not `WRITE_COMMANDS.has(command) || ...`.
const gateLine = handleBlock.split('\n').find(l =>
l.includes("command !== 'newtab'") &&
l.includes('tokenInfo') &&
l.includes('tabPolicy')
);
expect(gateLine).toBeTruthy();
expect(gateLine).not.toMatch(/WRITE_COMMANDS\.has\(command\)\s*\|\|/);
});
// Test 10b: chain command pre-validates subcommand scopes
test('chain handler checks scope for each subcommand before dispatch', () => {
const metaSrc = fs.readFileSync(path.join(import.meta.dir, '../src/meta-commands.ts'), 'utf-8');
const chainBlock = metaSrc.slice(
metaSrc.indexOf("case 'chain':"),
metaSrc.indexOf("case 'diff':")
);
expect(chainBlock).toContain('checkScope');
expect(chainBlock).toContain('Chain rejected');
expect(chainBlock).toContain('tokenInfo');
});
// Test 10c: handleMetaCommand accepts tokenInfo parameter
test('handleMetaCommand accepts tokenInfo for chain scope checking', () => {
const metaSrc = fs.readFileSync(path.join(import.meta.dir, '../src/meta-commands.ts'), 'utf-8');
const sig = metaSrc.slice(
metaSrc.indexOf('export async function handleMetaCommand'),
metaSrc.indexOf('): Promise<string>')
);
expect(sig).toContain('tokenInfo');
});
// Test 10d: server passes tokenInfo to handleMetaCommand
// v1.35.0.0: shutdown is now factory-scoped; the call site uses shutdownFn,
// a thin wrapper that delegates to activeShutdown (set by buildFetchHandler).
test('server passes tokenInfo to handleMetaCommand', () => {
expect(SERVER_SRC).toContain('handleMetaCommand(command, args, browserManager, shutdownFn, tokenInfo,');
});
// Test 10e: activity attribution includes clientId
test('activity events include clientId from token', () => {
const commandStartBlock = sliceBetween(SERVER_SRC, "Activity: emit command_start", "try {");
expect(commandStartBlock).toContain('clientId: tokenInfo?.clientId');
});
// ─── Tunnel liveness verification ─────────────────────────────
// Test 11a: /pair endpoint probes tunnel before returning tunnel_url
test('/pair verifies tunnel is alive before returning tunnel_url', () => {
const pairBlock = sliceBetween(SERVER_SRC, "url.pathname === '/pair'", "url.pathname === '/tunnel/start'");
// Must probe the tunnel URL
expect(pairBlock).toContain('verifiedTunnelUrl');
expect(pairBlock).toContain('Tunnel probe failed');
expect(pairBlock).toContain('marking tunnel as dead');
// Must tear down tunnel state on failure (via closeTunnel helper — clears
// tunnelActive, tunnelUrl, tunnelListener, and the tunnel Bun.serve listener)
expect(pairBlock).toContain('closeTunnel()');
});
// Test 11b: /pair returns null tunnel_url when tunnel is dead
test('/pair returns verified tunnel URL, not raw tunnelActive flag', () => {
const pairBlock = sliceBetween(SERVER_SRC, "url.pathname === '/pair'", "url.pathname === '/tunnel/start'");
// Should use verifiedTunnelUrl (probe result), not raw tunnelUrl
expect(pairBlock).toContain('tunnel_url: verifiedTunnelUrl');
// Must NOT use raw tunnelActive check for the response
expect(pairBlock).not.toContain('tunnel_url: tunnelActive ? tunnelUrl');
});
// Test 11c: /tunnel/start probes cached tunnel before returning already_active
test('/tunnel/start verifies cached tunnel is alive before returning already_active', () => {
const tunnelBlock = sliceBetween(SERVER_SRC, "url.pathname === '/tunnel/start'", "url.pathname === '/refs'");
// Must probe before returning cached URL
expect(tunnelBlock).toContain('Cached tunnel is dead');
// Must tear down tunnel state on stale detection (via closeTunnel helper)
expect(tunnelBlock).toContain('closeTunnel()');
// Must fall through to restart when dead
expect(tunnelBlock).toContain('restarting');
});
// Test 11d: CLI verifies tunnel_url from server before printing instruction block
test('CLI probes tunnel_url before using it in instruction block', () => {
const pairSection = sliceBetween(CLI_SRC, 'Determine the URL to use', 'local HOST: write config');
// Must probe the tunnel URL
expect(pairSection).toContain('cliProbe');
expect(pairSection).toContain('Tunnel unreachable from CLI');
// Must fall through to restart logic on failure
expect(pairSection).toContain('attempting restart');
});
// ─── Batch endpoint security ─────────────────────────────────
// Test 12a: /batch endpoint sits ABOVE the blanket root-only auth gate (same as /command)
test('/batch endpoint sits ABOVE the blanket root-only auth gate', () => {
const batchIdx = SERVER_SRC.indexOf("url.pathname === '/batch'");
const blanketGateIdx = SERVER_SRC.indexOf("Auth-required endpoints (root token only)");
expect(batchIdx).toBeGreaterThan(0);
expect(blanketGateIdx).toBeGreaterThan(0);
expect(batchIdx).toBeLessThan(blanketGateIdx);
});
// Test 12b: /batch uses getTokenInfo (accepts scoped tokens), not validateAuth (root-only)
test('/batch uses getTokenInfo for auth, not validateAuth', () => {
const batchBlock = sliceBetween(SERVER_SRC, "url.pathname === '/batch'", "url.pathname === '/command'");
expect(batchBlock).toContain('getTokenInfo');
expect(batchBlock).not.toContain('validateAuth');
});
// Test 12c: /batch enforces max command limit
test('/batch enforces max 50 commands per batch', () => {
const batchBlock = sliceBetween(SERVER_SRC, "url.pathname === '/batch'", "url.pathname === '/command'");
expect(batchBlock).toContain('commands.length > 50');
expect(batchBlock).toContain('Max 50 commands per batch');
});
// Test 12d: /batch rejects nested batches
test('/batch rejects nested batch commands', () => {
const batchBlock = sliceBetween(SERVER_SRC, "url.pathname === '/batch'", "url.pathname === '/command'");
expect(batchBlock).toContain("cmd.command === 'batch'");
expect(batchBlock).toContain('Nested batch commands are not allowed');
});
// Test 12e: /batch skips per-command rate limiting (batch counts as 1 request)
test('/batch skips per-command rate limiting', () => {
const batchBlock = sliceBetween(SERVER_SRC, "url.pathname === '/batch'", "url.pathname === '/command'");
expect(batchBlock).toContain('skipRateCheck: true');
});
// Test 12f: /batch skips per-command activity events (emits batch-level events)
test('/batch emits batch-level activity, not per-command', () => {
const batchBlock = sliceBetween(SERVER_SRC, "url.pathname === '/batch'", "url.pathname === '/command'");
expect(batchBlock).toContain('skipActivity: true');
// Should emit batch-level start and end events
expect(batchBlock).toContain("command: 'batch'");
});
// Test 12g: /batch validates command field in each command
test('/batch validates each command has a command field', () => {
const batchBlock = sliceBetween(SERVER_SRC, "url.pathname === '/batch'", "url.pathname === '/command'");
expect(batchBlock).toContain("typeof cmd.command !== 'string'");
expect(batchBlock).toContain('Missing "command" field');
});
// Test 12h: /batch passes tabId through to handleCommandInternal
test('/batch passes tabId to handleCommandInternal for multi-tab support', () => {
const batchBlock = sliceBetween(SERVER_SRC, "url.pathname === '/batch'", "url.pathname === '/command'");
expect(batchBlock).toContain('tabId: cmd.tabId');
expect(batchBlock).toContain('handleCommandInternal');
});
// ─── Pair-agent regression tests ──────────────────────────
// Regression: connect command crashed with "domains is not defined" because
// a stray `domains,` variable was in the status fetch body (cli.ts:852).
test('connect command status fetch body has no undefined variable references', () => {
const connectBlock = sliceBetween(CLI_SRC, 'Launching headed Chromium', 'Terminal agent started');
// The status fetch should use a clean JSON body
expect(connectBlock).toContain("command: 'status'");
// Must NOT contain a bare `domains` reference in the fetch body
// (it would be `domains,` on its own line, not part of a key like `domains:`)
const bodyMatch = connectBlock.match(/body:\s*JSON\.stringify\(\{([^}]+)\}\)/);
expect(bodyMatch).not.toBeNull();
if (bodyMatch) {
// The body should only contain command and args, no stray variables
expect(bodyMatch[1]).not.toMatch(/\bdomains\b/);
}
});
// Regression: pair-agent server died 15s after CLI exited because the server
// monitored the connect subprocess PID. pair-agent must set BROWSE_PARENT_PID=0
// to disable self-termination.
test('pair-agent disables parent PID monitoring via BROWSE_PARENT_PID=0', () => {
const pairBlock = sliceBetween(CLI_SRC, 'Ensure headed mode', 'handlePairAgent');
// The connect subprocess env must override BROWSE_PARENT_PID
expect(pairBlock).toContain("BROWSE_PARENT_PID");
expect(pairBlock).toContain("'0'");
// The connect command must propagate BROWSE_PARENT_PID=0 via the
// serverEnv object literal passed to startServer. The literal text
// `serverEnv.BROWSE_PARENT_PID` is NOT in source — the value is
// assigned via object-literal syntax (`BROWSE_PARENT_PID: '0'`)
// inside the `const serverEnv: Record<string, string> = { ... }`
// declaration. Assert both pieces appear in the connect block.
const connectBlock = sliceBetween(CLI_SRC, 'Launching headed Chromium', 'Terminal agent started');
expect(connectBlock).toContain("const serverEnv");
expect(connectBlock).toContain("BROWSE_PARENT_PID: '0'");
});
// Regression: newtab returned 403 for scoped tokens because the tab ownership
// check ran before the newtab handler, checking the active tab (owned by root).
test('newtab is excluded from tab ownership check', () => {
const ownershipBlock = sliceBetween(SERVER_SRC, 'Tab ownership check (own-only tokens / pair-agent isolation)', 'newtab with ownership for scoped tokens');
// The ownership check condition must exclude newtab
expect(ownershipBlock).toContain("command !== 'newtab'");
});
// CVE fix: cookie-picker HTML must NOT inline the auth token.
// getCookiePickerHTML() must not accept an authToken parameter.
test('cookie-picker UI does not accept or inline auth token', () => {
const uiSrc = fs.readFileSync(path.join(import.meta.dir, '../src/cookie-picker-ui.ts'), 'utf-8');
// Function signature must not include authToken
expect(uiSrc).not.toMatch(/getCookiePickerHTML\([^)]*authToken/);
// No AUTH_TOKEN interpolation in template
expect(uiSrc).not.toContain("AUTH_TOKEN = '${authToken");
expect(uiSrc).not.toContain("AUTH_TOKEN = '${auth");
});
// CVE fix: cookie-picker route handler uses one-time code exchange, not open access.
test('cookie-picker HTML route requires code or session cookie', () => {
const routeSrc = fs.readFileSync(path.join(import.meta.dir, '../src/cookie-picker-routes.ts'), 'utf-8');
// Must have code validation
expect(routeSrc).toContain('pendingCodes');
expect(routeSrc).toContain('validSessions');
// Must NOT pass authToken to getCookiePickerHTML
expect(routeSrc).not.toMatch(/getCookiePickerHTML\([^)]*authToken/);
// Must set HttpOnly session cookie
expect(routeSrc).toContain('HttpOnly');
expect(routeSrc).toContain('SameSite=Strict');
});
});
describe('Pair scope defaults and revocation surface', () => {
// Regression: the CLI only sent scopes when --restrict was passed, so the
// effective pairing default lived in two places (CLI omission + server
// fallback) and could silently drift. Both sides must reference the shared
// DEFAULT_PAIR_SCOPES constant, and the CLI must send scopes
// unconditionally (the old conditional-spread shape is banned).
test('/pair default and CLI pairing body share DEFAULT_PAIR_SCOPES', () => {
const pairBlock = sliceBetween(SERVER_SRC, "url.pathname === '/pair'", "url.pathname === '/tunnel/start'");
expect(pairBlock).toContain('DEFAULT_PAIR_SCOPES');
const cliBlock = sliceBetween(CLI_SRC, 'async function handlePairAgent', 'Determine the URL to use');
// Match the CODE shape, not a comment: a bare toContain('DEFAULT_PAIR_SCOPES')
// is satisfied by the explanatory comment and passes vacuously on a revert.
expect(cliBlock).toMatch(/scopes:\s*restrict\s*\?[\s\S]{0,200}?:\s*\[\.\.\.DEFAULT_PAIR_SCOPES\]/);
expect(cliBlock).not.toMatch(/\.\.\.\(restrict\s*\?/);
});
// control is the only scope behind an explicit flag; a scopes list must
// not be able to smuggle it into a pairing grant.
test('/pair rejects control inside a scopes list without the control flag', () => {
const pairBlock = sliceBetween(SERVER_SRC, "url.pathname === '/pair'", "url.pathname === '/tunnel/start'");
expect(pairBlock).toContain("pairBody.scopes.includes('control')");
});
// CLI-encoded clientIds (spaces, UTF-8) must round-trip through the revoke
// route; slicing the raw pathname 404s on every encoded name.
test('DELETE /token decodes the clientId path segment', () => {
const revokeBlock = sliceBetween(SERVER_SRC, "url.pathname.startsWith('/token/')", "url.pathname === '/agents'");
expect(revokeBlock).toContain('decodeURIComponent');
});
});