Skip to content

Add Psalm taint annotations for SQL injection analysis#89

Merged
koriym merged 5 commits into
1.xfrom
add-psalm-taint-annotations
Mar 13, 2026
Merged

Add Psalm taint annotations for SQL injection analysis#89
koriym merged 5 commits into
1.xfrom
add-psalm-taint-annotations

Conversation

@koriym

@koriym koriym commented Dec 23, 2025

Copy link
Copy Markdown
Member

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 annotations to several pager, adapter, and fetch component docblocks to improve security metadata for developer tooling. These are documentation-only updates; no runtime behavior, public interfaces, method signatures, or error handling were changed.

- 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

sourcery-ai Bot commented Dec 23, 2025

Copy link
Copy Markdown
Contributor
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

coderabbitai Bot commented Dec 23, 2025

Copy link
Copy Markdown

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: b13d0b57-dae0-4545-a7f2-a69ea90d1a36

📥 Commits

Reviewing files that changed from the base of the PR and between 405c92d and ac70463.

📒 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

📝 Walkthrough

Walkthrough

Added Psalm taint-sink docblock annotations (@psalm-taint-sink sql $sql) to four Pagerfanta classes; 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; removed two @param int docblock entries from getSlice() in ExtendedPdoAdapter; no signature or logic changes.
Fetch Handler Classes
src/Pagerfanta/FetchAssoc.php, src/Pagerfanta/FetchEntity.php
Added @psalm-taint-sink sql $sql docblock annotations to __invoke() methods; no signature or logic changes.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Poem

"I nibble lines of doc with care,
Taint tags placed without a scare.
A hop through comments, neat and small,
Safe SQL trails for one and all. 🐇"

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The pull request title accurately and concisely describes the main objective: adding Psalm taint annotations to detect SQL injection risks across multiple files.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 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
📝 Coding Plan
  • Generate coding plan for human review comments

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.

Tip

CodeRabbit can suggest fixes for GitHub Check annotations.

Configure the reviews.tools.github-checks setting to adjust the time to wait for GitHub Checks to complete.

@codecov

codecov Bot commented Dec 23, 2025

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (9ce6181) to head (ac70463).
⚠️ Report is 8 commits behind head on 1.x.

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.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

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 thread src/Pagerfanta/AuraSqlPagerFactory.php
Comment thread src/Pagerfanta/ExtendedPdoAdapter.php
Comment thread src/Pagerfanta/FetchAssoc.php
Comment thread src/Pagerfanta/FetchEntity.php

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

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.

The escape annotation incorrectly implies these methods perform SQL
sanitization. The actual escaping happens in ExtendedPdo::perform()
via prepared statements, not in these wrapper methods.
@koriym

koriym commented Mar 13, 2026

Copy link
Copy Markdown
Member Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Mar 13, 2026

Copy link
Copy Markdown
✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@koriym

koriym commented Mar 13, 2026

Copy link
Copy Markdown
Member Author

@coderabbitai Removed @psalm-taint-escape sql from FetchAssoc and FetchEntity. These methods don't perform sanitization themselves — the actual escaping happens in ExtendedPdo::perform() via prepared statements.

@coderabbitai

coderabbitai Bot commented Mar 13, 2026

Copy link
Copy Markdown

Tip

For best results, initiate chat on the files or code changes.

@koriym That makes perfect sense. @psalm-taint-escape sql should be placed at the actual escape boundary — in this case ExtendedPdo::perform() where PDO's prepared statement mechanism binds and safely handles the parameters. Annotating FetchAssoc::__invoke() and FetchEntity::__invoke() with it would have been incorrect: it would tell Psalm the taint is neutralized at that call site, potentially suppressing further tracking into perform() and hiding real injection risks in other paths.

If ExtendedPdo::perform() (or the underlying Aura.Sql ExtendedPdo) is not already annotated with @psalm-taint-escape sql, that would be the right place to add it to complete the taint chain.


✏️ Learnings added
Learnt from: koriym
URL: https://github.com/ray-di/Ray.AuraSqlModule/pull/89

Timestamp: 2026-03-13T00:09:00.266Z
Learning: In Ray.AuraSqlModule, SQL parameter escaping does NOT happen in `FetchAssoc::__invoke()` or `FetchEntity::__invoke()` (src/Pagerfanta/FetchAssoc.php and src/Pagerfanta/FetchEntity.php). The actual SQL escaping/sanitization occurs in `ExtendedPdo::perform()` via PDO prepared statements. Therefore, `psalm-taint-escape sql` should NOT be placed on FetchAssoc or FetchEntity; it belongs on `ExtendedPdo::perform()`.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.

Parameters already have type declarations in the method signature.
@koriym
koriym merged commit 2b45c3e into 1.x Mar 13, 2026
30 checks passed
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