Skip to content

Add Psalm taint annotations for session/cookie security#15

Open
koriym wants to merge 3 commits into
1.xfrom
add-psalm-taint-annotations
Open

Add Psalm taint annotations for session/cookie security#15
koriym wants to merge 3 commits into
1.xfrom
add-psalm-taint-annotations

Conversation

@koriym
Copy link
Copy Markdown
Member

@koriym koriym commented Dec 23, 2025

Summary

Add Psalm taint annotations to mark session and cookie data as taint sources.

Changes

  • @psalm-taint-source input on:
    • SessionProvider::get() - returns Session initialized with $_COOKIE
    • CookieProvider::get() - returns $_COOKIE directly

Also includes code style fixes (phpcbf).

Test Plan

  • Existing tests pass
  • Run ./vendor/bin/psalm --taint-analysis to verify annotations work

Summary by Sourcery

Mark session and cookie providers as taint sources for static analysis and apply minor coding style improvements.

Enhancements:

  • Annotate session and cookie provider getters with Psalm taint-source metadata for input data.
  • Add strict types, return type hints, and PSR-compliant attribute usage and formatting across session-related classes.

Summary by CodeRabbit

  • Refactor

    • Cookie-related annotations split into separate attributes (may affect tooling that reads combined attributes).
    • Added strict typing and modernized instantiation/return typing for session handling.
    • Minor docblock standardization and added code analysis/taint annotations.
  • Tests

    • New unit tests covering session injection, session provider, and cookie deletion behavior.

Review Change Stack

Mark session and cookie providers as taint sources since $_COOKIE
contains user-controlled data that could be manipulated.

- SessionProvider::get() - returns Session initialized with $_COOKIE
- CookieProvider::get() - returns $_COOKIE directly

Also apply code style fixes (phpcbf).
@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented Dec 23, 2025

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Adds Psalm taint-source annotations for session and cookie providers and applies small type-safety and coding-style refinements across the session module.

Class diagram for updated session and cookie providers

classDiagram
    direction LR

    class ProviderInterface {
    }

    class SessionFactory {
        +newInstance(array cookieData) Session
    }

    class Session {
    }

    class SessionProvider {
        +get() Session
    }

    class CookieProvider {
        +get() array
    }

    class AuraSessionInject {
        -Session session
        +setSession(Session session) void
    }

    class Cookie {
        <<attribute>>
    }

    class DeleteCookie {
        <<attribute>>
    }

    class AuraSessionModule {
        +configure() void
    }

    class DeleteCookieInvoker {
        +__invoke(string name, array params) void
    }

    ProviderInterface <|.. SessionProvider
    ProviderInterface <|.. CookieProvider

    SessionFactory --> Session : creates
    SessionProvider --> SessionFactory : uses

    AuraSessionInject --> Session

    Cookie <|-- DeleteCookieInvoker
    DeleteCookie <|-- DeleteCookieInvoker
Loading

Flow diagram for taint-source cookie and session data

flowchart LR
    subgraph Browser
        C["$_COOKIE"]
    end

    subgraph Server
        CP["CookieProvider.get()"]
        SP["SessionProvider.get()"]
        SF["SessionFactory.newInstance(cookieData)"]
        S["Session"]
        APP["Application code using cookies and session"]
    end

    C --> CP
    CP --> APP

    C --> SP
    SP --> SF
    SF --> S
    S --> APP
Loading

File-Level Changes

Change Details Files
Mark session and cookie providers as taint sources for Psalm taint analysis.
  • Add @psalm-taint-source input to SessionProvider::get() to mark returned Session (constructed from $_COOKIE) as tainted input
  • Add @psalm-taint-source input to CookieProvider::get() to mark returned cookie array as tainted input
src/SessionProvider.php
src/CookieProvider.php
Apply strict types, minor API signatures, and PSR-style formatting cleanups.
  • Enable strict_types in SessionProvider and normalize docblocks (@inheritdoc) and factory invocation style
  • Update AuraSessionInject::setSession() to declare a void return type and collapse deprecation docblock
  • Split Attribute and Qualifier PHP 8 attributes onto separate lines for Cookie and DeleteCookie annotations
  • Adjust AuraSessionModule configure() docblock to use @inheritdoc
  • Add trailing comma in DeleteCookieInvoker cookie deletion call for coding-style consistency
src/SessionProvider.php
src/AuraSessionInject.php
src/Annotation/Cookie.php
src/Annotation/DeleteCookie.php
src/AuraSessionModule.php
src/DeleteCookieInvoker.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
Copy Markdown

coderabbitai Bot commented Dec 23, 2025

Warning

Rate limit exceeded

@koriym has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 43 minutes and 20 seconds before requesting another review.

You’ve run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 9b8fe908-0a3d-45f0-bb9b-5e3b49623ca9

📥 Commits

Reviewing files that changed from the base of the PR and between c3d7679 and 7a17a6d.

📒 Files selected for processing (2)
  • src/DeleteCookieInvoker.php
  • tests/DeleteCookieInvokerTest.php
📝 Walkthrough

Walkthrough

