diff --git a/.jules/sentinel.md b/.jules/sentinel.md index 9c9d083b..a8376676 100644 --- a/.jules/sentinel.md +++ b/.jules/sentinel.md @@ -1,3 +1,8 @@ +## 2026-08-28 - [Sentinel: Unhandled hmac.compare_digest TypeError (DoS)] +**취약점:** `hmac.compare_digest`에서 발생한 예외가 처리되지 않아 500 Internal Server Error를 유발하고, 이를 통한 서비스 거부(DoS) 공격이 가능함 (CWE-400). +**학습 내용:** Python의 `hmac.compare_digest`는 ASCII 문자가 아닌 문자가 포함된 문자열을 비교할 때 `TypeError`를 발생시킴. 악의적인 사용자가 X-API-Key 헤더에 비-ASCII 문자를 전송하면 401 에러 대신 애플리케이션의 에러율을 높여 서버 장애를 일으킬 수 있음. +**예방 조치:** 모든 API 키 문자열 입력을 `utf-8` 바이트로 인코딩한 후 `hmac.compare_digest`로 전달하여 안전하게 비교를 수행하도록 함. + ## 2026-07-25 - [Cross-platform upload basename normalization] **Behavior:** Upload metadata now interprets both forward slashes and backslashes as path separators before extracting a basename. **Learning:** On POSIX systems, `pathlib.Path(filename).name` retains backslashes because they are ordinary characters there. That caused inconsistent manifest and converter filenames for Windows-style client paths. The upload itself is still written inside a trusted temporary workspace, and batch archive entry names are generated outputs; this change does not establish a filesystem traversal or archive-entry escape. diff --git a/saas_web.py b/saas_web.py index 63265e94..071b1419 100644 --- a/saas_web.py +++ b/saas_web.py @@ -114,7 +114,7 @@ async def require_api_key(request: Request, call_next): if configured_keys and not (request.method == "GET" and request.url.path == "/"): provided_key = request.headers.get("x-api-key", "") if not any( - hmac.compare_digest(provided_key, key) for key in configured_keys + hmac.compare_digest(provided_key.encode("utf-8"), key.encode("utf-8")) for key in configured_keys ): return JSONResponse( status_code=401, diff --git a/tests/test_saas_web.py b/tests/test_saas_web.py index 3b57e033..d06f9a96 100644 --- a/tests/test_saas_web.py +++ b/tests/test_saas_web.py @@ -1222,6 +1222,13 @@ def test_video_content_type_accepted_by_validator(self): ) ) + @patch.dict(os.environ, {"CODEC_CARVER_API_KEYS": "secret1,secret2"}, clear=True) + def test_hmac_non_ascii_api_key_401(self): + # Pass headers as bytes to bypass httpx's strict ASCII string check. + response = client.get("/jobs/123", headers={b"X-API-Key": "test🌟".encode("utf-8")}) + self.assertEqual(response.status_code, 401) + self.assertEqual(response.json(), {"error": "Invalid or missing API key"}) + if __name__ == "__main__": unittest.main()