Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
address review (codex): fix comment-apostrophe false-negative + spaced COPY-export false-positive
[._.]/ Adam Eivy14e31cc4e27c
diff --git a/server/lib/db.guards.test.js b/server/lib/db.guards.test.jsindex e662c28fc7..39f6b1d0ea 100644--- a/server/lib/db.guards.test.js+++ b/server/lib/db.guards.test.js@@ -309,11 +309,19 @@ describe('assertWriteAllowed broadened write forms (#1333)', () => {it('allows COPY … TO exports (a read), including the subquery form', () => {armProd();// COPY <table> TO and COPY (query) TO are exports — the inner FROM of the- // subquery must NOT read as a write.+ // subquery must NOT read as a write. The spaced/tabbed/newline-separated forms+ // are regression cases: a `(?!\()` lookahead let `\s+` backtrack and match the+ // subquery's FROM when extra whitespace preceded the `(`.expect(() => assertWriteAllowed('COPY authors TO STDOUT')).not.toThrow();expect(() =>assertWriteAllowed('COPY (SELECT * FROM authors) TO STDOUT'),).not.toThrow();+ expect(() =>+ assertWriteAllowed('COPY (SELECT * FROM authors) TO STDOUT'),+ ).not.toThrow();+ expect(() =>+ assertWriteAllowed('COPY\t(SELECT * FROM authors) TO STDOUT'),+ ).not.toThrow();});it('throws on a write hidden after a read in a multi-statement batch', () => {@@ -358,6 +366,17 @@ describe('assertWriteAllowed broadened write forms (#1333)', () => {).toThrow(/Refusing to mutate/i);});+ it('keeps an apostrophe inside a comment from hiding the write on the next line', () => {+ armProd();+ // Regression: a two-pass "mask strings then strip comments" normalizer let the+ // apostrophe in `don't` (inside the comment) pair with the `'x'` literal of the+ // DELETE, masking the DELETE away — a false-negative. The single-pass alternation+ // consumes the comment first, so the apostrophe stays inside it.+ expect(() =>+ assertWriteAllowed("-- don't touch prod\nDELETE FROM universes WHERE name = 'x'"),+ ).toThrow(/Refusing to mutate/i);+ });+it('does not false-positive on a write phrase that only appears inside a string literal', () => {armProd();expect(() =>diff --git a/server/lib/db.js b/server/lib/db.jsindex cad00b687f..62ab1b01d0 100644--- a/server/lib/db.js+++ b/server/lib/db.js@@ -93,22 +93,24 @@ export function isTestRunner() {// matches a write verb ANYWHERE, after stripping comments so a keyword named in// a comment can't trip it (and a write after a comment can't slip past).//-// Normalize the SQL before keyword-matching, in two passes whose order matters:-// 1. Blank out single-quoted string literals (`''` is Postgres's escaped quote,-// consumed by the alternation). This must come FIRST so a literal can neither-// HIDE a real write — `SELECT '/*'; DELETE …; SELECT '*/'` would otherwise have-// its quote-embedded `/*…*/` stripped as a comment, taking the DELETE with it —-// nor MASQUERADE as one (`SELECT 'UPDATE x SET y' AS note` is a read).-// 2. Strip line/block comments, so a verb named only in a comment (`-- DELETE FROM x`-// in a header) doesn't false-positive a harmless SELECT, and a write hidden after-// a non-leading comment is still caught.+// Normalize the SQL before keyword-matching in a SINGLE left-to-right pass that+// alternates over comments AND string literals, so whichever delimiter appears+// FIRST consumes its own span. Order matters and a two-pass "mask strings, then+// strip comments" is WRONG: an apostrophe inside a comment (`-- don't touch …`)+// would open a spurious string mask that swallows a real write on the next line+// (a false-negative — the exact failure this guard exists to prevent). Processing+// left-to-right keeps a comment's apostrophe part of the comment, and keeps a+// `/*` or `--` inside a string literal from starting a comment. Comments collapse+// to a space; string literals collapse to `''` (Postgres's escaped-quote form),+// so a write verb appearing only inside a literal can't trip the guard, and a+// quote-embedded comment delimiter can't hide a write between two literals.// Dollar-quoted bodies (`$$…$$`, used in function definitions) are not unwrapped —// stores don't send them, so this test-only backstop accepts that edge.function normalizeSqlForMatch(text) {- return text- .replace(/'(?:''|[^'])*'/g, "''")- .replace(/--[^\n]*/g, ' ')- .replace(/\/\*[\s\S]*?\*\//g, ' ');+ return text.replace(+ /--[^\n]*|\/\*[\s\S]*?\*\/|'(?:''|[^'])*'/g,+ (m) => (m[0] === "'" ? "''" : ' '),+ );}// CREATE TABLE … AS SELECT (CTAS) and the broader DDL family (CREATE/ALTER/DROP)@@ -132,10 +134,13 @@ const ROW_WRITE_PATTERNS = [// (a read followed by a session command) doesn't read as an UPDATE write./\bUPDATE\b\s+[^;]+?\bSET\b/i,// COPY <table> FROM — an import writes rows. `COPY (query) TO` / `COPY <table>- // TO` is an export (read): the `(?!\()` rejects the subquery-export form whose- // inner FROM would otherwise match, and requiring FROM before the next `;`- // leaves `COPY <table> TO …` (no FROM) alone.- /\bCOPY\s+(?!\()[^;]*?\bFROM\b/i,+ // TO` is an export (read): requiring the first non-blank token after COPY to be+ // a non-`(` char (`[^\s(]`) rejects the subquery-export form whose inner FROM+ // would otherwise match, and requiring FROM before the next `;` leaves `COPY+ // <table> TO …` (no FROM) alone. `[^\s(]` (not a `(?!\()` lookahead) is load-+ // bearing: a lookahead lets `\s+` backtrack and pass the assertion mid-whitespace+ // on `COPY (SELECT … FROM …) TO`, so the export would false-positive.+ /\bCOPY\s+[^\s(][^;]*?\bFROM\b/i,];// True when the SQL performs a row write in any of the recognized forms.