Re: How to use \restrict or \unrestirct in python

From: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>
To: ma lz <ma100(at)hotmail(dot)com>, "pgsql-general(at)lists(dot)postgresql(dot)org" <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: How to use \restrict or \unrestirct in python
Date: 2025-09-15 14:17:50
Message-ID: cedab53f-e2c2-4c59-a7ba-279dc0c02034@aklaver.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 9/15/25 00:16, ma lz wrote:
> To fix CVE-2025-8714, PG introduce \restrict , but if we use python
> ( like psycopg2), it does not support slash command.

The backslash commands are specific to psql:

https://www.postgresql.org/docs/current/app-psql.html

Per the release notes:

https://www.postgresql.org/docs/current/release-17-6.html

"
Since dump/restore operations typically involve running SQL commands as
superuser, the target database installation must trust the source
server. However, it does not follow that the operating system user who
executes psql to perform the restore should have to trust the source
server. The risk here is that an attacker who has gained superuser-level
control over the source server might be able to cause it to emit text
that would be interpreted as psql meta-commands. That would provide
shell-level access to the restoring user's own account, independently of
access to the target database.

To provide a positive guarantee that this can't happen, extend psql with
a \restrict command that prevents execution of further meta-commands,
and teach pg_dump to issue that before any data coming from the source
server.
"

psycopg2/psycopg uses the same underlying library, libpq, as psql but
psycopg does not support backslash commands. Therefore is cannot execute
them directly in the manner explained above. Python in general can
execute them indirectly by using something like:

subprocess.check_output()

to execute psql -f some_text_dump_file. It would be up to you to verify
what is in the the dump file.

--
Adrian Klaver
adrian(dot)klaver(at)aklaver(dot)com

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Michael Paquier 2025-09-15 23:59:15 Re: Latest patches break one of our unit-test, related to RLS
Previous Message David G. Johnston 2025-09-15 12:32:19 Re: How to use \restrict or \unrestirct in python