Potential fix for code scanning alert no. 36: Uncontrolled data used in path expression#31
Open
Potential fix for code scanning alert no. 36: Uncontrolled data used in path expression#31
Conversation
…in path expression Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
| base_dir = Path(self.cfg["dir"]).resolve() | ||
| raw_path = Path(path) | ||
| if not raw_path.is_absolute(): | ||
| p = (base_dir / raw_path).resolve(strict=False) |
Check failure
Code scanning / CodeQL
Uncontrolled data used in path expression High
| if not raw_path.is_absolute(): | ||
| p = (base_dir / raw_path).resolve(strict=False) | ||
| else: | ||
| p = raw_path.resolve(strict=False) |
Check failure
Code scanning / CodeQL
Uncontrolled data used in path expression High
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Potential fix for https://github.com/akadata/pistorm64/security/code-scanning/36
In general, file paths derived from user-controlled data should be normalized/canonicalized and then checked to ensure they are within an expected root directory before any filesystem operation such as
exists()or passing them into other components. This prevents directory traversal via..components, symlinks, or absolute paths.For this specific code, the best fix without changing visible behavior is:
pathinto an absolute, resolved path under the configured image directory (self.cfg["dir"]), usingPath.resolve(strict=False)oros.path.realpath.is_relative_to(Python 3.9+) or a manualrelative_to/exception pattern.p) for both theexists()check and constructing theinsertcommand, instead of the raw, potentially relative/oddly-encoded user input.Concretely, in
do_POSTunder the/api/insertbranch (around lines 1696–1716 inbin/adfmanager.py):path = param("path", "")and decoding, compute a base directorybase_dir = Path(self.cfg["dir"]).resolve()and createpasbase_dir.joinpath(path).resolve(strict=False)ifpathis relative. Ifpathis absolute, resolve it directly and then check that it lies underbase_dir.p = Path(path)line and subsequentp.exists()/path_within_dirusage with logic that:full_path = p.resolve(strict=False)(or equivalent).full_path.is_file()(orexists()as currently) only after verifying it is underbase_dir.full_pathin theinsertcommand.No new external dependencies are needed; we can do this with
pathlib.Path, already imported.Suggested fixes powered by Copilot Autofix. Review carefully before merging.