-
-
Notifications
You must be signed in to change notification settings - Fork 50
feat: attribute requests to clients and API keys in logs #265
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
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
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,73 @@ | ||
| """Unit tests for redact_sensitive_fields — secrets scrubbed, analytics fields kept.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import pytest | ||
|
|
||
| from infrastructure.logging import redact_sensitive_fields | ||
|
|
||
| REDACTED = "***REDACTED***" | ||
|
|
||
|
|
||
| def _redact(event_dict: dict) -> dict: | ||
| return redact_sensitive_fields(None, "info", dict(event_dict)) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "field", | ||
| [ | ||
| "password", | ||
| "password_hash", | ||
| "token", | ||
| "api_key", | ||
| "authorization", | ||
| "cookie", | ||
| "refresh_token", | ||
| "access_token", | ||
| "secret", | ||
| "key", | ||
| # substring heuristic | ||
| "jwt_secret", | ||
| "client_secret", | ||
| "device_token", | ||
| "raw_password", | ||
| ], | ||
| ) | ||
| def test_secret_fields_redacted(field: str): | ||
| assert _redact({field: "s3cr3t"})[field] == REDACTED | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("field", "value"), | ||
| [ | ||
| ("has_password", True), | ||
| ("password_protected", False), | ||
| ("key_id", "6a065366720e95786b0608fb"), | ||
| ("key_prefix", "abcd1234"), | ||
| ("token_prefix", "abcd1234"), | ||
| ("query_keys", ["password", "alias"]), | ||
| ], | ||
| ) | ||
| def test_safe_fields_pass_through(field: str, value): | ||
| assert _redact({field: value})[field] == value | ||
|
|
||
|
|
||
| def test_structural_keys_untouched(): | ||
| event = {"level": "info", "event": "url_created", "timestamp": "t", "logger": "x"} | ||
| assert _redact(event) == event | ||
|
|
||
|
|
||
| def test_mixed_event_dict(): | ||
| out = _redact( | ||
| { | ||
| "event": "api_key_created", | ||
| "key_id": "abc", | ||
| "key_prefix": "abcd1234", | ||
| "api_key": "spoo_raw", | ||
| "has_password": True, | ||
| } | ||
| ) | ||
| assert out["key_id"] == "abc" | ||
| assert out["key_prefix"] == "abcd1234" | ||
| assert out["has_password"] is True | ||
| assert out["api_key"] == REDACTED |
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,118 @@ | ||
| """Unit tests for request logging middleware helpers (_client_tag, _auth_kind).""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import pytest | ||
| from starlette.requests import Request | ||
|
|
||
| from middleware.logging import _auth_kind, _client_tag | ||
|
|
||
|
|
||
| def _request(headers: dict[str, str] | None = None) -> Request: | ||
| raw_headers = [(k.lower().encode(), v.encode()) for k, v in (headers or {}).items()] | ||
| scope = { | ||
| "type": "http", | ||
| "method": "GET", | ||
| "path": "/", | ||
| "headers": raw_headers, | ||
| "query_string": b"", | ||
| } | ||
| return Request(scope) | ||
|
|
||
|
|
||
| # ── _client_tag ────────────────────────────────────────────────────────────── | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("value", "expected"), | ||
| [ | ||
| ("dashboard", ("dashboard", None)), | ||
| ("landing", ("landing", None)), | ||
| ("snap/2.1.0", ("snap", "2.1.0")), | ||
| ("cli/0.3.0-beta.1", ("cli", "0.3.0-beta.1")), | ||
| ("bot", ("bot", None)), | ||
| ], | ||
| ) | ||
| def test_client_tag_valid(value: str, expected: tuple): | ||
| assert _client_tag(_request({"X-Spoo-Client": value})) == expected | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "value", | ||
| [ | ||
| "", | ||
| "Dashboard", # uppercase slug | ||
| "a" * 33, # slug too long | ||
| "snap/" + "1" * 17, # version too long | ||
| "snap/2.1.0/extra", | ||
| "sn ap", | ||
| "snap;DROP", | ||
| ], | ||
| ) | ||
| def test_client_tag_invalid_treated_as_absent(value: str): | ||
| assert _client_tag(_request({"X-Spoo-Client": value})) == (None, None) | ||
|
|
||
|
|
||
| def test_client_tag_missing_header(): | ||
| assert _client_tag(_request()) == (None, None) | ||
|
|
||
|
|
||
| def test_client_tag_strips_whitespace(): | ||
| assert _client_tag(_request({"X-Spoo-Client": " raycast "})) == ( | ||
| "raycast", | ||
| None, | ||
| ) | ||
|
|
||
|
|
||
| # ── _auth_kind ─────────────────────────────────────────────────────────────── | ||
|
|
||
|
|
||
| def test_auth_kind_api_key(): | ||
| req = _request({"Authorization": "Bearer spoo_abc123"}) | ||
| assert _auth_kind(req) == "api_key" | ||
|
|
||
|
|
||
| def test_auth_kind_jwt_bearer(): | ||
| req = _request({"Authorization": "Bearer aaa.bbb.ccc"}) | ||
| assert _auth_kind(req) == "jwt" | ||
|
|
||
|
|
||
| def test_auth_kind_bearer_other(): | ||
| req = _request({"Authorization": "Bearer something-else"}) | ||
| assert _auth_kind(req) == "bearer_other" | ||
|
|
||
|
|
||
| def test_auth_kind_access_token_cookie(): | ||
| req = _request({"Cookie": "access_token=aaa.bbb.ccc"}) | ||
| assert _auth_kind(req) == "jwt_cookie" | ||
|
|
||
|
|
||
| def test_auth_kind_legacy_session_cookie(): | ||
| req = _request({"Cookie": "session=xyz"}) | ||
| assert _auth_kind(req) == "session_cookie" | ||
|
|
||
|
|
||
| def test_auth_kind_access_token_beats_session_cookie(): | ||
| req = _request({"Cookie": "session=xyz; access_token=aaa.bbb.ccc"}) | ||
| assert _auth_kind(req) == "jwt_cookie" | ||
|
|
||
|
|
||
| def test_auth_kind_bearer_beats_cookie(): | ||
| req = _request( | ||
| {"Authorization": "Bearer spoo_abc", "Cookie": "access_token=aaa.bbb.ccc"} | ||
| ) | ||
| assert _auth_kind(req) == "api_key" | ||
|
|
||
|
|
||
| def test_auth_kind_anonymous(): | ||
| assert _auth_kind(_request()) == "anonymous" | ||
|
|
||
|
|
||
| def test_auth_kind_lowercase_bearer_scheme(): | ||
| # Must match dependencies/auth.py, which accepts the scheme | ||
| # case-insensitively; a lowercase bearer API key with a session | ||
| # cookie present must not be classified as jwt_cookie. | ||
| req = _request( | ||
| {"Authorization": "bearer spoo_abc123", "Cookie": "access_token=a.b.c"} | ||
| ) | ||
| assert _auth_kind(req) == "api_key" |
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.
Uh oh!
There was an error while loading. Please reload this page.