fix(sql): apply dialect rules when scanning comments and escapes for risk

This commit is contained in:
ordinary being authored and GitHub committed 2026-10-05 23:55:49 +08:00
1 parent 1a57e54f17
commit 67b9565979
3 files changed
+126 -15

No files matched your search

@@ -0,0 +1,89 @@
import { describe, expect, it } from "vitest";
import { classifySqlRisk, splitSqlStatementsForSafety, sqlSafetyText } from "@/lib/sql/sqlRisk";
import { classifyAiSqlExecution } from "@/lib/ai/aiSqlExecutionPolicy";
import type { ConnectionConfig } from "@/types/database";
/**
* `#` line comments and backslash-escaped quotes are MySQL lexer features.
* Applying them to every dialect hid statements from the classifier, so a write
* placed after a SQL Server `#temp` reference or a PostgreSQL `'dir\'` string was
* read as comment/string content and the whole script was auto-executed.
*/
const sqlServerConnection: ConnectionConfig = {
id: "conn-sqlserver",
name: "Reporting",
db_type: "sqlserver",
host: "db.internal",
port: 1433,
username: "app",
password: "",
};
const postgresConnection: ConnectionConfig = {
id: "conn-postgres",
name: "Reporting",
db_type: "postgres",
host: "db.internal",
port: 5432,
username: "app",
password: "",
};
describe("SQL risk dialect lexer rules", () => {
it("keeps the SQL Server DELETE after a #temp reference", () => {
const sql = "SELECT * FROM #tmp; DELETE FROM #tmp;";
expect(sqlSafetyText(sql, "sqlserver")).toContain("DELETE FROM #tmp");
expect(splitSqlStatementsForSafety(sql, "sqlserver")).toEqual(["SELECT * FROM #tmp", "DELETE FROM #tmp"]);
expect(classifySqlRisk(sql, { dialect: "sqlserver" }).risk).toBe("write");
});
it("does not auto-execute a SQL Server write hidden behind a #temp reference", () => {
const sql = "SELECT * FROM #tmp; DELETE FROM #tmp;";
expect(classifyAiSqlExecution(sql, sqlServerConnection)).toMatchObject({
action: "confirm",
category: "write",
});
});
it("keeps the PostgreSQL DELETE after a string that ends in a backslash", () => {
const sql = "SELECT 'dir\\'; DELETE FROM users;";
expect(sqlSafetyText(sql, "postgres")).toContain("DELETE FROM users");
// The classifier blanks out literals, so only the DELETE text has to survive.
expect(splitSqlStatementsForSafety(sql, "postgres")).toEqual(["SELECT", "DELETE FROM users"]);
expect(classifySqlRisk(sql, { dialect: "postgres" }).risk).toBe("write");
});
it("does not auto-execute a PostgreSQL write hidden behind a backslash string", () => {
const sql = "SELECT 'dir\\'; DELETE FROM users;";
expect(classifyAiSqlExecution(sql, postgresConnection)).toMatchObject({
action: "confirm",
category: "write",
});
});
it("still reads # as a line comment on MySQL", () => {
const sql = "SELECT * FROM #tmp; DELETE FROM #tmp;";
expect(sqlSafetyText(sql, "mysql")).not.toContain("DELETE FROM #tmp");
expect(splitSqlStatementsForSafety(sql, "mysql")).toEqual(["SELECT * FROM"]);
});
it("still honours backslash-escaped quotes on MySQL", () => {
const sql = "INSERT INTO notes VALUES ('it\\'s; still one value'); SELECT 1;";
// The semicolon inside the escaped string must not split the statement; the
// literal itself is blanked out by the classifier.
expect(splitSqlStatementsForSafety(sql, "mysql")).toEqual(["INSERT INTO notes VALUES ( )", "SELECT 1"]);
});
it("fails closed for an unknown dialect", () => {
const sql = "SELECT * FROM #tmp; DELETE FROM #tmp;";
expect(splitSqlStatementsForSafety(sql)).toEqual(["SELECT * FROM #tmp", "DELETE FROM #tmp"]);
expect(classifySqlRisk(sql).risk).toBe("write");
});
});
@@ -1,4 +1,4 @@
import type { ConnectionConfig } from "@/types/database"; import type { ConnectionConfig, DatabaseType } from "@/types/database";
import { assessProductionSql, productionContextForDatabase } from "@/lib/database/productionSafety"; import { assessProductionSql, productionContextForDatabase } from "@/lib/database/productionSafety";
import { classifySqlStatementRisk, splitSqlStatementsForSafety, sqlSafetyText } from "@/lib/sql/sqlRisk"; import { classifySqlStatementRisk, splitSqlStatementsForSafety, sqlSafetyText } from "@/lib/sql/sqlRisk";
@@ -18,12 +18,12 @@ const NON_PRODUCTION_RE = /\b(local|localhost|dev|develop|development|test|testi
const LOCAL_HOST_RE = /^(localhost|127(?:\.\d{1,3}){3}|0\.0\.0\.0|::1)$/i; const LOCAL_HOST_RE = /^(localhost|127(?:\.\d{1,3}){3}|0\.0\.0\.0|::1)$/i;
const NEGATIVE_EXECUTION_RE = /(不要|别|不用|禁止|只生成|仅生成|只写|仅写).{0,12}(执行|运行|跑)|do\s+not\s+execute|don't\s+execute|dont\s+execute|without\s+executing|only\s+(generate|write|return)/i; const NEGATIVE_EXECUTION_RE = /(不要|别|不用|禁止|只生成|仅生成|只写|仅写).{0,12}(执行|运行|跑)|do\s+not\s+execute|don't\s+execute|dont\s+execute|without\s+executing|only\s+(generate|write|return)/i;
export function stripAiSqlComments(sql: string): string { export function stripAiSqlComments(sql: string, dialect?: DatabaseType | string): string {
return sqlSafetyText(sql); return sqlSafetyText(sql, dialect);
} }
function sqlStatements(sql: string): string[] { function sqlStatements(sql: string, connection?: ConnectionConfig): string[] {
return splitSqlStatementsForSafety(sql); return splitSqlStatementsForSafety(sql, connection?.db_type);
} }
function classifyStatement(statement: string, connection?: ConnectionConfig): AiSqlExecutionCategory { function classifyStatement(statement: string, connection?: ConnectionConfig): AiSqlExecutionCategory {
@@ -60,7 +60,7 @@ export function classifyConnectionEnvironment(connection?: ConnectionConfig, dat
export function classifyAiSqlExecution(sql: string, connection?: ConnectionConfig, database?: string): AiSqlExecutionDecision { export function classifyAiSqlExecution(sql: string, connection?: ConnectionConfig, database?: string): AiSqlExecutionDecision {
const environment = classifyConnectionEnvironment(connection, database); const environment = classifyConnectionEnvironment(connection, database);
const statements = sqlStatements(sql); const statements = sqlStatements(sql, connection);
const reasons: string[] = []; const reasons: string[] = [];
if (!statements.length) { if (!statements.length) {
+31 -9
View File
@@ -32,6 +32,27 @@ const EXPLAIN_OPTION_KEYWORDS = new Set(["explain", "analyze", "analyse", "verbo
const PRIMARY_STATEMENT_KEYWORDS = new Set([...READ_KEYWORDS, ...WRITE_KEYWORDS, ...DDL_KEYWORDS, ...TRANSACTION_KEYWORDS, "with", "copy", "pragma", "use", "set"]); const PRIMARY_STATEMENT_KEYWORDS = new Set([...READ_KEYWORDS, ...WRITE_KEYWORDS, ...DDL_KEYWORDS, ...TRANSACTION_KEYWORDS, "with", "copy", "pragma", "use", "set"]);
const SAFE_READ_PRAGMA_NAMES = new Set(["table_info", "table_xinfo", "index_list", "index_info", "foreign_key_list", "database_list", "compile_options", "data_version"]); const SAFE_READ_PRAGMA_NAMES = new Set(["table_info", "table_xinfo", "index_list", "index_info", "foreign_key_list", "database_list", "compile_options", "data_version"]);
/**
* Engines whose lexer treats `#` as a line-comment opener and a backslash as a
* string escape. Both rules are MySQL family features: on SQL Server `#tmp` is a
* temporary table, and on PostgreSQL `standard_conforming_strings` is on by
* default, so `'dir\'` is a complete string. Assuming the MySQL rules everywhere
* hides SQL from the classifier — `SELECT * FROM #tmp; DELETE FROM #tmp;` (SQL
* Server) and `SELECT 'dir\'; DELETE FROM users;` (PostgreSQL) would otherwise be
* read as one read-only statement and auto-execute unconfirmed.
*
* Mirrors `is_mysql_compatible_database` in `crates/dbx-sql-core/src/sql.rs`,
* which gates `supports_hash_line_comments` the same way. An unknown dialect is
* deliberately not treated as MySQL: keeping `#` and backslashes literal can only
* split more statements than before, which raises the assessed risk rather than
* lowering it.
*/
const MYSQL_LEXER_DATABASE_TYPES = new Set<string>(["mysql", "doris", "starrocks", "manticoresearch", "goldendb"]);
function usesMysqlLexerRules(dialect?: DatabaseType | string): boolean {
return typeof dialect === "string" && MYSQL_LEXER_DATABASE_TYPES.has(dialect.trim().toLowerCase());
}
const RISK_ORDER: Record<SqlRiskLevel, number> = { const RISK_ORDER: Record<SqlRiskLevel, number> = {
read: 0, read: 0,
write: 1, write: 1,
@@ -40,8 +61,8 @@ const RISK_ORDER: Record<SqlRiskLevel, number> = {
unknown: 4, unknown: 4,
}; };
export function splitSqlStatementsForSafety(sql: string): string[] { export function splitSqlStatementsForSafety(sql: string, dialect?: DatabaseType | string): string[] {
return sqlSafetyText(sql) return sqlSafetyText(sql, dialect)
.split(";") .split(";")
.map((statement) => statement.trim()) .map((statement) => statement.trim())
.filter(Boolean); .filter(Boolean);
@@ -59,7 +80,7 @@ export function classifySqlRisk(sql: string, options: SqlRiskOptions = {}): SqlR
const searchEngineRisk = searchEngineAssessment(sql, options.dialect, { elasticsearch: classifyElasticsearchSourceRisk, solr: classifySolrSourceRisk, couchdb: classifyCouchDbSourceRisk }); const searchEngineRisk = searchEngineAssessment(sql, options.dialect, { elasticsearch: classifyElasticsearchSourceRisk, solr: classifySolrSourceRisk, couchdb: classifyCouchDbSourceRisk });
if (searchEngineRisk) return { ...searchEngineRisk, statements: [searchEngineRisk] }; if (searchEngineRisk) return { ...searchEngineRisk, statements: [searchEngineRisk] };
const statements = splitSqlStatementsForSafety(sql).map((statement) => classifySqlStatementRisk(statement, options)); const statements = splitSqlStatementsForSafety(sql, options.dialect).map((statement) => classifySqlStatementRisk(statement, options));
if (!statements.length) return { risk: "unknown", statements: [] }; if (!statements.length) return { risk: "unknown", statements: [] };
const highest = statements.reduce<SqlRiskStatementAssessment>((current, statement) => (RISK_ORDER[statement.risk] > RISK_ORDER[current.risk] ? statement : current), { risk: "read" }); const highest = statements.reduce<SqlRiskStatementAssessment>((current, statement) => (RISK_ORDER[statement.risk] > RISK_ORDER[current.risk] ? statement : current), { risk: "read" });
return { ...highest, statements }; return { ...highest, statements };
@@ -154,7 +175,8 @@ export function isSqlRiskMutation(risk: SqlRiskLevel): boolean {
return risk !== "read"; return risk !== "read";
} }
export function sqlSafetyText(sql: string): string { export function sqlSafetyText(sql: string, dialect?: DatabaseType | string): string {
const mysqlLexer = usesMysqlLexerRules(dialect);
let output = ""; let output = "";
let index = 0; let index = 0;
@@ -169,7 +191,7 @@ export function sqlSafetyText(sql: string): string {
continue; continue;
} }
if (char === "#") { if (char === "#" && mysqlLexer) {
index += 1; index += 1;
while (index < sql.length && sql[index] !== "\n" && sql[index] !== "\r") index += 1; while (index < sql.length && sql[index] !== "\n" && sql[index] !== "\r") index += 1;
output += " "; output += " ";
@@ -199,14 +221,14 @@ export function sqlSafetyText(sql: string): string {
} }
if (char === "'") { if (char === "'") {
index = readQuotedEnd(sql, index, "'", "'"); index = readQuotedEnd(sql, index, "'", "'", mysqlLexer);
output += " "; output += " ";
continue; continue;
} }
if (char === '"' || char === "`" || char === "[") { if (char === '"' || char === "`" || char === "[") {
const close = char === "[" ? "]" : char; const close = char === "[" ? "]" : char;
const end = readQuotedEnd(sql, index, char, close); const end = readQuotedEnd(sql, index, char, close, mysqlLexer);
output += ` ${unquoteIdentifier(sql.slice(index, end), char, close).replace(/[;]/g, " ")} `; output += ` ${unquoteIdentifier(sql.slice(index, end), char, close).replace(/[;]/g, " ")} `;
index = end; index = end;
continue; continue;
@@ -347,10 +369,10 @@ function dollarQuoteTagAt(sql: string, index: number): string | undefined {
return match?.[0]; return match?.[0];
} }
function readQuotedEnd(sql: string, start: number, open: string, close: string): number { function readQuotedEnd(sql: string, start: number, open: string, close: string, backslashEscapes: boolean): number {
let index = start + open.length; let index = start + open.length;
while (index < sql.length) { while (index < sql.length) {
if (sql[index] === "\\" && (open === "'" || open === '"')) { if (backslashEscapes && sql[index] === "\\" && (open === "'" || open === '"')) {
index += 2; index += 2;
continue; continue;
} }