Fix JWT::JWK::Set sharing its key collection when copied - #751
Open
anakinj wants to merge 1 commit 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
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: