Repository navigation
fix(upload): normalize Windows separators before basename extraction #393
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
12
commits into
main
Choose a base branch
from
sentinel/fix-windows-path-traversal-5281566321372097578
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 6 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
efc022a
보안: 업로드 파일 이름의 윈도우 경로 탐색 취약점 수정
seonghobae 340e03e
보안: 다운로드되지 않은 완료 작업의 임시 파일 무한 누적에 의한 리소스 고갈(DoS) 취약점 수정
seonghobae aba9b8c
docs(security): describe Windows basename normalization accurately
seonghobae 2abeab6
test(security): assert sanitized uploads stay within root
seonghobae 3e3fff0
보안: 개발 서버의 하드코딩된 외부 바인드(0.0.0.0) 취약점 수정
seonghobae a7d2fbc
test(upload): restack canonical filename boundary on current main
seonghobae 50fcb88
보안: 개발 서버의 하드코딩된 외부 바인드(0.0.0.0) 취약점 수정
seonghobae 5417e24
보안: 다운로드되지 않은 완료 작업의 임시 파일 무한 누적에 의한 리소스 고갈(DoS) 취약점 수정
seonghobae afd9762
보안: 설정된 API 키가 없을 때 인증이 우회되는 취약점 수정 (Fail-Closed)
seonghobae 2aa243b
보안: 다운로드되지 않은 완료 작업의 임시 파일 무한 누적에 의한 리소스 고갈(DoS) 취약점 수정
seonghobae 4188c5c
보안: 설정된 API 키가 없을 때 인증이 우회되는 취약점 수정 (Fail-Closed)
seonghobae 9580e25
보안: 취약점이 있는 httpx2 의존성을 2.13.1 버전으로 업데이트
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| """Cross-platform upload filename normalization regressions. | ||
|
|
||
| These tests lock the public upload boundary to one canonical basename helper and | ||
| verify that every converter source remains inside its request-scoped root. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import io | ||
| import json | ||
| import tempfile | ||
| import unittest | ||
| import zipfile | ||
| from pathlib import Path | ||
| from types import SimpleNamespace | ||
| from unittest.mock import patch | ||
|
|
||
| try: | ||
| from fastapi import BackgroundTasks | ||
|
|
||
| import saas_web | ||
|
|
||
| _HAS_FASTAPI = True | ||
| except ImportError: | ||
| _HAS_FASTAPI = False | ||
|
|
||
|
|
||
| @unittest.skipUnless( | ||
| _HAS_FASTAPI, | ||
| "fastapi not installed (optional integration dependency)", | ||
| ) | ||
| class UploadFilenameNormalizationTests(unittest.TestCase): | ||
| """Keep Windows and POSIX upload names canonical and workspace-contained.""" | ||
|
|
||
| def test_canonical_basename_handles_both_separator_families_and_fallbacks(self): | ||
| """One helper normalizes client paths and rejects empty relative names.""" | ||
| cases = { | ||
| r"..\..\etc\passwd": "passwd", | ||
| "../../etc/passwd": "passwd", | ||
| r"C:\recordings\meeting.wav": "meeting.wav", | ||
| "/tmp/recordings/meeting.wav": "meeting.wav", | ||
| "meeting.wav": "meeting.wav", | ||
| "": "upload.tmp", | ||
| ".": "upload.tmp", | ||
| "..": "upload.tmp", | ||
| } | ||
|
|
||
| for raw_name, expected in cases.items(): | ||
| with self.subTest(raw_name=raw_name): | ||
| self.assertEqual( | ||
| saas_web._safe_upload_basename(raw_name), | ||
| expected, | ||
| ) | ||
|
opencode-agent[bot] marked this conversation as resolved.
Outdated
|
||
|
|
||
| @patch("saas_web.media_shrinker.convert_file") | ||
| def test_single_upload_uses_canonical_contained_source(self, mock_convert_file): | ||
| """The single-upload converter receives the basename inside its input root.""" | ||
| with tempfile.TemporaryDirectory() as output_directory: | ||
| output_path = Path(output_directory) / "output.flac" | ||
| output_path.write_bytes(b"audio") | ||
| mock_convert_file.return_value = [SimpleNamespace(output_path=output_path)] | ||
|
|
||
| response = saas_web.shrink_media( | ||
| BackgroundTasks(), | ||
| file=SimpleNamespace( | ||
| filename=r"..\..\etc\passwd", | ||
| content_type="audio/wav", | ||
| file=io.BytesIO(b"audio"), | ||
| ), | ||
| target_bytes=10_000, | ||
| ) | ||
|
|
||
| call = mock_convert_file.call_args.kwargs | ||
| source = Path(call["source"]) | ||
| root = Path(call["root"]) | ||
| try: | ||
| self.assertEqual(Path(response.path), output_path) | ||
| self.assertEqual(source.name, "passwd") | ||
| self.assertTrue(source.resolve().is_relative_to(root.resolve())) | ||
| self.assertEqual(source.parent.resolve(), root.resolve()) | ||
| finally: | ||
| saas_web.cleanup_temp_dir(source.parent.parent) | ||
|
|
||
| @patch("saas_web.media_shrinker.convert_file") | ||
| def test_batch_upload_checks_every_converter_source(self, mock_convert_file): | ||
| """Every batch item is canonicalized and contained before conversion.""" | ||
|
|
||
| def fake_convert(source, root, output_dir, target_bytes): | ||
| self.assertEqual(target_bytes, 10_000) | ||
| output_path = Path(output_dir) / f"{Path(source).stem}.flac" | ||
| output_path.write_bytes(b"shrunk") | ||
| return [SimpleNamespace(output_path=output_path)] | ||
|
|
||
| mock_convert_file.side_effect = fake_convert | ||
| response = saas_web.shrink_media_batch( | ||
| BackgroundTasks(), | ||
| files=[ | ||
| SimpleNamespace( | ||
| filename=r"..\..\etc\passwd", | ||
| content_type="audio/wav", | ||
| file=io.BytesIO(b"first"), | ||
| ), | ||
| SimpleNamespace( | ||
| filename="folder/meeting.wav", | ||
| content_type="audio/wav", | ||
| file=io.BytesIO(b"second"), | ||
| ), | ||
| ], | ||
| target_bytes=10_000, | ||
| ) | ||
|
|
||
| archive_path = Path(response.path) | ||
| try: | ||
| with zipfile.ZipFile(archive_path) as archive: | ||
| manifest = json.loads(archive.read("results.json")) | ||
| self.assertEqual( | ||
| [item["filename"] for item in manifest["results"]], | ||
| ["passwd", "meeting.wav"], | ||
| ) | ||
| self.assertEqual(mock_convert_file.call_count, 2) | ||
| observed_names = [] | ||
| for call in mock_convert_file.call_args_list: | ||
| source = Path(call.kwargs["source"]) | ||
| root = Path(call.kwargs["root"]) | ||
| observed_names.append(source.name) | ||
| self.assertTrue(source.resolve().is_relative_to(root.resolve())) | ||
| self.assertEqual(source.parent.resolve(), root.resolve()) | ||
| self.assertEqual(observed_names, ["passwd", "meeting.wav"]) | ||
| finally: | ||
| saas_web.cleanup_temp_dir(archive_path.parent) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
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.
Uh oh!
There was an error while loading. Please reload this page.