Add Python script static analysis handler#28
Merged
Conversation
Adds a handler that can auto-approve safe Python scripts by analyzing their AST. Uses a whitelist approach: only scripts using known-safe modules and builtins are approved. Threat model: This protects against AI coding assistants accidentally running dangerous scripts, NOT against adversarial/malicious code. An AI won't deliberately craft bypass code - it might just accidentally use os.system() or open() for file writes. Auto-approved: - python --version, --help - python -m json.tool, calendar, pydoc - Scripts using only safe modules (json, re, math, collections, etc.) Requires confirmation: - python -c (inline code) - python -m <arbitrary module> - Scripts importing os, subprocess, pathlib, socket, etc. - Scripts using eval, exec, open, __import__ Blocked modules include those with hidden file I/O capabilities: - gzip, bz2, lzma (have .open() methods) - codecs (codecs.open()) - inspect (getsource reads files)
Add coverage for: - Legacy process modules (popen2, commands) - XML parsing (XXE vulnerabilities) - Additional deserialization (dill, marshal, jsonpickle) - Archive extraction (tarfile, zipfile) - CGI/httpoxy vulnerabilities - Frame/traceback introspection attributes - Full set of os.spawn*/exec* variants - Subprocess method names
New test classes: - TestDangerousModulesBandit: marshal, dill, jsonpickle, configparser - TestDangerousModulesXML: xml.etree, xml.sax, xml.dom, xml.parsers - TestDangerousModulesArchive: tarfile, zipfile - TestDangerousModulesCGI: cgi, cgitb, wsgiref.handlers - TestDangerousModulesLegacy: commands, popen2 - TestDangerousAttributes: subprocess methods, os.spawn/exec variants - TestDangerousReflection: frame/generator/code introspection - TestDangerousFileOps: file read/write methods, pathlib methods - TestNetworkOps: socket, urllib, http, ftp, telnet, xmlrpc - TestUnitAnalysisExtended: direct analyzer tests Also fixes detection of non-dunder reflection attributes (gi_frame, f_globals, etc.) by adding REFLECTION_ATTRS set to SafetyAnalyzer.
Only calendar is truly inert (just prints output). The others: - timeit: executes its statement argument as code - json.tool: reads files passed as arguments - pydoc: imports modules, executing their top-level code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a handler that auto-approves safe Python scripts by statically analyzing their AST. Uses a strict whitelist approach.
Threat Model
This is NOT a sandbox. We're protecting against AI coding assistants (Claude Code) accidentally running dangerous scripts, not against adversarial or malicious code.
Key insight: An AI won't deliberately craft
tokenize.open()bypasses or().__class__.__bases__[0].__subclasses__()exploits. It will accidentally reach foros.system(),subprocess.run(), oropen("file.txt", "w").What gets auto-approved
Safe modules (pure computation, no I/O):
json,re,math,collections,itertools,functools,hashlib,datetime,dataclasses,typing,ast,zlib, etc.What requires confirmation
Blocked modules (likely AI mistakes):
os,subprocess,shutilpathlib,io,open()socket,http,urllibgzip,bz2,lzma.open()codecs.open()inspectgetsource()Blocked builtins:
eval,exec,open,__import__,globals,getattrConservative defaults
pandas,requests)Test plan
just checkpasses on Python 3.11-3.14