Skip to content

Commit bb0b615

Browse files
authored
fix(serve): prevent code injection in capture_dependencies path interpolation (#5792)
capture_dependencies built a `python -c` script by f-string interpolating pkl_path and dest_path, both derived from a user-supplied model directory, directly into Python source. A directory name containing `"` could break out of the string literal and execute arbitrary Python via expression chaining (e.g. `model" + __import__("os").system("...") + "`), giving an attacker local code execution when a victim extracted a malicious tarball and passed it to ModelBuilder with dependencies={"auto": True}. Use the !r conversion so repr() emits properly escaped Python string literals for both paths. Any embedded quotes, backslashes, or control characters are escaped, so the paths arrive at get_requirements_for_pkl_file as inert string constants rather than executable code. Affects Linux/macOS; Windows was not exploitable because `"` is an invalid filename character on NTFS. Reported via AWS Vulnerability Reporting Program (P414309851, CWE-94).
1 parent 92f8d42 commit bb0b615

1 file changed

Lines changed: 7 additions & 4 deletions

File tree

sagemaker-serve/src/sagemaker/serve/detector/dependency_manager.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ def capture_dependencies(dependencies: dict, work_dir: Path, capture_all: bool =
3636
if "auto" in dependencies and dependencies["auto"]:
3737
import site
3838

39-
pkl_path = work_dir.joinpath(PKL_FILE_NAME).resolve()
40-
dest_path = path.resolve()
39+
pkl_path = str(work_dir.joinpath(PKL_FILE_NAME).resolve())
40+
dest_path = str(path.resolve())
4141
site_packages_dir = site.getsitepackages()[0]
4242
pickle_command_dir = "/sagemaker/serve/detector"
4343

@@ -46,15 +46,18 @@ def capture_dependencies(dependencies: dict, work_dir: Path, capture_all: bool =
4646
"-c",
4747
]
4848

49+
# Use repr() to emit properly escaped Python string literals so that
50+
# attacker-controlled path characters (e.g. '"') cannot break out of
51+
# the literal and inject code into the -c script (CWE-94).
4952
if capture_all:
5053
command.append(
5154
f"from sagemaker.serve.detector.pickle_dependencies import get_all_requirements;"
52-
f'get_all_requirements("{dest_path}")'
55+
f"get_all_requirements({dest_path!r})"
5356
)
5457
else:
5558
command.append(
5659
f"from sagemaker.serve.detector.pickle_dependencies import get_requirements_for_pkl_file;"
57-
f'get_requirements_for_pkl_file("{pkl_path}", "{dest_path}")'
60+
f"get_requirements_for_pkl_file({pkl_path!r}, {dest_path!r})"
5861
)
5962

6063
subprocess.run(

0 commit comments

Comments
 (0)