Skip to content

Remove shell usage from detector tools#4866

Open
SimoneMariaRomeo wants to merge 1 commit into
Scottcjn:mainfrom
SimoneMariaRomeo:codex/detector-shell-free
Open

Remove shell usage from detector tools#4866
SimoneMariaRomeo wants to merge 1 commit into
Scottcjn:mainfrom
SimoneMariaRomeo:codex/detector-shell-free

Conversation

@SimoneMariaRomeo
Copy link
Copy Markdown

Summary

  • Replaces subprocess.check_output("lspci", shell=True) with argv-list execution and no shell mediation.
  • Replaces subprocess.check_output("dir", shell=True) with os.listdir(".") for the OS keyword scan.
  • Updates the existing OS detector tests and adds an AST regression that prevents these detector tools from reintroducing shell=True.

Fixes #4813.

Validation

  • python -m pytest tests\test_detector_tools_no_shell.py tools\test_os_detector.py -q -> 3 passed
  • python -m py_compile tools\gpu_display_detector.py tools\os_detector.py tools\test_os_detector.py tests\test_detector_tools_no_shell.py -> passed
  • git diff --check -- tools\gpu_display_detector.py tools\os_detector.py tools\test_os_detector.py tests\test_detector_tools_no_shell.py -> passed
  • python tools\bcos_spdx_check.py --base-ref origin/main -> OK
  • python -m ruff check tools\gpu_display_detector.py tools\os_detector.py tools\test_os_detector.py tests\test_detector_tools_no_shell.py --select E9,F821,F811 --output-format=concise -> unavailable locally: No module named ruff

@github-actions github-actions Bot added BCOS-L1 Beacon Certified Open Source tier BCOS-L1 (required for non-doc PRs) tests Test suite changes size/M PR: 51-200 lines labels May 12, 2026
Copy link
Copy Markdown
Contributor

@jaxint jaxint left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks for contributing. Approved.

Copy link
Copy Markdown

@saim256 saim256 left a comment

Choose a reason for hiding this comment

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

Approving current head 42de7b0.

The #4813 fix is focused: gpu_display_detector.py no longer invokes lspci through a shell, os_detector.py uses os.listdir('.') instead of shelling out to dir, and the AST regression catches future shell=True reintroductions in both detector tools. The existing OS detector behavior is preserved by updating the focused tests to mock os.listdir().

Validation performed:

  • python -m pytest tests\test_detector_tools_no_shell.py tools\test_os_detector.py -q -> 3 passed
  • python -m py_compile tools\gpu_display_detector.py tools\os_detector.py tools\test_os_detector.py tests\test_detector_tools_no_shell.py -> passed
  • python tools\bcos_spdx_check.py --base-ref origin/main -> BCOS SPDX check: OK
  • git diff --check origin/main...HEAD -- tools/gpu_display_detector.py tools/os_detector.py tools/test_os_detector.py tests/test_detector_tools_no_shell.py -> passed
  • python -m ruff check tools\gpu_display_detector.py tools\os_detector.py tools\test_os_detector.py tests\test_detector_tools_no_shell.py --select E9,F821,F811 --output-format=concise -> All checks passed

Copy link
Copy Markdown
Contributor

@godd-ctrl godd-ctrl left a comment

Choose a reason for hiding this comment

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

Approved current head 42de7b0.

The detector tools no longer use shell=True for the legacy OS and GPU display probes. The GPU probe now invokes lspci as an argv list, the OS probe uses os.listdir instead of shelling out to dir, and the added AST regression covers shell=True in both detector tools.

Validation performed:

  • git diff --name-status origin/main...HEAD -> tools/gpu_display_detector.py, tools/os_detector.py, tools/test_os_detector.py, tests/test_detector_tools_no_shell.py
  • git diff --check origin/main...HEAD -> passed
  • python tools\bcos_spdx_check.py --base-ref origin/main -> passed
  • python -m py_compile tools\gpu_display_detector.py tools\os_detector.py tools\test_os_detector.py tests\test_detector_tools_no_shell.py -> passed
  • python -m pytest tools\test_os_detector.py tests\test_detector_tools_no_shell.py -q -> 3 passed
  • rg scan for shell=True/check_output string/os.system/os.popen patterns in the touched files -> no matches

No blocking issues found in the reviewed diff.

Copy link
Copy Markdown
Contributor

@jaxint jaxint left a comment

Choose a reason for hiding this comment

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

Great contribution! LGTM.

Copy link
Copy Markdown
Contributor

@jaxint jaxint left a comment

Choose a reason for hiding this comment

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

Great contribution! LGTM.

Copy link
Copy Markdown
Contributor

@jaxint jaxint left a comment

Choose a reason for hiding this comment

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

Great contribution! LGTM.

Copy link
Copy Markdown
Contributor

@jaxint jaxint left a comment

Choose a reason for hiding this comment

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

Great contribution! LGTM.

Copy link
Copy Markdown
Contributor

@jaxint jaxint left a comment

Choose a reason for hiding this comment

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

Great contribution! LGTM.

Copy link
Copy Markdown

@loganoe loganoe left a comment

Choose a reason for hiding this comment

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

Review for Scottcjn/rustchain-bounties#73 on current head 42de7b0cc670597394405d2a3b00551bd89b05d7.

The detector hardening is scoped and correct: gpu_display_detector.py now invokes lspci as an argv list instead of through a shell, os_detector.py replaces the shell-mediated dir scan with os.listdir('.'), and the added AST regression guards both detector tools against reintroducing shell=True.

Validation performed:

  • PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 uv run --no-project --with pytest --with flask python -m pytest tests/test_detector_tools_no_shell.py tools/test_os_detector.py -q -> 3 passed
  • python3 -m py_compile tools/gpu_display_detector.py tools/os_detector.py tools/test_os_detector.py tests/test_detector_tools_no_shell.py -> passed
  • git diff --check origin/main...HEAD -- tools/gpu_display_detector.py tools/os_detector.py tools/test_os_detector.py tests/test_detector_tools_no_shell.py -> passed
  • python3 tools/bcos_spdx_check.py --base-ref origin/main -> OK

No blocking issues found in this diff.

Copy link
Copy Markdown
Contributor

@508704820 508704820 left a comment

Choose a reason for hiding this comment

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

Code Review: Remove Shell Usage from Detector Tools

Summary

Adds AST-based test that detects subprocess.run(shell=True) in detector tools and removes any such usage. This prevents shell injection vulnerabilities in GPU display and OS detector tools.

What Works Well

  1. AST-based detection: Parses Python source code to find shell=True usage
  2. Comprehensive scan: Checks all detector tool files
  3. 38-line test: Clean, focused verification
  4. Prevention: Acts as a CI gate — future PRs with shell=True will fail tests

Verdict: Approve

Good proactive security measure. AST scanning prevents future shell injection regressions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

BCOS-L1 Beacon Certified Open Source tier BCOS-L1 (required for non-doc PRs) size/M PR: 51-200 lines tests Test suite changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[SECURITY] Detector tools invoke fixed commands through the shell

6 participants