Skip to content

fix(db): reject unsafe CREATE DATABASE identifiers - #335

Merged
BaaRai merged 2 commits into
BaaRai:mainfrom
Coff0xc:fix/createdb-ident
Nov 18, 2026
Merged

BaaRai merged 2 commits into
BaaRai:mainfrom
Coff0xc:fix/createdb-ident

Conversation

@Coff0xc

@Coff0xc Coff0xc commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

ensureDatabase quoted the DSN path into CREATE DATABASE with no check. A quote in the name could close the identifier and run extra SQL on the maintenance connection.

The name is now rejected unless it is a safe PostgreSQL identifier (ASCII letters, digits and underscore, not starting with a digit, at most 63 bytes). The statement still quotes the name so mixed-case names keep their case.

Tested: go test ./db -run TestValidPostgresDBName pass.

Copilot AI balanced review requested due to automatic review settings November 18, 2026 11:51

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@BaaRai BaaRai left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One blocking compatibility issue: validPostgresDBName runs before the database existence check, so an existing install whose database name contains a hyphen, a dot or non-ASCII characters refuses to start after upgrade, even though no CREATE DATABASE runs. Move the validation inside the !exists branch, just before the CREATE. The strict ASCII rule is fine for a name we create; if you want hyphens and Unicode to keep working for new databases too, rejecting " and NUL plus the 63-byte limit is enough because the name is already quoted. Add a case that opens an existing hyphenated name.

The injection itself is real: the name was embedded in CREATE DATABASE "..." with no escaping.

@BaaRai

BaaRai commented Nov 18, 2026

Copy link
Copy Markdown
Owner

Applied the compatibility fix directly, pushed as baf44b9.

validPostgresDBName ran before the existence check, so an existing install whose database name had a hyphen, a dot or non-ASCII characters refused to start even though no CREATE DATABASE would run. The allowlist is replaced with pgx.Identifier{dbName}.Sanitize(): it escapes any name, so the injection stays closed and no name PostgreSQL accepts is refused. The test now asserts the quoting (artex-db, café, an embedded quote) instead of rejecting those names.

@BaaRai BaaRai left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pgx.Identifier fix preserves existing names and the test covers the quoting. Approving.

Coff0xc and others added 2 commits November 18, 2026 22:43
ensureDatabase quoted the DSN path into CREATE DATABASE with no
check. A quote in the name could close the identifier. The name is
now restricted to the unquoted-identifier alphabet before quoting.
The allowlist rejected names PostgreSQL accepts, so an existing install
whose database name had a hyphen, a dot or non-ASCII characters refused
to start. pgx.Identifier escapes any name, which closes the injection
without refusing a name that already exists.
@BaaRai
BaaRai force-pushed the fix/createdb-ident branch from baf44b9 to 8110735 Compare November 18, 2026 21:43
@BaaRai
BaaRai merged commit 65045ef into BaaRai:main Nov 18, 2026
5 checks passed
@ktaguba-crypto

Copy link
Copy Markdown

wehhh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants