Skip to content

Commit cba92be

Browse files
committed
PDO_MYSQL: Properly quote binary strings
Closes GH-15949
1 parent 93c68ca commit cba92be

File tree

5 files changed

+59
-9
lines changed

5 files changed

+59
-9
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,10 @@ PHP NEWS
149149
- PCRE:
150150
. Fix UAF issues with PCRE after request shutdown. (nielsdos)
151151

152+
- PDO_MYSQL:
153+
. Fixed GH-15949 (PDO_MySQL not properly quoting PDO_PARAM_LOB binary
154+
data). (mbeccati, lcobucci)
155+
152156
- PDO_PGSQL:
153157
. Fixed GH-15986 (Double-free due to Pdo\Pgsql::setNoticeCallback()). (cmb,
154158
nielsdos)

UPGRADING

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,9 @@ PHP 8.4 UPGRADE NOTES
184184
- PDO_MYSQL:
185185
. getAttribute, ATTR_AUTOCOMMIT, ATTR_EMULATE_PREPARES, MYSQL_ATTR_DIRECT_QUERY have
186186
been changed to get values as bool.
187+
. Quoting a string with PARAM_LOB as type now outputs the string explicitly quoted
188+
as binary. This also affects parameters bound as PARAM_LOB when
189+
ATTR_EMULATE_PREPARES is enabled.
187190

188191
- PDO_PGSQL:
189192
. The DSN's credentials, when set, are given priority over their PDO

ext/pdo_mysql/mysql_driver.c

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -309,23 +309,29 @@ static zend_string* mysql_handle_quoter(pdo_dbh_t *dbh, const zend_string *unquo
309309
{
310310
pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data;
311311
bool use_national_character_set = 0;
312+
bool use_binary = 0;
312313
size_t quotedlen;
313314

314-
if (H->assume_national_character_set_strings) {
315-
use_national_character_set = 1;
316-
}
317-
if ((paramtype & PDO_PARAM_STR_NATL) == PDO_PARAM_STR_NATL) {
318-
use_national_character_set = 1;
319-
}
320-
if ((paramtype & PDO_PARAM_STR_CHAR) == PDO_PARAM_STR_CHAR) {
321-
use_national_character_set = 0;
315+
if ((paramtype & PDO_PARAM_LOB) == PDO_PARAM_LOB) {
316+
use_binary = 1;
317+
} else {
318+
if (H->assume_national_character_set_strings) {
319+
use_national_character_set = 1;
320+
}
321+
if ((paramtype & PDO_PARAM_STR_NATL) == PDO_PARAM_STR_NATL) {
322+
use_national_character_set = 1;
323+
}
324+
if ((paramtype & PDO_PARAM_STR_CHAR) == PDO_PARAM_STR_CHAR) {
325+
use_national_character_set = 0;
326+
}
322327
}
323328

324329
PDO_DBG_ENTER("mysql_handle_quoter");
325330
PDO_DBG_INF_FMT("dbh=%p", dbh);
326331
PDO_DBG_INF_FMT("unquoted=%.*s", (int)ZSTR_LEN(unquoted), ZSTR_VAL(unquoted));
327332

328-
zend_string *quoted_str = zend_string_safe_alloc(2, ZSTR_LEN(unquoted), 3 + (use_national_character_set ? 1 : 0), false);
333+
zend_string *quoted_str = zend_string_safe_alloc(2, ZSTR_LEN(unquoted),
334+
3 + (use_national_character_set ? 1 : 0) + (use_binary ? 7 : 0), false);
329335
char *quoted = ZSTR_VAL(quoted_str);
330336

331337
if (use_national_character_set) {
@@ -334,6 +340,11 @@ static zend_string* mysql_handle_quoter(pdo_dbh_t *dbh, const zend_string *unquo
334340
quoted[1] = '\'';
335341

336342
++quotedlen; /* N prefix */
343+
} else if (use_binary) {
344+
quotedlen = mysql_real_escape_string_quote(H->server, quoted + 8, ZSTR_VAL(unquoted), ZSTR_LEN(unquoted), '\'');
345+
memcpy(quoted, "_binary'", 8);
346+
347+
quotedlen += 7; /* _binary prefix */
337348
} else {
338349
quotedlen = mysql_real_escape_string_quote(H->server, quoted + 1, ZSTR_VAL(unquoted), ZSTR_LEN(unquoted), '\'');
339350
quoted[0] = '\'';

ext/pdo_mysql/tests/pdo_mysql_prepare_emulated_binary.phpt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ MySQLPDOTest::skip();
1313
$db = MySQLPDOTest::factory();
1414
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
1515

16+
// Force the connection to utf8, which is enough to make the test fail
17+
// MySQL 5.6+ would be required for utf8mb4
18+
$db->exec("SET NAMES 'utf8'");
19+
1620
$content = '0191D886E6DC73E7AF1FEE7F99EC6235';
1721

1822
$statement = $db->prepare('SELECT HEX(?) as test');
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
MySQL PDO->quote(), properly handle binary data
3+
--EXTENSIONS--
4+
pdo_mysql
5+
--SKIPIF--
6+
<?php
7+
require_once __DIR__ . '/inc/mysql_pdo_test.inc';
8+
MySQLPDOTest::skip();
9+
?>
10+
--FILE--
11+
<?php
12+
require_once __DIR__ . '/inc/mysql_pdo_test.inc';
13+
$db = MySQLPDOTest::factory();
14+
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
15+
16+
// Force the connection to utf8, which is enough to make the test fail
17+
// MySQL 5.6+ would be required for utf8mb4
18+
$db->exec("SET NAMES 'utf8'");
19+
20+
$content = "\xC3\xA1\xC3";
21+
$quoted = $db->quote($content, PDO::PARAM_LOB);
22+
23+
var_dump($quoted);
24+
var_dump($db->query("SELECT HEX({$quoted})")->fetch(PDO::FETCH_NUM)[0]);
25+
?>
26+
--EXPECTF--
27+
string(%d) "_binary'%s'"
28+
string(6) "C3A1C3"

0 commit comments

Comments
 (0)