Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

fix(safety): honour PostgreSQL E'...' escape strings in the risk scanner

ordinary being74aaa4f643bc
diff --git a/apps/desktop/src/lib/__tests__/sql/sqlRisk.dialect.spec.ts b/apps/desktop/src/lib/__tests__/sql/sqlRisk.dialect.spec.ts
index 6dda54123d..f504f29a0b 100644
--- a/apps/desktop/src/lib/__tests__/sql/sqlRisk.dialect.spec.ts
+++ b/apps/desktop/src/lib/__tests__/sql/sqlRisk.dialect.spec.ts
@@ -65,6 +65,40 @@ describe("SQL risk dialect lexer rules", () => {
});
});
+ it("scans PostgreSQL escape strings by the dialect lexer rules", () => {
+ // Inside E'...' a backslash escapes the quote, so the DELETE stays literal
+ // data and must not raise the risk (matches the Rust scanner).
+ const inside = "SELECT E'it\\'s; DELETE FROM users;'";
+
+ expect(sqlSafetyText(inside, "postgres")).not.toContain("DELETE FROM users");
+ expect(classifySqlRisk(inside, { dialect: "postgres" }).risk).toBe("read");
+
+ // Outside the E-string the DELETE is real code and must be classified.
+ const after = "SELECT E'it\\'s'; DELETE FROM users;";
+
+ expect(sqlSafetyText(after, "postgres")).toContain("DELETE FROM users");
+ expect(splitSqlStatementsForSafety(after, "postgres")).toEqual(["SELECT E", "DELETE FROM users"]);
+ expect(classifySqlRisk(after, { dialect: "postgres" }).risk).toBe("write");
+ expect(classifySqlRisk(after, { dialect: "kingbase" }).risk).toBe("write");
+ });
+
+ it("does not auto-execute a PostgreSQL write hidden behind an escape string", () => {
+ const sql = "SELECT E'it\\'s'; DELETE FROM users;";
+
+ expect(classifyAiSqlExecution(sql, postgresConnection)).toMatchObject({
+ action: "confirm",
+ category: "write",
+ });
+ });
+
+ it("keeps an E that ends an identifier out of the escape-string rules", () => {
+ // `xE` is one identifier, so the following literal is ordinary (same as
+ // `ends_with_escape_string_prefix` in the Rust scanner).
+ const sql = "SELECT xE'it\\'s'; DELETE FROM users;";
+
+ expect(sqlSafetyText(sql, "postgres")).not.toContain("DELETE FROM users");
+ });
+
it("still reads # as a line comment on MySQL", () => {
const sql = "SELECT * FROM #tmp; DELETE FROM #tmp;";
diff --git a/apps/desktop/src/lib/database/__tests__/productionSafety.spec.ts b/apps/desktop/src/lib/database/__tests__/productionSafety.spec.ts
index 6a4ec172e3..32046077e8 100644
--- a/apps/desktop/src/lib/database/__tests__/productionSafety.spec.ts
+++ b/apps/desktop/src/lib/database/__tests__/productionSafety.spec.ts
@@ -66,6 +66,11 @@ describe("production SQL safety", () => {
}
});
+ it("keeps production DDL visible after PostgreSQL E'...' escape strings", () => {
+ const pg = connection({ db_type: "postgres" });
+ expect(assessProductionSql("SELECT E'a\\''; DROP DATABASE prod_app;", pg, "staging")).toMatchObject({ active: true, reason: "sql_target", isMutation: true, databases: ["prod_app"] });
+ });
+
it("matches the shared SQL target safety corpus", () => {
for (const corpusCase of productionSafetyCorpus) {
const assessment = assessProductionSql(
diff --git a/apps/desktop/src/lib/database/productionSafety.ts b/apps/desktop/src/lib/database/productionSafety.ts
index b3dd3e35f0..91a83828cb 100644
--- a/apps/desktop/src/lib/database/productionSafety.ts
+++ b/apps/desktop/src/lib/database/productionSafety.ts
@@ -1,6 +1,6 @@
import type { ConnectionConfig, DatabaseType } from "@/types/database";
import { nacosNamespaceIdentity } from "@/lib/nacos/nacosNamespaceVisibility";
-import { classifySqlRisk, isSqlRiskMutation, usesMysqlLexerRules } from "@/lib/sql/sqlRisk";
+import { classifySqlRisk, endsWithEscapeStringPrefix, isSqlRiskMutation, usesMysqlLexerRules, usesPostgresEscapeStrings } from "@/lib/sql/sqlRisk";
export type ProductionContextReason = "connection" | "database" | "sql_target";
@@ -272,6 +272,7 @@ function splitTargetStatements(sql: string): string[] {
*/
function sqlTargetSafetyText(sql: string, dialect?: DatabaseType | string, quotedIdentifiers = new Map<string, string>()): SqlTargetSafetyText {
const mysqlLexer = usesMysqlLexerRules(dialect);
+ const postgresEscapeStrings = usesPostgresEscapeStrings(dialect);
let output = "";
let index = 0;
@@ -316,7 +317,8 @@ function sqlTargetSafetyText(sql: string, dialect?: DatabaseType | string, quote
}
if (char === "'") {
- index = readQuotedEnd(sql, index, "'", "'", mysqlLexer);
+ const backslashEscapes = mysqlLexer || (postgresEscapeStrings && endsWithEscapeStringPrefix(sql, index));
+ index = readQuotedEnd(sql, index, "'", "'", backslashEscapes);
output += " ";
continue;
}
diff --git a/apps/desktop/src/lib/sql/sqlRisk.ts b/apps/desktop/src/lib/sql/sqlRisk.ts
index 2095120d18..df462e0553 100644
--- a/apps/desktop/src/lib/sql/sqlRisk.ts
+++ b/apps/desktop/src/lib/sql/sqlRisk.ts
@@ -55,6 +55,32 @@ export function usesMysqlLexerRules(dialect?: DatabaseType | string): boolean {
return typeof dialect === "string" && MYSQL_LEXER_DATABASE_TYPES.has(dialect.trim().toLowerCase());
}
+/**
+ * PostgreSQL-family engines also read a backslash as an escape inside `E'...'`
+ * literals, even though `standard_conforming_strings` keeps ordinary `'...'`
+ * literals backslash-literal. The `E`/`e` must start its own token, so
+ * `DATE'2020-01-01'` and `x$e'...'` stay ordinary strings. Mirrors the
+ * `postgres_escape_strings` rule of `SqlScanLexerRules` in
+ * `crates/dbx-core/src/safety/production_safety.rs`.
+ */
+const POSTGRES_ESCAPE_STRING_DATABASE_TYPES = new Set<string>(["postgres", "opengauss", "gaussdb", "vastbase", "kingbase", "highgo", "uxdb", "kwdb"]);
+
+export function usesPostgresEscapeStrings(dialect?: DatabaseType | string): boolean {
+ return typeof dialect === "string" && POSTGRES_ESCAPE_STRING_DATABASE_TYPES.has(dialect.trim().toLowerCase());
+}
+
+/**
+ * True when the `E'`/`e'` introducer before `quoteIndex` starts its own token,
+ * so `DATE'2020-01-01'` and `x$e'...'` stay ordinary strings. Mirrors
+ * `ends_with_escape_string_prefix` in `crates/dbx-core/src/safety/production_safety.rs`.
+ */
+export function endsWithEscapeStringPrefix(sql: string, quoteIndex: number): boolean {
+ const introducer = sql[quoteIndex - 1];
+ if (introducer !== "E" && introducer !== "e") return false;
+ const before = sql[quoteIndex - 2];
+ return before === undefined || !/[\p{L}\p{N}_$]/u.test(before);
+}
+
const RISK_ORDER: Record<SqlRiskLevel, number> = {
read: 0,
write: 1,
@@ -184,6 +210,7 @@ export function isSqlRiskMutation(risk: SqlRiskLevel): boolean {
export function sqlSafetyText(sql: string, dialect?: DatabaseType | string): string {
const mysqlLexer = usesMysqlLexerRules(dialect);
+ const postgresEscapeStrings = usesPostgresEscapeStrings(dialect);
let output = "";
let index = 0;
@@ -228,7 +255,8 @@ export function sqlSafetyText(sql: string, dialect?: DatabaseType | string): str
}
if (char === "'") {
- index = readQuotedEnd(sql, index, "'", "'", mysqlLexer);
+ const backslashEscapes = mysqlLexer || (postgresEscapeStrings && endsWithEscapeStringPrefix(sql, index));
+ index = readQuotedEnd(sql, index, "'", "'", backslashEscapes);
output += " ";
continue;
}