From d85f8e3933fd57cc6fde426d29a7eded812d1781 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Wed, 18 Nov 2026 19:35:51 +0100 Subject: [PATCH 1/4] Track what DBAL queries store in each column and what they read back --- stubs/DBAL/before-4/Connection.phpstub | 16 ++++ stubs/DBAL/since-4/Connection.phpstub | 14 +++ stubs/DBAL/since-4/Result.phpstub | 110 ++++++++++++++++++++++++ stubs/DBAL/since-4/Statement.phpstub | 34 ++++++++ tests/acceptance/DatabaseTaints.feature | 75 ++++++++++++++++ 5 files changed, 249 insertions(+) create mode 100644 stubs/DBAL/since-4/Result.phpstub create mode 100644 stubs/DBAL/since-4/Statement.phpstub create mode 100644 tests/acceptance/DatabaseTaints.feature diff --git a/stubs/DBAL/before-4/Connection.phpstub b/stubs/DBAL/before-4/Connection.phpstub index 88d6223..b6ad466 100644 --- a/stubs/DBAL/before-4/Connection.phpstub +++ b/stubs/DBAL/before-4/Connection.phpstub @@ -45,11 +45,13 @@ class Connection implements DriverConnection /** * @throws DBALException * @psalm-taint-sink sql $statement + * @psalm-database-prepare $statement */ public function prepare(string $statement): Statement {} /** * @psalm-taint-sink sql $statement + * @psalm-database-query $statement none */ public function exec(string $statement): int {} @@ -67,6 +69,7 @@ class Connection implements DriverConnection * * @throws DBALException * @psalm-taint-sink sql $query + * @psalm-database-query $query $params none */ public function executeUpdate(string $query, array $params = [], array $types = []): int {} @@ -78,6 +81,7 @@ class Connection implements DriverConnection * * @throws DBALException * @psalm-taint-sink sql $sql + * @psalm-database-query $sql $params result */ public function executeQuery($sql, array $params = [], $types = [], ?Cache\QueryCacheProfile $qcp = null) {} @@ -87,6 +91,7 @@ class Connection implements DriverConnection * * @throws DBALException * @psalm-taint-sink sql $sql + * @psalm-database-query $sql $params none */ public function executeStatement($sql, array $params = [], array $types = []): int {} @@ -114,6 +119,7 @@ class Connection implements DriverConnection * @throws Exception * * @psalm-taint-sink sql $query + * @psalm-database-query $query $params result */ public function fetchAssociative(string $query, array $params = [], array $types = []) {} @@ -130,6 +136,7 @@ class Connection implements DriverConnection * @throws Exception * * @psalm-taint-sink sql $query + * @psalm-database-query $query $params result */ public function fetchNumeric(string $query, array $params = [], array $types = []) {} @@ -146,6 +153,7 @@ class Connection implements DriverConnection * @throws Exception * * @psalm-taint-sink sql $query + * @psalm-database-query $query $params value */ public function fetchOne(string $query, array $params = [], array $types = []) {} @@ -161,6 +169,7 @@ class Connection implements DriverConnection * @throws Exception * * @psalm-taint-sink sql $query + * @psalm-database-query $query $params rows */ public function fetchAllNumeric(string $query, array $params = [], array $types = []): array {} @@ -176,6 +185,7 @@ class Connection implements DriverConnection * @throws Exception * * @psalm-taint-sink sql $query + * @psalm-database-query $query $params rows */ public function fetchAllAssociative(string $query, array $params = [], array $types = []): array {} @@ -209,6 +219,7 @@ class Connection implements DriverConnection * @throws Exception * * @psalm-taint-sink sql $query + * @psalm-database-query $query $params rows */ public function fetchAllAssociativeIndexed(string $query, array $params = [], array $types = []): array {} @@ -224,6 +235,7 @@ class Connection implements DriverConnection * @throws Exception * * @psalm-taint-sink sql $query + * @psalm-database-query $query $params column */ public function fetchFirstColumn(string $query, array $params = [], array $types = []): array {} @@ -239,6 +251,7 @@ class Connection implements DriverConnection * @throws Exception * * @psalm-taint-sink sql $query + * @psalm-database-query $query $params rows */ public function iterateNumeric(string $query, array $params = [], array $types = []): Traversable {} @@ -255,6 +268,7 @@ class Connection implements DriverConnection * @throws Exception * * @psalm-taint-sink sql $query + * @psalm-database-query $query $params rows */ public function iterateAssociative(string $query, array $params = [], array $types = []): Traversable {} @@ -288,6 +302,7 @@ class Connection implements DriverConnection * @throws Exception * * @psalm-taint-sink sql $query + * @psalm-database-query $query $params rows */ public function iterateAssociativeIndexed(string $query, array $params = [], array $types = []): Traversable {} @@ -303,6 +318,7 @@ class Connection implements DriverConnection * @throws Exception * * @psalm-taint-sink sql $query + * @psalm-database-query $query $params column */ public function iterateColumn(string $query, array $params = [], array $types = []): Traversable {} } diff --git a/stubs/DBAL/since-4/Connection.phpstub b/stubs/DBAL/since-4/Connection.phpstub index 8091cec..d4b2325 100644 --- a/stubs/DBAL/since-4/Connection.phpstub +++ b/stubs/DBAL/since-4/Connection.phpstub @@ -19,6 +19,7 @@ class Connection * @throws Exception * * @psalm-taint-sink sql $sql + * @psalm-database-prepare $sql */ public function prepare(string $sql): Statement {} @@ -33,6 +34,7 @@ class Connection * @throws Exception * * @psalm-taint-sink sql $sql + * @psalm-database-query $sql $params result */ public function executeQuery( string $sql, @@ -61,6 +63,7 @@ class Connection * @throws Exception * * @psalm-taint-sink sql $sql + * @psalm-database-query $sql $params none */ public function executeStatement(string $sql, array $params = [], array $types = []): int|string {} @@ -87,6 +90,7 @@ class Connection * @throws Exception * * @psalm-taint-sink sql $query + * @psalm-database-query $query $params result */ public function fetchAssociative(string $query, array $params = [], array $types = []): array|false {} @@ -102,6 +106,7 @@ class Connection * @throws Exception * * @psalm-taint-sink sql $query + * @psalm-database-query $query $params result */ public function fetchNumeric(string $query, array $params = [], array $types = []): array|false {} @@ -117,6 +122,7 @@ class Connection * @throws Exception * * @psalm-taint-sink sql $query + * @psalm-database-query $query $params value */ public function fetchOne(string $query, array $params = [], array $types = []): mixed {} @@ -131,6 +137,7 @@ class Connection * @throws Exception * * @psalm-taint-sink sql $query + * @psalm-database-query $query $params rows */ public function fetchAllNumeric(string $query, array $params = [], array $types = []): array {} @@ -145,6 +152,7 @@ class Connection * @throws Exception * * @psalm-taint-sink sql $query + * @psalm-database-query $query $params rows */ public function fetchAllAssociative(string $query, array $params = [], array $types = []): array {} @@ -176,6 +184,7 @@ class Connection * @throws Exception * * @psalm-taint-sink sql $query + * @psalm-database-query $query $params rows */ public function fetchAllAssociativeIndexed(string $query, array $params = [], array $types = []): array {} @@ -190,6 +199,7 @@ class Connection * @throws Exception * * @psalm-taint-sink sql $query + * @psalm-database-query $query $params column */ public function fetchFirstColumn(string $query, array $params = [], array $types = []): array {} @@ -204,6 +214,7 @@ class Connection * @throws Exception * * @psalm-taint-sink sql $query + * @psalm-database-query $query $params rows */ public function iterateNumeric(string $query, array $params = [], array $types = []): Traversable {} @@ -219,6 +230,7 @@ class Connection * @throws Exception * * @psalm-taint-sink sql $query + * @psalm-database-query $query $params rows */ public function iterateAssociative(string $query, array $params = [], array $types = []): Traversable {} @@ -250,6 +262,7 @@ class Connection * @throws Exception * * @psalm-taint-sink sql $query + * @psalm-database-query $query $params rows */ public function iterateAssociativeIndexed(string $query, array $params = [], array $types = []): Traversable {} @@ -264,6 +277,7 @@ class Connection * @throws Exception * * @psalm-taint-sink sql $query + * @psalm-database-query $query $params column */ public function iterateColumn(string $query, array $params = [], array $types = []): Traversable {} } diff --git a/stubs/DBAL/since-4/Result.phpstub b/stubs/DBAL/since-4/Result.phpstub new file mode 100644 index 0000000..fb573b5 --- /dev/null +++ b/stubs/DBAL/since-4/Result.phpstub @@ -0,0 +1,110 @@ +|false + * + * @throws Exception + * + * @psalm-database-fetch result + */ + public function fetchNumeric(): array|false {} + + /** + * @return array|false + * + * @throws Exception + * + * @psalm-database-fetch result + */ + public function fetchAssociative(): array|false {} + + /** + * @return mixed|false + * + * @throws Exception + * + * @psalm-database-fetch value + */ + public function fetchOne(): mixed {} + + /** + * @return list> + * + * @throws Exception + * + * @psalm-database-fetch rows + */ + public function fetchAllNumeric(): array {} + + /** + * @return list> + * + * @throws Exception + * + * @psalm-database-fetch rows + */ + public function fetchAllAssociative(): array {} + + /** + * @return array> + * + * @throws Exception + * + * @psalm-database-fetch rows + */ + public function fetchAllAssociativeIndexed(): array {} + + /** + * @return list + * + * @throws Exception + * + * @psalm-database-fetch column + */ + public function fetchFirstColumn(): array {} + + /** + * @return Traversable> + * + * @throws Exception + * + * @psalm-database-fetch rows + */ + public function iterateNumeric(): Traversable {} + + /** + * @return Traversable> + * + * @throws Exception + * + * @psalm-database-fetch rows + */ + public function iterateAssociative(): Traversable {} + + /** + * @return Traversable> + * + * @throws Exception + * + * @psalm-database-fetch rows + */ + public function iterateAssociativeIndexed(): Traversable {} + + /** + * @return Traversable + * + * @throws Exception + * + * @psalm-database-fetch column + */ + public function iterateColumn(): Traversable {} +} diff --git a/stubs/DBAL/since-4/Statement.phpstub b/stubs/DBAL/since-4/Statement.phpstub new file mode 100644 index 0000000..4f0ea57 --- /dev/null +++ b/stubs/DBAL/since-4/Statement.phpstub @@ -0,0 +1,34 @@ +executeStatement('INSERT INTO comments (author, body) VALUES (?, ?)', ['admin', $_GET['body']]); + } + + """ + # Psalm enables cache when there's a composer.lock file + And I have empty composer.lock + + Scenario: A column a query stored input in is tainted where a query reads it + Given I have the following code + """ + function show(Connection $db): void { + $row = $db->fetchAssociative('SELECT author, body FROM comments'); + if ($row !== false) { + echo (string) $row['body']; + } + } + """ + When I run Psalm with taint analysis + Then I see these errors + | Type | Message | + | TaintedHtml | Detected tainted HTML | + | TaintedTextWithQuotes | Detected tainted text with possible quotes | + And I see no other errors + + Scenario Outline: The other columns are not tainted + Given I have the following code + """ + function show(Connection $db): void { + echo (string) ; + } + """ + When I run Psalm with taint analysis + Then I see no errors + Examples: + | read | + | $db->fetchOne('SELECT author FROM comments') | + | ($db->fetchAssociative('SELECT author, body FROM comments') ?: [])['author'] | + | $db->fetchOne('SELECT title FROM posts') | + + Scenario: The rows of a prepared statement's result hold what an update with named parameters stored + Given I have the "doctrine/dbal" package satisfying the ">= 4.0" + And I have the following code + """ + function update(Connection $db): void { + $statement = $db->prepare('UPDATE users SET bio = :bio WHERE id = :id'); + $statement->bindValue('bio', $_GET['bio']); + $statement->bindValue('id', 1); + $statement->executeStatement(); + } + + function show(Connection $db): void { + foreach ($db->prepare('SELECT bio FROM users')->executeQuery()->fetchAllAssociative() as $user) { + echo (string) $user['bio']; + } + } + """ + When I run Psalm with taint analysis + Then I see these errors + | Type | Message | + | TaintedHtml | Detected tainted HTML | + | TaintedTextWithQuotes | Detected tainted text with possible quotes | + And I see no other errors From b02c5067830f40469272eb5821675c15168a98c3 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Fri, 20 Nov 2026 19:19:46 +0100 Subject: [PATCH 2/4] Require Psalm 7.13, which reads the database annotations --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index b79001e..b7033fd 100644 --- a/composer.json +++ b/composer.json @@ -29,7 +29,7 @@ "php": "^8.2", "composer-runtime-api": "^2", "psalm/psalm-plugin-api": "^0.4 || ^0.5", - "vimeo/psalm": "^7.0.0-rc28|dev-master" + "vimeo/psalm": "^7.13|dev-master" }, "conflict": { "doctrine/collections": "<1.8", From 76fd320fe4770e11b342059830bad4c0d31416c1 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Fri, 20 Nov 2026 19:38:20 +0100 Subject: [PATCH 3/4] Read a column of the rows without a falsy comparison in the test --- tests/acceptance/DatabaseTaints.feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/acceptance/DatabaseTaints.feature b/tests/acceptance/DatabaseTaints.feature index 09f56cd..9f4e3e9 100644 --- a/tests/acceptance/DatabaseTaints.feature +++ b/tests/acceptance/DatabaseTaints.feature @@ -47,7 +47,7 @@ Feature: Database taints Examples: | read | | $db->fetchOne('SELECT author FROM comments') | - | ($db->fetchAssociative('SELECT author, body FROM comments') ?: [])['author'] | + | ($db->fetchAllAssociative('SELECT author, body FROM comments')[0]['author'] ?? '') | | $db->fetchOne('SELECT title FROM posts') | Scenario: The rows of a prepared statement's result hold what an update with named parameters stored From 2fb996d49847307b185faac739ca332b15df1b3b Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Fri, 20 Nov 2026 22:55:09 +0100 Subject: [PATCH 4/4] CI: install as 2.x-dev so Psalm's requirement on this plugin is satisfied --- .github/workflows/ci.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 42920cd..36a7bfb 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -59,7 +59,7 @@ jobs: - name: Install composer dependencies run: composer update --prefer-dist env: - COMPOSER_ROOT_VERSION: dev-master + COMPOSER_ROOT_VERSION: 2.x-dev - name: Generate aux code run: vendor/bin/codecept build @@ -122,7 +122,7 @@ jobs: - name: Install composer dependencies run: composer update --prefer-dist env: - COMPOSER_ROOT_VERSION: dev-master + COMPOSER_ROOT_VERSION: 2.x-dev - name: Run PHPCodeSniffer run: vendor/bin/phpcs --report=checkstyle --cache=~/.cache/phpcs -q --parallel=1 | cs2pr @@ -176,19 +176,19 @@ jobs: run: composer update --prefer-dist --no-interaction if: ${{ matrix.deps == 'high' }} env: - COMPOSER_ROOT_VERSION: dev-master + COMPOSER_ROOT_VERSION: 2.x-dev - name: Install composer dependencies (low deps) run: composer update --prefer-dist --no-interaction --prefer-stable --prefer-lowest if: ${{ matrix.deps == 'low' }} env: - COMPOSER_ROOT_VERSION: dev-master + COMPOSER_ROOT_VERSION: 2.x-dev - name: Install composer dependencies (stable deps) run: composer update --prefer-dist --no-interaction --prefer-stable if: ${{ matrix.deps == 'stable' }} env: - COMPOSER_ROOT_VERSION: dev-master + COMPOSER_ROOT_VERSION: 2.x-dev - name: Show Psalm version run: vendor/bin/psalm --version