-
Notifications
You must be signed in to change notification settings - Fork 4
Handle pg_dump restrict commands in local restore #611
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,12 @@ | |
| re.compile(r"^\s*REVOKE\b", re.IGNORECASE), | ||
| re.compile(r"^\s*ALTER\s+DEFAULT\s+PRIVILEGES\b", re.IGNORECASE), | ||
| ) | ||
| PSQL_META_COMMAND_PATTERNS = ( | ||
| # Newer pg_dump versions emit these psql-only commands for safer restores. | ||
| # Older local psql clients reject them, so drop them from staged restores. | ||
| re.compile(r"^\s*\\restrict\b", re.IGNORECASE), | ||
| re.compile(r"^\s*\\unrestrict\b", re.IGNORECASE), | ||
| ) | ||
|
|
||
|
|
||
| class LocalDbRestoreError(RuntimeError): | ||
|
|
@@ -81,9 +87,13 @@ def _sanitize_sql_dump(source_path: Path, target_path: Path) -> None: | |
| with open(source_path, "r", encoding="utf-8") as infile: | ||
| with open(target_path, "w", encoding="utf-8") as outfile: | ||
| for line in infile: | ||
| if any( | ||
| matches_role_sql = any( | ||
| pattern.search(line) for pattern in ROLE_DEPENDENT_SQL_PATTERNS | ||
| ): | ||
| ) | ||
| matches_psql_meta = any( | ||
| pattern.search(line) for pattern in PSQL_META_COMMAND_PATTERNS | ||
| ) | ||
| if matches_role_sql or matches_psql_meta: | ||
| continue | ||
| outfile.write(line) | ||
| except UnicodeError as exc: | ||
|
|
@@ -235,6 +245,7 @@ def restore_local_db_from_sql( | |
| ) from exc | ||
|
|
||
| return LocalDbRestoreResult( | ||
| sql_file=staged_sql_file, | ||
|
Comment on lines
247
to
+248
Comment on lines
247
to
+248
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Useful? React with 👍 / 👎. |
||
| source=source_description, | ||
| host=host, | ||
| port=port, | ||
|
|
||
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.
\restrictsafety guardPostgreSQL 17.6 added
\restrictspecifically to block injected psql meta-commands during plain-text restores (see the 17.6 release notes for CVE-2025-8714, andapp-psql's note that restricted mode only allows\unrestrict). Because this sanitizer removes those lines and then still feeds the file topsql -f, any dump restored from GCS or another compromised source can once again execute meta-commands such as\!on the operator's machine. For older local clients we should fail fast or require a newer psql, not silently discard the protection.Useful? React with 👍 / 👎.