Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: skipped content type validation for GET requests #28

Merged
merged 4 commits into from
Oct 11, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions jwt_signature_validator/encoded_payload.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,17 @@

class EncodedPayloadSignatureMiddleware:
def __init__(
self,
app,
jwt_secret: str,
jwt_algorithms: list[str],
protect_hosts: list = None,
self,
app,
jwt_secret: str,
jwt_algorithms: list[str],
protect_hosts: list = None,
):
self.app = app
self.protect_hosts = protect_hosts
self.jwt_secret = jwt_secret
self.jwt_algorithms = jwt_algorithms
self.validate_request_types = ["POST", "PUT", "DELETE"]
if not self.protect_hosts:
self.protect_hosts = ["*"]

Expand Down Expand Up @@ -71,10 +72,10 @@ async def verify_signature():
try:
signature = jwt.decode(signature, self.jwt_secret, self.jwt_algorithms)
except (
InvalidSignatureError,
ExpiredSignatureError,
MissingRequiredClaimError,
DecodeError,
InvalidSignatureError,
ExpiredSignatureError,
MissingRequiredClaimError,
DecodeError,
) as inv_exp:
logging.error(inv_exp)
raise HTTPException(
Expand All @@ -84,14 +85,14 @@ async def verify_signature():
return {"type": receive_["type"], "body": signature, "more_body": False}

headers = MutableHeaders(scope=scope)
if headers.get("Content-Type") is None:
if headers.get("Content-Type") is None and scope.get("method", "POST") in self.validate_request_types:
raise HTTPException(status_code=406, detail="Unacceptable Content Type!")
elif headers.get("Content-Type") == "application/json":
host = headers.get("host", "").split(":")[0]
is_protected_host = False
for pattern in self.protect_hosts:
if host == pattern or (
pattern.startswith("*") and host.endswith(pattern[1:])
pattern.startswith("*") and host.endswith(pattern[1:])
):
is_protected_host = True
break
Expand Down