Repository navigation
fix: unseal requestState tokens sealed with a fractional ttl - #714
Merged
koic merged 1 commit intoNov 28, 2026
Merged
Conversation
## Motivation and Context `RequestStateSecurity.new` accepts any positive `Numeric` as `ttl:`, but `seal` writes `exp` as `Time.now.to_i + ttl` and `unseal` only accepts an `Integer` `exp`. With a fractional ttl such as `30.5`, `60.0` or `Rational(5, 2)`, `exp` becomes a `Float` (or a `Rational`, which JSON writes as a string), so every token is rejected as `expired` the moment it comes back, and every `input_required` round that uses `requestState` fails with an invalid-state error. `ttl: Float::INFINITY` passes the constructor check and then makes `seal` raise `JSON::GeneratorError`. `seal` now writes `exp` as `(Time.now + ttl).to_i`, so `exp` stays a whole number of seconds and `unseal` is unchanged: an `Integer` ttl gives exactly the previous `exp`, and a fractional one ends within the second it falls in, the same one-second granularity an `Integer` ttl has. Tokens already issued keep unsealing. The constructor also requires a finite ttl, so `Float::INFINITY` and `Float::NAN` raise `ArgumentError` there instead of failing at the first `seal`. ## How Has This Been Tested? New tests in `test/mcp/server/request_state_security_test.rb`: tokens sealed with a ttl of `30.5`, `60.0` and `Rational(5, 2)` unseal right away and are rejected once the ttl has passed; `Float::INFINITY` and `Float::NAN` are refused by the constructor. Both fail against the previous library (`expired`, and no `ArgumentError`), and reverting either half of the change fails one of them. ## Breaking Changes None. A non-finite ttl now raises `ArgumentError` at construction; it could not seal a token before. Fixes modelcontextprotocol#713.
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.
A
RequestStateSecuritybuilt with a fractionalttl:now unseals its own tokens, and a ttl that is not finite is refused at construction.Fixes #713
Motivation and Context
RequestStateSecurity.newaccepts any positiveNumericasttl:(lib/mcp/server/request_state_security.rb:41), butsealwritesexpasTime.now.to_i + ttl(:54) andunsealonly accepts anIntegerexp(:110). With a ttl such as30.5,60.0orRational(5, 2),expbecomes aFloat(or aRational, which JSON writes as a string), so every token is rejected asexpiredthe moment it comes back, and everyinput_requiredround that usesrequestStatefails with an invalid-state error.ttl: Float::INFINITYpasses the constructor check and then makessealraiseJSON::GeneratorError.sealnow writesexpas(Time.now + ttl).to_i, soexpstays a whole number of seconds andunsealis unchanged: anIntegerttl gives exactly the previousexp, and a fractional one ends within the second it falls in, the same one-second granularity anIntegerttl has. Tokens already issued keep unsealing. The constructor also requires a finite ttl, soFloat::INFINITYandFloat::NANraiseArgumentErrorthere instead of failing at the firstseal.How Has This Been Tested?
New tests in
test/mcp/server/request_state_security_test.rb:30.5,60.0andRational(5, 2)unseal right away and are rejected once the ttl has passed.Float::INFINITYandFloat::NANare refused by the constructor.Both fail against the previous library (
InvalidStateError: expired, and noArgumentError). Reverting either half of the change fails one of them.bundle exec rake test,bundle exec rake rubocop,bundle exec yard --no-outputandbundle exec rake conformance:test(baseline) pass locally on Ruby 4.0.9.Breaking Changes
None. A non-finite ttl now raises
ArgumentErrorat construction; it could not seal a token before.Types of changes
Checklist