Skip to content

Commit c9fd0d8

Browse files
committed
build.run: prohibit absolute paths in BuildPlan.add_file.
This makes the build impure and also causes the contents of a file outside of the build directory to be overwritten. The check in `BuildPlan.execute_local` is also expanded to cover the possibility of an absolute path sneaking through.
1 parent 4ffadff commit c9fd0d8

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

amaranth/build/run.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ def add_file(self, filename, content):
3333
forward slashes (``/``).
3434
"""
3535
assert isinstance(filename, str) and filename not in self.files
36+
if (pathlib.PurePosixPath(filename).is_absolute or
37+
pathlib.PureWindowsPath(filename).is_absolute):
38+
raise ValueError(f"Filename {filename!r} must not be an absolute path")
3639
self.files[filename] = content
3740

3841
def digest(self, size=64):
@@ -78,9 +81,9 @@ def execute_local(self, root="build", *, run_script=True, env=None):
7881

7982
for filename, content in self.files.items():
8083
filename = pathlib.Path(filename)
81-
# Forbid parent directory components completely to avoid the possibility
82-
# of writing outside the build root.
83-
assert ".." not in filename.parts
84+
# Forbid parent directory components and absolute paths completely to avoid
85+
# the possibility of writing outside the build root.
86+
assert not filename.is_absolute and ".." not in filename.parts
8487
dirname = os.path.dirname(filename)
8588
if dirname:
8689
os.makedirs(dirname, exist_ok=True)

0 commit comments

Comments
 (0)