Skip to content

Commit f7529ab

Browse files
committed
Reports name the routes that change data and were never tested
The dynamic scan is read only by design: its own checks never send a request that creates, changes or deletes anything, and the engines only post the forms they find. A clean report said nothing about that, so it read as cover for routes nobody had touched. The code reading now also lists the routes that change data, with their method, file and line: Express, Koa and the like, FastAPI and Flask decorators, Spring mappings, Go routers, Rails and Laravel route files, and ASP.NET actions, where a bare [HttpPost] takes its controller's [Route] and [controller] is filled in. After the scan they are compared with what the engines sent, by method: a POST the engine really sent to that path counts as tested, a GET to the same path does not. The PDF gets a section after the code section: how many such routes the code declares, how many the scan tested, and the rest by method, route and place in the code, with what to check in each.
1 parent 1746573 commit f7529ab

5 files changed

Lines changed: 153 additions & 5 deletions

File tree

‎app/core/dast_paths.py‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,3 +492,38 @@ def open_among_protected(records: list, declared=lambda path: True) -> list:
492492
"refused_count": len(refused),
493493
"open_beside": len(opened) - 1})
494494
return out
495+
496+
497+
# ------------------------------------------------ write routes left untested
498+
499+
MAX_UNTESTED = 150
500+
501+
502+
def untested_writes(writes: list, records: list) -> dict:
503+
"""Which of the code's write routes the engines sent that method to.
504+
505+
This product's own checks never send a request that changes something,
506+
and the engines only post the forms they find, so most routes that
507+
create, change or delete are never exercised. Saying which, by name, is
508+
how a report tells its reader where a code review or a test by a person
509+
has to look. -> {"total", "tested", "untested": [{method, route,
510+
repository, file, line}]}
511+
"""
512+
seen = {}
513+
for rec in records or []:
514+
for method in rec.get("m") or []:
515+
seen.setdefault(method.upper(), []).append(rec.get("p") or "")
516+
out, tested, done = [], 0, set()
517+
for w in writes or []:
518+
key = (w.get("method"), (w.get("route") or "").lower())
519+
if key in done:
520+
continue
521+
done.add(key)
522+
index = Index({0: [w["route"]]}, skip_generic=False)
523+
if any(index.lookup(p) for p in seen.get(w["method"], [])):
524+
tested += 1
525+
continue
526+
if len(out) < MAX_UNTESTED:
527+
out.append({k: w.get(k) for k in ("method", "route", "repository", "file", "line")})
528+
return {"total": len(done), "tested": tested, "untested": out,
529+
"more": max(0, len(done) - tested - len(out))}

