Skip to content

Conversation

@koriym
Copy link
Member

@koriym koriym commented Dec 23, 2025

Summary

  • Add @psalm-taint-sink sql to mark SQL query parameters as SQL injection sinks
  • Add @psalm-taint-escape sql to mark fetch methods as SQL escape points (prepared statements)

Changed files

  • ExtendedPdoAdapter::__construct() - marks $sql parameter as SQL sink
  • AuraSqlPagerFactory::newInstance() - marks $sql parameter as SQL sink
  • FetchAssoc::__invoke() - marks as SQL sink and escape (uses prepared statements)
  • FetchEntity::__invoke() - marks as SQL sink and escape (uses prepared statements)

These annotations enable Psalm's taint analysis to detect SQL injection vulnerabilities by tracking tainted data flow from sources to sinks.

Summary by Sourcery

Annotate SQL-related methods and constructors with Psalm taint metadata to improve static detection of SQL injection risks.

Enhancements:

  • Mark ExtendedPdoAdapter constructor SQL parameter as a Psalm SQL taint sink.
  • Annotate FetchAssoc and FetchEntity invokers as both SQL taint sinks and SQL escape points for prepared queries.
  • Mark AuraSqlPagerFactory::newInstance SQL argument as a Psalm SQL taint sink to support taint tracking across pager construction.

Summary by CodeRabbit

  • Documentation
    • Added static-analysis taint-tracking annotations across pager, adapter, and fetch components to enrich security-related metadata and developer tooling insights. These are doc-only changes; no behavior, public interfaces, or runtime logic were modified.

✏️ Tip: You can customize this high-level summary in your review settings.

- Add @psalm-taint-sink sql to ExtendedPdoAdapter::__construct()
- Add @psalm-taint-sink sql to AuraSqlPagerFactory::newInstance()
- Add @psalm-taint-sink sql and @psalm-taint-escape sql to FetchAssoc::__invoke()
- Add @psalm-taint-sink sql and @psalm-taint-escape sql to FetchEntity::__invoke()

These annotations enable Psalm's taint analysis to detect SQL injection vulnerabilities
by tracking tainted data flow from sources to sinks.
@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Dec 23, 2025

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Adds Psalm taint analysis annotations to SQL-related methods to mark SQL query strings as taint sinks and prepared-statement-based fetchers as SQL escape points for better SQL injection detection.

Sequence diagram for Psalm SQL taint flow through Pagerfanta adapters

sequenceDiagram
    actor UserCode
    participant AuraSqlPagerFactory
    participant ExtendedPdoAdapter
    participant FetchAssoc
    participant FetchEntity
    participant ExtendedPdoInterface

    UserCode->>AuraSqlPagerFactory: newInstance(pdo, sql, params, paging, uriTemplate, entity)
    Note over AuraSqlPagerFactory: sql parameter is a psalm taint sink (sql)

    AuraSqlPagerFactory->>ExtendedPdoAdapter: __construct(pdo, sql, params)
    Note over ExtendedPdoAdapter: sql parameter is a psalm taint sink (sql)

    alt Fetch associative arrays
        ExtendedPdoAdapter->>FetchAssoc: __invoke(sql, params)
        Note over FetchAssoc: sql parameter is a psalm taint sink and escape (sql)
        FetchAssoc->>ExtendedPdoInterface: executePrepared(sql, params)
        ExtendedPdoInterface-->>FetchAssoc: result rows
        FetchAssoc-->>ExtendedPdoAdapter: array
    else Fetch entities
        ExtendedPdoAdapter->>FetchEntity: __invoke(sql, params)
        Note over FetchEntity: sql parameter is a psalm taint sink and escape (sql)
        FetchEntity->>ExtendedPdoInterface: executePrepared(sql, params)
        ExtendedPdoInterface-->>FetchEntity: hydrated entities
        FetchEntity-->>ExtendedPdoAdapter: array
    end

    ExtendedPdoAdapter-->>UserCode: Pagerfanta results
Loading

Updated class diagram for Pagerfanta SQL adapter taint-annotated methods

