Skip to content

Commit 2cbb1fb

Browse files
committed
Ensure memfs checks symlinked files are inside output directory
1 parent 6100833 commit 2cbb1fb

1 file changed

Lines changed: 8 additions & 0 deletions

File tree

‎snekbox/snekio/memfs.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,21 @@ def files(
146146
count = 0
147147
total_size = 0
148148
files = glob.iglob(pattern, root_dir=str(self.output), recursive=True, include_hidden=False)
149+
output_root = self.output.resolve()
150+
149151
for file in (Path(self.output, f) for f in files):
150152
if timeout and (time.monotonic() - start_time) > timeout:
151153
raise TimeoutError("File parsing timeout exceeded in MemFS.files")
152154

153155
if not file.is_file():
154156
continue
155157

158+
# Resolve all symlinks and verify the target is still within the output directory.
159+
real_path = file.resolve()
160+
if not real_path.is_relative_to(output_root):
161+
log.info(f"Skipping file {file} as it is outside the output directory")
162+
continue
163+
156164
# file.is_file allows file to be a regular file OR a symlink pointing to a regular file.
157165
# It is important that we follow symlinks here, so when we check st_size later it is the
158166
# size of the underlying file rather than of the symlink.

0 commit comments

Comments
 (0)