Skip to content

Add pdo_sqlite tests for empty filename and in-memory uri #7662

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions ext/pdo_sqlite/tests/pdo_sqlite_empty_filename.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--TEST--
PDO_sqlite: Testing empty filename
--EXTENSIONS--
pdo_sqlite
--FILE--
<?php

// create with empty filename
$db = new PDO('sqlite:');

var_dump($db->exec('CREATE TABLE test1 (id INT);'));

// create with empty URI
$db = new PDO('sqlite:file:?cache=shared');

var_dump($db->exec('CREATE TABLE test1 (id INT);'));
?>
--EXPECT--
int(0)
int(0)
12 changes: 12 additions & 0 deletions ext/pdo_sqlite/tests/pdo_sqlite_filename_uri.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ pdo_sqlite
--FILE--
<?php

// create with in-memory database using shared cached
$db = new PDO('sqlite:file::memory:?cache=shared');

var_dump($db->exec('CREATE TABLE test1 (id INT);'));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this also open a second connection to check that it is actually shared?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could. I don't know if php is responsible for all the uri options working though. I was mainly testing the special case filenames.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that we don't directly pass the DSN to SQLite3, that additional check would make sense. It may also make sense to add an INI section with open_basedir=, because if open_basedir is set, file: URIs are not supported by PHP.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FreeBSD fails for some reason. What is the recommended open_basedir setting and then target directory to use to trigger a fail on all platforms?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you could use open_basedir=. for that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you could use open_basedir=. for that.

And write to what directory? If it doesn't restrict and creates a file, does it matter where it goes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found an example test to follow.


// create second connection to in-memory database
$db = new PDO('sqlite:file::memory:?cache=shared');

var_dump($db->exec('SELECT * from test1'));

// create with default read-write|create mode
$filename = "file:" . __DIR__ . DIRECTORY_SEPARATOR . "pdo_sqlite_filename_uri.db";

Expand All @@ -29,6 +39,8 @@ if (file_exists($filename)) {
?>
--EXPECTF--
int(0)
int(0)
int(0)

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in %s
Stack trace:
Expand Down
47 changes: 47 additions & 0 deletions ext/pdo_sqlite/tests/pdo_sqlite_open_basedir.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
--TEST--
PDO_sqlite: Testing filenames with open_basedir
--EXTENSIONS--
pdo_sqlite
--INI--
open_basedir=.
--FILE--
<?php

// create in basedir
$filename = 'pdo_sqlite_filename.db';

$db = new PDO('sqlite:' . $filename);

var_dump($db->exec('CREATE TABLE test1 (id INT);'));

// create outside basedir
$filename = '..' . DIRECTORY_SEPARATOR . 'pdo_sqlite_filename.db';

new PDO('sqlite:' . $filename);
?>

--CLEAN--
<?php
$filenames = [
'pdo_sqlite_filename.db',
'..' . DIRECTORY_SEPARATOR . 'pdo_sqlite_filename.db',
];
foreach ($filenames as $filename) {
if (file_exists($filename)) {
unlink($filename);
}
}
?>
--EXPECTF--
int(0)

Fatal error: Uncaught PDOException: PDO::__construct(): open_basedir restriction in effect. File(%s) is not within the allowed path(s): (%s) in %s:%d
Stack trace:
%s
#1 {main}

Next PDOException: open_basedir prohibits opening %s in %s:%d
Stack trace:
%s
#1 {main}
thrown in %s
32 changes: 32 additions & 0 deletions ext/pdo_sqlite/tests/pdo_sqlite_open_basedir_uri.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
--TEST--
PDO_sqlite: Testing URIs with open_basedir
--EXTENSIONS--
pdo_sqlite
--INI--
open_basedir={TMP}
--FILE--
<?php

// create in basedir
$filename = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'pdo_sqlite_filename.db';

new PDO('sqlite:file:' . $filename);
?>

--CLEAN--
<?php
$filenames = [
sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'pdo_sqlite_filename.db',
];
foreach ($filenames as $filename) {
if (file_exists($filename)) {
unlink($filename);
}
}
?>
--EXPECTF--
Fatal error: Uncaught PDOException: open_basedir prohibits opening %s in %s:%d
Stack trace:
%s
#1 {main}
thrown in %s