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
4 changes: 4 additions & 0 deletions .jules/sentinel.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,7 @@
**Vulnerability:** Argument Injection via relative paths starting with a hyphen in command-line utilities (CWE-88).
**Learning:** Even when `ffmpeg` inputs are protected by `-i`, command-line utilities (like `ffprobe` and `ffmpeg` filters) can interpret user input (like a file path) starting with a hyphen (e.g., `-version.wav`) as options if passed as a relative path.
**Prevention:** File paths must be converted to absolute paths using `.resolve()` before they are passed to `subprocess.run`. This prefixes the path with a root, drive, or UNC prefix rather than a leading hyphen, thereby averting the possibility of argument injection.
## 2024-07-04 - ์„œ๋ธŒํ”„๋กœ์„ธ์Šค ํƒ€์ž„์•„์›ƒ ๋ถ€์žฌ๋กœ ์ธํ•œ DoS ์ทจ์•ฝ์ 
**Vulnerability:** `subprocess.run` ํ˜ธ์ถœ ์‹œ `timeout` ์ธ์ž๊ฐ€ ๋ˆ„๋ฝ๋˜์–ด ์™ธ๋ถ€ ๋ฐ”์ด๋„ˆ๋ฆฌ(ffmpeg, ffprobe ๋“ฑ)๊ฐ€ ๋ฌดํ•œ ๋Œ€๊ธฐ ์ƒํƒœ์— ๋น ์งˆ ๊ฒฝ์šฐ ์‹œ์Šคํ…œ ๋ฆฌ์†Œ์Šค ๊ณ ๊ฐˆ(DoS)์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.
**Learning:** Python์˜ `subprocess.run`์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ํƒ€์ž„์•„์›ƒ์„ ์ ์šฉํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ, ์‹ ๋ขฐํ•  ์ˆ˜ ์—†๋Š” ์™ธ๋ถ€ ๋ช…๋ น์ด๋‚˜ I/O ๋Œ€๊ธฐ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์˜๊ตฌ์ ์œผ๋กœ ๋ฉˆ์ถœ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
**Prevention:** `subprocess.run`์„ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ํ•ญ์ƒ ์ ์ ˆํ•œ `timeout`(์˜ˆ: ํ”„๋กœ๋ธŒ๋Š” 60์ดˆ, ๋ณ€ํ™˜์€ 3600์ดˆ)์„ ์„ค์ •ํ•˜๊ณ  `subprocess.TimeoutExpired` ์˜ˆ์™ธ๋ฅผ ์ฒ˜๋ฆฌํ•˜์—ฌ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์•ˆ์ „ํ•˜๊ฒŒ ๋ณต๊ตฌ๋  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
80 changes: 50 additions & 30 deletions media_shrinker.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,9 +465,13 @@ def probe_media(
"-i",
str(Path(source_path).resolve()),
]
completed = subprocess.run(
command, check=False, capture_output=True, text=True, shell=False
)
try:
completed = subprocess.run(
command, check=False, capture_output=True, text=True, shell=False, timeout=60
)
except subprocess.TimeoutExpired as exc:
raise MediaShrinkerError(f"ffprobe timed out for {source_path}") from exc

