Take the portal's JWKS from the tunnel and verify by kid
The welcome frame's keys replace the set pinned at enrolment, but only over wss:// or loopback, the rule enrolment applies to the portal address. Portal tokens are verified against the key their kid names, so the hub can publish a new key beside the old one. A welcome without keys, from an older hub, leaves the pinned set alone.
This commit is contained in:
@@ -6,10 +6,12 @@ flows repository. Deleting it is the local, unilateral way to sever the
|
||||
connection — the portal's tokens stop verifying immediately, whatever the
|
||||
portal still believes.
|
||||
|
||||
The portal's public keys are stored here rather than fetched: they are pinned
|
||||
at enrolment, when a person was holding a claim code they had just read off the
|
||||
portal's own screen. A hub whose DNS or TLS is later hijacked cannot re-key an
|
||||
instance that already enrolled; it can only fail to verify.
|
||||
The portal's public keys are stored here rather than fetched per request. They
|
||||
are pinned at enrolment, when a person was holding a claim code they had just
|
||||
read off the portal's own screen, and replaced by the set the hub sends each
|
||||
time the tunnel attaches, so the hub can publish a new key beside the old one.
|
||||
That makes the tunnel's TLS what a later key rests on: only a hub that answers
|
||||
at the enrolled address, to this instance's own credential, can hand it one.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -123,10 +125,16 @@ def decode_portal_token(token: str) -> dict[str, Any]:
|
||||
if config is None:
|
||||
raise InvalidTokenError("this instance is not enrolled with a portal")
|
||||
|
||||
# By `kid`, so the hub can publish the next key beside the current one and
|
||||
# tokens signed with either verify while it rotates.
|
||||
kid = jwt.get_unverified_header(token).get("kid")
|
||||
try:
|
||||
key = jwt.PyJWKSet.from_dict(config.jwks).keys[0]
|
||||
except (IndexError, jwt.PyJWKError) as exc:
|
||||
raise InvalidTokenError(f"the pinned portal key is unusable: {exc}") from exc
|
||||
keys = jwt.PyJWKSet.from_dict(config.jwks).keys
|
||||
except (jwt.PyJWKError, jwt.PyJWKSetError) as exc:
|
||||
raise InvalidTokenError(f"the pinned portal keys are unusable: {exc}") from exc
|
||||
key = next((candidate for candidate in keys if candidate.key_id == kid), None)
|
||||
if key is None:
|
||||
raise InvalidTokenError("signed with a key the portal has not published")
|
||||
|
||||
claims: dict[str, Any] = jwt.decode(
|
||||
token,
|
||||
|
||||
Reference in New Issue
Block a user