Injection audit
Source and package versions
One AIWAF repository.
Python: py/aiwaf/ · Node.js: js/ · Rust/WASM: rust/ · Java: java/.
Source and canonical README.
Versions shown are the latest published releases; each package is versioned independently.
Current versions are aiwaf 1.3.6 (Python) and
aiwaf 1.3.6 (npm), with Rust/Python 0.2.20 and
WASM 0.2.16 and Java 1.4.0. Sandbox guides retain their separately pinned builds.
audit reads your source code and reports where request input can reach a dangerous operation: SQL, shell commands, file paths, outbound requests, HTML output, redirects and more. It parses files without running them, and it doesn't change how requests are handled. Python, Node.js and Java each have their own analyser.
Run an audit
aiwaf audit path/to/app # Python
npx aiwaf audit path/to/app # Node.js
java -cp <classpath> com.aiwaf.cli.AiwafConsole audit src # Java, needs a JDK
Each finding names the file and line, the kind of operation, the call, the request input that reaches it, and the function that handles the request:
app/views.py:12: sql (high confidence) request.GET['q'] reaches cursor.execute in search
Scanned 48 files and found 1 possible injection paths.
| Option | What it does |
|---|---|
--format text|json|sarif | Readable text (the default), the shared JSON report, or SARIF 2.1.0 for code scanning |
--output <file> | Writes the report to a file instead of standard output |
--include-tests | Also scans test files and folders, which are skipped by default |
--fail-on-findings | Exits with status 1 when anything is found, for CI |
--baseline <file> | Leaves out findings already in an earlier JSON report |
--manifest <file> | Saves the findings into a route manifest; see route checks |
The exit status is 0 when the scan completes, 1 with --fail-on-findings when something is found, and 2 for a bad path or argument. A finding is a path worth reading, not a confirmed vulnerability: a reverse proxy, for example, is reported for its upstream request, because forwarding input is its job.
What it reports
| Operation | Examples |
|---|---|
sql, nosql | cursor.execute, db.query, jdbcTemplate.update, collection.find |
command | os.system, subprocess.run(..., shell=True), exec, Runtime.exec, ProcessBuilder |
path | open, fs.readFile, new File, pathlib paths, file: URIs |
ssrf | requests.get, fetch, RestTemplate |
code, template, deserialization | eval, render_template_string, pickle.loads, yaml.load |
xss | Text returned from a Flask view, HttpResponse, mark_safe, Express res.send, Koa ctx.body, Java response.getWriter() |
redirect | redirect, res.redirect, sendRedirect |
ldap, xpath | LDAP search filters, xpath, XPath.evaluate |
xxe | XML parsers with external entities turned on |
redos | Regular expressions built from input, as in re.compile(value) or new RegExp(value) |
prototype (Node.js) | obj[key] = value with a key from input, _.merge of a request body |
Header injection isn't reported: Node.js, Werkzeug, Django and servlet containers reject newlines in header values.
Where input comes from
| Language | Request input it recognises |
|---|---|
| Python | Django, Flask and FastAPI views; Graphene, Strawberry and Ariadne resolvers; FastAPI WebSockets, Django Channels consumers and Flask-SocketIO handlers |
| Node.js | Express, Fastify, Hapi, Koa, NestJS (including @Args and @MessageBody), Next.js routes and server actions, Remix loaders, tRPC input, GraphQL resolver maps, AdonisJS, Sails, ws and Socket.IO |
| Java | Spring MVC and WebFlux, Spring GraphQL @Argument, Netflix DGS @InputArgument, Jakarta Servlet, JAX-RS, @MessageMapping and @OnMessage |
Route values the framework has already converted to numbers, booleans, dates or UUIDs don't count as input: Python parameters typed int or UUID, Flask <int:id> converters, NestJS ParseIntPipe, and Spring parameters typed Long or UUID.
How far it follows input
- Helpers and return values: input passed to a helper is followed into it, and a helper that returns input carries it back. Helpers are followed three calls deep.
- Other files and classes: relative imports and
require(), Node.jstsconfig.jsonorjsconfig.jsonpathsandbaseUrl, workspace packages, and methods on instances of the project's classes, through constructors, typed fields and parameters, NestJS constructor injection and FastAPIDepends. A finding in another file names the handler's file inentry_file. - Collections and objects: lists and dicts filled with input, followed by key and position, so
m['safe']isn't confused withm['input']; objects that keep input in a field. - Callbacks and state:
.then(),.map()and Java lambdas; values middleware keeps on the request orflask.g; request and session attributes. - Values known before running: a condition,
switchormatchon a value decided in the code takes only the branch that runs.
A helper called with the same input from many routes is walked once, and its findings are recorded for each route. In a test project of 400 routes sharing 512 database calls, a scan took 0.7 seconds in Node.js, 1.1 seconds in Python and 1.7 seconds in Java.
Checks and encoders
A check that returns or throws when a value fails makes the value safe for what follows:
def view(request):
name = request.GET["name"]
if not name.isalnum():
return HttpResponseBadRequest()
os.system("lookup " + name) # not reported
Regex tests, allowlists (value in ALLOWED, ALLOWED.includes(value)), comparisons with a literal, url_has_allowed_host_and_scheme, numeric checks and schemas that only accept numbers or UUIDs count. A '..' check makes a value safe for file paths only. A denylist doesn't count: if name in KNOWN: continue skips some values but lets the rest through.
Encoders make a value safe only for what they encode for:
| Encoder | Safe for |
|---|---|
int(), parseInt, Number, uuid.UUID, secure_filename | Everything |
html.escape, escapeHtml, encodeForHTML, HtmlUtils.htmlEscape | HTML (xss) only, not SQL |
shlex.quote, shellescape, encodeForOS | Shell commands |
path.basename, os.path.basename | File paths |
re.escape, escapeRegExp, Pattern.quote | Regular expressions |
escape_filter_chars, encodeForLDAP | LDAP filters |
Measured accuracy
The OWASP Benchmark is a set of small labelled test cases: each is a real vulnerability or a safe look-alike. OWASP scores a tool by the share of vulnerable cases it finds minus the share of safe cases it flags, so 0 is no better than guessing and +100 is perfect.
| Benchmark | Injection cases | Before (1.2.4) | 1.2.5 |
|---|---|---|---|
| BenchmarkJava | 1,572 | +3.0 | +100: all found, no false alarms |
| BenchmarkPython | 677 | +4.0 | +100: all found, no false alarms |
The benchmarks are small synthetic cases, so this isn't proof of the same on real applications, and Node.js has no labelled benchmark. To repeat the measurement, clone a benchmark and run scripts/audit_benchmark.py from the AIWAF repository:
python scripts/audit_benchmark.py --source ../BenchmarkPython --language python
java -cp <classpath> com.aiwaf.cli.AiwafConsole audit ../BenchmarkJava/src --format json --output java.json
python scripts/audit_benchmark.py --source ../BenchmarkJava --report java.jsonUse it in CI
Code scanning. --format sarif writes SARIF 2.1.0 with one rule per operation and stable fingerprints, so GitHub code scanning shows findings on pull requests and tracks them across commits:
- run: aiwaf audit . --format sarif --output aiwaf.sarif
- uses: github/codeql-action/upload-sarif@v4
with:
sarif_file: aiwaf.sarif
Only new findings. Save a JSON report once, and pass it as a baseline. Findings are matched by file, operation, call, function and input, not by line, so moved code still matches:
aiwaf audit . --format json --output aiwaf-baseline.json
aiwaf audit . --baseline aiwaf-baseline.json --fail-on-findings
Reviewed findings. A comment aiwaf: ignore on a finding's line or the line above leaves it out, and aiwaf: ignore[sql, xss] leaves out only those operations. The report counts what it left out.
cursor.execute(query) # aiwaf: ignore[sql] -- query is built from constants aboveRoute checks from findings
aiwaf init audits your source as it builds the route manifest, and aiwaf audit --manifest <file> refreshes the saved findings. Requests to those routes are then checked for patterns that matter to that operation, in the parameter that reaches it, and blocked with 403 by default. Set AIWAF_INJECTION_RISK_MODE to log to only log matches, or off. In this release the runtime checks cover sql, nosql, command, path, ssrf, code, template and deserialization; findings of the other operations are saved in the manifest without a runtime check. See configuration.
What it can miss
- Reflection, dynamic calls, and code in installed libraries.
- Helpers more than three calls deep.
- Node.js
package.jsonimports(#alias) and subpathexportsmaps. - Java interfaces with more than five implementations.
- Python treats a project function as an HTML escaper when its name says so, such as
escape_for_html; a misleadingly named function can hide an XSS finding.
A clean report doesn't prove the code is safe. Continue with testing and configuration.
