Skip to content

Correctly check content type #114

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

Merged
merged 1 commit into from
Mar 4, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 3 additions & 2 deletions src/cryptojwt/key_bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from .jwk.rsa import RSAKey
from .jwk.rsa import new_rsa_key
from .utils import as_unicode
from .utils import check_content_type
from .utils import httpc_params_loader

__author__ = "Roland Hedberg"
Expand Down Expand Up @@ -513,8 +514,8 @@ def _parse_remote_response(self, response):
"""
# Check if the content type is the right one.
try:
if response.headers["Content-Type"] != "application/json":
LOGGER.warning("Wrong Content_type (%s)", response.headers["Content-Type"])
if not check_content_type(response.headers["Content-Type"], "application/json"):
LOGGER.warning("Wrong Content_type (%s)", respeonse.headers["Content-Type"])
except KeyError:
pass

Expand Down
7 changes: 7 additions & 0 deletions src/cryptojwt/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import base64
import cgi
import functools
import importlib
import json
Expand Down Expand Up @@ -264,3 +265,9 @@ def httpc_params_loader(httpc_params):
if "timeout" not in httpc_params:
httpc_params["timeout"] = DEFAULT_HTTPC_TIMEOUT
return httpc_params


def check_content_type(content_type, mime_type):
"""Return True if the content type contains the MIME type"""
mt, _ = cgi.parse_header(content_type)
return mime_type == mt
17 changes: 17 additions & 0 deletions tests/test_31_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from cryptojwt.utils import check_content_type


def test_check_content_type():
assert check_content_type(content_type="application/json", mime_type="application/json") == True
assert (
check_content_type(
content_type="application/json; charset=utf-8", mime_type="application/json"
)
== True
)
assert (
check_content_type(
content_type="application/html; charset=utf-8", mime_type="application/json"
)
== False
)