-
Couldn't load subscription status.
- Fork 8k
ext/pdo: Fix a UAF when changing default fetch class ctor args #17579
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
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8601fc4
ext/pdo: Add tests with UAF
Girgias d262b43
ext/pdo: Fix a UAF when changing default fetch class ctor args
Girgias 3e76b66
Add tests
Girgias e124848
New approach
Girgias e8a68cb
[skip ci] Push so Niels can check
Girgias 031185a
Fixes
nielsdos 313b2fd
Fix tests + cyclic ctor
Girgias a5b5cd3
Use \%S test expectation specifier
Girgias File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
ext/pdo/tests/pdo_fetch_class_change_ctor_args_during_fetch1.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| --TEST-- | ||
| PDO Common: PDO::FETCH_CLASS with a constructor that changes the ctor args within PDO::fetch() | ||
| --EXTENSIONS-- | ||
| pdo | ||
| --SKIPIF-- | ||
| <?php | ||
| $dir = getenv('REDIR_TEST_DIR'); | ||
| if (false == $dir) die('skip no driver'); | ||
| require_once $dir . 'pdo_test.inc'; | ||
| PDOTest::skip(); | ||
| ?> | ||
| --FILE-- | ||
| <?php | ||
| if (getenv('REDIR_TEST_DIR') === false) putenv('REDIR_TEST_DIR='.__DIR__ . '/../../pdo/tests/'); | ||
| require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc'; | ||
| $db = PDOTest::factory(); | ||
|
|
||
| class Test { | ||
| public string $val1; | ||
| public string $val2; | ||
|
|
||
| public function __construct(mixed $v) { | ||
| var_dump($v); | ||
| if ($v instanceof PDOStatement) { | ||
| $v->setFetchMode(PDO::FETCH_CLASS, 'Test', [$this->val2]); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // TODO Rename test table to pdo_fetch_class_change_ctor_one in PHP-8.4 | ||
| $db->exec('CREATE TABLE test(id int NOT NULL PRIMARY KEY, val1 VARCHAR(10), val2 VARCHAR(10))'); | ||
| $db->exec("INSERT INTO test VALUES(1, 'A', 'alpha')"); | ||
| $db->exec("INSERT INTO test VALUES(2, 'B', 'beta')"); | ||
| $db->exec("INSERT INTO test VALUES(3, 'C', 'gamma')"); | ||
| $db->exec("INSERT INTO test VALUES(4, 'D', 'delta')"); | ||
|
|
||
| $stmt = $db->prepare('SELECT val1, val2 FROM test'); | ||
| $stmt->setFetchMode(PDO::FETCH_CLASS, 'Test', [$stmt]); | ||
|
|
||
| $stmt->execute(); | ||
| var_dump($stmt->fetch()); | ||
|
|
||
| ?> | ||
| --EXPECTF-- | ||
| object(PDOStatement)#%d (1) { | ||
| ["queryString"]=> | ||
| string(27) "SELECT val1, val2 FROM test" | ||
| } | ||
| object(Test)#%d (2) { | ||
| ["val1"]=> | ||
| string(1) "A" | ||
| ["val2"]=> | ||
| string(5) "alpha" | ||
| } |
53 changes: 53 additions & 0 deletions
53
ext/pdo/tests/pdo_fetch_class_change_ctor_args_during_fetch2.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| --TEST-- | ||
| PDO Common: PDO::FETCH_CLASS with a constructor that changes the ctor args within PDO::fetchObject() | ||
| --EXTENSIONS-- | ||
| pdo | ||
| --SKIPIF-- | ||
| <?php | ||
| $dir = getenv('REDIR_TEST_DIR'); | ||
| if (false == $dir) die('skip no driver'); | ||
| require_once $dir . 'pdo_test.inc'; | ||
| PDOTest::skip(); | ||
| ?> | ||
| --FILE-- | ||
| <?php | ||
| if (getenv('REDIR_TEST_DIR') === false) putenv('REDIR_TEST_DIR='.__DIR__ . '/../../pdo/tests/'); | ||
| require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc'; | ||
| $db = PDOTest::factory(); | ||
|
|
||
| class Test { | ||
| public string $val1; | ||
| public string $val2; | ||
|
|
||
| public function __construct(mixed $v) { | ||
| var_dump($v); | ||
| if ($v instanceof PDOStatement) { | ||
| $v->setFetchMode(PDO::FETCH_CLASS, 'Test', [$this->val2]); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // TODO Rename test table to pdo_fetch_class_change_ctor_two in PHP-8.4 | ||
| $db->exec('CREATE TABLE test(id int NOT NULL PRIMARY KEY, val1 VARCHAR(10), val2 VARCHAR(10))'); | ||
| $db->exec("INSERT INTO test VALUES(1, 'A', 'alpha')"); | ||
| $db->exec("INSERT INTO test VALUES(2, 'B', 'beta')"); | ||
| $db->exec("INSERT INTO test VALUES(3, 'C', 'gamma')"); | ||
| $db->exec("INSERT INTO test VALUES(4, 'D', 'delta')"); | ||
|
|
||
| $stmt = $db->prepare('SELECT val1, val2 FROM test'); | ||
|
|
||
| $stmt->execute(); | ||
| var_dump($stmt->fetchObject('Test', [$stmt])); | ||
|
|
||
| ?> | ||
| --EXPECTF-- | ||
| object(PDOStatement)#%s (1) { | ||
| ["queryString"]=> | ||
| string(27) "SELECT val1, val2 FROM test" | ||
| } | ||
| object(Test)#%s (2) { | ||
| ["val1"]=> | ||
| string(1) "A" | ||
| ["val2"]=> | ||
| string(5) "alpha" | ||
| } |
81 changes: 81 additions & 0 deletions
81
ext/pdo/tests/pdo_fetch_class_change_ctor_args_during_fetch3.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| --TEST-- | ||
| PDO Common: PDO::FETCH_CLASS with a constructor that changes the ctor args within PDO::fetchAll() (no args variation) | ||
| --EXTENSIONS-- | ||
| pdo | ||
| --SKIPIF-- | ||
| <?php | ||
| $dir = getenv('REDIR_TEST_DIR'); | ||
| if (false == $dir) die('skip no driver'); | ||
| require_once $dir . 'pdo_test.inc'; | ||
| PDOTest::skip(); | ||
| ?> | ||
| --FILE-- | ||
| <?php | ||
| if (getenv('REDIR_TEST_DIR') === false) putenv('REDIR_TEST_DIR='.__DIR__ . '/../../pdo/tests/'); | ||
| require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc'; | ||
| $db = PDOTest::factory(); | ||
|
|
||
| class Test { | ||
| public string $val1; | ||
| public string $val2; | ||
|
|
||
| public function __construct(mixed $v) { | ||
| var_dump($v); | ||
| if ($v instanceof PDOStatement) { | ||
| $v->setFetchMode(PDO::FETCH_CLASS, 'Test', [$this->val2]); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // TODO Rename test table to pdo_fetch_class_change_ctor_three in PHP-8.4 | ||
| $db->exec('CREATE TABLE test(id int NOT NULL PRIMARY KEY, val1 VARCHAR(10), val2 VARCHAR(10))'); | ||
| $db->exec("INSERT INTO test VALUES(1, 'A', 'alpha')"); | ||
| $db->exec("INSERT INTO test VALUES(2, 'B', 'beta')"); | ||
| $db->exec("INSERT INTO test VALUES(3, 'C', 'gamma')"); | ||
| $db->exec("INSERT INTO test VALUES(4, 'D', 'delta')"); | ||
|
|
||
| $stmt = $db->prepare('SELECT val1, val2 FROM test'); | ||
| $stmt->setFetchMode(PDO::FETCH_CLASS, 'Test', [$stmt]); | ||
|
|
||
| $stmt->execute(); | ||
| var_dump($stmt->fetchAll()); | ||
|
|
||
| ?> | ||
| --EXPECTF-- | ||
| object(PDOStatement)#%d (1) { | ||
| ["queryString"]=> | ||
| string(27) "SELECT val1, val2 FROM test" | ||
| } | ||
| string(5) "alpha" | ||
| string(5) "alpha" | ||
| string(5) "alpha" | ||
| array(4) { | ||
| [0]=> | ||
| object(Test)#%d (2) { | ||
| ["val1"]=> | ||
| string(1) "A" | ||
| ["val2"]=> | ||
| string(5) "alpha" | ||
| } | ||
| [1]=> | ||
| object(Test)#%d (2) { | ||
| ["val1"]=> | ||
| string(1) "B" | ||
| ["val2"]=> | ||
| string(4) "beta" | ||
| } | ||
| [2]=> | ||
| object(Test)#%d (2) { | ||
| ["val1"]=> | ||
| string(1) "C" | ||
| ["val2"]=> | ||
| string(5) "gamma" | ||
| } | ||
| [3]=> | ||
| object(Test)#%d (2) { | ||
| ["val1"]=> | ||
| string(1) "D" | ||
| ["val2"]=> | ||
| string(5) "delta" | ||
| } | ||
| } |
80 changes: 80 additions & 0 deletions
80
ext/pdo/tests/pdo_fetch_class_change_ctor_args_during_fetch4.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| --TEST-- | ||
| PDO Common: PDO::FETCH_CLASS with a constructor that changes the ctor args within PDO::fetchAll() (args in fetchAll) | ||
| --EXTENSIONS-- | ||
| pdo | ||
| --SKIPIF-- | ||
| <?php | ||
| $dir = getenv('REDIR_TEST_DIR'); | ||
| if (false == $dir) die('skip no driver'); | ||
| require_once $dir . 'pdo_test.inc'; | ||
| PDOTest::skip(); | ||
| ?> | ||
| --FILE-- | ||
| <?php | ||
| if (getenv('REDIR_TEST_DIR') === false) putenv('REDIR_TEST_DIR='.__DIR__ . '/../../pdo/tests/'); | ||
| require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc'; | ||
| $db = PDOTest::factory(); | ||
|
|
||
| class Test { | ||
| public string $val1; | ||
| public string $val2; | ||
|
|
||
| public function __construct(mixed $v) { | ||
| var_dump($v); | ||
| if ($v instanceof PDOStatement) { | ||
| $v->setFetchMode(PDO::FETCH_CLASS, 'Test', [$this->val2]); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // TODO Rename test table to pdo_fetch_class_change_ctor_four in PHP-8.4 | ||
| $db->exec('CREATE TABLE test(id int NOT NULL PRIMARY KEY, val1 VARCHAR(10), val2 VARCHAR(10))'); | ||
| $db->exec("INSERT INTO test VALUES(1, 'A', 'alpha')"); | ||
| $db->exec("INSERT INTO test VALUES(2, 'B', 'beta')"); | ||
| $db->exec("INSERT INTO test VALUES(3, 'C', 'gamma')"); | ||
| $db->exec("INSERT INTO test VALUES(4, 'D', 'delta')"); | ||
|
|
||
| $stmt = $db->prepare('SELECT val1, val2 FROM test'); | ||
|
|
||
| $stmt->execute(); | ||
| var_dump($stmt->fetchAll(PDO::FETCH_CLASS, 'Test', [$stmt])); | ||
|
|
||
| ?> | ||
| --EXPECTF-- | ||
| object(PDOStatement)#%d (1) { | ||
| ["queryString"]=> | ||
| string(27) "SELECT val1, val2 FROM test" | ||
| } | ||
| string(5) "alpha" | ||
| string(5) "alpha" | ||
| string(5) "alpha" | ||
| array(4) { | ||
| [0]=> | ||
| object(Test)#%d (2) { | ||
| ["val1"]=> | ||
| string(1) "A" | ||
| ["val2"]=> | ||
| string(5) "alpha" | ||
| } | ||
| [1]=> | ||
| object(Test)#%d (2) { | ||
| ["val1"]=> | ||
| string(1) "B" | ||
| ["val2"]=> | ||
| string(4) "beta" | ||
| } | ||
| [2]=> | ||
| object(Test)#%d (2) { | ||
| ["val1"]=> | ||
| string(1) "C" | ||
| ["val2"]=> | ||
| string(5) "gamma" | ||
| } | ||
| [3]=> | ||
| object(Test)#%d (2) { | ||
| ["val1"]=> | ||
| string(1) "D" | ||
| ["val2"]=> | ||
| string(5) "delta" | ||
| } | ||
| } |
50 changes: 50 additions & 0 deletions
50
ext/pdo/tests/pdo_fetch_class_change_ctor_args_during_fetch5.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| --TEST-- | ||
| PDO Common: PDO::FETCH_CLASS with a constructor that changes the ctor args within PDO::fetchAll() (via warning and error handler) | ||
| --EXTENSIONS-- | ||
| pdo | ||
| --SKIPIF-- | ||
| <?php | ||
| $dir = getenv('REDIR_TEST_DIR'); | ||
| if (false == $dir) die('skip no driver'); | ||
| require_once $dir . 'pdo_test.inc'; | ||
| PDOTest::skip(); | ||
| ?> | ||
| --FILE-- | ||
| <?php | ||
| if (getenv('REDIR_TEST_DIR') === false) putenv('REDIR_TEST_DIR='.__DIR__ . '/../../pdo/tests/'); | ||
| require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc'; | ||
| $db = PDOTest::factory(); | ||
|
|
||
| // Warning to hook into error handler | ||
| $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); | ||
|
|
||
| class B { | ||
| public function __construct() {} | ||
| } | ||
|
|
||
| // TODO Rename test table to pdo_fetch_class_change_ctor_five in PHP-8.4 | ||
| $db->exec('CREATE TABLE test(id int NOT NULL PRIMARY KEY, val1 VARCHAR(10), val2 VARCHAR(10))'); | ||
| $db->exec("INSERT INTO test VALUES(1, 'A', 'alpha')"); | ||
| $db->exec("INSERT INTO test VALUES(2, 'B', 'beta')"); | ||
| $db->exec("INSERT INTO test VALUES(3, 'C', 'gamma')"); | ||
| $db->exec("INSERT INTO test VALUES(4, 'D', 'delta')"); | ||
|
|
||
| $stmt = $db->prepare('SELECT val1, val2 FROM test'); | ||
| $stmt->execute(); | ||
|
|
||
| function stuffingErrorHandler(int $errno, string $errstr, string $errfile, int $errline) { | ||
| global $stmt; | ||
| $stmt->setFetchMode(PDO::FETCH_CLASS, 'B', [$errstr]); | ||
| echo $errstr, PHP_EOL; | ||
| } | ||
| set_error_handler(stuffingErrorHandler(...)); | ||
|
|
||
| var_dump($stmt->fetchAll(PDO::FETCH_CLASS|PDO::FETCH_SERIALIZE, 'B', [$stmt])); | ||
|
|
||
| ?> | ||
| --EXPECTF-- | ||
| PDOStatement::fetchAll(): The PDO::FETCH_SERIALIZE mode is deprecated | ||
| PDOStatement::fetchAll(): SQLSTATE[HY000]: General error: cannot unserialize class | ||
| PDOStatement::fetchAll(): SQLSTATE[HY000]: General error%S | ||
| array(0) { | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This TODO should be removed upon squash-merging.