From 130477c48c1337f58f597298c6f29e961c4e0546 Mon Sep 17 00:00:00 2001 From: wuxiemian Date: Sun, 12 Jul 2026 21:30:37 +0800 Subject: [PATCH] fix(safety): guard object browser batch empty --- .../src/components/objects/ObjectBrowser.vue | 8 ++++-- .../productionGuardEntrypoints.test.ts | 28 +++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/apps/desktop/src/components/objects/ObjectBrowser.vue b/apps/desktop/src/components/objects/ObjectBrowser.vue index e9d225e96a..1d5d1e0d54 100644 --- a/apps/desktop/src/components/objects/ObjectBrowser.vue +++ b/apps/desktop/src/components/objects/ObjectBrowser.vue @@ -1534,9 +1534,13 @@ async function confirmBatchEmptyTables() { const plan = batchEmptyPlan.value.slice(); if (plan.length === 0) return; const asynchronousMutation = effectiveDatabaseType.value === "clickhouse"; - const result = await runBatchTableEmpty(plan, async ({ sql }) => { - await api.executeQuery(props.connection.id, props.database, sql); + const reviewSql = plan.map(({ sql }) => sql).join(";\n"); + const result = await executeObjectBrowserSqlWithProductionGuard(reviewSql, () => { + return runBatchTableEmpty(plan, async ({ sql }) => { + await api.executeQuery(props.connection.id, props.database, sql); + }); }); + if (!result) return; for (const failure of result.failed) { console.error(`Failed to empty table "${failure.target.target.name}":`, failure.error); } diff --git a/packages/app-tests/productionGuardEntrypoints.test.ts b/packages/app-tests/productionGuardEntrypoints.test.ts index 5e4a94f1e9..ce61514c49 100644 --- a/packages/app-tests/productionGuardEntrypoints.test.ts +++ b/packages/app-tests/productionGuardEntrypoints.test.ts @@ -6,6 +6,26 @@ function readSource(path: string): string { return readFileSync(path, "utf8"); } +function functionBody(source: string, name: string): string { + const signature = `function ${name}(`; + const asyncSignature = `async ${signature}`; + const signatureIndex = source.indexOf(asyncSignature) >= 0 ? source.indexOf(asyncSignature) : source.indexOf(signature); + assert.notEqual(signatureIndex, -1, `Could not find function ${name}`); + const bodyStart = source.indexOf("{", signatureIndex); + assert.notEqual(bodyStart, -1, `Could not find body for ${name}`); + + let depth = 0; + for (let index = bodyStart; index < source.length; index += 1) { + const char = source[index]; + if (char === "{") depth += 1; + if (char === "}") { + depth -= 1; + if (depth === 0) return source.slice(bodyStart + 1, index); + } + } + throw new Error(`Could not parse body for ${name}`); +} + test("secondary write entrypoints use the shared production SQL guard", () => { const entrypoints = [ { @@ -77,3 +97,11 @@ test("secondary write entrypoints use the shared production SQL guard", () => { assert.ok(source.includes(entrypoint.sourceKey), `${entrypoint.path} should label the confirmation source`); } }); + +test("object browser batch empty reviews the frozen SQL plan before executing", () => { + const source = readSource("apps/desktop/src/components/objects/ObjectBrowser.vue"); + const body = functionBody(source, "confirmBatchEmptyTables"); + assert.match(body, /executeObjectBrowserSqlWithProductionGuard\(\s*reviewSql/, "batch empty must use the Object Browser production guard"); + assert.match(body, /runBatchTableEmpty/, "batch empty should still use the batch executor after confirmation"); + assert.ok(body.indexOf("executeObjectBrowserSqlWithProductionGuard") < body.indexOf("runBatchTableEmpty"), "production guard must be entered before the batch executor"); +});