Skip to content

Commit 88d0b5c

Browse files
committed
Require exp when verifying access tokens
ruby-jwt only checks expiry when the claim is present. A signed token with no exp therefore never expired. Match protocol#1706, python-sdks#779, and node-sdks#710.
1 parent 418ee8a commit 88d0b5c

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

‎lib/livekit/token_verifier.rb‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,15 @@ def initialize(api_key: nil, api_secret: nil)
1010
end
1111

1212
def verify(token)
13-
decoded_token = JWT.decode(token, @api_secret, true, algorithm: AccessToken::SIGNING_ALGORITHM)
13+
decoded_token = JWT.decode(
14+
token,
15+
@api_secret,
16+
true,
17+
{
18+
algorithm: AccessToken::SIGNING_ALGORITHM,
19+
required_claims: ["exp"],
20+
},
21+
)
1422
decoded = decoded_token.first
1523
if decoded["iss"] != @api_key
1624
raise "Invalid issuer"

‎spec/livekit/token_verifier_spec.rb‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@
1717
expect(grant.attributes["mykey"]).to eq("myvalue")
1818
end
1919

20+
it "fails when exp is missing" do
21+
payload = {
22+
"iss" => TEST_KEY,
23+
"sub" => "user",
24+
"nbf" => Time.now.to_i - 5,
25+
"video" => { "roomJoin" => true, "room" => "testroom" },
26+
}
27+
jwt = JWT.encode(payload, TEST_SECRET, "HS256")
28+
v = described_class.new(api_key: TEST_KEY, api_secret: TEST_SECRET)
29+
expect { v.verify(jwt) }.to raise_error(JWT::MissingRequiredClaim)
30+
end
31+
2032
it "fails on expired tokens" do
2133
token = LiveKit::AccessToken.new(api_key: TEST_KEY, api_secret: TEST_SECRET,
2234
identity: "test_identity", ttl: -10)

0 commit comments

Comments
 (0)