Security: Potential Command Injection via sys.argv in setup.py#2203
Open
tomaioo wants to merge 2 commits into
Open
Security: Potential Command Injection via sys.argv in setup.py#2203tomaioo wants to merge 2 commits into
tomaioo wants to merge 2 commits into
Conversation
The setup.py file checks `'develop' not in sys.argv` to conditionally execute code. While this specific usage is not directly exploitable, the pattern of inspecting sys.argv for command strings is fragile. More critically, the pyct.build.examples() call and subsequent shutil.rmtree() operate on paths derived from __file__, which could be manipulated if an attacker controls the working directory or environment. The rmtree on example_path after setup() could delete unexpected directories if path resolution is tricked. Signed-off-by: tomaioo <203048277+tomaioo@users.noreply.github.com>
|
Hi @tomaioo, thanks for the PR! Would you mind filing a quick New contributor introduction issue when you get a chance? It helps us point you at issues that fit what you'd like to work on. Most fields are optional. Reviewing your PR doesn't depend on it, just a friendly nudge. |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR aims to harden setup.py by reducing reliance on fragile sys.argv string checks and by ensuring the post-setup() cleanup (shutil.rmtree) only deletes directories within the project directory.
Changes:
- Introduces
_package_dirand a more explicit_is_developflag to control whetherpyct.build.examples(...)runs. - Adds a realpath-based containment check before deleting the generated
xrspatial/examplesdirectory.
Comments suppressed due to low confidence (1)
setup.py:14
- The PR description proposes avoiding conditional behavior based on
sys.argvby using setuptools command classes, but the updated code still branches onsys.argv. If the intent is to harden this pattern, consider moving this logic into a customcmdclass(or checking the actual command via setuptools APIs) instead of scanning argv strings.
_is_develop = any(arg == 'develop' or arg.endswith('develop') for arg in sys.argv)
if not _is_develop:
pyct.build.examples(example_path, __file__, force=True)
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Comment on lines
+10
to
+13
| _package_dir = os.path.dirname(os.path.abspath(__file__)) | ||
| example_path = os.path.join(_package_dir, 'xrspatial', 'examples') | ||
| _is_develop = len(sys.argv) > 1 and sys.argv[1] == 'develop' | ||
| if not _is_develop: |
Comment on lines
9
to
+13
| if __name__ == '__main__': | ||
| example_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), | ||
| 'xrspatial', 'examples') | ||
| if 'develop' not in sys.argv: | ||
| _package_dir = os.path.dirname(os.path.abspath(__file__)) | ||
| example_path = os.path.join(_package_dir, 'xrspatial', 'examples') | ||
| _is_develop = len(sys.argv) > 1 and sys.argv[1] == 'develop' | ||
| if not _is_develop: |
Comment on lines
+25
to
+26
| if _real_example_path.startswith(_real_package_dir + os.sep): | ||
| shutil.rmtree(example_path) |
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.
Summary
Security: Potential Command Injection via sys.argv in setup.py
Problem
Severity:
Low| File:setup.py:L1The setup.py file checks
'develop' not in sys.argvto conditionally execute code. While this specific usage is not directly exploitable, the pattern of inspecting sys.argv for command strings is fragile. More critically, the pyct.build.examples() call and subsequent shutil.rmtree() operate on paths derived from file, which could be manipulated if an attacker controls the working directory or environment. The rmtree on example_path after setup() could delete unexpected directories if path resolution is tricked.Solution
Avoid conditional behavior based on sys.argv strings. Use setuptools command classes instead. Validate that example_path is within the expected project directory before performing rmtree operations.
Changes
setup.py(modified)