From 67b95659792bab68462a6d2657fc617726cdd068 Mon Sep 17 00:00:00 2001 From: ordinary being Date: Mon, 5 Oct 2026 23:55:49 +0800 Subject: [PATCH] fix(sql): apply dialect rules when scanning comments and escapes for risk --- .../lib/__tests__/sql/sqlRisk.dialect.spec.ts | 89 +++++++++++++++++++ .../src/lib/ai/aiSqlExecutionPolicy.ts | 12 +-- apps/desktop/src/lib/sql/sqlRisk.ts | 40 +++++++-- 3 files changed, 126 insertions(+), 15 deletions(-) create mode 100644 apps/desktop/src/lib/__tests__/sql/sqlRisk.dialect.spec.ts diff --git a/apps/desktop/src/lib/__tests__/sql/sqlRisk.dialect.spec.ts b/apps/desktop/src/lib/__tests__/sql/sqlRisk.dialect.spec.ts new file mode 100644 index 0000000000..6dda54123d --- /dev/null +++ b/apps/desktop/src/lib/__tests__/sql/sqlRisk.dialect.spec.ts @@ -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"); + }); +}); diff --git a/apps/desktop/src/lib/ai/aiSqlExecutionPolicy.ts b/apps/desktop/src/lib/ai/aiSqlExecutionPolicy.ts index 01c101eedf..05ecb4f555 100644 --- a/apps/desktop/src/lib/ai/aiSqlExecutionPolicy.ts +++ b/apps/desktop/src/lib/ai/aiSqlExecutionPolicy.ts @@ -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 { 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 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 { - return sqlSafetyText(sql); +export function stripAiSqlComments(sql: string, dialect?: DatabaseType | string): string { + return sqlSafetyText(sql, dialect); } -function sqlStatements(sql: string): string[] { - return splitSqlStatementsForSafety(sql); +function sqlStatements(sql: string, connection?: ConnectionConfig): string[] { + return splitSqlStatementsForSafety(sql, connection?.db_type); } 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 { const environment = classifyConnectionEnvironment(connection, database); - const statements = sqlStatements(sql); + const statements = sqlStatements(sql, connection); const reasons: string[] = []; if (!statements.length) { diff --git a/apps/desktop/src/lib/sql/sqlRisk.ts b/apps/desktop/src/lib/sql/sqlRisk.ts index 99454d044b..82c6a0ea74 100644 --- a/apps/desktop/src/lib/sql/sqlRisk.ts +++ b/apps/desktop/src/lib/sql/sqlRisk.ts @@ -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 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(["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 = { read: 0, write: 1, @@ -40,8 +61,8 @@ const RISK_ORDER: Record = { unknown: 4, }; -export function splitSqlStatementsForSafety(sql: string): string[] { - return sqlSafetyText(sql) +export function splitSqlStatementsForSafety(sql: string, dialect?: DatabaseType | string): string[] { + return sqlSafetyText(sql, dialect) .split(";") .map((statement) => statement.trim()) .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 }); 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: [] }; const highest = statements.reduce((current, statement) => (RISK_ORDER[statement.risk] > RISK_ORDER[current.risk] ? statement : current), { risk: "read" }); return { ...highest, statements }; @@ -154,7 +175,8 @@ export function isSqlRiskMutation(risk: SqlRiskLevel): boolean { 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 index = 0; @@ -169,7 +191,7 @@ export function sqlSafetyText(sql: string): string { continue; } - if (char === "#") { + if (char === "#" && mysqlLexer) { index += 1; while (index < sql.length && sql[index] !== "\n" && sql[index] !== "\r") index += 1; output += " "; @@ -199,14 +221,14 @@ export function sqlSafetyText(sql: string): string { } if (char === "'") { - index = readQuotedEnd(sql, index, "'", "'"); + index = readQuotedEnd(sql, index, "'", "'", mysqlLexer); output += " "; continue; } if (char === '"' || 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, " ")} `; index = end; continue; @@ -347,10 +369,10 @@ function dollarQuoteTagAt(sql: string, index: number): string | undefined { 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; while (index < sql.length) { - if (sql[index] === "\\" && (open === "'" || open === '"')) { + if (backslashEscapes && sql[index] === "\\" && (open === "'" || open === '"')) { index += 2; continue; }