Skip to content

Commit a398a2f

Browse files
committed
Merge branch 'PHP-8.1'
* PHP-8.1: Fix GH-9032: SQLite3 authorizer crashes on NULL values
2 parents 89216b2 + ca84d06 commit a398a2f

File tree

4 files changed

+57
-1
lines changed

4 files changed

+57
-1
lines changed

ext/pdo_sqlite/sqlite_driver.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,9 @@ static const struct pdo_dbh_methods sqlite_methods = {
738738

739739
static char *make_filename_safe(const char *filename)
740740
{
741+
if (!filename) {
742+
return NULL;
743+
}
741744
if (*filename && strncasecmp(filename, "file:", 5) == 0) {
742745
if (PG(open_basedir) && *PG(open_basedir)) {
743746
return NULL;
@@ -766,7 +769,7 @@ static int authorizer(void *autharg, int access_type, const char *arg3, const ch
766769
char *filename;
767770
switch (access_type) {
768771
case SQLITE_ATTACH: {
769-
filename = make_filename_safe(arg3);
772+
filename = make_filename_safe(arg3);
770773
if (!filename) {
771774
return SQLITE_DENY;
772775
}

ext/pdo_sqlite/tests/gh9032.phpt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--TEST--
2+
SQLite3 authorizer crashes on NULL values
3+
--EXTENSIONS--
4+
pdo_sqlite
5+
--INI--
6+
open_basedir=.
7+
--FILE--
8+
<?php
9+
$db = new PDO("sqlite::memory:", null, null, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
10+
11+
$db->exec('attach database \':memory:\' AS "db1"');
12+
var_dump($db->exec('create table db1.r (id int)'));
13+
14+
try {
15+
$st = $db->prepare('attach database :a AS "db2"');
16+
$st->execute([':a' => ':memory:']);
17+
var_dump($db->exec('create table db2.r (id int)'));
18+
} catch (PDOException $ex) {
19+
echo $ex->getMessage(), PHP_EOL;
20+
}
21+
?>
22+
--EXPECT--
23+
int(0)
24+
SQLSTATE[HY000]: General error: 23 not authorized

ext/sqlite3/sqlite3.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2067,6 +2067,9 @@ static int php_sqlite3_authorizer(void *autharg, int action, const char *arg1, c
20672067
/* Check open_basedir restrictions first */
20682068
if (PG(open_basedir) && *PG(open_basedir)) {
20692069
if (action == SQLITE_ATTACH) {
2070+
if (!arg1) {
2071+
return SQLITE_DENY;
2072+
}
20702073
if (memcmp(arg1, ":memory:", sizeof(":memory:")) && *arg1) {
20712074
if (strncmp(arg1, "file:", 5) == 0) {
20722075
/* starts with "file:" */

ext/sqlite3/tests/gh9032.phpt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
SQLite3 authorizer crashes on NULL values
3+
--EXTENSIONS--
4+
sqlite3
5+
--INI--
6+
open_basedir=.
7+
--FILE--
8+
<?php
9+
$db = new SQLite3(":memory:");
10+
$db->enableExceptions(true);
11+
12+
$db->exec('attach database \':memory:\' AS "db1"');
13+
var_dump($db->exec('create table db1.r (id int)'));
14+
15+
try {
16+
$st = $db->prepare('attach database :a AS "db2"');
17+
$st->bindValue("a", ":memory:");
18+
$st->execute();
19+
var_dump($db->exec('create table db2.r (id int)'));
20+
} catch (Exception $ex) {
21+
echo $ex->getMessage(), PHP_EOL;
22+
}
23+
?>
24+
--EXPECT--
25+
bool(true)
26+
Unable to prepare statement: 23, not authorized

0 commit comments

Comments
 (0)