Skip to content

Commit b5c287e

Browse files
SakiTakamachidevnexen
authored andcommitted
Fix GH-12423: Changed to prioritize DSN authentication information over arguments.
Added connection test Close GH-12424
1 parent 5465cea commit b5c287e

File tree

4 files changed

+88
-2
lines changed

4 files changed

+88
-2
lines changed

NEWS

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ Intl:
1616
Opcache:
1717
. Added large shared segments support for FreeBSD. (David Carlier)
1818

19+
PDO_PGSQL:
20+
. Fixed GH-12423, DSN credentials being prioritized over the user/password
21+
PDO constructor arguments. (SakiTakamachi)
22+
1923
PGSQL:
2024
. Added the possibility to have no conditions for pg_select. (OmarEmaraDev)
2125

UPGRADING

+4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ PHP 8.4 UPGRADE NOTES
2626
Consult sections 2. New Features and 6. New Functions for a list of
2727
newly implemented methods and constants.
2828

29+
- PDO_PGSQL:
30+
. The DSN's credentials, when set, are given priority over their PDO
31+
constructor counterparts, being closer to the documentation states.
32+
2933
- SimpleXML:
3034
. Get methods called, or casting to a string on a SimpleXMLElement will no
3135
longer implicitly reset the iterator data, unless explicitly rewound.

ext/pdo_pgsql/pgsql_driver.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1281,8 +1281,8 @@ static int pdo_pgsql_handle_factory(pdo_dbh_t *dbh, zval *driver_options) /* {{{
12811281
}
12821282

12831283
/* escape username and password, if provided */
1284-
tmp_user = _pdo_pgsql_escape_credentials(dbh->username);
1285-
tmp_pass = _pdo_pgsql_escape_credentials(dbh->password);
1284+
tmp_user = !strstr((char *) dbh->data_source, "user=") ? _pdo_pgsql_escape_credentials(dbh->username) : NULL;
1285+
tmp_pass = !strstr((char *) dbh->data_source, "password=") ? _pdo_pgsql_escape_credentials(dbh->password) : NULL;
12861286

12871287
/* support both full connection string & connection string + login and/or password */
12881288
if (tmp_user && tmp_pass) {

ext/pdo_pgsql/tests/gh12423.phpt

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
--TEST--
2+
GitHub #12424 (Fix GH-12423: [pdo_pgsql] Changed to prioritize DSN authentication information over arguments.)
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('pdo') || !extension_loaded('pdo_pgsql')) die('skip not loaded');
6+
require __DIR__ . '/../../../ext/pdo/tests/pdo_test.inc';
7+
require __DIR__ . '/config.inc';
8+
PDOTest::skip();
9+
?>
10+
--FILE--
11+
<?php
12+
require __DIR__ . '/config.inc';
13+
14+
[
15+
'ENV' => [
16+
'PDOTEST_DSN' => $dsnWithCredentials,
17+
'PDOTEST_USER' => $user,
18+
'PDOTEST_PASS' => $password,
19+
],
20+
] = __DIR__ . '/common.phpt';
21+
22+
$dsn = str_replace(" user={$user} password={$password}", '', $dsnWithCredentials);
23+
24+
echo "dsn without credentials / correct user / correct password\n";
25+
try {
26+
$db = new PDO($dsn, $user, $password, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
27+
echo "Connected.\n\n";
28+
} catch (PDOException $e) {
29+
echo $e->getMessage();
30+
}
31+
32+
echo "dsn with credentials / no user / no password\n";
33+
try {
34+
$db = new PDO("{$dsn} user={$user} password={$password}", null, null, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
35+
echo "Connected.\n\n";
36+
} catch (PDOException $e) {
37+
echo $e->getMessage();
38+
}
39+
40+
echo "dsn with correct user / incorrect user / correct password\n";
41+
try {
42+
$db = new PDO("{$dsn} user={$user}", 'hoge', $password, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
43+
echo "Connected.\n\n";
44+
} catch (PDOException $e) {
45+
echo $e->getMessage();
46+
}
47+
48+
echo "dsn with correct password / correct user / incorrect password\n";
49+
try {
50+
$db = new PDO("{$dsn} password={$password}", $user, 'fuga', [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
51+
echo "Connected.\n\n";
52+
} catch (PDOException $e) {
53+
echo $e->getMessage();
54+
}
55+
56+
echo "dsn with correct credentials / incorrect user / incorrect password\n";
57+
try {
58+
$db = new PDO("{$dsn} user={$user} password={$password}", 'hoge', 'fuga', [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
59+
echo "Connected.\n";
60+
} catch (PDOException $e) {
61+
echo $e->getMessage();
62+
}
63+
?>
64+
--EXPECT--
65+
dsn without credentials / correct user / correct password
66+
Connected.
67+
68+
dsn with credentials / no user / no password
69+
Connected.
70+
71+
dsn with correct user / incorrect user / correct password
72+
Connected.
73+
74+
dsn with correct password / correct user / incorrect password
75+
Connected.
76+
77+
dsn with correct credentials / incorrect user / incorrect password
78+
Connected.

0 commit comments

Comments
 (0)