llm-security/tests/fixtures/ast-scan/sinks.py
Kjell Tore Guttormsen 76f190cf78 fix(llm-security): AST-taint clears taint on reassignment (#29) + sink test coverage (#28)
py-ast-taint.py Assign handler never removed a name from the tainted set on reassignment, so a source-then-constant/sanitizer rebind (g=os.getenv(); g='safe'; os.system(g), or x=shlex.quote(x)) kept stale taint and fired false AST-CMD-EXEC findings. Assign now clears taint for target names when the RHS is not a source. No cross-expression propagation added — the f-string/concat/alias recall gap (#27) stays deferred to v8.

#28: added fixtures+assertions locking the tainted subprocess/os.system AST-CMD-EXEC sink and the open(...,'w') AST-FILE-WRITE sink. Suite 1931/0.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

Claude-Session: https://claude.ai/code/session_01TcQyMTQfyrsAapaCMPxTtQ
2026-07-18 10:14:51 +02:00

20 lines
619 B
Python

import os
import subprocess
# Locks the subprocess/os.system command sinks and the file-write sink (#28).
def run_user_command():
cmd = input("cmd> ") # tainted source
subprocess.run(cmd, shell=True) # sink: subprocess.* -> AST-CMD-EXEC
def shell_from_env():
target = os.getenv("TARGET") # tainted source
os.system(target) # sink: os.system -> AST-CMD-EXEC
def leak_to_file():
payload = os.environ["DATA"] # tainted source
log = open("out.txt", "w") # write handle
log.write(payload) # sink: file.write -> AST-FILE-WRITE (high)