Check the old finding's scope first#
A finding that vanishes after a refactor has two explanations, and the scan output alone cannot tell them apart. Either the change removed the path, or it moved the path outside what the rule and engine look at. Start from the last report that contained the finding. If its source, intermediate steps and sink all sat in one function, the engine may have needed them there.
Semgrep's open-source engine is in that position by default. Its command-line source at v1.187.0 lists the deeper modes as separate options: --pro-intrafile is "Intra-file inter-procedural taint analysis" and --pro is "Inter-file analysis". Without them, a taint rule follows values within a function body. A refactor that extracts a helper can move the source out of that body.
A refactor that hid the path#
The constructed Flask route below builds an ORDER BY clause from a query parameter. A local taint rule treats flask.request.args.get(...) as a source and the SQL text passed to execute as a sink:
@app.route("/orders/sorted")
def orders_sorted():
column = request.args.get("sort", "created_at")
sql = f"SELECT id, status FROM orders ORDER BY {column}"
return {"rows": db().execute(sql).fetchall()}
Semgrep 1.187.0 (semgrep scan --config rule.yaml before/ --dataflow-traces) reports it, with the whole trace inside orders_sorted:
20┆ return {"rows": db().execute(sql).fetchall()}
Taint comes from:
18┆ column = request.args.get("sort", "created_at")
Taint flows through these intermediate variables:
18┆ column = request.args.get("sort", "created_at")
19┆ sql = f"SELECT id, status FROM orders ORDER BY {column}"
...
Ran 1 rule on 1 file: 1 finding.
The refactor moves parameter parsing into a helper module, as teams do when several routes read the same parameters:
-from flask import Flask, request
+from flask import Flask
+from params import requested_sort
@@
- column = request.args.get("sort", "created_at")
+ column = requested_sort()
# params.py
from flask import request
def requested_sort(default="created_at"):
return request.args.get("sort", default)
The same scan on the refactored tree:
...
Ran 1 rule on 2 files: 0 findings.
The source now lives in params.py and the sink in reports.py. The value takes the same route at run time. The rule matches the source expression where it appears, inside requested_sort, and the engine does not carry that taint out through the function's return to the call site in another file.
Let a test decide#
A test that sends a hostile sort key through the route answers what the scan cannot. This probe uses Flask's test client and a subquery that reverses the row order when a guess about the first character of the admin password hash is right:
for guess in ("p", "x"):
sort = f"(SELECT CASE WHEN substr(password_hash,1,1)='{guess}' THEN -id ELSE id END FROM users)"
print(f"{sys.argv[1]:16} guess {guess!r}:", c.get("/orders/sorted", query_string={"sort": sort}).json["rows"])
before guess 'p': [[2, 'paid'], [1, 'open']]
before guess 'x': [[1, 'open'], [2, 'paid']]
after_helper guess 'p': [[2, 'paid'], [1, 'open']]
after_helper guess 'x': [[1, 'open'], [2, 'paid']]
The refactored route leaks exactly as the original did. The bug is still there; only the report went away.
You can also confirm that the loss was a visibility gap by telling the rule about the helper. Adding params.requested_sort(...) as a second source brings the finding back on the refactored tree, with the trace now starting at column = requested_sort():
21┆ return {"rows": db().execute(sql).fetchall()}
Taint comes from:
19┆ column = requested_sort()
...
Ran 1 rule on 2 files: 1 finding.
A pattern rule behaves differently. bandit 1.9.4 reports B608 on the f-string line in every version of the file (before/reports.py:19, after_helper/reports.py:20), because it never followed the value in the first place. A pattern finding that survives a refactor says the line still has the same shape, and nothing more.
A refactor that removed the path#
The disconfirmation is a second refactor that does fix the bug. It maps the requested value through a fixed table of column names, so only application-owned strings reach the SQL text:
+SORTABLE = {"created_at": "created_at", "id": "id", "status": "status"}
@@
- column = requested_sort()
+ column = SORTABLE.get(requested_sort(), "created_at")
The original rule reports nothing, which looks the same as before. The probe shows the difference:
after_allowlist guess 'p': [[1, 'open'], [2, 'paid']]
after_allowlist guess 'x': [[1, 'open'], [2, 'paid']]
The row order no longer depends on the guess, because an unknown key falls back to created_at. This finding is gone because the path is gone.
The widened rule does not make that call for you. With requested_sort modeled as a source, it still reports this version (Ran 1 rule on 2 files: 1 finding.), with the trace through SORTABLE.get(...). Semgrep's rule source describes the default behind that as "the assumption that function calls always propagate taint", and a dictionary lookup is a call. Widening the rule restores visibility; deciding whether the value is constrained is the test's job.
Record the test that closed it#
When you close a finding after a refactor, attach the evidence that the path is gone: the probe or regression test, run against the new code, with its output. "No longer reported" is the absence of evidence from a tool whose reach the refactor may have changed. Keep the test in the suite. If someone later replaces the allowlist with a pass-through helper, the test fails, while the scan, for the reasons above, may stay quiet.
Two quick checks tell you how much to trust a quiet scan after a refactor:
- Compare the old finding's trace with the new code. If any line of the trace moved to another function or file, the engine may have lost it.
- Look for a deeper mode or a rule change. If your scanner offers interprocedural or cross-file analysis that your pipeline does not run, the quiet result belongs to the shallower mode.
Decide from what the test shows#
| What changed | Evidence to collect | Decision |
|---|---|---|
| Finding vanished; its source and sink are now in different functions or files | A test that drives a hostile input from the source to the sink | Keep the finding open while the test reproduces the bug; fix it |
| Finding vanished; a rule that models the moved helper brings it back | The same test | Treat the gap as a scan limit; add the helper to the rule so the next refactor stays visible |
| Finding vanished; code now maps input to fixed values | The test, showing behavior no longer depends on the input | Close it and keep the test as the regression |
| Pattern finding survived the refactor | Where the interpolated values now come from | Re-trace by hand; survival says nothing about the path |
| Finding vanished with no code change near it | The rule and engine versions of both scans | Compare the scan configuration before reading the code |
For how a single-line pattern differs from a traced path, see the guide to pattern-match and data-flow findings. For turning the probe into a kept test, see the guide to turning a review comment into a regression test.