Skip to content

Commit

Permalink
🔒 Fix JWT verification
Browse files Browse the repository at this point in the history
A JWT has a header field `alg` that specifies the algorithm used in the
signature.

PyJWT checks this with `alg in algorithms`, this "works" because
`"HS256" in "HS256"` is true, but so is `"" in "HS256"` and
`"HS2" in "HS256"`.

Luckily there currently are no PyJWT algorithms like that.
There is no HMAC SHA2, and the Null encryption is named "none" not "".
  • Loading branch information
CharString committed Feb 8, 2024
1 parent 94008c5 commit 20d9345
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions vng_api_common/middleware.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# https://pyjwt.readthedocs.io/en/latest/usage.html#reading-headers-without-validation
# -> we can put the organization/service in the headers itself
import logging
from typing import Any, Dict, List, Optional
from typing import Any, Dict, Iterable, List, Optional

from django.conf import settings
from django.db import models, transaction
Expand All @@ -28,7 +28,7 @@ def __init__(self, encoded: str = None):
self.encoded = encoded

@property
def applicaties(self) -> Optional[list]:
def applicaties(self) -> Iterable[Applicatie]:
if self.client_id is None:
return []

Expand Down Expand Up @@ -138,7 +138,7 @@ def payload(self) -> Optional[Dict[str, Any]]:
payload = jwt.decode(
self.encoded,
key,
algorithms="HS256",
algorithms=["HS256"],
leeway=settings.JWT_LEEWAY,
)
except jwt.InvalidSignatureError:
Expand Down

0 comments on commit 20d9345

Please sign in to comment.