‎app/core/dast_report.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,11 @@ def build(db, asset, scan, user=None, organization: str = "") -> dict:
393393
code_sinks = []
394394
reachable_sinks = [s for s in code_sinks if s.get("reachable")]
395395
context_sinks = [s for s in code_sinks if not s.get("reachable")]
396+
# The code's write routes the scan never sent their method to. Read
397+
# only by design, so these are where a review or a person has to look.
398+
from app.core import dast_paths
399+
400+
writes = dast_paths.load(scan.paths).get("writes") or {}
396401
# Real SAST findings behind this site, grouped by the weakness type they are,
397402
# so a corroboration can name the exact code finding (its GCR id), not just
398403
# say "a code sink". That is the SAST telemetry driving a positive match.
@@ -486,6 +491,7 @@ def build(db, asset, scan, user=None, organization: str = "") -> dict:
486491
"overall": overall, "by_sev": by_sev, "categories": categories,
487492
"risk_matrix": risk_matrix, "data_profile": data_profile,
488493
"reachable_sinks": reachable_sinks, "context_sinks": context_sinks,
494+
"writes": writes,
489495
"heat_max": heat_max, "triaged": triaged, "ai_summary": ai_summary,
490496
"narrative": narrative(asset.host, view, overall, by_sev, triaged, profile_words),
491497
"top": [f for f in findings if f.get("severity") in ("critical", "high")

‎app/core/dast_whitebox.py‎

Lines changed: 85 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,85 @@ def _clean_route(raw: str) -> str:
104104
return path
105105

106106

107+
MAX_WRITES = 200
108+
WRITE_METHODS = ("POST", "PUT", "PATCH", "DELETE")
109+
110+
# Routes that change something, with their method. The method is group "m"
111+
# and the path group "p". A dynamic scan of this product is read only by
112+
# design, so these are the routes it never tests; listing them says where a
113+
# code review or a test with a person has to look instead.
114+
WRITE_PATTERNS = [
115+
# Express, Koa, Fastify, Hono: app.post('/x')
116+
re.compile(r"\b(?:app|router|server|api|route[rs]?)\.(?P<m>post|put|patch|delete)\(\s*['\"`](?P<p>/[^'\"`\s]*)"),
117+
# Flask, FastAPI: @router.post("/x")
118+
re.compile(r"@\w+\.(?P<m>post|put|patch|delete)\(\s*['\"](?P<p>/[^'\"\s]*)"),
119+
# Spring: @PostMapping("/x")
120+
re.compile(r"@(?P<m>Post|Put|Patch|Delete)Mapping\(\s*(?:value\s*=\s*|path\s*=\s*)?\{?\s*['\"](?P<p>/?[^'\"\s]*)"),
121+
# Go: r.POST("/x"), e.Post("/x")
122+
re.compile(r"\.(?P<m>POST|PUT|PATCH|DELETE|Post|Put|Patch|Delete)\(\s*['\"`](?P<p>/[^'\"`\s]*)"),
123+
# Rails routes.rb: post '/x'
124+
re.compile(r"^\s*(?P<m>post|put|patch|delete)\s+['\"](?P<p>/?[^'\"\s]*)", re.M),
125+
# Laravel: Route::post('/x'
126+
re.compile(r"Route::(?P<m>post|put|patch|delete)\(\s*['\"](?P<p>/?[^'\"\s]*)"),
127+
]
128+
# ASP.NET: [HttpPost], [HttpPost("x")], joined with the controller's [Route].
129+
_CS_CLASS_ROUTE = re.compile(
130+
r"\[Route\(\s*\"(?P<p>[^\"]*)\"\s*\)\][^{;]*?\bclass\s+(?P<c>\w+)", re.S)
131+
_CS_CLASS = re.compile(r"\bclass\s+(?P<c>\w+)")
132+
_CS_WRITE = re.compile(r"\[Http(?P<m>Post|Put|Patch|Delete)(?:\(\s*\"(?P<p>[^\"]*)\"[^)]*\))?\]")
133+
134+
135+
def _line_of(text: str, index: int) -> int:
136+
return text.count("\n", 0, index) + 1
137+
138+
139+
def _aspnet_writes(text: str) -> list:
140+
"""[(method, path, line)] for ASP.NET actions that change something."""
141+
if "[Http" not in text:
142+
return []
143+
classes = [(m.start(), m.group("c")) for m in _CS_CLASS.finditer(text)]
144+
routes = {m.group("c"): m.group("p") for m in _CS_CLASS_ROUTE.finditer(text)}
145+
out = []
146+
for m in _CS_WRITE.finditer(text):
147+
owner = ""
148+
for start, name in classes:
149+
if start < m.start():
150+
owner = name
151+
template = m.group("p")
152+
if template is not None and template.startswith(("/", "~/")):
153+
raw = template.lstrip("~")
154+
else:
155+
base = routes.get(owner, "")
156+
if not base and template is None:
157+
continue
158+
raw = "/" + "/".join(x.strip("/") for x in (base, template or "") if x.strip("/"))
159+
controller = owner[:-len("Controller")] if owner.endswith("Controller") else owner
160+
raw = re.sub(r"\[controller\]", controller, raw, flags=re.I)
161+
if "[action]" in raw.lower():
162+
continue
163+
out.append((m.group("m").upper(), raw, _line_of(text, m.start())))
164+
return out
165+
166+
167+
def writes_in(text: str, rel: str) -> list:
168+
"""Routes in one file that change something. -> [{method, route, file, line}]"""
169+
found = []
170+
if rel.endswith(".cs"):
171+
found = _aspnet_writes(text)
172+
else:
173+
for pattern in WRITE_PATTERNS:
174+
for m in pattern.finditer(text):
175+
found.append((m.group("m").upper(), m.group("p"), _line_of(text, m.start())))
176+
out, seen = [], set()
177+
for method, raw, line in found:
178+
route = _clean_route(raw)
179+
if not route or route == "/" or (method, route) in seen:
180+
continue
181+
seen.add((method, route))
182+
out.append({"method": method, "route": route, "file": rel, "line": line})
183+
return out
184+
185+
107186
def _openapi_paths(text: str, name: str) -> list:
108187
"""Paths from an OpenAPI or Swagger file, JSON or YAML."""
109188
out = []
@@ -145,8 +224,9 @@ def _sinks_in(text: str, rel: str, file_routes: list) -> list:
145224

146225

147226
def read(root: Path, host: str = "") -> dict:
148-
"""-> {routes: [path], sinks: [...], host_files: [file], files: read}"""
149-
routes, sinks, host_files, read_count = [], [], [], 0
227+
"""-> {routes: [path], sinks: [...], host_files: [file], files: read,
228+
writes: [{method, route, file, line}]}"""
229+
routes, sinks, host_files, read_count, writes = [], [], [], 0, []
150230
host_re = re.compile(r"(?<![A-Za-z0-9.-])" + re.escape(host.lower()) + r"(?![A-Za-z0-9-])",
151231
re.I) if host else None
152232
for dirpath, dirnames, filenames in os.walk(root):
@@ -191,7 +271,9 @@ def read(root: Path, host: str = "") -> dict:
191271
routes.append(cleaned)
192272
if len(sinks) < MAX_SINKS:
193273
sinks += _sinks_in(text, rel, file_routes)
274+
if len(writes) < MAX_WRITES:
275+
writes += writes_in(text, rel)
194276
if len(routes) >= MAX_ROUTES * 2 and len(sinks) >= MAX_SINKS:
195277
break
196278
return {"routes": routes[:MAX_ROUTES], "sinks": sinks[:MAX_SINKS],
197-
"host_files": host_files, "files": read_count}
279+
"host_files": host_files, "files": read_count, "writes": writes[:MAX_WRITES]}