if completed.returncode != 0:
raise MediaShrinkerError(
f"ffprobe failed for {source_path}: {completed.stderr.strip()}"
Expand Down Expand Up @@ -520,18 +524,23 @@ def detect_silence_intervals(
) -> list[SilenceInterval]:
"""Run ffmpeg silencedetect and return paired silence intervals."""

completed = subprocess.run(
build_silencedetect_command(
source_path,
ffmpeg_path=ffmpeg_path,
silence_noise=silence_noise,
silence_min_duration_seconds=silence_min_duration_seconds,
),
check=False,
capture_output=True,
text=True,
shell=False,
)
try:
completed = subprocess.run(
build_silencedetect_command(
source_path,
ffmpeg_path=ffmpeg_path,
silence_noise=silence_noise,
silence_min_duration_seconds=silence_min_duration_seconds,
),
check=False,
capture_output=True,
text=True,
shell=False,
timeout=3600,
)
except subprocess.TimeoutExpired as exc:
raise MediaShrinkerError(f"silencedetect timed out for {source_path}") from exc

if completed.returncode != 0:
raise MediaShrinkerError(
f"silencedetect failed for {source_path}: {completed.stderr.strip()}"
Expand Down Expand Up @@ -646,13 +655,18 @@ def download_from_icloud(source_path: Path, *, brctl_path: str = "brctl") -> Non
raise MediaShrinkerError(
f"iCloud download requested but '{brctl_path}' was not found"
)
completed = subprocess.run(
build_icloud_download_command(source_path, brctl_path=brctl_path),
check=False,
capture_output=True,
text=True,
shell=False,
)
try:
completed = subprocess.run(
build_icloud_download_command(source_path, brctl_path=brctl_path),
check=False,
capture_output=True,
text=True,
shell=False,
timeout=3600,
)
except subprocess.TimeoutExpired as exc:
raise MediaShrinkerError(f"iCloud download timed out for {source_path}") from exc

if completed.returncode != 0:
raise MediaShrinkerError(
f"iCloud download failed for {source_path}: {completed.stderr.strip()}"
Expand Down Expand Up @@ -1590,10 +1604,12 @@ def _execute_plan(
)
try:
completed = subprocess.run(
command, check=False, capture_output=True, text=True, shell=False
command, check=False, capture_output=True, text=True, shell=False, timeout=3600
)
except FileNotFoundError as exc:
raise MediaShrinkerError(f"ffmpeg not found: {ffmpeg_path}") from exc
except subprocess.TimeoutExpired as exc:
raise MediaShrinkerError(f"ffmpeg timed out for {source}") from exc

if completed.returncode != 0:
raise MediaShrinkerError(
Expand Down Expand Up @@ -1658,13 +1674,17 @@ def _copy_macos_creation_time(
creation_date = datetime.fromtimestamp(float(birthtime)).strftime(
"%m/%d/%Y %H:%M:%S"
)
subprocess.run(
[setfile_path, "-d", creation_date, str(dest.resolve())],
check=False,
capture_output=True,
text=True,
shell=False,
)
try:
subprocess.run(
[setfile_path, "-d", creation_date, str(dest.resolve())],
check=False,
capture_output=True,
text=True,
shell=False,
timeout=60,
)
except subprocess.TimeoutExpired:
pass


def _format_result(root: Path, result: ConversionResult) -> str:
Expand Down
11 changes: 0 additions & 11 deletions tests/test_media_shrinker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1705,7 +1705,6 @@ def test_attribute_copy_helpers_ignore_platform_errors(self) -> None:

class FastPathTests(unittest.TestCase):
def test_copy_extended_attributes_dummy(self) -> None:
import os
from media_shrinker import _copy_extended_attributes

# We need to hit lines 703-704
Expand All @@ -1720,7 +1719,6 @@ def test_copy_extended_attributes_dummy(self) -> None:

def test_copy_macos_creation_time_dummy(self) -> None:
from media_shrinker import _copy_macos_creation_time
import stat

# Hit 1632
class MockStat:
Expand All @@ -1747,7 +1745,6 @@ def test_format_result_dummy(self) -> None:
self.assertIn("foo.txt", s)

def test_copy_extended_attributes_dummy_success(self) -> None:
import os
from media_shrinker import _copy_extended_attributes

# Hit 703-704
Expand All @@ -1763,7 +1760,6 @@ def test_copy_extended_attributes_dummy_success(self) -> None:

def test_copy_macos_creation_time_dummy_none(self) -> None:
from media_shrinker import _copy_macos_creation_time
import stat

# Hit 1632
class MockStat:
Expand All @@ -1775,7 +1771,6 @@ class MockStat:
_copy_macos_creation_time(MockStat(), dest, "/bin/echo")

def test_copy_extended_attributes_dummy_set_fail(self) -> None:
import os
from media_shrinker import _copy_extended_attributes

# Hit 703-704
Expand All @@ -1792,7 +1787,6 @@ def test_copy_extended_attributes_dummy_set_fail(self) -> None:

def test_copy_macos_creation_time_dummy_not_found(self) -> None:
from media_shrinker import _copy_macos_creation_time
import stat

# Hit 1632
class MockStat:
Expand All @@ -1807,7 +1801,6 @@ class MockStat:

def test_copy_macos_creation_time_dummy_success(self) -> None:
from media_shrinker import _copy_macos_creation_time
import stat

# Hit 1632
class MockStat:
Expand Down Expand Up @@ -1838,7 +1831,6 @@ class MockStat:
preserve_file_attributes(src, dest)

def test_copy_extended_attributes_dummy_success_branch(self) -> None:
import os
from media_shrinker import _copy_extended_attributes

# Hit 703-704
Expand All @@ -1856,7 +1848,6 @@ def test_copy_extended_attributes_dummy_success_branch(self) -> None:

def test_copy_extended_attributes_dummy_listxattr_missing(self) -> None:
import builtins
import os
from media_shrinker import _copy_extended_attributes

# Hit early return inside _copy_extended_attributes when OS doesn't support it
Expand All @@ -1877,7 +1868,6 @@ def fake_hasattr(obj, name):

def test_preserve_file_attributes_chmod_error(self) -> None:
from media_shrinker import preserve_file_attributes
import stat

# Hit 703-704
class MockStat:
Expand All @@ -1897,7 +1887,6 @@ class MockStat:

def test_preserve_file_attributes_with_setfile(self) -> None:
from media_shrinker import preserve_file_attributes
import stat

# Hit 703-704
class MockStat:
Expand Down
Loading