Repository navigation
fix(deparser): quote access method names - #382
Conversation
IndexStmt, CreateStmt, IntoClause, exclusion constraints and the operator class/family forms wrote the access method after USING verbatim, so a hand-built AST could put any SQL there. It is an identifier: quote it.
|
I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".
|
|
Review complete. 🟠 1 high · 🟡 2 medium 💬 Inline comments (2)
📍 Findings outside the diff (1) — 🟠 1 high — defects on lines GitHub can't attach comments to🟠 High — Quote // packages/deparser/src/deparser.ts
9398 output.push(node.amname);The patch quotes access methods at nine sites but leaves This PR closes an injection gap in the deparser by wrapping access-method names in
Three verified findings remain: unquoted Reviewed commit: e893f3a |
There was a problem hiding this comment.
Quotes access-method names in deparser USING clauses across nine emit sites, adding tests for injected and quoted AM names — but leaves three operator-class/family AM sites unquoted and one adjacent identifier raw.
Key findings
- 🟠 Quote
amnamein operator class/family statements — deparser.ts:9398 - 🟡 Quote
classNamealongsideaccessMethodin COMMENT handler — deparser.ts:7140 - 🟡 Handle schema-qualified opclass rename/owner lists — deparser.ts:8536
| const className = objectParts[objectParts.length - 1]; | ||
| const accessMethod = objectParts[objectParts.length - 2]; | ||
| output.push(`${className} USING ${accessMethod}`); | ||
| output.push(`${className} USING ${QuoteUtils.quoteIdentifier(accessMethod)}`); |
There was a problem hiding this comment.
🟡 security · medium
Quote className alongside accessMethod in COMMENT handler
The opclass/family COMMENT branch quotes accessMethod but interpolates className raw into the emitted SQL (packages/deparser/src/deparser.ts:7140). className is a raw identifier from objectParts, so a value needing quoting (uppercase, spaces, operators) breaks out of the identifier context in the deparsed COMMENT ON OPERATOR CLASS ... USING ... statement — the same injection class this PR fixes for access methods, and inconsistent with every sibling opclass handler which quotes both parts.
📋 Prompt for AI Agents
In packages/deparser/src/deparser.ts at line 7140, wrap className in QuoteUtils.quoteIdentifier(...) in the template string, matching how accessMethod is already quoted on the same line. Add a round-trip test with a class name needing quoting to packages/deparser/tests/misc/access-method-quoting.test.ts.
| if (items.length === 2) { | ||
| const accessMethod = items[0].String?.sval || ''; | ||
| const objectName = items[1].String?.sval || ''; | ||
| output.push(`${QuoteUtils.quoteIdentifier(objectName)} USING ${accessMethod}`); | ||
| output.push(`${QuoteUtils.quoteIdentifier(objectName)} USING ${QuoteUtils.quoteIdentifier(accessMethod)}`); | ||
| } else { | ||
| output.push(this.visit(node.object, context)); | ||
| } |
There was a problem hiding this comment.
🟡 bug · medium
Handle schema-qualified opclass rename/owner lists
RenameStmt (packages/deparser/src/deparser.ts:8536) and AlterOwnerStmt (line 8611) special-case operator class/family objects only when the object List has exactly 2 items; a 3-item schema-qualified list (accessMethod, schema, name) falls through to the generic List visit, which emits the parts without the required USING access_method suffix. ALTER OPERATOR FAMILY s.f USING btree RENAME TO g is valid PostgreSQL and deparses to SQL that does not reparse. The sibling DropStmt path (line 4748) handles the 3-item shape, so coverage is inconsistent across the same statement family.
📋 Prompt for AI Agents
In packages/deparser/src/deparser.ts, extend the OPFAMILY/OPCLASS handling in RenameStmt (lines 8534-8542) and AlterOwnerStmt (lines 8609-8617) to handle items.length === 3 by emitting QuoteUtils.quoteQualifiedIdentifier(items[1].String.sval, items[2].String.sval) USING QuoteUtils.quoteIdentifier(items[0].String.sval), matching the existing pattern at line 4748 in DropStmt. Add round-trip tests for the schema-qualified RENAME and OWNER TO forms in packages/deparser/tests/misc/access-method-quoting.test.ts.
The name parts are already quoted, so quoting the method again doubled the quotes; and a schema-qualified name took the schema for the method.
There was a problem hiding this comment.
Devin Review found 1 finding. Devin automatically fixed it.
Fixed by Devin (1)
- Double-quoted access methods break comments — objectParts are already quoted; removed the second quoting and added round-trip tests for COMMENT ON OPERATOR CLASS/FAMILY with a quoted method.
Summary
The deparser wrote an access method after
USINGverbatim, so a hand-built AST could put arbitrary SQL there:An access method is an identifier, so every site that holds the raw name now goes through
QuoteUtils.quoteIdentifier:IndexStmt.accessMethod,CreateStmt.accessMethod,IntoClause.accessMethod, exclusionConstraint.access_method, and the… USING <am>forms for operator classes/families. Output for anything a parser can produce is unchanged (plain names such asgin,gist,hnsw,ivfflat,bm25are not quoted); the existing deparser suites pass with no snapshot change.Also fixed while testing quoted methods:
COMMENT ON OPERATOR CLASS/FAMILY s.f USING amtook the schema for the access method and dropped the real one (f USING s). Its name parts are already quoted by theStringvisitor, so that site is not quoted again.Context: constructive-io/constructive-planning#2546. The path that executes in constructive-db is its in-database PL/pgSQL deparser, fixed separately there; this is the same hardening for the TypeScript deparser.
Tests are in
__tests__/misc/access-method-quoting.test.ts(hand-built ASTs, because parsed SQL can never yield these values, plus round-trips of the extension methods and quoted names).Link to Devin session: https://app.devin.ai/sessions/00072bdfa0ce4a4abb209b52b9e9f50c
Open in Devin Desktop: https://app.devin.ai/desktop/session/00072bdfa0ce4a4abb209b52b9e9f50c?variant=devin
Requested by: @pyramation
🟢 Devin fixed the finding · Follow Devin's work