‎app/core/external.py‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,7 @@ def remember_code_lost(asset_id: int, lost) -> None:
752752

753753

754754
MAX_SAMPLE_LINKS = 5
755+
MAX_WHITEBOX_WRITES = 300
755756

756757

757758
def sample_links_of(asset) -> list:
@@ -2422,7 +2423,7 @@ def _whitebox(scan_id: int, asset_id: int, host: str) -> dict:
24222423
plan = plan[:MAX_WHITEBOX_REPOS]
24232424
adhoc = {"git": asset.adhoc_git_url or "", "archive": asset.adhoc_archive or "",
24242425
"asset_id": asset_id} if asset is not None else {}
2425-
out = {"repos": [], "routes": [], "by_repo": {}, "sinks": []}
2426+
out = {"repos": [], "routes": [], "by_repo": {}, "sinks": [], "writes": []}
24262427
for repo_id, name, connection, branch in plan:
24272428
if scan_id in _stopping:
24282429
break
@@ -2447,6 +2448,9 @@ def _whitebox(scan_id: int, asset_id: int, host: str) -> dict:
24472448
for sink in got.get("sinks") or []:
24482449
if len(out["sinks"]) < MAX_WHITEBOX_SINKS:
24492450
out["sinks"].append(dict(sink, repository=name))
2451+
for write in got.get("writes") or []:
2452+
if len(out["writes"]) < MAX_WHITEBOX_WRITES:
2453+
out["writes"].append(dict(write, repository=name))
24502454
except Exception as exc: # noqa: BLE001 - the scan carries on without it
24512455
entry["error"] = f"{type(exc).__name__}: {exc}"[:200]
24522456
out["repos"].append(entry)
@@ -2482,6 +2486,9 @@ def _whitebox(scan_id: int, asset_id: int, host: str) -> dict:
24822486
for sink in got.get("sinks") or []:
24832487
if len(out["sinks"]) < MAX_WHITEBOX_SINKS:
24842488
out["sinks"].append(dict(sink, repository=label))
2489+
for write in got.get("writes") or []:
2490+
if len(out["writes"]) < MAX_WHITEBOX_WRITES:
2491+
out["writes"].append(dict(write, repository=label))
24852492
except Exception as exc: # noqa: BLE001 - the scan carries on without it
24862493
entry["error"] = f"{type(exc).__name__}: {exc}"[:200]
24872494
out["repos"].append(entry)
@@ -3498,7 +3505,8 @@ def fetch(path):
34983505
first = {r["p"] for r in records if r.get("v") and index.any_match(r["p"])}
34993506
dast_paths.verify(records, controls, fetch, first)
35003507
paths = {"control": {p: c["status"] for p, c in controls.items()},
3501-
"paths": records, "counts": dast_paths.counts(records)}
3508+
"paths": records, "counts": dast_paths.counts(records),
3509+
"writes": dast_paths.untested_writes(wb.get("writes") or [], records)}
35023510
sinks = _correlate_sinks(wb.get("sinks") or [], records)
35033511
_update(scan_id, paths=json.dumps(paths), paths_count=paths["counts"]["answered"],
35043512
paths_real=paths["counts"]["reachable"],

‎app/web/templates/dast_report_pdf.html‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,23 @@ <h3>5.2 Areas to review (not reached by the scan)</h3>
329329
{% endif %}
330330
{% endif %}
331331

332+
{% if writes and writes.untested %}
333+
<h2 class="new-page">{{ '6' if (reachable_sinks or context_sinks) else '5' }}. Routes that change data, not tested</h2>
334+
<p>{{ ("This scan is read only by design: its own checks never send a request that creates, changes or deletes anything, and the engines only submit the forms they find. The code behind the site declares " ~ writes.total ~ " route" ~ ('' if writes.total == 1 else 's') ~ " that change data, and the scan sent the matching method to " ~ writes.tested ~ " of them. The rest are below. A clean report says nothing about them.") | sentence_spaced }}</p>
335+
<p>{{ "Check each one in the code, or with a person testing it: that it asks for a sign in, that it checks the caller may change that record, and that it does not trust a record number from the request." | sentence_spaced }}</p>
336+
<table>
337+
<thead><tr><th>Method</th><th>Route</th><th>Where</th></tr></thead>
338+
<tbody>
339+
{% for w in writes.untested[:80] %}
340+
<tr><td>{{ w.method }}</td><td class="mono">{{ w.route }}</td>
341+
<td class="mono">{{ w.repository }} {{ w.file }}:{{ w.line }}</td></tr>
342+
{% endfor %}
343+
</tbody>
344+
</table>
345+
{% set rest = (writes.untested | length - 80 if writes.untested | length > 80 else 0) + (writes.more or 0) %}
346+
{% if rest %}<p class="faint">{{ rest }} more not shown.</p>{% endif %}
347+
{% endif %}
348+
332349
<h2 class="new-page">Appendix A. The scan underneath</h2>
333350
<table>
334351
<tr><td class="k">Scan number</td><td>{{ scan.id }}</td></tr>

0 commit comments

Comments
 (0)