Repository navigation
fix(crs-compose): ignore symlinks and hidden files in artifact inputs and forwarding - #530
imshubham22apr-gif wants to merge 2 commits into
Conversation
… and forwarding Signed-off-by: imshubham22apr-gif <imshubham.22apr@gmail.com>
vikramsingh920
left a comment
There was a problem hiding this comment.
Thanks for working on hardening artifact copying!
I tested this locally and checked the implementation, and noticed an important edge case regarding how shutil.copytree handles symlinks=False:
1. symlinks=False in shutil.copytree follows and copies the target file
In Python's standard library shutil.copytree:
symlinks=True: Copies the symlink object itself.symlinks=False(the default): Does not ignore symlinks; instead, it follows the symlink and copies the contents of the target file into the destination tree.
Because shutil.ignore_patterns(".*") only checks filename strings matching .*, symlinks pointing to outside files are still followed and copied as regular files.
Suggested Fix:
To properly ignore both hidden files and symlinks during recursive copying, a custom ignore callable can be used:
def _ignore_hidden_and_symlinks(directory: str, contents: list[str]) -> set[str]:
ignored = set()
for name in contents:
path = os.path.join(directory, name)
if name.startswith(".") or os.path.islink(path):
ignored.add(name)
return ignoredAnd pass ignore=_ignore_hidden_and_symlinks to shutil.copytree().
2. Unit Test Symlink Creation False-Positive on Windows
In test_copy_forward_artifact_sources_skips_hidden_and_symlinks:
try:
symlink_file.symlink_to(outside)
except OSError:
passOn environments where unprivileged symlink creation raises OSError (e.g. non-Developer mode Windows), link.diff is silently never created in the source directory. This causes assert not (dst / "patches" / "link.diff").exists() to pass falsely because the file never existed in the first place.
Suggested Fix:
If symlink_to raises OSError, consider skipping the test so it doesn't give a false positive:
try:
symlink_file.symlink_to(outside)
except OSError:
pytest.skip("Symlinks cannot be created without elevated privileges on this platform")|
Thanks @vikramsingh920 for catching both of these! Addressed in 15aebf9:
|
Summary
Hardens artifact input copying and forward artifact copying in
CRSComposeagainst copying symlinks, out-of-tree links, or hidden bookkeeping files into the exchange directory.Changes
oss_crs/src/crs_compose.py:_copy_forward_artifact_sources(): passedignore=shutil.ignore_patterns(".*")andsymlinks=Falsetoshutil.copytree()._copy_artifact_input(): passedignore=shutil.ignore_patterns(".*")andsymlinks=Falsetoshutil.copytree()for recursive directories; filtered out symlinks and hidden files (not f.is_symlink() and not f.name.startswith(".")) when copying flat directories.oss_crs/tests/unit/test_directed_inputs_behavior.py:CHANGELOG.md.Verification
uv run pytest oss_crs/tests/unit/test_directed_inputs_behavior.py -k "test_copy_forward_artifact_sources"passed.uv run ruff check oss_crs/src/crs_compose.py oss_crs/tests/unit/test_directed_inputs_behavior.pypassed.uv run ruff format --check oss_crs/src/crs_compose.py oss_crs/tests/unit/test_directed_inputs_behavior.pypassed.uv run pyright oss_crs/src/crs_compose.py oss_crs/tests/unit/test_directed_inputs_behavior.pypassed (0 errors, 0 warnings).