classDiagram
    class AdapterInterface
    class FetcherInterface
    class AuraSqlPagerInterface
    class ExtendedPdoInterface

    class ExtendedPdoAdapter {
        - ExtendedPdoInterface pdo
        - string sql
        + __construct(ExtendedPdoInterface pdo, string sql, array params)
    }

    class AuraSqlPagerFactory {
        - AuraSqlPagerInterface auraSqlPager
        + __construct(AuraSqlPagerInterface auraSqlPager)
        + newInstance(ExtendedPdoInterface pdo, string sql, array params, int paging, string uriTemplate, string entity)
    }

    class FetchAssoc {
        - ExtendedPdoInterface pdo
        + __construct(ExtendedPdoInterface pdo)
        + __invoke(string sql, array params) array
    }

    class FetchEntity {
        - ExtendedPdoInterface pdo
        - string entity
        + __construct(ExtendedPdoInterface pdo, string entity)
        + __invoke(string sql, array params) array
    }

    ExtendedPdoAdapter ..> FetcherInterface : uses
    FetchAssoc ..|> FetcherInterface
    FetchEntity ..|> FetcherInterface

    AuraSqlPagerFactory ..> ExtendedPdoAdapter : creates
    FetchAssoc ..> ExtendedPdoInterface : uses
    FetchEntity ..> ExtendedPdoInterface : uses
    ExtendedPdoAdapter ..> ExtendedPdoInterface : uses
    AuraSqlPagerFactory ..> AuraSqlPagerInterface : returns instances
Loading

File-Level Changes

Change Details Files
Mark SQL-construction points as taint sinks for Psalm.
  • Annotate the constructor SQL parameter as a Psalm SQL taint sink to signal dangerous query construction.
  • Annotate the AuraSqlPagerFactory::newInstance SQL parameter as a Psalm SQL taint sink to propagate taint into pager creation.
src/Pagerfanta/ExtendedPdoAdapter.php
src/Pagerfanta/AuraSqlPagerFactory.php
Annotate fetcher invokers as both SQL sinks and escape points (prepared statements) for Psalm taint analysis.
  • Add Psalm SQL taint sink annotation to the SQL argument of FetchAssoc::__invoke while also marking it as an escape point due to prepared statement usage.
  • Add Psalm SQL taint sink annotation to the SQL argument of FetchEntity::__invoke while also marking it as an escape point due to prepared statement usage.
src/Pagerfanta/FetchAssoc.php
src/Pagerfanta/FetchEntity.php

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@coderabbitai
Copy link

coderabbitai bot commented Dec 23, 2025

Walkthrough

Psalm taint annotations were added to docblocks in four Pagerfanta classes to mark SQL sinks and escapes for static analysis. No runtime behavior, method signatures, or control flow were changed.

Changes

Cohort / File(s) Summary
SQL Entry Point Classes
src/Pagerfanta/AuraSqlPagerFactory.php, src/Pagerfanta/ExtendedPdoAdapter.php
Added @psalm-taint-sink sql $sql docblock annotations to newInstance() and the constructor respectively; no signature or logic changes.
Fetch Handler Classes
src/Pagerfanta/FetchAssoc.php, src/Pagerfanta/FetchEntity.php
Added @psalm-taint-sink sql $sql and @psalm-taint-escape sql docblock annotations to __invoke() methods; no signature or logic changes.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Possibly related PRs

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Add Psalm taint annotations for SQL injection analysis' accurately reflects the main change: adding Psalm taint-sink and taint-escape annotations across multiple files for SQL injection detection.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch add-psalm-taint-annotations

📜 Recent review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 37a5474 and cf67678.

📒 Files selected for processing (1)
  • src/Pagerfanta/ExtendedPdoAdapter.php
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/Pagerfanta/ExtendedPdoAdapter.php
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
  • GitHub Check: Sourcery review
  • GitHub Check: PHPUnit - PHP 8.4 (windows-latest, highest)

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@codecov
Copy link

codecov bot commented Dec 23, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (9ce6181) to head (cf67678).

Additional details and impacted files
@@             Coverage Diff             @@
##                 1.x       #89   +/-   ##
===========================================
  Coverage     100.00%   100.00%           
  Complexity       142       142           
===========================================
  Files             41        41           
  Lines            505       505           
===========================================
  Hits             505       505           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey - I've left some high level feedback:

  • For FetchAssoc::__invoke() and FetchEntity::__invoke(), consider whether marking $sql as both @psalm-taint-sink sql and @psalm-taint-escape sql on the same method matches Psalm’s expectations (sometimes escapes are better modeled on lower-level helper methods or return values rather than on the same signature as the sink).
  • If array $params can also carry tainted SQL fragments (e.g., when building dynamic queries or passing raw fragments), consider whether additional taint annotations are needed on $params to accurately reflect the dataflow model.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- For `FetchAssoc::__invoke()` and `FetchEntity::__invoke()`, consider whether marking `$sql` as both `@psalm-taint-sink sql` and `@psalm-taint-escape sql` on the same method matches Psalm’s expectations (sometimes escapes are better modeled on lower-level helper methods or return values rather than on the same signature as the sink).
