Fix JWT::JWK::Set sharing its key collection when copied - #751
Open
anakinj wants to merge 2 commits into
Open
Conversation
anakinj
force-pushed
the
fix/jwk-set-key-array-ownership
branch
from
September 2, 2026 13:49
4ddf558 to
d4b2413
Compare
JWT::JWK::Set.new(other_set) returned the source set's `keys` array by reference, despite the "Simple duplication" comment on that branch. The class also defined no `initialize_copy`, so `dup` and `clone` shared the array as well. Mutating a copy through `add`, `<<`, `delete`, `select!`, `reject!` or `uniq!` therefore mutated the original too, which matters because key sets are commonly cached and shared between consumers. `union` was unaffected, as `merge` rebinds `@keys` with `+=` instead of mutating. Both paths now copy the array. The JWK objects themselves stay shared. Fixes jwt#750
anakinj
force-pushed
the
fix/jwk-set-key-array-ownership
branch
from
September 2, 2026 14:01
d4b2413 to
ce143f6
Compare
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
initialize_copy should duplicate @keys directly to avoid corrupting copied subclass state through an overridden accessor.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
Fixes shared key-array ownership when copying JWT::JWK::Set instances.
Changes:
- Duplicates key collections during construction and object copying.
- Adds regression tests.
- Updates the changelog.
File summaries
| File | Description |
|---|---|
lib/jwt/jwk/set.rb |
Adds independent collection copying. |
spec/jwt/jwk/set_spec.rb |
Tests copy isolation. |
CHANGELOG.md |
Documents the fix. |
Review details
- Files reviewed: 3/3 changed files
- Comments generated: 1
- Review effort level: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
After `super`, `initialize_copy` already holds a reference to the source set's array in `@keys`, so duplicate that directly instead of going through the `keys` reader. A subclass overriding `keys` with a filtered or computed view then keeps its own state on copy. Also move the union regression test out of the duplication context, since it does not duplicate anything.
anakinj
force-pushed
the
fix/jwk-set-key-array-ownership
branch
from
September 3, 2026 13:07
f0a6c9b to
650851a
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes #750.
JWT::JWK::Set.new(other_set)returned the source set'skeysarray by reference, despite the# Simple duplicationcomment on that branch. The class also defined noinitialize_copy, sodupandcloneshared the array as well — a case not covered in the issue.Mutating a copy through
add,<<,delete,select!,reject!oruniq!therefore mutated the original too:This matters because key sets are commonly cached and shared between consumers, so one caller filtering its own copy can silently empty another's.
unionwas unaffected, sincemergerebinds@keyswith+=rather than mutating in place. There is a regression test for that too.Both copy paths now
dupthe array. TheJWT::JWKobjects themselves are still shared — only the collection is copied.Checklist
Before the PR can be merged be sure the following are checked: