Skip to content
Closed
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions .jules/sentinel.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,8 @@
**Vulnerability:** Path traversal in `media_shrinker.py` via unresolved `..` segments or symlink escapes before deriving conversion output paths.
**Learning:** `Path.relative_to()` is only a lexical containment check unless both the source and root have first been resolved into canonical absolute paths. Relative paths and symlinks can otherwise bypass root-boundary assumptions.
**Prevention:** Resolve both source and root once, reject sources outside the resolved root with a sanitized `MediaShrinkerError`, and derive `rel_source` from the resolved paths before planning outputs.

## 2024-08-14 - hmac.compare_digest ๋น„-ASCII DoS ์ทจ์•ฝ์ 
**Vulnerability:** HTTP ํ—ค๋”(x-api-key)์— ๋น„-ASCII ๋ฌธ์ž๊ฐ€ ํฌํ•จ๋  ๋•Œ `hmac.compare_digest`๊ฐ€ `TypeError`๋ฅผ ๋ฐœ์ƒ์‹œ์ผœ ์„œ๋ฒ„ ์—๋Ÿฌ(500) ๋ฐ DoS ์ทจ์•ฝ์  ์œ ๋ฐœ.
**Learning:** Python์˜ `hmac.compare_digest`๋Š” ๋น„-ASCII ๋ฌธ์ž๊ฐ€ ํฌํ•จ๋œ ๋ฌธ์ž์—ด ๋น„๊ต๋ฅผ ์ง€์›ํ•˜์ง€ ์•Š๋Š”๋‹ค.
**Prevention:** `hmac.compare_digest`์— ์‚ฌ์šฉ์ž ์ž…๋ ฅ์„ ์ „๋‹ฌํ•˜๊ธฐ ์ „์— ํ•ญ์ƒ `.encode('utf-8')`์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ”์ดํŠธ๋กœ ๋ช…์‹œ์  ์ธ์ฝ”๋”ฉ์„ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•œ๋‹ค.
3 changes: 2 additions & 1 deletion saas_web.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,9 @@ async def require_api_key(request: Request, call_next):
configured_keys = get_configured_api_keys()
if configured_keys and not (request.method == "GET" and request.url.path == "/"):
provided_key = request.headers.get("x-api-key", "")
provided_key_bytes = provided_key.encode("utf-8")
if not any(
hmac.compare_digest(provided_key, key) for key in configured_keys
hmac.compare_digest(provided_key_bytes, key.encode("utf-8")) for key in configured_keys
):
return JSONResponse(
status_code=401,
Expand Down
23 changes: 23 additions & 0 deletions tests/test_saas_web.py
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,29 @@ def test_missing_header_rejected_when_keys_configured(self):
self.assertEqual(response.json(), {"error": "Invalid or missing API key"})
self.assertNotIn("secret-key", response.text)

def test_non_ascii_key_handled_safely(self):
# We must bypass the TestClient for this test because TestClient/httpx natively
# rejects non-ASCII headers. We want to ensure the middleware itself doesn't crash.
import starlette.requests
from saas_web import require_api_key
import asyncio

async def mock_call_next(request):
return "SUCCESS"

scope = {
'type': 'http',
'method': 'GET',
'path': '/api/foo',
'headers': [(b'x-api-key', 'non-ascii๐ŸŽ‰'.encode('utf-8'))]
}
request = starlette.requests.Request(scope)

with patch.dict(os.environ, {"CODEC_CARVER_API_KEYS": "secret-key"}):
response = asyncio.run(require_api_key(request, mock_call_next))

self.assertEqual(response.status_code, 401)

def test_wrong_key_rejected(self):
with patch.dict(os.environ, {"CODEC_CARVER_API_KEYS": "secret-key"}):
response = self._post_shrink(headers={"X-API-Key": "wrong-key"})
Expand Down
Loading