Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import hmac

from fastapi import Request
from fastapi.responses import JSONResponse
from starlette.middleware.base import BaseHTTPMiddleware
Expand Down Expand Up @@ -219,8 +221,11 @@ def _check_authentication(self, request: Request, auth_header: tuple[str, str]):
# Get the actual header value from the request
actual_value = request.headers.get(expected_key)

# Validate the header value matches the expected value
if not actual_value or actual_value != expected_value:
# Validate the header value matches the expected value. Use
# hmac.compare_digest so the compare runs in time that does not
# depend on the position of the first differing byte. The short
# circuit on `not actual_value` keeps None and empty values out.
if not actual_value or not hmac.compare_digest(actual_value, expected_value):
return {
"valid": False,
"message": f"This API is restricted, expecting header '{expected_key}' with valid value",
Expand Down