Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wO3lc-001HZv-2G for pgsql-bugs@arkaria.postgresql.org; Sat, 16 May 2026 01:24:36 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wO3lZ-0028dx-2w for pgsql-bugs@arkaria.postgresql.org; Sat, 16 May 2026 01:24:33 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wO3lZ-0028dp-1s for pgsql-bugs@lists.postgresql.org; Sat, 16 May 2026 01:24:33 +0000 Received: from mail-australiaeastazolkn19011028.outbound.protection.outlook.com ([52.103.72.28] helo=SY2PR01CU004.outbound.protection.outlook.com) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1wO3lX-00000000qJZ-0s1J for pgsql-bugs@lists.postgresql.org; Sat, 16 May 2026 01:24:33 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=YHuy23bdx5SsLzT6VnL4o3pUxEeZDJCFlAEcW8nPNPOkwHLolBkOp9U+X76to3IBM+Qy+uijr/TCoHQaL8P1BLZaHz0PBvDdMZ4YAKmwRLlqI3geNbOeODFK3/CRySTkXPwQHczLfMzOdEtcVpLJF4Ky4HQvSnXbZCc3GHZc+pttUZtArK9ZmuVK8fYMFzE4QBqS1+rbJd1402Ttmtn1nGKGxeHriPv3RewX00wliXFIGsh68zkgeDAZhtv9uqzfl9fAlw3cO0chBaCdh5XfcN3kMeTrrNuXVJPhP8rIt4q8LywGPa7QudesyRVeegGH9E8DjdhKZOkn1dpHqo83CA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=DuaH/q72uH5w9vm5qdeD3FQQiKG3Jm2peGCwovuKcXA=; b=Rvsk0LbO9patZK0k6ia6Nd4tvYIrsRLKD7qVyIrThYtMNR1uIKtRSmiyoMfTIt0cCUpgp2ehPV02cHW4aFbMdBzOr2JvtRsRTIJXjc8D9zzfjCmL0m7bvWLmHt2fk7UpwQhtlzH+D/amgCD1AkqrMJy89+Ucvh6p80OqISonCtzps71vJjlrd737VIeunGgNd25pCNfxlXZojfXSLAeweYQHlw+cJ40kf16TLCLWVsdLjAInwJ4v1uEG1IWLbu3BUIuulv/q12WvW+U2gB/aPxMtFLEaTnC4B1DRv19sDZOSepGwYCqKwxSbJkz1qNKvHSvNlYAvtwggwKYNsLIEFw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=hotmail.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=DuaH/q72uH5w9vm5qdeD3FQQiKG3Jm2peGCwovuKcXA=; b=PFCqn2haisiUW2elBfaDJWRCpA0n8loQtGSc1FALjuj2dovH2Dyv0HDM5Mtsdkr4oNu8AFWiQP4DdjE96mTCKuF7kZ2CnOF7JJwAavV4iTJfAzk7dvS7/0u09S/S5/fW2qWCIMItcSIniwHLAKLTZNp6AF0Yg2O+gcXYbxnmN1LfrHAfTH2kgJpy6+QvajgcMpDMp/w3BwzEMW7xRSI3J5Z0ZHnyKlhNPT9d5hM1gYnWOqy4s6CkV/dCXfewNH2M46hE9IozMidDOHqnsJZK811PHTp3kGGK9GGS/EuTPigVQwS287Ts8LigyTE7Iz5BUudevt63l/gwmXJBw9WPtg== Received: from SY7PR01MB10921.ausprd01.prod.outlook.com (2603:10c6:10:334::16) by SY9PR01MB10818.ausprd01.prod.outlook.com (2603:10c6:10:321::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.25.21; Sat, 16 May 2026 01:24:21 +0000 Received: from SY7PR01MB10921.ausprd01.prod.outlook.com ([fe80::7908:e00:4ab1:d120]) by SY7PR01MB10921.ausprd01.prod.outlook.com ([fe80::7908:e00:4ab1:d120%5]) with mapi id 15.21.0025.020; Sat, 16 May 2026 01:24:21 +0000 From: Japin Li To: pgsql-bugs@lists.postgresql.org Cc: zengman@halodbtech.com Subject: Re: BUG #19478: `dblink_close` can be used for injection. In-Reply-To: <19478-37289e8b0d1a1299@postgresql.org> (PG Bug reporting form's message of "Fri, 15 May 2026 01:29:54 +0000") References: <19478-37289e8b0d1a1299@postgresql.org> User-Agent: mu4e 1.12.12; emacs 29.3 Date: Sat, 16 May 2026 09:24:15 +0800 Message-ID: Content-Type: text/plain X-ClientProxiedBy: TY4P286CA0122.JPNP286.PROD.OUTLOOK.COM (2603:1096:405:37c::7) To SY7PR01MB10921.ausprd01.prod.outlook.com (2603:10c6:10:334::16) X-Microsoft-Original-Message-ID: <87v7codu4g.fsf@hotmail.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SY7PR01MB10921:EE_|SY9PR01MB10818:EE_ X-MS-Office365-Filtering-Correlation-Id: 96ae05ed-9cc9-488b-02b2-08deb2e9d9fa X-Microsoft-Antispam: BCL:0;ARA:14566002|41001999006|5072599009|22091999003|24121999003|55001999006|8060799015|19110799012|23021999003|8022599003|24021099003|15080799012|30101999006|1602099012|10035399007|26121999007|4302099013|3412199025|440099028|12091999003|20061999003; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?5qX44DULcFyM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Q?5kttkRoTy5xfgytKWMMc4AC3IK0m1dpE9aKKSc82xQKJjAp5dHwTcs8f9tTc?= =?us-ascii?Q?7UdRUBsD4mmFnOOfciKGBFXkOYZ+2+/3xbLokgOxGq8KhM1W2cc6ZCL3PTmY?= =?us-ascii?Q?yMRkXSufbj+bRO3eXrkPW2j4xrQFzibHFHTeI3wlbRbnnh7LF+SNdLJOGMgJ?= =?us-ascii?Q?Nvr9pak2LpNLSmWOPoJVDtwf7SR0GrqPjF4tonvQqJyKkFnlXBSw/ZfiCXGU?= =?us-ascii?Q?Xid2YtCMl/iRuP8Ypmk22KPIB7utDeqviuNX/4njBb7mGT54F5qsRVpupQRf?= =?us-ascii?Q?to/Iw8I1rZ8S67Y2uaHmqS2o4t1fK4Zza7cXnWF4Abdtu8iFSAHb9QvisPhH?= =?us-ascii?Q?wvilC7QuN6CGSpR2iu+v?= X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?1TqHhUcWFgPKxw7P0uHlsoBvpWh2jFW/j2mYdSSmDiXIOAN18d/qIwi6iHOo?= =?us-ascii?Q?VrcBnVecsshrLqH7rU726RdggaNWuvL1xnQr2vrhXavvO6hVOocAGiEUgn2C?= =?us-ascii?Q?jeGAXzI1sREnbAYM52mC0OKwdqoudy1Vv+PwlQq0aCI8sPKFsj77iNCwSOkM?= =?us-ascii?Q?5+HqqYMIdpFlfnK4Lla5d06wIaCNedPdJgwUBZ9vg2qbeDvQwd3e6+Pygjql?= =?us-ascii?Q?B10/7l/4UQOjeyuj9dR1xgBGqt8k+vK4EMF0e3X3dvbTFzOconOYSoB9ptN7?= =?us-ascii?Q?aMIyP0k+VEtS/NWw2dGjqBKUZTup7Iz/9pC98oI8nhUVUX/wMXM4Kx/iElA3?= =?us-ascii?Q?4pbTMQtiJp91Gy/pIrIrC6uQwaiUTK1xce8EUliJGPbLSWerUtYGRwYr1ckL?= =?us-ascii?Q?CniutZAuIxKWYKc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Ze32f9z?= =?us-ascii?Q?IHsw7UUOivzzzCyBUKoq3KSvQckrn6+L7nVNd9dH4SNtzHviIy8rZMdeoKEU?= =?us-ascii?Q?4JMKqPLbEAR+21Z/pgZpDDyHVwVm?= X-OriginatorOrg: sct-15-20-9412-4-msonline-outlook-feddd.templateTenant X-MS-Exchange-CrossTenant-Network-Message-Id: 96ae05ed-9cc9-488b-02b2-08deb2e9d9fa X-MS-Exchange-CrossTenant-AuthSource: SY7PR01MB10921.ausprd01.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 May 2026 01:24:21.2648 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: SY9PR01MB10818 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Fri, 15 May 2026 at 01:29, PG Bug reporting form wrote: > The following bug has been logged on the website: > > Bug reference: 19478 > Logged by: Man Zeng > Email address: zengman@halodbtech.com > PostgreSQL version: 18.4 > Operating system: 24.04.1-Ubuntu > Description: > > Hi all, > > I think we can impose stricter restrictions on the parameters of > `dblink_close`. > For example, when calling `dblink_close`, certain operations can be achieved > through SQL concatenation, > which I believe is unexpected behavior. > > ```sql > postgres@zxm-VMware-Virtual-Platform:~/Z-Xiao-M$ psql > psql (19devel) > Type "help" for help. > > postgres=# \c test > You are now connected to database "test" as user "postgres". > test=# CREATE EXTENSION IF NOT EXISTS dblink; > CREATE EXTENSION > test=# SELECT dblink_connect('c', 'dbname=' || current_database()); > dblink_connect > ---------------- > OK > (1 row) > > test=# SELECT dblink_open('c', 'cur', 'SELECT 1'); > dblink_open > ------------- > OK > (1 row) > > test=# -- CLOSE: CREATE TABLE > test=# SELECT dblink_close('c', 'cur; CREATE TABLE hacked(id int); --'); > dblink_close > -------------- > OK > (1 row) > > test=# \d+ hacked > Table "public.hacked" > Column | Type | Collation | Nullable | Default | Storage | Compression | > Stats target | Description > --------+---------+-----------+----------+---------+---------+-------------+--------------+------------- > id | integer | | | | plain | | > | > Access method: heap > > test=# SELECT dblink_disconnect('c'); > dblink_disconnect > ------------------- > OK > (1 row) > > test=# SELECT dblink_connect('c', 'dbname=' || current_database()); > dblink_connect > ---------------- > OK > (1 row) > > test=# SELECT dblink_open('c', 'cur', 'SELECT 1'); > dblink_open > ------------- > OK > (1 row) > > test=# -- CLOSE: DROP TABLE > test=# SELECT dblink_close('c', 'cur; DROP TABLE hacked; --'); > dblink_close > -------------- > OK > (1 row) > > test=# \d+ hacked > Did not find any relation named "hacked". > test=# > ``` > > This is my SQL for reproducing the problem. > ```sql > CREATE EXTENSION IF NOT EXISTS dblink; > > SELECT dblink_connect('c', 'dbname=' || current_database()); > SELECT dblink_open('c', 'cur', 'SELECT 1'); > > -- CLOSE: CREATE TABLE > SELECT dblink_close('c', 'cur; CREATE TABLE hacked(id int); --'); > > SELECT dblink_disconnect('c'); > \d+ hacked > > SELECT dblink_connect('c', 'dbname=' || current_database()); > SELECT dblink_open('c', 'cur', 'SELECT 1'); > > -- CLOSE: DROP TABLE > SELECT dblink_close('c', 'cur; DROP TABLE hacked; --'); > > \d+ hacked > SELECT dblink_disconnect('c'); > ``` > > The solution to this problem is also very simple. > ``` > postgres@zxm-VMware-Virtual-Platform:~/code/postgres/contrib$ git diff > diff --git a/contrib/dblink/dblink.c b/contrib/dblink/dblink.c > index 9798cb535bc..0a9334aa160 100644 > --- a/contrib/dblink/dblink.c > +++ b/contrib/dblink/dblink.c > @@ -543,7 +543,7 @@ dblink_close(PG_FUNCTION_ARGS) > > conn = rconn->conn; > > - appendStringInfo(&buf, "CLOSE %s", curname); > + appendStringInfo(&buf, "CLOSE %s", quote_ident_cstr(curname)); > > /* close the cursor */ > res = libpqsrv_exec(conn, buf.data, dblink_we_get_result); > ``` > > This is the feedback from the security team. > ``` > Thanks for your report. We consider dblink_close() to be caller-trusted, > and thus this is not considered a security vulnerability. Feel free to > resubmit to pgsql-bugs@lists.postgresql.org. > ``` > > Any thought? According to the documentation [1], it should be a cursor name. Wrapping it in quotes can prevent attacks like SQL injection. I think your modification is correct, and we should add test cases for it. [1] https://www.postgresql.org/docs/current/contrib-dblink-close.html > -- > regards, > Man Zeng -- Regards, Japin Li ChengDu WenWu Information Technology Co., Ltd.