Skip to content

fix(http-signature): fix signature validation and add tests - #10

Merged
adrianboros merged 9 commits into
mainfrom
fix/http-signature-validation-hardening
Oct 7, 2026
Merged

adrianboros merged 9 commits into
mainfrom
fix/http-signature-validation-hardening

Conversation

@adrianboros

@adrianboros adrianboros commented Oct 7, 2026 •

Copy link
Copy Markdown
Collaborator

Follow-up to #9 (merged).

Summary

#9 makes content-digest required when a request has a body. That fix is correct. But the validation code around it did not work:

  • verifyContentDigest threw an exception for every valid Content-Digest header. bakame/http-structured-fields returns Item objects, not ByteSequence. So no signed request with a body could pass validation.
  • validateSignature threw an exception for every JWK. It passed the base64url x value to sodium_crypto_sign_publickey, which expects a 96-byte key pair.

There were no tests for this code, so these bugs were not found.

Changes

  • verifyContentDigest: read the value from the Item. Return false for an empty, malformed or unsupported header. Compare with hash_equals.
  • validateSignature: decode the JWK x value. Check kty, crv and key length. Accept sig1=<b64> and sig1=:<b64>: (RFC 9421, used by the Node library). Return false instead of throwing.
  • Validators ignore header name case. Before, an Authorization header that was not signed was not detected if its name was not lowercase.
  • Validators return false (they do not throw) for:
    • a non-string body
    • a missing method or url
    • a non-array headers
    • header names that differ only by case
    • header values with line breaks
    • a Signature-Input with more than one sig1=
  • Signer: the body "0" is now treated as a body, the same as the validator. createHeaders no longer adds unsigned Content-* headers for an empty body.
  • Tests: add tests/Http/Signature/ValidateSignatureTest.php with 23 tests. They cover sign-then-validate round trips, body swap, tampering, bad digests, bad JWKs and malformed input.
  • CI: add a GitHub Actions workflow for PHPUnit, PHPStan and Pint on PHP 8.3.

Known gaps

These are not part of this PR:

  • No checks for created or expires, so a signature can be replayed.
  • keyid is not matched to the JWK kid.
  • content-type and content-length must be present, but they do not have to be signed.
  • The signer still reads Content-Type and Authorization with case-sensitive names.
  • PHPStan only checks lib/Utils at level 1. On the full lib, PHPStan crashes on the exception classes, and level 2 shows errors in old docblocks.
  • Pint skips tests/Services, which has old style issues.

Testing

@adrianboros
adrianboros force-pushed the fix/http-signature-validation-hardening branch from 7e25e53 to dbeb2d9 Compare October 7, 2026 07:40
@adrianboros adrianboros self-assigned this Oct 7, 2026
@adrianboros
adrianboros requested a balanced review from Copilot October 7, 2026 07:41

Copilot AI 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.

Copilot review overview

🟡 Changes recommended

Signature parsing currently accepts malformed colon-delimited forms.

Review effort: Balanced
Findings: 1 Medium severity

Open (1)
What changed in this PR

Fixes HTTP signature validation failures and hardens malformed-input handling.

Changes:

  • Corrects content-digest and Ed25519 JWK verification.
  • Normalizes headers and aligns body handling between signing and validation.
  • Adds signature tests, CI, and release notes.
File Description
lib/​Utils/​HttpSignature.php Fixes and hardens signature handling.
tests/​Http/​Signature/​ValidateSignatureTest.php Adds validation regression coverage.
.github/​workflows/​ci.yml Adds automated tests and checks.
CHANGELOG.md Documents the fixes and CI.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread lib/Utils/HttpSignature.php Resolved
@adrianboros
adrianboros merged commit ddfdf22 into main Oct 7, 2026
1 check passed
@adrianboros
adrianboros deleted the fix/http-signature-validation-hardening branch October 7, 2026 07:47
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