- If `array $params` can also carry tainted SQL fragments (e.g., when building dynamic queries or passing raw fragments), consider whether additional taint annotations are needed on `$params` to accurately reflect the dataflow model.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 4

📜 Review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9ce6181 and 4d0754b.

📒 Files selected for processing (4)
  • src/Pagerfanta/AuraSqlPagerFactory.php
  • src/Pagerfanta/ExtendedPdoAdapter.php
  • src/Pagerfanta/FetchAssoc.php
  • src/Pagerfanta/FetchEntity.php
🧰 Additional context used
🪛 GitHub Check: cs / Coding Standards
src/Pagerfanta/FetchEntity.php

[failure] 27-27:
Expected 1 line between description and annotations, found 0.

src/Pagerfanta/ExtendedPdoAdapter.php

[failure] 33-33:
Incorrect annotations group.

src/Pagerfanta/FetchAssoc.php

[failure] 19-19:
Expected 1 line between description and annotations, found 0.

src/Pagerfanta/AuraSqlPagerFactory.php

[failure] 18-18:
Expected 1 line between description and annotations, found 0.

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Sourcery review

Comment on lines 25 to 29
/**
* {@inheritDoc}
* @psalm-taint-sink sql $sql
* @psalm-taint-escape sql
*/
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

🧩 Analysis chain

🏁 Script executed:

find . -name "FetchEntity.php" -type f

Repository: ray-di/Ray.AuraSqlModule

Length of output: 100


🏁 Script executed:

cat -n src/Pagerfanta/FetchEntity.php | head -40

Repository: ray-di/Ray.AuraSqlModule

Length of output: 1247


🌐 Web query:

PHP docblock PSR standard blank line between inheritDoc and annotations

💡 Result:

Short answer: There is no final PSR that specifically mandates a blank line between an inline {@inheritdoc} description and the annotations. PSR‑5 (the PHPDoc draft) exists but never became a final PSR; most projects follow conventions/tools instead.

Recommended/common practice:

  • Treat {@inheritdoc} as part of the docblock description and put a blank line before the tag block (i.e., leave one blank line between description/{@inheritdoc} and the annotations). This is the convention used by Symfony/other standards and enforced by php-cs-fixer’s phpdoc_separation rule (group annotations and separate different groups by a single blank line). [1][2][3]

Sources:

  • PSR‑5 (PHPDoc) draft / notes. [1]
  • php-cs-fixer rule: phpdoc_separation (group annotations; different groups separated by a single blank line). [2]
  • Symfony coding standards: advice to avoid one-line /** {@inheritdoc} */ and to separate annotation groups with a single blank line. [3]

🌐 Web query:

Psalm @psalm-taint-escape sql prepared statements documentation

💡 Result:

Short answer: use Psalm's taint annotations — @psalm-taint-escape to mark that a function/assignment removes a taint, @psalm-taint-sink to declare SQL sinks, and prefer parameterised (prepared) queries (so values are not treated as SQL) — see Psalm docs below.

Key docs:

  • @psalm-taint-escape (how to mark that code sanitises/escapes a taint). [1]
  • @psalm-taint-sink (how to declare SQL sinks like exec() or custom wrappers). [2]
  • Security analysis intro (recommendation: use parameterised/prepared queries to avoid SQL injection; examples showing $pdo->exec as a sink). [3]

Practical notes:

  • Prefer PDO prepared/parameterised queries — they prevent SQL injection (and Psalm’s docs explicitly recommend parameterised queries). [3][4]
  • If you have a sanitizer function that makes a value safe for SQL, annotate it with @psalm-taint-escape sql so Psalm won’t report a taint. Example pattern shown in the docs. [1]
  • If you wrap DB calls in your own class, declare the method parameter as a taint sink (e.g. /** @psalm-taint-sink sql $sql */) to let Psalm flag unsafe calls. [2]

