Repository navigation
fix(db): reject unsafe CREATE DATABASE identifiers - #335
Conversation
BaaRai
left a comment
There was a problem hiding this comment.
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.
597c869 to
32d2107
Compare
|
Applied the compatibility fix directly, pushed as baf44b9.
|
BaaRai
left a comment
There was a problem hiding this comment.
The pgx.Identifier fix preserves existing names and the test covers the quoting. Approving.
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.
baf44b9 to
8110735
Compare
|
wehhh |
ensureDatabasequoted the DSN path intoCREATE DATABASEwith 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 TestValidPostgresDBNamepass.