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 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", 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 9e6d4b4..d5b6769 100644 --- a/stubs/DBAL/since-4/Connection.phpstub +++ b/stubs/DBAL/since-4/Connection.phpstub @@ -25,6 +25,7 @@ class Connection * @throws Exception * * @psalm-taint-sink sql $sql + * @psalm-database-prepare $sql */ public function prepare(string $sql): Statement {} @@ -39,6 +40,7 @@ class Connection * @throws Exception * * @psalm-taint-sink sql $sql + * @psalm-database-query $sql $params result */ public function executeQuery( string $sql, @@ -67,6 +69,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 {} @@ -93,6 +96,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 {} @@ -108,6 +112,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 {} @@ -123,6 +128,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 {} @@ -137,6 +143,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 {} @@ -151,6 +158,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 {} @@ -182,6 +190,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 {} @@ -196,6 +205,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 {} @@ -210,6 +220,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 {} @@ -225,6 +236,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 {} @@ -256,6 +268,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 {} @@ -270,6 +283,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->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 + 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