You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 5eb4e84
Browse filesBrowse the repository at this point in the historyBrowse files
[Server] Rotate the requestState signing key with a key ring
Builder::setRequestState() and RequestStateCodec take a list of keys:
the first key signs, every key verifies, so an operator can put a new
key in front and drop the old one once the states it signed expired,
without failing the multi round-trip calls in flight.
A state names no key: verification tries each key of the ring, so the
wire format is unchanged and instances of an older release still verify
the states a ring signs. A single key works as before.
Copy file name to clipboardExpand all lines: CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -26,6 +26,7 @@ All notable changes to `mcp/sdk` will be documented in this file.
26
26
* Add `Psr6SessionStore` and `Psr6NotificationBus`, which work like `Psr16SessionStore` and `Psr16NotificationBus` over a PSR-6 cache pool. `Builder::setDiscovery()` also accepts a PSR-6 pool as `$cache`.
27
27
* Fix a nullable tool parameter with a default (e.g. `?string $category = 'books'`) receiving the default when the client explicitly sends `null`: `ReferenceHandler` now passes the `null` through.
28
28
* Fix a `subscriptions/listen` stream that can carry nothing (the server announces none of the requested types, or the client asks for none) being held open for the full subscription lifetime: it is acknowledged with an empty `notifications` filter and closed gracefully right away, instead of pinning a PHP-FPM worker. Adds `NotificationFilter::isEmpty()`.
29
+
* Rotate the `requestState` signing key without failing the calls in flight: `Builder::setRequestState()` and `RequestStateCodec` take a list of keys, the first signing and every key verifying. The wire format is unchanged, and a single key works as before.
Copy file name to clipboardExpand all lines: src/Server/Stateless/RequestStateCodec.php
+48-10Lines changed: 48 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -23,6 +23,10 @@
23
23
* {@see self::verify()} refuses anything whose MAC or expiry does not hold.
24
24
* Signed, not encrypted: nothing secret belongs in the payload.
25
25
*
26
+
* Takes one key or a key ring to rotate without failing the calls in flight:
27
+
* the first key signs, every key verifies. The value names no key, so the
28
+
* wire format is the same either way.
29
+
*
26
30
* @author Christopher Hertel <mail@christopher-hertel.de>
27
31
*/
28
32
finalclass RequestStateCodec
@@ -32,14 +36,36 @@ final class RequestStateCodec
32
36
33
37
privateconstALGORITHM = 'sha256';
34
38
39
+
/** @var non-empty-list<string> */
40
+
privatereadonlyarray$keys;
41
+
42
+
/**
43
+
* @param string|list<string> $key the signing key, or a key ring whose first key signs and whose every key verifies
44
+
*/
35
45
publicfunction__construct(
36
-
privatereadonlystring$key,
46
+
string|array$key,
37
47
privatereadonlyint$ttlSeconds = 600,
38
48
) {
39
-
if (\strlen($this->key) < self::MINIMUM_KEY_BYTES) {
40
-
thrownewInvalidArgumentException(\sprintf('The requestState signing key must be at least %d bytes, got %d.', self::MINIMUM_KEY_BYTES, \strlen($this->key)));
49
+
$keys = \is_string($key) ? [$key] : $key;
50
+
51
+
if ([] === $keys || !array_is_list($keys)) {
52
+
thrownewInvalidArgumentException('The requestState key ring must be a non-empty list of keys.');
53
+
}
54
+
55
+
foreach ($keysas$i => $candidate) {
56
+
$name = \is_string($key) ? 'The requestState signing key' : \sprintf('The requestState signing key at index %d', $i);
57
+
58
+
if (!\is_string($candidate)) {
59
+
thrownewInvalidArgumentException(\sprintf('%s must be a string, got %s.', $name, get_debug_type($candidate)));
60
+
}
61
+
62
+
if (\strlen($candidate) < self::MINIMUM_KEY_BYTES) {
63
+
thrownewInvalidArgumentException(\sprintf('%s must be at least %d bytes, got %d.', $name, self::MINIMUM_KEY_BYTES, \strlen($candidate)));
64
+
}
41
65
}
42
66
67
+
$this->keys = $keys;
68
+
43
69
if ($this->ttlSeconds < 1) {
44
70
thrownewInvalidArgumentException(\sprintf('The requestState TTL must be at least one second, got %d.', $this->ttlSeconds));
45
71
}
@@ -55,13 +81,13 @@ public function mint(array $payload, ?int $now = null): string
0 commit comments