Skip to content

Commit 8b8a073

Browse files
committed
Test invalid Plex JWK signatures
1 parent ceb3cec commit 8b8a073

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

tests/test_myplex.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# -*- coding: utf-8 -*-
2+
import jwt
3+
24
import pytest
35
from plexapi.exceptions import BadRequest, NotFound, Unauthorized
46
from plexapi.myplex import MyPlexAccount, MyPlexInvite, MyPlexJWTLogin
@@ -368,7 +370,7 @@ def test_myplex_ping(account):
368370
assert account.ping()
369371

370372

371-
def test_myplex_jwt_login(account, tmp_path):
373+
def test_myplex_jwt_login(account, tmp_path, monkeypatch):
372374
jwtlogin = MyPlexJWTLogin(
373375
token=account.authToken,
374376
scopes=['username', 'email', 'friendly_name']
@@ -378,7 +380,6 @@ def test_myplex_jwt_login(account, tmp_path):
378380
jwtlogin.generateKeypair(keyfiles=(tmp_path / 'private.key', tmp_path / 'public.key'))
379381
jwtlogin.registerDevice()
380382
jwtToken = jwtlogin.refreshJWT()
381-
assert jwtlogin.decodePlexJWT()
382383
assert jwtlogin.decodedJWT['user']['username'] == account.username
383384
assert MyPlexAccount(token=jwtToken) == account
384385

@@ -390,5 +391,14 @@ def test_myplex_jwt_login(account, tmp_path):
390391
assert jwtlogin.verifyJWT()
391392
newjwtToken = jwtlogin.refreshJWT()
392393
assert newjwtToken != jwtToken
393-
assert jwtlogin.decodePlexJWT()
394394
assert MyPlexAccount(token=newjwtToken) == account
395+
396+
plexPublicJWKs = jwtlogin._getPlexPublicJWK()
397+
invalidJWK = plexPublicJWKs[0].copy()
398+
invalidJWK['x'] += b'0'
399+
monkeypatch.setattr(MyPlexJWTLogin, "_getPlexPublicJWK", lambda: [invalidJWK] + plexPublicJWKs)
400+
assert jwtlogin.decodePlexJWT()
401+
402+
monkeypatch.setattr(MyPlexJWTLogin, "_getPlexPublicJWK", lambda: [invalidJWK])
403+
with pytest.raises(jwt.InvalidSignatureError):
404+
jwtlogin.decodePlexJWT()

0 commit comments

Comments
 (0)