Repository navigation
π‘οΈ Sentinel: [CRITICAL] hmac.compare_digest TypeError DoS μ·¨μ½μ μμ #721
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
Open
seonghobae
wants to merge
2
commits into
main
Choose a base branch
from
sentinel-fix-hmac-compare-digest-4038775018812930782
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1222,6 +1222,25 @@ def test_video_content_type_accepted_by_validator(self): | |
| ) | ||
| ) | ||
|
|
||
| class TestSaaSHmacSecurity(unittest.TestCase): | ||
| def test_hmac_compare_digest_non_ascii(self): | ||
| from unittest import mock | ||
| from fastapi.testclient import TestClient | ||
|
Comment on lines
+1225
to
+1228
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| import saas_web | ||
|
|
||
| with mock.patch.dict("os.environ", {"CODEC_CARVER_API_KEYS": "test-key-1, test-key-2"}): | ||
| client = TestClient(saas_web.app) | ||
|
|
||
| # Test valid request (to ensure endpoint logic doesn't block it initially) | ||
| response = client.get("/jobs/123", headers={"X-API-Key": "test-key-1"}) | ||
| self.assertNotEqual(response.status_code, 401) | ||
|
|
||
| # Test request with non-ASCII header | ||
| # Using raw bytes tuples to prevent client string decoding errors | ||
| headers = [(b"x-api-key", "νκΈν€".encode("utf-8"))] | ||
| response = client.get("/jobs/123", headers=headers) | ||
| self.assertEqual(response.status_code, 401) | ||
| self.assertEqual(response.json(), {"error": "Invalid or missing API key"}) | ||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
π‘ UTF-8 API keys fail authentication
When a client sends a configured non-ASCII key as UTF-8 header bytes,
require_api_keyreturns 401. Starlette decodes headers as Latin-1, so re-encoding that text cannot recover the client's bytes.Learn more
The authentication middleware compares an HTTP header against keys loaded from configuration. Starlette exposes header values as Latin-1-decoded strings, even when a client sends UTF-8 bytes. Encoding that string as UTF-8 changes non-ASCII bytes before the comparison, so the configured UTF-8 key cannot match. The configured key remains a Python Unicode string in get_configured_api_keys.
Example: With a configured key
cafΓ©, a client sendsX-API-Keyasb'caf\xc3\xa9'. Starlette providescafΓΒ©; encoding it yieldsb'caf\xc3\x83\xc2\xa9', not the configuredb'caf\xc3\xa9'. The request returns 401 instead of reaching the endpoint.Recommended fix: Compare the raw X-API-Key header bytes from
request.scope['headers']against configured keys encoded as UTF-8, preserving the existing missing-header and duplicate-header behavior. Add tests for matching and mismatching non-ASCII configured keys with raw UTF-8 header bytes.Was this helpful? React with π or π to provide feedback.