-
Notifications
You must be signed in to change notification settings - Fork 0
fix(auth): compare API keys at the raw ASGI boundary #520
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
Draft
seonghobae
wants to merge
13
commits into
main
Choose a base branch
from
sentinel/fix-hmac-non-ascii-1412282215803553404
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.
Draft
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
a67bc12
🛡️ Sentinel: [MEDIUM] hmac.compare_digest의 Non-ASCII 예외 처리 수정
seonghobae 4710e64
🛡️ Sentinel: [MEDIUM] hmac.compare_digest의 Non-ASCII 예외 처리 수정
seonghobae 8f7cdb8
test(auth): expose valid Unicode API-key mismatch at ASGI boundary
seonghobae 2f09917
🛡️ Sentinel: [MEDIUM] hmac.compare_digest의 Non-ASCII 예외 처리 수정
seonghobae bd126a0
test(auth): reject duplicated raw API key headers
seonghobae 4e8f965
fix(auth): compare exactly one raw API key header
seonghobae 9777cc0
🛡️ Sentinel: [MEDIUM] hmac.compare_digest의 Non-ASCII 예외 처리 수정
seonghobae fa241e0
docs(auth): remove inaccurate sentinel transport note
seonghobae 9ee4627
chore(auth): adopt current protected main without rewriting auth repair
seonghobae 66f6040
docs(auth): record raw-header API-key contract
seonghobae cf730d0
docs(auth): align sentinel guidance with raw ASGI boundary
seonghobae 0eb2d1b
Fix Unicode API Key validation crash and prevent header ambiguity\n\n…
seonghobae 08d1c3c
merge(auth): restore raw ASGI credential contract on current main
seonghobae 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 |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import asyncio | ||
| import os | ||
| import unittest | ||
| from unittest.mock import patch | ||
|
|
||
| try: | ||
| import saas_web | ||
| from starlette.requests import Request | ||
| from starlette.responses import Response | ||
|
|
||
| _HAS_WEB_STACK = True | ||
| except ImportError: # pragma: no cover - optional integration dependency boundary | ||
| _HAS_WEB_STACK = False | ||
|
|
||
|
|
||
| @unittest.skipUnless(_HAS_WEB_STACK, "web integration dependencies are not installed") | ||
| class TestApiKeyHeaderMultiplicity(unittest.TestCase): | ||
| """Lock the credential boundary to exactly one raw X-API-Key header.""" | ||
|
|
||
| @staticmethod | ||
| def _request(raw_headers: list[tuple[bytes, bytes]]) -> "Request": | ||
| return Request( | ||
| { | ||
| "type": "http", | ||
| "http_version": "1.1", | ||
| "method": "POST", | ||
| "scheme": "https", | ||
| "path": "/shrink", | ||
| "raw_path": b"/shrink", | ||
| "query_string": b"", | ||
| "headers": raw_headers, | ||
| "client": ("127.0.0.1", 12345), | ||
| "server": ("codec-carver.test", 443), | ||
| } | ||
| ) | ||
|
|
||
| def _assert_duplicate_is_rejected(self, raw_values: list[bytes]) -> None: | ||
| async def call_next(_request: "Request") -> "Response": | ||
| self.fail("duplicated credentials must not reach the protected handler") | ||
|
|
||
| request = self._request([(b"x-api-key", value) for value in raw_values]) | ||
| with patch.dict(os.environ, {"CODEC_CARVER_API_KEYS": "안녕"}): | ||
| response = asyncio.run(saas_web.require_api_key(request, call_next)) | ||
|
|
||
| self.assertEqual(response.status_code, 401) | ||
|
|
||
| def test_duplicate_api_key_headers_fail_closed_when_values_differ(self) -> None: | ||
| self._assert_duplicate_is_rejected( | ||
| ["안녕".encode("utf-8"), "다름".encode("utf-8")] | ||
| ) | ||
|
|
||
| def test_duplicate_api_key_headers_fail_closed_when_values_match(self) -> None: | ||
| value = "안녕".encode("utf-8") | ||
| self._assert_duplicate_is_rejected([value, value]) | ||
|
|
||
|
|
||
| if __name__ == "__main__": # pragma: no cover | ||
| unittest.main() |
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 |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import asyncio | ||
| import os | ||
| import unittest | ||
| from unittest.mock import patch | ||
|
|
||
| try: | ||
| import saas_web | ||
| from starlette.requests import Request | ||
| from starlette.responses import Response | ||
|
|
||
| _HAS_WEB_STACK = True | ||
| except ImportError: # pragma: no cover - optional integration dependency boundary | ||
| _HAS_WEB_STACK = False | ||
|
|
||
|
|
||
| @unittest.skipUnless(_HAS_WEB_STACK, "web integration dependencies are not installed") | ||
| class TestUnicodeApiKeyContract(unittest.TestCase): | ||
| """Exercise API-key authentication from the raw ASGI header boundary.""" | ||
|
|
||
| @staticmethod | ||
| def _request(raw_api_key: bytes) -> "Request": | ||
| return Request( | ||
| { | ||
| "type": "http", | ||
| "http_version": "1.1", | ||
| "method": "POST", | ||
| "scheme": "https", | ||
| "path": "/shrink", | ||
| "raw_path": b"/shrink", | ||
| "query_string": b"", | ||
| "headers": [(b"x-api-key", raw_api_key)], | ||
| "client": ("127.0.0.1", 12345), | ||
| "server": ("codec-carver.test", 443), | ||
| } | ||
| ) | ||
|
|
||
| def test_configured_unicode_key_matches_its_raw_utf8_header(self) -> None: | ||
| reached_handler = False | ||
|
|
||
| async def call_next(_request: "Request") -> "Response": | ||
| nonlocal reached_handler | ||
| reached_handler = True | ||
| return Response(status_code=204) | ||
|
|
||
| configured_key = "안녕" | ||
| with patch.dict(os.environ, {"CODEC_CARVER_API_KEYS": configured_key}): | ||
| response = asyncio.run( | ||
| saas_web.require_api_key( | ||
| self._request(configured_key.encode("utf-8")), | ||
| call_next, | ||
| ) | ||
| ) | ||
|
|
||
| self.assertTrue( | ||
| reached_handler, | ||
| "a valid configured Unicode key must not be rejected after ASGI header decoding", | ||
| ) | ||
| self.assertEqual(response.status_code, 204) | ||
|
|
||
| def test_different_raw_utf8_key_is_rejected(self) -> None: | ||
| async def call_next(_request: "Request") -> "Response": | ||
| self.fail("invalid credentials must not reach the protected handler") | ||
|
|
||
| with patch.dict(os.environ, {"CODEC_CARVER_API_KEYS": "안녕"}): | ||
| response = asyncio.run( | ||
| saas_web.require_api_key( | ||
| self._request("다름".encode("utf-8")), | ||
| call_next, | ||
| ) | ||
| ) | ||
|
|
||
| self.assertEqual(response.status_code, 401) | ||
|
|
||
|
|
||
| if __name__ == "__main__": # pragma: no cover | ||
| unittest.main() |
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
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.
🔍 API-key storage still violates governance
get_configured_api_keysstill reads secrets from the environment. AGENTS.md explicitly requires migrating this known deviation to a credential registry.Was this helpful? React with 👍 or 👎 to provide feedback.