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: pass usedforsecurity to hashlib.md5 to fix error on FIPS-enabled systems #1366

Merged
merged 15 commits into from Dec 17, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions starlette/_compat.py
@@ -0,0 +1,14 @@
import hashlib
import inspect


# TODO: Remove when dropping support for PY38. inspect.signature() is used to
# detect whether the usedforsecurity argument is available as this fix may also
# have been applied by downstream package maintainers to other versions in
# their repositories.
adriangb marked this conversation as resolved.
Show resolved Hide resolved
if "usedforsecurity" in inspect.signature(hashlib.md5).parameters:
def md5_hexdigest(data: bytes, *, usedforsecurity: bool = True) -> str:
return hashlib.md5(data, usedforsecurity=usedforsecurity).hexdigest()
else:
def md5_hexdigest(data: bytes, *, usedforsecurity: bool = True) -> str:
return hashlib.md5(data).hexdigest()
4 changes: 2 additions & 2 deletions starlette/responses.py
@@ -1,4 +1,3 @@
import hashlib
import http.cookies
import json
import os
Expand All @@ -12,6 +11,7 @@

import anyio

from starlette._compat import md5_hexdigest
from starlette.background import BackgroundTask
from starlette.concurrency import iterate_in_threadpool
from starlette.datastructures import URL, MutableHeaders
Expand Down Expand Up @@ -287,7 +287,7 @@ def set_stat_headers(self, stat_result: os.stat_result) -> None:
content_length = str(stat_result.st_size)
last_modified = formatdate(stat_result.st_mtime, usegmt=True)
etag_base = str(stat_result.st_mtime) + "-" + str(stat_result.st_size)
etag = hashlib.md5(etag_base.encode()).hexdigest()
etag = md5_hexdigest(etag_base.encode(), usedforsecurity=False)
Kludex marked this conversation as resolved.
Show resolved Hide resolved

self.headers.setdefault("content-length", content_length)
self.headers.setdefault("last-modified", last_modified)
Expand Down