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 @@ -60,3 +60,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-10 - hmac.compare_digest์˜ ๋น„ASCII ๋ฌธ์ž ์ฒ˜๋ฆฌ ์ทจ์•ฝ์  ์ˆ˜์ •
**์ทจ์•ฝ์ :** `saas_web.py`์˜ `require_api_key` ๋ฏธ๋“ค์›จ์–ด์—์„œ API ํ‚ค ํ™•์ธ ์‹œ `hmac.compare_digest`์— ๋น„ASCII ๋ฌธ์ž์—ด(์˜ˆ: ์ด๋ชจ์ง€๋‚˜ ํŠน์ˆ˜๋ฌธ์ž๊ฐ€ ํฌํ•จ๋œ ์•…์„ฑ ํ—ค๋”)์„ ์ „๋‹ฌํ•  ๊ฒฝ์šฐ `TypeError`๊ฐ€ ๋ฐœ์ƒํ•˜์—ฌ 500 ์„œ๋ฒ„ ์—๋Ÿฌ(DoS ๊ณต๊ฒฉ ์œ„ํ—˜)๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ์ทจ์•ฝ์ ์„ ๋ฐœ๊ฒฌํ–ˆ์Šต๋‹ˆ๋‹ค.
**ํ•™์Šต:** `hmac.compare_digest`๋Š” Python 3์—์„œ ๋‘ ์ธ์ž๊ฐ€ ๋ชจ๋‘ ๋ฌธ์ž์—ด์ผ ๋•Œ ASCII ๋ฌธ์ž๋งŒ ํ—ˆ์šฉํ•ฉ๋‹ˆ๋‹ค. ํด๋ผ์ด์–ธํŠธ(๊ณต๊ฒฉ์ž)๊ฐ€ ์ œ์–ดํ•˜๋Š” ์ž…๋ ฅ๊ฐ’์„ ๊ธฐ๋ฐ˜์œผ๋กœ ์„œ๋ฒ„ ๋‚ด๋ถ€ ํ•จ์ˆ˜๊ฐ€ ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๋ฉด ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ฐ€์šฉ์„ฑ์ด ์ €ํ•˜๋  ์ˆ˜ ์žˆ์Œ์„ ํ™•์ธํ–ˆ์Šต๋‹ˆ๋‹ค.
**์˜ˆ๋ฐฉ:** `hmac.compare_digest`๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ๋น„๊ต ๋Œ€์ƒ์ด ๋˜๋Š” ๋ชจ๋“  ๋ฌธ์ž์—ด์„ ๋ช…์‹œ์ ์œผ๋กœ ๋ฐ”์ดํŠธ ํ˜•์‹(`.encode('utf-8')`)์œผ๋กœ ์ธ์ฝ”๋”ฉํ•œ ํ›„ ๋น„๊ตํ•˜์—ฌ, ๋น„ASCII ๋ฌธ์ž๋กœ ์ธํ•œ ์˜ˆ์™ธ ๋ฐœ์ƒ์„ ๋ฏธ์—ฐ์— ๋ฐฉ์ง€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
2 changes: 1 addition & 1 deletion saas_web.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
28 changes: 28 additions & 0 deletions tests/test_saas_web.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
import io
import json
import os
try:
import pytest
except ImportError:
pytest = None
import tempfile
import unittest
import zipfile
Expand Down Expand Up @@ -1124,5 +1128,29 @@ def test_video_content_type_accepted_by_validator(self):
)


class TestApiKeyNonAsciiHeader(unittest.IsolatedAsyncioTestCase):
async def test_api_key_non_ascii_header(self):
try:
import starlette.requests
except ImportError:
self.skipTest("starlette not installed")
from unittest.mock import patch, MagicMock
from saas_web import require_api_key

scope = {
'type': 'http',
'method': 'POST',
'path': '/shrink',
'headers': [(b'x-api-key', 'invalid-non-ascii-๐Ÿ”ฅ'.encode('utf-8'))]
}
request = starlette.requests.Request(scope)
mock_call_next = MagicMock()

with patch('saas_web.get_configured_api_keys', return_value=['valid-key']):
response = await require_api_key(request, mock_call_next)
self.assertEqual(response.status_code, 401)
import json
self.assertEqual(json.loads(response.body), {"error": "Invalid or missing API key"})

if __name__ == '__main__':
unittest.main()
Loading