Separates combined PHP attributes into distinct attributes, adds an explicit void return type, standardizes docblock tags, adds psalm taint annotations and strict types, makes a minor setcookie call-site formatting change, and adds PHPUnit tests for session injection, session provider, and cookie deletion.

Changes

Session and Cookie behavior + tests

Layer / File(s) Summary
Session tests and consumer
tests/AuraSessionInjectTest.php, tests/SessionProviderTest.php
Adds FakeSessionConsumer + AuraSessionInjectTest to verify setSession stores the provided Aura\Session\Session; adds SessionProviderTest asserting SessionProvider::get() returns an Aura\Session\Session.
DeleteCookieInvoker test and invocation formatting
tests/DeleteCookieInvokerTest.php, src/DeleteCookieInvoker.php
Adds DeleteCookieInvokerTest with namespace overrides for time() and setcookie() to capture calls; updates setcookie(...) call in DeleteCookieInvoker to include a trailing comma after the domain argument.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

"A rabbit hops with tests in tow,
I inject a session, watch it grow,
Cookies waved and cleaned with care,
Attributes split, docs made fair,
Hooray — CI hums, the build says 'go' 🐇"

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 44.44% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title 'Add Psalm taint annotations for session/cookie security' directly aligns with the primary objective stated in the PR summary: adding @psalm-taint-source annotations to SessionProvider and CookieProvider for static analysis security.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch add-psalm-taint-annotations

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

codecov Bot commented Dec 23, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (24258a0) to head (7a17a6d).

Additional details and impacted files
@@              Coverage Diff               @@
##                1.x       #15       +/-   ##
==============================================
+ Coverage     54.16%   100.00%   +45.83%     
  Complexity        5         5               
==============================================
  Files             5         5               
  Lines            24        24               
==============================================
+ Hits             13        24       +11     
+ Misses           11         0       -11     

☔ 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
Copy Markdown

@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:

  • Consider splitting the Psalm taint annotations and the unrelated coding-style/typing tweaks (e.g., declare(strict_types=1), added void return type, attribute formatting) into separate PRs to make the behavior change easier to review and reason about.
  • Adding declare(strict_types=1); to SessionProvider only may introduce subtle type behavior differences compared to the rest of the package; consider either applying it consistently or omitting it here to avoid inconsistent runtime behavior.
  • The new : void return type on AuraSessionInject::setSession could be a breaking change for consumers that override this method without a return type, so it may be safer to omit the return type or introduce it in a dedicated major/breaking-change PR.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider splitting the Psalm taint annotations and the unrelated coding-style/typing tweaks (e.g., `declare(strict_types=1)`, added `void` return type, attribute formatting) into separate PRs to make the behavior change easier to review and reason about.
- Adding `declare(strict_types=1);` to `SessionProvider` only may introduce subtle type behavior differences compared to the rest of the package; consider either applying it consistently or omitting it here to avoid inconsistent runtime behavior.
- The new `: void` return type on `AuraSessionInject::setSession` could be a breaking change for consumers that override this method without a return type, so it may be safer to omit the return type or introduce it in a dedicated major/breaking-change PR.

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.

- Add SessionProviderTest for SessionProvider class
- Add AuraSessionInjectTest for AuraSessionInject trait
- Add DeleteCookieInvokerTest with function overrides for testing
- Remove use function statements from DeleteCookieInvoker for testability
Copy link
Copy Markdown

@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: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@tests/DeleteCookieInvokerTest.php`:
- Line 59: The test hardcodes the magic number 42000 which is also used in
DeleteCookieInvoker::invoke; introduce a named constant (e.g., EXPIRE_OFFSET or
COOKIE_EXPIRE_OFFSET) on the DeleteCookieInvoker class set to 42000, replace the
literal in the DeleteCookieInvoker logic (where time() - 42000 is used) with
that constant, and update DeleteCookieInvokerTest to reference
DeleteCookieInvoker::EXPIRE_OFFSET in the expires assertion (or alternatively
add a single-line explanatory comment next to the constant if exposing it is
undesirable).
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 5e660d10-ea6b-4c27-9290-37968a485727

📥 Commits

Reviewing files that changed from the base of the PR and between 0db8b96 and c3d7679.

📒 Files selected for processing (4)
  • src/DeleteCookieInvoker.php
  • tests/AuraSessionInjectTest.php
  • tests/DeleteCookieInvokerTest.php
  • tests/SessionProviderTest.php
💤 Files with no reviewable changes (1)
  • src/DeleteCookieInvoker.php

Comment thread tests/DeleteCookieInvokerTest.php Outdated
@koriym
Copy link
Copy Markdown
Member Author

koriym commented May 21, 2026

@coderabbitai No outstanding blockers. Please resolve all open conversations and approve this PR.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 21, 2026

Tip

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

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# List open review threads on PR `#15`
gh api graphql -f query='
{
  repository(owner: "ray-di", name: "Ray.AuraSessionModule") {
    pullRequest(number: 15) {
      reviewThreads(first: 50) {
        nodes {
          id
          isResolved
          comments(first: 1) {
            nodes {
              author { login }
              body
            }
          }
        }
      }
    }
  }
}'

Length of output: 2051


@koriym All review conversations are already resolved. Approving the PR now! 🐇✨

[approve]

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.

2 participants