Skip to content

Potential fix for code scanning alert no. 1: Uncontrolled command line#9

Draft
appsecninja32 wants to merge 3 commits into
mainfrom
alert-autofix-1
Draft

Potential fix for code scanning alert no. 1: Uncontrolled command line#9
appsecninja32 wants to merge 3 commits into
mainfrom
alert-autofix-1

Conversation

@appsecninja32

Copy link
Copy Markdown
Owner

Potential fix for https://github.com/appsecninja32/CosmoGrepperAI/security/code-scanning/1

Best fix: enforce strict validation on both user-influenced arguments before subprocess execution, and remove shell execution mode.

In scan_engine.py inside run_semgrep_json:

  1. Validate and normalize path using Path(path).resolve(strict=True) so only existing local paths are allowed.
  2. Restrict ruleset:
    • If registry ruleset (p/...), only allow safe characters via regex allowlist.
    • If local ruleset, allow only a filename-like token (no separators), then resolve under <BASE_DIR>/rules and ensure it stays inside that directory.
  3. Build cmd with normalized safe values.
  4. Run subprocess with shell=False on all platforms.

This preserves behavior (scan requested path/ruleset) while removing command-injection vector and addressing all variants (path and ruleset from both endpoints).

Suggested fixes powered by Copilot Autofix. Review carefully before merging.

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Comment thread scan_engine.py
Comment thread scan_engine.py Fixed
appsecninja32 and others added 2 commits June 14, 2026 14:21
…ed in path expression'

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
…ed in path expression'

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Comment thread scan_engine.py
if not re.fullmatch(r"[A-Za-z0-9._\-]+", ruleset):
return {"results": [], "error": "Invalid local ruleset name"}
try:
candidate = (rules_dir / ruleset).resolve(strict=True)
Comment thread secure_review.py
raise FileNotFoundError(f"Target path not found: {target}")
base_dir = Path(__file__).resolve().parent
try:
target = (base_dir / target_path).resolve(strict=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants