scan on Windows: static tools scan an empty temp dir (false "No findings") and source files are rewritten in place

Aperta
#25 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
48/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
cli, testing

Direzione di ricerca

Start with cli/file_filter.py, cli/scan_cli.py, and the file staging logic around scan/static_scanner.py:602-618; trace how Windows absolute paths are joined to the temporary directory. Reproduce with the bookstore example and check both the staged files and the original file’s modification time. Done means Windows static tools receive the target files, source files remain unchanged, and the reported findings include relative paths.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Note: Setup, reproduction and debugging were done with Claude Code (Anthropic's AI coding assistant), including the root-cause analysis and the suggested fix. I reviewed the findings before filing.

Summary

On Windows, switch-trust scan never hands the target's files to bandit / OpenGrep / detect-secrets, so the static layer always reports 0 findings. As a side effect, each scanned file is rewritten in place over the original, because the "copy into the temp dir" lands on the source path. With no LLM configured, the result is a clean "No findings." report for code that has real issues.

Two smaller Windows blockers that have to be worked around before a scan even starts are listed at the end.

Environment
  • Windows 11 (10.0.26200), Python 3.12.14
  • switch-trust-cli 1.7.2 (confirmed in the PyPI wheel and on main at 4e3cac5)
  • OpenGrep 1.32.0 on PATH
  • No LLM provider configured (static layer only)
Steps to reproduce
  1. Work around the two startup blockers below (termios, encoding).
  2. switch-trust scan examples\bookstore_agent --output out.json
  3. Check the modified time of examples\bookstore_agent\agent.py before and after.
Expected

The README shows this example producing missing-authentication and unbounded-loop findings; the static rules alone should at least flag openai-agent-no-input-guardrails and openai-runner-no-max-turns (running OpenGrep directly on the folder with agent_opengrep_rules.yaml does return them). The source file should not be modified.

Actual
Findings  0
Tools     bandit, opengrep, detect-secrets
No findings.

agent.py's modified time changes to the exact moment the static layer starts:

mtime: 2026-10-26 20:34:01.9506
log:   2026-10-26 20:34:01,951 INFO switch_trust.scan.static_scanner: Running Bandit on 1 files...
Root cause
  1. cli/file_filter.py:65 makes the target absolute (os.path.abspath), and cli/scan_cli.py:272 builds RepoFile(path=rf.path, ...) with that absolute path.
  2. scan/static_scanner.py:602 (and :618 for requirements) writes each file to os.path.join(py_dir, repo_file.path.lstrip(os.sep)).
  3. On Windows the path is C:\Users\me\project\agent.py. lstrip(os.sep) strips nothing (it starts with a drive letter), so the second argument is still absolute, and os.path.join discards py_dir and returns the original path:
    >>> ntpath.join(r"C:\Users\me\AppData\Local\Temp\tmpab12\src", r"C:\Users\me\project\agent.py".lstrip("\\"))
    'C:\\Users\\me\\project\\agent.py'
    
  4. The "copy" is written over the source file (text mode, "w"), py_dir stays empty, and bandit/OpenGrep scan an empty folder.

This happens for any scan target on a drive-letter path, C: included, so it affects every Windows user. On Linux/macOS the same code works by luck (/home/x/a.py → home/x/a.py stays inside the temp dir), but the findings' paths still don't match the RepoFile keys, so the exact-path lookup in tool_dispatcher.py (f.filepath == file_path) falls back to basename matching.

Effect on files: in our case the rewrite produced identical bytes, but in general it can convert LF to CRLF, and since files are read with errors="replace", any non-UTF-8 bytes would be permanently replaced with U+FFFD.

Suggested fix

Make RepoFile paths relative to the scan root, the same way scan/agent_scanner.py already does in _read_inventory_files:

# cli/scan_cli.py, handle_scan()
root = path if os.path.isdir(path) else os.path.dirname(path)
...
repo_file = RepoFile(
    path=os.path.relpath(rf.path, root), content=content, size=len(content)
)

and, as a guard so a stray absolute path (e.g. C:\...) can never escape the temp dir again:

# scan/static_scanner.py
dest = os.path.join(py_dir, os.path.splitdrive(repo_file.path)[1].lstrip("\\/"))

With both applied locally, the same scan returns 3 static findings (input guardrails, output guardrails, max_turns), agent.py is untouched, and findings carry relative paths ("file": "agent.py"). Single-file targets work too.

Other Windows blockers (hit before the above)
  1. ModuleNotFoundError: No module named 'termios' on any command: cli/console.py imports termios/tty at module level. They are Unix-only and only needed for the arrow-key menu in select(); importing them inside the TTY branch and falling back to Prompt.ask on win32 fixes it.
  2. UnicodeEncodeError: 'charmap' codec can't encode characters at the start of every scan: static_scanner.py:609 writes the OpenGrep rules with open(rules_path, "w") and no encoding, and the rules contain characters outside cp1252 (Windows' default). encoding="utf-8" fixes it. A few other open(..., "w") calls also lack an encoding (scan_cli.py:61, runner.py:273, repository_json.py:530); PYTHONUTF8=1 works around all of them.

Happy to test a fix on Windows.

Lingua principale
Python
Stelle
75
Fork
7
Merge medio
38m
PR unite (30g)
2

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di sandbox-quantum/switch-trust-cli

Tutte le issue di sandbox-quantum/switch-trust-cli

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.