-
Notifications
You must be signed in to change notification settings - Fork 339
Strip psql meta-commands on load #736
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
PostgreSQL 15.14+/16.10+/17.6+ pg_dump adds \restrict and \unrestrict psql meta-commands to schema dumps as a security measure (CVE-2025-8714). These commands cannot be executed directly against the PostgreSQL server via sqlDB.Exec(), causing 'syntax error at or near "\"' when running dbmate load. Add StripPsqlMetaCommands() function to remove all psql meta-commands (lines starting with backslash) before executing the schema SQL. Fixes #735 Co-authored-by: adrian <adrian@foxglove.dev>
|
Cursor Agent can help with this pull request. Just |
|
@stevenringo want to test this branch? |
|
Works for me. Before: root@4efda5872260:/src/testdata# ../dist/dbmate -e POSTGRES_TEST_URL load
Reading: ./db/schema.sql
Error: pq: syntax error at or near "\"After: root@4efda5872260:/src/testdata# ../dist/dbmate -e POSTGRES_TEST_URL load
Reading: ./db/schema.sql |
dossy
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is fine, although it might be useful to disclaim that this dbutil.StripPsqlMetaCommands() is a very naive implementation, because there are certainly valid SQL inputs to psql that it will get wrong.
# dist/dbmate -e POSTGRES_TEST_URL create
Creating: dbmate_test
# cat >test.sql <<-'EOT'
> create table t (x text);
> insert into t (x) values ('postgres understands
> \how to parse this');
> EOT
# cat test.sql
create table t (x text);
insert into t (x) values ('postgres understands
\how to parse this');
# psql $POSTGRES_TEST_URL < test.sql
CREATE TABLE
INSERT 0 1
# psql $POSTGRES_TEST_URL -c 'select * from t'
x
----------------------
postgres understands+
\how to parse this
(1 row)
# dist/dbmate -e POSTGRES_TEST_URL drop
Dropping: dbmate_test
# dist/dbmate -e POSTGRES_TEST_URL create
Creating: dbmate_test
# dist/dbmate -e POSTGRES_TEST_URL --schema-file test.sql load
Reading: test.sql
Error: pq: unterminated quoted string at or near "'postgres understands
"As long as the expectation is that dbmate load is only used to execute DDL statements which are highly unlikely to have embedded newlines in string values, then we're fine.
To illustrate what I mean:
# cat >test2.sql <<-'EOT'
> create table bozo (x text default 'only a bozo
> \would do this');
> EOT
# cat test2.sql
create table bozo (x text default 'only a bozo
\would do this');
# dist/dbmate -e POSTGRES_TEST_URL drop
Dropping: dbmate_test
# dist/dbmate -e POSTGRES_TEST_URL create
Creating: dbmate_test
# psql $POSTGRES_TEST_URL < test2.sql
CREATE TABLE
# psql $POSTGRES_TEST_URL -c '\dS bozo'
Table "public.bozo"
Column | Type | Collation | Nullable | Default
--------+------+-----------+----------+-----------------------
x | text | | | 'only a bozo +
| | | | \would do this'::text
# dist/dbmate -e POSTGRES_TEST_URL drop
Dropping: dbmate_test
# dist/dbmate -e POSTGRES_TEST_URL create
Creating: dbmate_test
# dist/dbmate -e POSTGRES_TEST_URL --schema-file test2.sql load
Reading: test2.sql
Error: pq: unterminated quoted string at or near "'only a bozo
"Such an unlikely edge case. If someone actually gets bitten by this, we can all point back at this comment and laugh about it.
|
I still don't really understand how the implementation mitigates anything. And also, shouldn't the underlying Go library be handling this? (sorry, not a Go developer) |
Add comments clarifying that this is a naive line-based implementation that works for pg_dump output but could theoretically break on hand-crafted SQL with multi-line strings containing backslashes at line start. Co-authored-by: adrian <adrian@foxglove.dev>
|
@dossy thanks, I added some more comments to document those limitations |
Strip psql meta-commands from schema files to fix
dbmate loadfailures on newer PostgreSQL versions.PostgreSQL 15.14+/16.10+/17.6+
pg_dumpoutputs\restrictand\unrestrictcommands as a security measure (CVE-2025-8714). These arepsqlclient meta-commands, not SQL statements, causingsqlDB.Exec()to fail with a syntax error whendbmate loadattempts to execute them directly against the database server. This PR adds a filter to remove these lines before execution.Fixes #735
Note
Ensures
dbmate loadignorespsqlmeta-commands produced by newerpg_dumpso schema loads don’t fail.dbutil.StripPsqlMetaCommandsto remove lines starting with\(e.g.,\restrict,\unrestrict) from SQL inputDB.LoadSchema()before executingschema.sqlWritten by Cursor Bugbot for commit ec8d54c. This will update automatically on new commits. Configure here.