From cded108f129b0bbb0bb4662b0ee030e5af418d3f Mon Sep 17 00:00:00 2001 From: Andy Wang Date: Thu, 26 Nov 2026 18:37:02 +0800 Subject: [PATCH] fix: unseal requestState tokens sealed with a fractional ttl ## 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 #713. --- lib/mcp/server/request_state_security.rb | 7 ++++--- .../mcp/server/request_state_security_test.rb | 20 +++++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/lib/mcp/server/request_state_security.rb b/lib/mcp/server/request_state_security.rb index bc96a858..92922655 100644 --- a/lib/mcp/server/request_state_security.rb +++ b/lib/mcp/server/request_state_security.rb @@ -38,8 +38,8 @@ def initialize(key:, ttl: DEFAULT_TTL, audience: nil) unless key.is_a?(String) && key.bytesize == KEY_BYTES raise ArgumentError, "key must be a #{KEY_BYTES}-byte String" end - unless ttl.is_a?(Numeric) && ttl.positive? - raise ArgumentError, "ttl must be a positive number of seconds" + unless ttl.is_a?(Numeric) && ttl.finite? && ttl.positive? + raise ArgumentError, "ttl must be a positive, finite number of seconds" end @key = key.dup.force_encoding(Encoding::BINARY).freeze @@ -51,7 +51,8 @@ def initialize(key:, ttl: DEFAULT_TTL, audience: nil) def seal(state, method:, target:, arguments_digest:) claims = { v: 1, - exp: Time.now.to_i + @ttl, + # Whole seconds, as `verify!` requires: an `Integer` TTL keeps its exact value, a fractional one ends within the second it falls in. + exp: (Time.now + @ttl).to_i, m: method, t: target, a: arguments_digest, diff --git a/test/mcp/server/request_state_security_test.rb b/test/mcp/server/request_state_security_test.rb index 29b7d794..045eaff2 100644 --- a/test/mcp/server/request_state_security_test.rb +++ b/test/mcp/server/request_state_security_test.rb @@ -14,6 +14,26 @@ class RequestStateSecurityTest < ActiveSupport::TestCase assert_raises(ArgumentError) { RequestStateSecurity.new(key: KEY, ttl: 0) } end + test "rejects a ttl that is not finite" do + assert_raises(ArgumentError) { RequestStateSecurity.new(key: KEY, ttl: Float::INFINITY) } + assert_raises(ArgumentError) { RequestStateSecurity.new(key: KEY, ttl: Float::NAN) } + end + + test "unseals a token sealed with a fractional ttl until it expires" do + [30.5, 60.0, Rational(5, 2)].each do |ttl| + security = RequestStateSecurity.new(key: KEY, ttl: ttl) + sealed = security.seal("state", method: "tools/call", target: "t", arguments_digest: "d") + + assert_equal "state", security.unseal(sealed, method: "tools/call", target: "t", arguments_digest: "d") + + travel ttl + 2 do + assert_raises(RequestStateSecurity::InvalidStateError) do + security.unseal(sealed, method: "tools/call", target: "t", arguments_digest: "d") + end + end + end + end + test "seals and unseals a state bound to the originating request" do security = RequestStateSecurity.new(key: KEY, audience: "my_server") sealed = security.seal("plain-state", method: "tools/call", target: "my_tool", arguments_digest: "digest")