Skip to content

fix(encryption): read the recipient header from the builder state - #698

Merged
Spomky merged 1 commit into
4.2.xfrom
fix/jwe-builder-shared-header-after-recipient
Aug 28, 2026
Merged

fix(encryption): read the recipient header from the builder state#698
Spomky merged 1 commit into
4.2.xfrom
fix/jwe-builder-shared-header-after-recipient

Conversation

@Spomky

@Spomky Spomky commented Aug 28, 2026

Copy link
Copy Markdown
Member

Fixes #678.

JWEBuilder::withSharedProtectedHeader() and JWEBuilder::withSharedHeader() iterated over the already
registered recipients and called $recipient->getHeader(), but addRecipient() stores plain arrays
(['key' => ..., 'header' => ..., 'key_encryption_algorithm' => ...]).

Setting a shared header after a recipient therefore raised a fatal
Error: Call to a member function getHeader() on array instead of performing the
duplicated-header-parameter check. As a consequence that check only ever ran when the recipient was
added last.

Both loops now read $recipient['header'].

Tests

Three regression tests in EncrypterTest, all failing before the fix:

  • sharedHeaderCanBeSetAfterTheRecipientHasBeenAdded — builds, serializes to jwe_json_flattened and
    decrypts a JWE whose shared (unprotected) header is set after addRecipient()
  • duplicatedHeaderWhenTheSharedHeaderIsSetAfterTheRecipient
  • duplicatedHeaderWhenTheSharedProtectedHeaderIsSetAfterTheRecipient

The PHPStan baseline is updated accordingly: the method.nonObject entry for getHeader() is gone and
the offsetAccess.nonOffsetAccessible count for 'header' goes from 1 to 3.

Replacing those array entries by readonly value objects remains part of the builder redesign planned for
4.3/5.0, as does the order sensitivity of addRecipient() mentioned at the end of the issue.

@Spomky Spomky added this to the 4.2.1 milestone Aug 28, 2026
@Spomky Spomky self-assigned this Aug 28, 2026
@Spomky Spomky added the bug label Aug 28, 2026
`JWEBuilder::withSharedProtectedHeader()` and `JWEBuilder::withSharedHeader()`
called `getHeader()` on the already registered recipients, but `addRecipient()`
stores plain arrays. Setting a shared header after a recipient therefore raised
a fatal `Error` instead of running the duplicated-header-parameter check.

Closes #678
@Spomky
Spomky force-pushed the fix/jwe-builder-shared-header-after-recipient branch from e643a02 to 892925a Compare August 28, 2026 12:30
@Spomky
Spomky merged commit 949247b into 4.2.x Aug 28, 2026
17 checks passed
@Spomky
Spomky deleted the fix/jwe-builder-shared-header-after-recipient branch August 28, 2026 12:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JWEBuilder: fatal error when a shared header is set after a recipient has been added

1 participant