Sources:
[1] Psalm — Avoiding false-positives / escaping tainted input. https://psalm.dev/docs/security_analysis/avoiding_false_positives/
[2] Psalm — Custom taint sinks. https://psalm.dev/docs/security_analysis/custom_taint_sinks/
[3] Psalm — Security Analysis / Introduction (parameterised queries recommendation; sql sinks). https://psalm.dev/docs/security_analysis/
[4] PHP manual — PDO::prepare (prepared statements prevent SQL injection). https://www.php.net/manual/en/pdo.prepare.php

Would you like a small annotated code example (PDO prepared + @psalm annotations) for your case?


Add blank line between docblock description and Psalm annotations, and remove @psalm-taint-escape sql.

PSR-5 and standard tooling (php-cs-fixer's phpdoc_separation rule) require a blank line between {@inheritDoc} and annotation tags. Additionally, @psalm-taint-escape sql is misleading here—this method declares $sql as a sink (correct) but does not itself escape the SQL. The actual sanitization happens in Aura's perform() method via prepared statements, not in __invoke(). Remove the escape annotation to avoid suggesting this method is responsible for SQL safety.

 /**
  * {@inheritDoc}
+ *
  * @psalm-taint-sink sql $sql
- * @psalm-taint-escape sql
  */
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
/**
* {@inheritDoc}
* @psalm-taint-sink sql $sql
* @psalm-taint-escape sql
*/
/**
* {@inheritDoc}
*
* @psalm-taint-sink sql $sql
*/
🧰 Tools
🪛 GitHub Check: cs / Coding Standards

[failure] 27-27:
Expected 1 line between description and annotations, found 0.

🤖 Prompt for AI Agents
In src/Pagerfanta/FetchEntity.php around lines 25 to 29, add a blank line
between the docblock description ({@inheritDoc}) and the Psalm annotation tags,
and remove the `@psalm-taint-escape sql` annotation while keeping
`@psalm-taint-sink sql`; specifically, restructure the docblock so there is an
empty line after the description and delete the escape annotation to avoid
implying this method performs SQL sanitization.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

♻️ Duplicate comments (1)
src/Pagerfanta/FetchEntity.php (1)

29-29: Consider the placement of the escape annotation.

The @psalm-taint-escape sql annotation suggests this method escapes SQL taint, but the actual sanitization occurs in $this->pdo->perform() (line 34) via prepared statements, not in __invoke() itself. This concern was raised in a previous review but remains unaddressed. While marking the wrapper method as an escape point works for taint analysis purposes, it may be more precise to annotate only the actual escape point.

🧹 Nitpick comments (1)
src/Pagerfanta/FetchAssoc.php (1)

21-21: Consider the placement of the escape annotation.

Similar to FetchEntity, the @psalm-taint-escape sql annotation suggests this method escapes SQL taint, but the actual sanitization occurs in $this->pdo->perform() (line 26) via prepared statements, not in __invoke() itself. While marking the wrapper method as an escape point works for taint analysis purposes, it may be more precise to annotate only the actual escape point in the call stack.

📜 Review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4d0754b and 37a5474.

📒 Files selected for processing (3)
  • src/Pagerfanta/AuraSqlPagerFactory.php
  • src/Pagerfanta/FetchAssoc.php
  • src/Pagerfanta/FetchEntity.php
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
  • GitHub Check: Sourcery review
  • GitHub Check: sa / ComposerRequireChecker
  • GitHub Check: PHPUnit - PHP 8.3 (ubuntu-latest, highest)
🔇 Additional comments (3)
src/Pagerfanta/AuraSqlPagerFactory.php (1)

16-20: LGTM! Formatting and annotation are correct.

The blank line between {@inheritDoc} and the Psalm annotation now conforms to the coding standard, and the @psalm-taint-sink sql $sql annotation appropriately marks the SQL parameter as a potential injection point for static analysis.

src/Pagerfanta/FetchEntity.php (1)

25-28: LGTM! Formatting and sink annotation are correct.

The blank line between {@inheritDoc} and the annotations now conforms to the coding standard, and the @psalm-taint-sink sql $sql annotation appropriately marks the SQL parameter as a potential injection point for static analysis.

src/Pagerfanta/FetchAssoc.php (1)

17-20: LGTM! Formatting and sink annotation are correct.

The blank line between {@inheritDoc} and the annotations now conforms to the coding standard, and the @psalm-taint-sink sql $sql annotation appropriately marks the SQL parameter as a potential injection point for static analysis.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant