diff --git a/integrations/claude-code-plugin/tests/hook-contract.test.mjs b/integrations/claude-code-plugin/tests/hook-contract.test.mjs index 7150c7da1b..bb3a7e4b6c 100644 --- a/integrations/claude-code-plugin/tests/hook-contract.test.mjs +++ b/integrations/claude-code-plugin/tests/hook-contract.test.mjs @@ -13,6 +13,7 @@ import os from "node:os"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { hostEnv } from "./host-env.mjs"; +import { removeSandbox } from "./hook-sandbox.mjs"; const here = path.dirname(fileURLToPath(import.meta.url)); const root = path.join(here, ".."); @@ -285,7 +286,7 @@ test("malformed, empty and non-object payloads: every hook exits 0 silently", as assertContract(await runAll(cases), 8000); } finally { await loom.close(); - fs.rmSync(box.dir, { recursive: true, force: true }); + await removeSandbox(box); } }); @@ -305,7 +306,7 @@ test("no token anywhere: every hook exits 0 silently and sends no request", asyn assert.deepEqual(loom.ops, [], "a hook without a token reached the server"); } finally { await loom.close(); - fs.rmSync(box.dir, { recursive: true, force: true }); + await removeSandbox(box); } }); @@ -330,7 +331,7 @@ test("server unreachable, answering 401, or hanging: every hook stays within its } finally { await unauthorized.close(); await hang.close(); - fs.rmSync(box.dir, { recursive: true, force: true }); + await removeSandbox(box); } }); @@ -348,7 +349,7 @@ test("a host that never closes stdin does not hold a hook open", async () => { assertContract(await runAll(cases), 8000); } finally { await loom.close(); - fs.rmSync(box.dir, { recursive: true, force: true }); + await removeSandbox(box); } }); @@ -366,7 +367,7 @@ test("a well-formed session against a healthy server: every hook exits 0 silentl assert.ok(loom.ops.length > 0, "no hook reached a healthy server"); } finally { await loom.close(); - fs.rmSync(box.dir, { recursive: true, force: true }); + await removeSandbox(box); } }); @@ -385,6 +386,6 @@ test("a payload over the stdin cap is dropped, not parsed: every hook exits 0 si assertContract(await runAll(cases, 4), 15000); } finally { await loom.close(); - fs.rmSync(box.dir, { recursive: true, force: true }); + await removeSandbox(box); } }); diff --git a/integrations/claude-code-plugin/tests/hook-sandbox.mjs b/integrations/claude-code-plugin/tests/hook-sandbox.mjs new file mode 100644 index 0000000000..1279401027 --- /dev/null +++ b/integrations/claude-code-plugin/tests/hook-sandbox.mjs @@ -0,0 +1,22 @@ +import fs from "node:fs"; +import path from "node:path"; +import { setTimeout as delay } from "node:timers/promises"; + +export async function removeSandbox(box, { timeoutMs = 45000 } = {}) { + const deadline = performance.now() + timeoutMs; + // The flusher removes its job only after its last cursor/recording write. + while (fs.readdirSync(box.tmp).some((name) => /^loom-flush-\d+-\d+\.json$/.test(name))) { + if (performance.now() >= deadline) { + const jobs = fs.readdirSync(box.tmp).filter((name) => /^loom-flush-\d+-\d+\.json$/.test(name)); + const pending = jobs.slice(0, 8).map((name) => ({ + name, + bytes: fs.statSync(path.join(box.tmp, name), { throwIfNoEntry: false })?.size ?? null, + })); + throw new Error( + `unfinished detached jobs in hook sandbox: ${JSON.stringify({ count: jobs.length, jobs: pending })}`, + ); + } + await delay(10); + } + fs.rmSync(box.dir, { recursive: true, force: true }); +} diff --git a/integrations/claude-code-plugin/tests/hook-sandbox.test.mjs b/integrations/claude-code-plugin/tests/hook-sandbox.test.mjs new file mode 100644 index 0000000000..2744cd3e04 --- /dev/null +++ b/integrations/claude-code-plugin/tests/hook-sandbox.test.mjs @@ -0,0 +1,211 @@ +import "./temp-home.mjs"; +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { fork, spawn } from "node:child_process"; +import fs from "node:fs"; +import os from "node:os"; +import http from "node:http"; +import path from "node:path"; +import { once } from "node:events"; +import { setImmediate as immediate } from "node:timers/promises"; +import { removeSandbox } from "./hook-sandbox.mjs"; +import { hostEnv } from "./host-env.mjs"; +import { dispatchFlush } from "../lib/flush-job.mjs"; + +function sandbox() { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), "loom-hook-drain-")); + const tmp = path.join(dir, "tmp"); + fs.mkdirSync(tmp); + return { dir, tmp }; +} + +test("sandbox removal waits for its detached job's final write", async () => { + const box = sandbox(); + const script = path.join(box.dir, "writer.cjs"); + fs.writeFileSync( + script, + ` +const fs = require("node:fs"); +const path = require("node:path"); +const root = process.argv[2]; +const job = path.join(root, "tmp", "loom-flush-1-" + process.pid + ".json"); +fs.writeFileSync(job, "{}"); +process.send("ready"); +process.once("message", () => { + fs.writeFileSync(path.join(root, "cursor"), "done"); + fs.unlinkSync(job); + process.send("written", () => process.disconnect()); +}); +`, + ); + const child = fork(script, [box.dir], { + env: hostEnv({}), + stdio: ["ignore", "ignore", "pipe", "ipc"], + detached: true, + }); + const closed = once(child, "close"); + let removing; + let released = false; + let stderr = ""; + child.stderr.on("data", (part) => { + stderr += part; + }); + try { + const [ready] = await once(child, "message"); + assert.equal(ready, "ready"); + let removed = false; + removing = removeSandbox(box).then(() => { + removed = true; + }); + await immediate(); + assert.equal(removed, false, "the owned writer still has a pending job"); + assert.equal(fs.existsSync(box.dir), true); + const written = once(child, "message"); + released = true; + child.send("release"); + assert.equal((await written)[0], "written"); + await removing; + assert.deepEqual(await closed, [0, null], stderr); + assert.equal(fs.existsSync(box.dir), false); + } finally { + if (!released && child.connected) child.send("release"); + await closed; + if (removing) await removing; + fs.rmSync(box.dir, { recursive: true, force: true }); + } +}); + +test("an unfinished job refuses removal at the cleanup deadline", async () => { + const box = sandbox(); + const job = path.join(box.tmp, "loom-flush-2-3.json"); + fs.writeFileSync(job, "{}"); + try { + await assert.rejects(removeSandbox(box, { timeoutMs: 0 }), (error) => { + assert.match(error.message, /^unfinished detached jobs in hook sandbox: /); + const pending = JSON.parse(error.message.split("sandbox: ")[1]); + assert.equal(pending.count, 1); + assert.deepEqual(pending.jobs, [{ name: path.basename(job), bytes: 2 }]); + return true; + }); + assert.equal(fs.existsSync(job), true); + } finally { + fs.rmSync(box.dir, { recursive: true, force: true }); + } +}); + +test("an empty job census permits removal with other sandbox files", async () => { + const box = sandbox(); + fs.writeFileSync(path.join(box.tmp, "cursor"), "42"); + await removeSandbox(box); + assert.equal(fs.existsSync(box.dir), false); +}); + +test("sandbox removal waits for the real flusher's completion marker", async () => { + const box = sandbox(); + let arrived; + const request = new Promise((resolve) => { + arrived = resolve; + }); + let respond; + const server = http.createServer((req, res) => { + req.resume(); + req.on("end", () => { + respond = () => { + if (!res.writableEnded) { + res.writeHead(200, { "content-type": "application/json" }); + res.end("{}"); + } + }; + arrived(); + }); + }); + await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve)); + let child; + let closed; + let removing; + const token = [ + "e30", + Buffer.from(JSON.stringify({ org: "test", namespaces: ["scope"] })).toString("base64url"), + "test", + ].join("."); + try { + assert.equal( + dispatchFlush( + { + appends: [{ org: "test", namespace: "scope", agent: "test", session_id: "test" }], + cursorPath: path.join(box.tmp, "cursor"), + advanceTo: 42, + }, + { + tmp: box.tmp, + timeoutMs: 1000, + spawnImpl: (command, args, options) => { + child = spawn(command, args, { + ...options, + env: hostEnv({ + HOME: box.dir, + TMPDIR: box.tmp, + LOOM_BASE_URL: `http://127.0.0.1:${server.address().port}`, + LOOM_API_KEY: token, + LOOM_HOOK_TIMEOUT_MS: "1000", + LOOM_AUTO_SIGNUP: "off", + }), + }); + closed = once(child, "close"); + return child; + }, + }, + ), + true, + ); + await Promise.race([ + request, + closed.then(() => { + throw new Error("flusher exited before its request"); + }), + ]); + let removed = false; + removing = removeSandbox(box).then(() => { + removed = true; + }); + await immediate(); + assert.equal(removed, false); + assert.equal(fs.existsSync(box.dir), true); + respond(); + assert.deepEqual(await closed, [0, null]); + await removing; + assert.equal(fs.existsSync(box.dir), false); + } finally { + respond?.(); + try { + if (closed) await closed; + if (removing) await removing; + } finally { + await new Promise((resolve) => server.close(resolve)); + fs.rmSync(box.dir, { recursive: true, force: true }); + } + } +}); + +test("unfinished job details are bounded and contain no job contents", async () => { + const box = sandbox(); + const secret = "private-capture-not-in-diagnostics"; + for (let i = 0; i < 10; i++) fs.writeFileSync(path.join(box.tmp, `loom-flush-3-${i}.json`), secret); + try { + await assert.rejects(removeSandbox(box, { timeoutMs: 0 }), (error) => { + assert.equal(error.message.includes(secret), false); + const pending = JSON.parse(error.message.split("sandbox: ")[1]); + assert.equal(pending.count, 10); + assert.equal(pending.jobs.length, 8); + for (const job of pending.jobs) { + assert.deepEqual(Object.keys(job), ["name", "bytes"]); + assert.match(job.name, /^loom-flush-3-\d+\.json$/); + assert.equal(job.bytes, Buffer.byteLength(secret)); + } + return true; + }); + assert.equal(fs.readdirSync(box.tmp).length, 10); + } finally { + fs.rmSync(box.dir, { recursive: true, force: true }); + } +}); diff --git a/tests/integration/test_private_database_log_boundary.py b/tests/integration/test_private_database_log_boundary.py index 223ce31f77..f85ba99a2b 100644 --- a/tests/integration/test_private_database_log_boundary.py +++ b/tests/integration/test_private_database_log_boundary.py @@ -3,6 +3,8 @@ from __future__ import annotations import os +from contextlib import contextmanager +from contextvars import ContextVar from uuid import uuid4 import httpx @@ -11,6 +13,30 @@ from tests.integration._resources import ch_client_from_env, unique_org +@contextmanager +def _observe_request_capture(patch, capture): + from server.core import ch + from server.settings import settings + + # Child work belongs to this request; tasks already running do not. + request = ContextVar("private_log_test_capture", default=False) + original_path = settings.ch_capture_statements + original_capture = ch.capture_statement + + def capture_request(sql, _path): + path = str(capture) if request.get() else original_path + if path: + original_capture(sql, path) + + patch.setattr(ch, "capture_statement", capture_request) + patch.setattr(settings, "ch_capture_statements", str(capture)) + token = request.set(True) + try: + yield + finally: + request.reset(token) + + async def test_native_private_query_log_is_inaccessible_to_sql_and_analyst( control_plane_url, mint_test_token, monkeypatch, tmp_path ): @@ -20,7 +46,6 @@ async def test_native_private_query_log_is_inaccessible_to_sql_and_analyst( from server.core.diagnostics import private_work from server.insights.app import sql_access from server.insights.domain.sql_surface import Scope - from server.settings import settings org, namespace = unique_org("private-log"), "docs" token = mint_test_token(org=org, namespaces=[namespace], roles=["writer"]) @@ -32,8 +57,7 @@ async def test_native_private_query_log_is_inaccessible_to_sql_and_analyst( try: await ch.connect() capture = tmp_path / "statements.jsonl" - with monkeypatch.context() as patch: - patch.setattr(settings, "ch_capture_statements", str(capture)) + with monkeypatch.context() as patch, _observe_request_capture(patch, capture): query_id = "docsrc-private-log-" + uuid4().hex with private_work(): value = await ch.raw_bytes( diff --git a/tests/unit/test_private_capture_observation.py b/tests/unit/test_private_capture_observation.py new file mode 100644 index 0000000000..7999b34aad --- /dev/null +++ b/tests/unit/test_private_capture_observation.py @@ -0,0 +1,117 @@ +"""Exercise the native-log test's capture boundary without a database.""" + +from __future__ import annotations + +import asyncio +import json + +import pytest + +from server.core import ch, diagnostics +from server.settings import settings +from tests.integration import test_private_database_log_boundary as boundary + + +class NativeBoundaryReached(Exception): + """The capture assertions passed; the real database checks start here.""" + + +class CaptureDriver: + def __init__(self, background, *, leak=None): + self.background = background + self.leak = leak + self.ready = asyncio.Event() + self.finished = asyncio.Event() + self.disconnected = False + + async def connect(self): + pass + + async def disconnect(self): + self.disconnected = True + + async def public_work(self): + await self.ready.wait() + try: + assert not diagnostics.codes_only() + if self.background: + ch.set_query_tag("test.unrelated_public_capture") + ch.statement_settings("SELECT 1 AS unrelated_public_control") + finally: + self.finished.set() + + async def raw_bytes(self, sql, *, settings): + assert diagnostics.codes_only() + ch.statement_settings(sql, settings, cls="read") + self.ready.set() + await self.finished.wait() + if self.leak == "owner": + self.force_private_capture(sql) + elif self.leak == "child": + await asyncio.create_task(self.private_child(sql)) + return b"synthetic-private-code\n" + + @staticmethod + def force_private_capture(sql): + assert diagnostics.codes_only() + ch.capture_statement(sql, settings.ch_capture_statements) + + async def private_child(self, sql): + self.force_private_capture(sql) + + async def query_one(self, sql): + assert not diagnostics.codes_only() + ch.statement_settings(sql) + return {"public_control": 1} + + async def execute(self, sql): + assert sql == "SYSTEM FLUSH LOGS" + raise NativeBoundaryReached + + +def captured_sql(path): + return [json.loads(line)["sql"] for line in path.read_text().splitlines()] + + +async def run_boundary(monkeypatch, tmp_path, *, background, leak=None): + driver = CaptureDriver(background, leak=leak) + monkeypatch.setenv("LOOM_CH_HOST", "unit-capture-only") + monkeypatch.setattr(ch, "get_client", lambda: driver) + # This task predates the private request and must not inherit its scope. + public = asyncio.create_task(driver.public_work()) + tag = ch.query_tag_var.set("") + try: + await boundary.test_native_private_query_log_is_inaccessible_to_sql_and_analyst( + "http://unused.invalid", lambda **_: "unit-token", monkeypatch, tmp_path + ) + finally: + driver.ready.set() + await public + ch.query_tag_var.reset(tag) + assert driver.disconnected + + +@pytest.mark.parametrize("background", [False, True]) +@pytest.mark.parametrize("original_sink", [False, True]) +async def test_capture_observes_only_the_logical_request( + monkeypatch, tmp_path, background, original_sink +): + original = tmp_path / "original.jsonl" + monkeypatch.setattr(settings, "ch_capture_statements", str(original) if original_sink else "") + with pytest.raises(NativeBoundaryReached): + await run_boundary(monkeypatch, tmp_path, background=background) + assert captured_sql(tmp_path / "statements.jsonl") == ["SELECT 1 AS public_control"] + if original_sink and background: + assert captured_sql(original) == ["SELECT 1 AS unrelated_public_control"] + else: + assert not original.exists() + + +@pytest.mark.parametrize("leak", ["owner", "child"]) +async def test_private_capture_from_owner_or_child_still_fails(monkeypatch, tmp_path, leak): + monkeypatch.setattr(settings, "ch_capture_statements", "") + with pytest.raises(AssertionError): + await run_boundary(monkeypatch, tmp_path, background=True, leak=leak) + assert "SELECT 'synthetic-private-code' FORMAT TabSeparated" in captured_sql( + tmp_path / "statements.jsonl" + )