Skip to content

Security: Potential Command Injection via sys.argv in setup.py#2203

Open
tomaioo wants to merge 2 commits into
xarray-contrib:mainfrom
tomaioo:fix/security/potential-command-injection-via-sys-argv
Open

Security: Potential Command Injection via sys.argv in setup.py#2203
tomaioo wants to merge 2 commits into
xarray-contrib:mainfrom
tomaioo:fix/security/potential-command-injection-via-sys-argv

Conversation

@tomaioo
Copy link
Copy Markdown

@tomaioo tomaioo commented May 20, 2026

Summary

Security: Potential Command Injection via sys.argv in setup.py

Problem

Severity: Low | File: setup.py:L1

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.

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)

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>
@github-actions
Copy link
Copy Markdown

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.

@github-actions github-actions Bot added the performance PR touches performance-sensitive code label May 20, 2026
@brendancol brendancol requested a review from Copilot May 21, 2026 03:03
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_dir and a more explicit _is_develop flag to control whether pyct.build.examples(...) runs.
  • Adds a realpath-based containment check before deleting the generated xrspatial/examples directory.
Comments suppressed due to low confidence (1)

setup.py:14

  • The PR description proposes avoiding conditional behavior based on sys.argv by using setuptools command classes, but the updated code still branches on sys.argv. If the intent is to harden this pattern, consider moving this logic into a custom cmdclass (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)

Comment thread setup.py Outdated
Comment thread setup.py
_real_example_path = os.path.realpath(example_path)
_real_package_dir = os.path.realpath(_package_dir)
if _real_example_path.startswith(_real_package_dir + os.sep):
shutil.rmtree(example_path)
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 3 comments.

Comment thread setup.py
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 thread setup.py
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 thread setup.py
Comment on lines +25 to +26
if _real_example_path.startswith(_real_package_dir + os.sep):
shutil.rmtree(example_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance PR touches performance-sensitive code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants