Skip to content

ext/pgsql: escape table name, delimiter, null marker in pg_copy_from/to#56

Closed
iliaal wants to merge 1 commit intomasterfrom
staging/pgsql-copy-escape-v2
Closed

ext/pgsql: escape table name, delimiter, null marker in pg_copy_from/to#56
iliaal wants to merge 1 commit intomasterfrom
staging/pgsql-copy-escape-v2

Conversation

@iliaal
Copy link
Copy Markdown
Owner

@iliaal iliaal commented May 9, 2026

Staging PR for php#21985 v2. Picks up devnexen review feedback on the original PR.

null_as was embedded raw inside E'%s', so a caller string could close the literal and run side DDL. Routed through PQescapeLiteral, same for the delimiter.

pg_copy_to had the same identifier hole pre-PR. Bare table names go through build_tablename; the parenthesised (query) form documented in bug 73498 still passes through unchanged.

Tests cover the null_as injection and the pg_copy_to table-name injection. 06_bug73498.phpt confirms the (query) BC is intact.

The COPY query embedded the table_name argument with a raw "%s" and the
delimiter and null marker inside literal E'..' wrappers, so caller-supplied
strings could break out and run side queries. Route bare table names
through build_tablename (the same helper pg_insert/update/select/delete
have used since bug #62978), and pass the delimiter and null marker
through PQescapeLiteral. pg_copy_to keeps the parenthesised (query)
source form documented in bug 73498, but wraps it in an extra paren pair
so a string like (SELECT 1); DROP TABLE x; -- becomes a syntax error
inside the outer parens instead of escaping out into a second statement.

Closes phpGH-21985
@iliaal iliaal force-pushed the staging/pgsql-copy-escape-v2 branch from 0b0a808 to bc6f630 Compare May 9, 2026 12:22
@iliaal
Copy link
Copy Markdown
Owner Author

iliaal commented May 9, 2026

Verified on php#21985.

@iliaal iliaal closed this May 9, 2026
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.

1 participant