fix(security): escape SQL identifiers in migrate.ts and searchDocs.ts#104
Open
xiaolai wants to merge 1 commit into
Open
fix(security): escape SQL identifiers in migrate.ts and searchDocs.ts#104xiaolai wants to merge 1 commit into
xiaolai wants to merge 1 commit into
Conversation
DB_SCHEMA (migrate.ts) and schema/entityPrefix (searchDocs.ts) were interpolated directly into SQL as unquoted identifiers. Use client.escapeIdentifier() in migrate.ts and a local escId() helper in searchDocs.ts so names containing SQL metacharacters cannot break queries. Co-Authored-By: Claude Code <noreply@anthropic.com>
This was referenced Apr 20, 2026
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
DB_SCHEMA(sourced fromprocess.env.DB_SCHEMA) andschema/entityPrefix(insearchDocs.ts) were interpolated directly into SQL template literals as unquoted identifiers. If a schema name contains double-quote characters or other SQL metacharacters, the generated SQL can break or behave unexpectedly.Affected lines (before this PR):
src/migrate.tslines 37, 42, 51:CREATE SCHEMA IF NOT EXISTS ${schema},CREATE TABLE IF NOT EXISTS ${schema}.migrations,SELECT set FROM ${schema}.migrationssrc/apis/searchDocs.tslines 149–155:FROM ${schema}.${entityPrefix}_chunks,JOIN ${schema}.${entityPrefix}_pagesFix
src/migrate.ts: Callclient.escapeIdentifier(schema)once at the top of theloadmethod and use the result throughout (thepgClientclass provides this method).src/apis/searchDocs.ts: Add a minimal inline helperescId = (name) => '"' + name.replace(/"/g, '""') + '"'(the standard SQL identifier quoting rule) and use it for bothschemaandentityPrefixin theFROM/JOINclauses.entityPrefixis already constrained to an enum-derived set of safe values, so the practical injection risk is low — but quoting both identifiers consistently is good defence-in-depth.Why it matters
The
DB_SCHEMAvalue is operator-supplied configuration. A schema name likepublic" CASCADE; DROP TABLE migrations; --would produce malformed SQL without quoting. The fix is a one-liner using the standard pg escaping API.