feat(diagnostics): add SQL execution debug logging
This commit is contained in:
1 parent
fc1f02a931
commit
badb3b61b9
19 files changed
+874
-54
No files matched your search
+107
@@ -0,0 +1,107 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { test } from "vitest";
|
||||
import { logSqlDiagnostic, redactSqlForDiagnostics, sqlDiagnosticsEnabled } from "../src/sql-diagnostics.js";
|
||||
|
||||
test("SQL diagnostics are disabled unless explicitly enabled", () => {
|
||||
assert.equal(sqlDiagnosticsEnabled({}), false);
|
||||
assert.equal(sqlDiagnosticsEnabled({ DBX_SQL_DEBUG: "0" }), false);
|
||||
assert.equal(sqlDiagnosticsEnabled({ DBX_SQL_DEBUG: "1" }), true);
|
||||
assert.equal(sqlDiagnosticsEnabled({ DBX_MCP_DEBUG_SQL: "true" }), true);
|
||||
});
|
||||
|
||||
test("redacts sensitive literals and bounds large SQL diagnostics", () => {
|
||||
const sql = `select * from users where password = 'secret-123' and token="tok-456" and api_key=plain ${"x".repeat(900)}`;
|
||||
const redacted = redactSqlForDiagnostics(sql);
|
||||
|
||||
assert.doesNotMatch(redacted, /secret-123|tok-456|api_key=plain/);
|
||||
assert.match(redacted, /\[REDACTED\]/);
|
||||
assert.match(redacted, /api_key=\[REDACTED\]/);
|
||||
assert.match(redacted, /truncated/);
|
||||
assert.ok(redacted.length < sql.length);
|
||||
});
|
||||
|
||||
test("disabled SQL diagnostic logging does not write statements", () => {
|
||||
const original = console.error;
|
||||
const messages: unknown[][] = [];
|
||||
console.error = (...args: unknown[]) => messages.push(args);
|
||||
try {
|
||||
logSqlDiagnostic("test", "select 'secret-123'", {}, {});
|
||||
} finally {
|
||||
console.error = original;
|
||||
}
|
||||
|
||||
assert.equal(messages.length, 0);
|
||||
});
|
||||
|
||||
test("enabled SQL diagnostic logging emits redacted statements only", () => {
|
||||
const original = console.error;
|
||||
const messages: unknown[][] = [];
|
||||
console.error = (...args: unknown[]) => messages.push(args);
|
||||
try {
|
||||
logSqlDiagnostic("test", "select 'secret-123' as password", {}, { DBX_SQL_DEBUG: "1" });
|
||||
} finally {
|
||||
console.error = original;
|
||||
}
|
||||
|
||||
assert.equal(messages.length, 1);
|
||||
const rendered = messages.flat().join(" ");
|
||||
assert.doesNotMatch(rendered, /secret-123/);
|
||||
assert.match(rendered, /\[REDACTED\]/);
|
||||
});
|
||||
|
||||
test("dollar-quoted strings are redacted", () => {
|
||||
const redacted = redactSqlForDiagnostics("select $$secret$$");
|
||||
assert.doesNotMatch(redacted, /secret/);
|
||||
assert.match(redacted, /\[REDACTED\]/);
|
||||
});
|
||||
|
||||
test("space-separated sensitive assignments are redacted", () => {
|
||||
const redacted = redactSqlForDiagnostics("select * from t where password = mysecret");
|
||||
assert.doesNotMatch(redacted, /mysecret/);
|
||||
assert.match(redacted, /\[REDACTED\]/);
|
||||
});
|
||||
|
||||
test("postgres positional parameters are not treated as dollar quotes ($1, $2, ...)", () => {
|
||||
const redacted = redactSqlForDiagnostics("select * from t where id = $1 and name = 'alice'");
|
||||
assert.match(redacted, /\$1\b/);
|
||||
assert.doesNotMatch(redacted, /alice/);
|
||||
});
|
||||
|
||||
test("multiple postgres positional parameters all survive redaction", () => {
|
||||
const redacted = redactSqlForDiagnostics("select $1, $2, $3, $42 from t");
|
||||
assert.match(redacted, /\$1\b/);
|
||||
assert.match(redacted, /\$2\b/);
|
||||
assert.match(redacted, /\$3\b/);
|
||||
assert.match(redacted, /\$42\b/);
|
||||
});
|
||||
|
||||
test("empty-tag dollar quote $$secret$$ is redacted", () => {
|
||||
const redacted = redactSqlForDiagnostics("select $$secret$$ from t");
|
||||
assert.match(redacted, /\$\$\[REDACTED\]\$\$/);
|
||||
assert.doesNotMatch(redacted, /secret/);
|
||||
});
|
||||
|
||||
test("named-tag dollar quote $tag$hello$tag$ is redacted", () => {
|
||||
const redacted = redactSqlForDiagnostics("select $tag$hello$tag$ from t");
|
||||
assert.match(redacted, /\$\[REDACTED\]\$/);
|
||||
assert.doesNotMatch(redacted, /hello/);
|
||||
});
|
||||
|
||||
test("lone trailing dollar sign does not throw and passes through", () => {
|
||||
const redacted = redactSqlForDiagnostics("select 1 $");
|
||||
assert.match(redacted, /\$/);
|
||||
});
|
||||
|
||||
test("bounds redaction before scanning an unclosed literal", () => {
|
||||
const redacted = redactSqlForDiagnostics(`select '${"secret-".repeat(1000)}`, 32);
|
||||
assert.doesNotMatch(redacted, /secret-/);
|
||||
assert.match(redacted, /\[REDACTED\]/);
|
||||
assert.match(redacted, /truncated/);
|
||||
});
|
||||
|
||||
test("does not leak a sensitive value cut at the diagnostic boundary", () => {
|
||||
const redacted = redactSqlForDiagnostics(`password = ${"secret-token".repeat(1000)}`, 24);
|
||||
assert.doesNotMatch(redacted, /secret-token|secret-/);
|
||||
assert.match(redacted, /password = \[REDACTED\]/);
|
||||
assert.match(redacted, /truncated/);
|
||||
});
|
||||
+75
@@ -1,6 +1,7 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { test } from "vitest";
|
||||
import { evaluateSqlSafety, splitSqlStatements, sqlSafetyFromEnv } from "../src/sql-safety.js";
|
||||
import { supportsHashLineComments } from "../src/sql-risk.js";
|
||||
|
||||
test("allows read-only SQL by default", () => {
|
||||
const decision = evaluateSqlSafety("select * from users limit 5");
|
||||
@@ -110,3 +111,77 @@ test("sqlSafetyFromEnv supports explicitly disabling writes", () => {
|
||||
assert.equal(options.allowWrites, false);
|
||||
assert.equal(options.allowDangerous, false);
|
||||
});
|
||||
|
||||
// --- Dialect-aware `#` comment handling ---
|
||||
|
||||
test("supportsHashLineComments matches Rust mysql-compatible dialect set", () => {
|
||||
for (const dbType of ["mysql", "doris", "starrocks", "manticoresearch", "goldendb"]) {
|
||||
assert.equal(supportsHashLineComments(dbType), true, dbType);
|
||||
}
|
||||
for (const dbType of ["postgres", "sqlite", "sqlserver", "oracle", "duckdb", "bigquery", "redshift", ""]) {
|
||||
assert.equal(supportsHashLineComments(dbType), false, dbType);
|
||||
}
|
||||
assert.equal(supportsHashLineComments(undefined), false);
|
||||
});
|
||||
|
||||
test("splitSqlStatements splits PG `#` operator correctly (hashLineComments omitted/default)", () => {
|
||||
assert.deepEqual(splitSqlStatements("SELECT a # b; SELECT 2"), ["SELECT a # b", "SELECT 2"]);
|
||||
});
|
||||
|
||||
test("splitSqlStatements splits PG `#` operator correctly (hashLineComments: false)", () => {
|
||||
assert.deepEqual(splitSqlStatements("SELECT a # b; SELECT 2", { hashLineComments: false }), [
|
||||
"SELECT a # b",
|
||||
"SELECT 2",
|
||||
]);
|
||||
});
|
||||
|
||||
test("splitSqlStatements treats `#` as comment with hashLineComments: true (MySQL)", () => {
|
||||
// With hashLineComments: true, the `;` inside the `#` comment must NOT split.
|
||||
// The comment text is preserved in the output (splitter only delimits on `;`, it doesn't strip).
|
||||
assert.deepEqual(
|
||||
splitSqlStatements("SELECT 1; # trailing ; comment\nSELECT 2", { hashLineComments: true }),
|
||||
["SELECT 1", "# trailing ; comment\nSELECT 2"],
|
||||
);
|
||||
});
|
||||
|
||||
test("splitSqlStatements preserves JSONB operator text verbatim", () => {
|
||||
const result = splitSqlStatements("SELECT data #>> '{a,b}' FROM t");
|
||||
assert.equal(result.length, 1);
|
||||
assert.equal(result[0], "SELECT data #>> '{a,b}' FROM t");
|
||||
});
|
||||
|
||||
test("splitSqlStatements handles `#` as operator mid-statement (PG)", () => {
|
||||
assert.deepEqual(splitSqlStatements("SELECT 1 # 2; DELETE FROM t"), [
|
||||
"SELECT 1 # 2",
|
||||
"DELETE FROM t",
|
||||
]);
|
||||
});
|
||||
|
||||
test("evaluateSqlSafety blocks PG injection that bypasses # as comment (regression)", () => {
|
||||
// Before fix: # would strip "2; DELETE FROM t" as comment, classify as read-only.
|
||||
// After fix: # is treated as an operator, so DELETE FROM t is seen as a second write statement.
|
||||
const decision = evaluateSqlSafety("SELECT 1 # 2; DELETE FROM t", {
|
||||
allowWrites: false,
|
||||
allowMultipleStatements: true,
|
||||
});
|
||||
assert.equal(decision.allowed, false);
|
||||
assert.match(decision.reason ?? "", /read-only/i);
|
||||
});
|
||||
|
||||
test("evaluateSqlSafety allows MySQL `#` comment with hashLineComments: true", () => {
|
||||
const decision = evaluateSqlSafety("SELECT 1 # delete note", {
|
||||
allowWrites: false,
|
||||
allowMultipleStatements: true,
|
||||
hashLineComments: true,
|
||||
});
|
||||
assert.equal(decision.allowed, true);
|
||||
});
|
||||
|
||||
test("evaluateSqlSafety with hashLineComments: false still sees DELETE after `#` operator", () => {
|
||||
const decision = evaluateSqlSafety("SELECT 1 # 2; DELETE FROM t", {
|
||||
allowWrites: false,
|
||||
allowMultipleStatements: true,
|
||||
hashLineComments: false,
|
||||
});
|
||||
assert.equal(decision.allowed, false);
|
||||
});
|
||||
Reference in new issue
Block a user