Skip to content

Fix HMAC CSRF token payload#91

Open
samdark wants to merge 1 commit into
masterfrom
issue-32-update-csrf-docs
Open

Fix HMAC CSRF token payload#91
samdark wants to merge 1 commit into
masterfrom
issue-32-update-csrf-docs

Conversation

@samdark
Copy link
Copy Markdown
Member

@samdark samdark commented Jun 8, 2026

Summary

  • change HMAC CSRF tokens so the emitted payload contains expiration and random data, while the HMAC remains bound to the current identity
  • stop exposing the default session identity in decoded token payloads
  • update OWASP CSRF cheat sheet terminology/link and clarify HMAC token replay semantics

Fixes #32.

Tests

  • composer test
  • vendor/bin/psalm
  • git diff --check

Copilot AI review requested due to automatic review settings June 8, 2026 09:39
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Jun 8, 2026

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: d5825895-4505-4282-948b-c8e7129582a6

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch issue-32-update-csrf-docs

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.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This pull request updates the stateless HMAC CSRF token format so the emitted payload no longer exposes the session identity and instead carries only expiration metadata plus random data, while keeping the signature bound to the current identity. It also refreshes OWASP CSRF cheat-sheet wording/links and adds tests covering the new token semantics.

Changes:

  • Reworked HmacCsrfToken so token payload = {expiration}~{random} and the HMAC is computed using a session-bound identity without embedding it in the payload.
  • Added tests to ensure token values change per call and that decoded payloads do not contain the session identity.
  • Updated README/CHANGELOG to reflect the revised “HMAC signed token” terminology and OWASP link/semantics.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
src/Hmac/HmacCsrfToken.php Changes HMAC token encoding/validation to avoid exposing identity and to include random payload + expiration.
tests/Hmac/HmacCsrfTokenTest.php Adds coverage for non-deterministic token values and verifying identity is not present in decoded payloads.
README.md Updates documentation terminology, OWASP link anchor, and clarifies replay semantics.
CHANGELOG.md Notes the bug fix for #32 and documentation update.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 98 to +102
private function extractData(string $token): ?array
{
try {
$raw = $this->mac->getMessage(
StringHelper::base64UrlDecode($token),
$this->secretKey,
true,
);
} catch (DataIsTamperedException $e) {
$payload = StringHelper::base64UrlDecode($token);
$hashLength = $this->getHashLength();

Comment on lines +136 to 139
private function getHashLength(): int
{
return StringHelper::byteLength($this->generateHash(''));
}
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.

Whether the package needs to be updated according to OWASP?

2 participants