Skip to content

Commit 3c07efb

Browse files
committed
Add path validation for symlink extraction
1 parent cde14de commit 3c07efb

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

samcli/local/lambdafn/zip.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ def _extract(file_info, output_dir, zip_ref):
4848
-------
4949
string
5050
Returns the target path the Zip Entry was extracted to.
51+
52+
Raises
53+
------
54+
ValueError
55+
If the extraction path would escape the output directory
5156
"""
5257

5358
# Handle any regular file/directory entries
@@ -57,6 +62,14 @@ def _extract(file_info, output_dir, zip_ref):
5762
source = zip_ref.read(file_info.filename).decode("utf8")
5863
link_name = os.path.normpath(os.path.join(output_dir, file_info.filename))
5964

65+
output_dir_abs = os.path.abspath(output_dir)
66+
link_name_abs = os.path.abspath(link_name)
67+
68+
if not link_name_abs.startswith(output_dir_abs + os.sep) and link_name_abs != output_dir_abs:
69+
raise ValueError(
70+
f"Entry '{file_info.filename}' would extract outside target directory."
71+
)
72+
6073
# make leading dirs if needed
6174
leading_dirs = os.path.dirname(link_name)
6275
if not os.path.exists(leading_dirs):

0 commit comments

Comments
 (0)