Skip to content

Commit 96499e1

Browse files
committed
A site's dynamic scans page five at a time, and each can be deleted
The Dynamic scans list on a site's page shows five scans at a time, newest first, with Newer and Older to page through the rest. Each finished scan has a Delete button, which asks first and keeps you on the page you were on; a running one has Stop instead. Deleting the newest scan makes the one before it the site's last scan.
1 parent a38d980 commit 96499e1

2 files changed

Lines changed: 65 additions & 16 deletions

File tree

‎app/web/routes_external.py‎

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -435,8 +435,24 @@ async def scan_many(request: Request, user: User = Depends(current_user)):
435435

436436
# ------------------------------------------------------------------ one site
437437

438+
SCANS_PER_PAGE = 5
439+
440+
441+
def _safe_back(raw, asset_id: int, fallback: str) -> str:
442+
"""Where a button on one of our pages asked to go back to: the list, or
443+
this site's page, with at most a page number of its scans. Anything else
444+
is ignored, so the field cannot send anybody off the site."""
445+
import re as _re
446+
447+
text = str(raw or "")
448+
if text == "/external" or _re.fullmatch(rf"/external/{int(asset_id)}(\?scans=\d{{1,5}})?", text):
449+
return text
450+
return fallback
451+
452+
438453
@router.get("/external/{asset_id}")
439-
def detail_page(asset_id: int, request: Request, user: User = Depends(current_user)):
454+
def detail_page(asset_id: int, request: Request, scans: int = 1,
455+
user: User = Depends(current_user)):
440456
from app.core import ai, dast_engines
441457

442458
with session_scope() as db:
@@ -446,29 +462,36 @@ def detail_page(asset_id: int, request: Request, user: User = Depends(current_us
446462
links = external.Mapper(db, user).links(asset)
447463
names = [l["full_name"] for l in links]
448464
counts = external.sast_counts(db, names, user)
449-
scans = (db.query(ExternalDastScan)
450-
.filter(ExternalDastScan.asset_id == asset.id)
451-
.order_by(ExternalDastScan.id.desc()).limit(100).all())
465+
# Newest first, a page at a time. Started, then finished, time
466+
# order and id order agree, so the id is the cheap way to sort.
467+
scan_query = db.query(ExternalDastScan).filter(ExternalDastScan.asset_id == asset.id)
468+
scan_total = scan_query.count()
469+
scan_pages = max(1, -(-scan_total // SCANS_PER_PAGE))
470+
scan_page = min(max(1, scans), scan_pages)
471+
page_scans = (scan_query.order_by(ExternalDastScan.id.desc())
472+
.offset((scan_page - 1) * SCANS_PER_PAGE).limit(SCANS_PER_PAGE).all())
473+
running = external.running_scans(db, [asset.id])
452474
linked = {l["full_name"]: counts.get(l["full_name"], {}) for l in links if l["linked"]}
453475
try:
454476
review = json.loads(asset.ai_review or "{}") if asset.ai_review else {}
455477
except ValueError:
456478
review = {}
479+
active_scan = running.get(asset.id)
457480
busy = (asset.telemetry_status == "running" or asset.ai_status == "running"
458-
or any(s.status in ("queued", "running") or s.ai_status == "running"
459-
for s in scans))
460-
active_scan = next((s for s in scans if s.status in ("queued", "running")), None)
481+
or active_scan is not None
482+
or any(s.ai_status == "running" for s in page_scans))
461483
context = _shared(db, user)
462484
context.update(
463485
active_page="external", asset=asset, tele=external.telemetry_of(asset),
464486
tags=asset.tag_list(), links=links, counts=counts,
465487
risk=external.combined_risk(asset, linked),
466488
due=external.due_status(asset, opts["max_age_days"]),
467-
blocked=external.blocked(db, asset), scans=scans, active_scan=active_scan,
489+
blocked=external.blocked(db, asset), scans=page_scans, active_scan=active_scan,
490+
scan_page=scan_page, scan_pages=scan_pages, scan_total=scan_total,
468491
fields=external.fields_of(asset), security_headers=external.SECURITY_HEADERS,
469492
picks_json=json.dumps(external.picks_of(db, asset)),
470493
environments=external.ENVIRONMENTS, statuses=external.STATUSES,
471-
scan_views=[external.scan_dict(s) for s in scans],
494+
scan_views=[external.scan_dict(s) for s in page_scans],
472495
servers=db.query(DastServer).order_by(DastServer.name).all(),
473496
profiles=dast_engines.PROFILES, active=dast_engines.ACTIVE_PROFILES,
474497
plans=dast_engines.PLANS, engines=dast_engines.installed(),
@@ -704,23 +727,24 @@ async def scan_action(asset_id: int, scan_id: int, action: str, request: Request
704727
scan = _scan(db, asset_id, scan_id)
705728
host = asset.host
706729
if action == "delete":
730+
form = await request.form()
731+
back = _safe_back(form.get("back"), asset_id, f"/external/{asset_id}")
707732
if scan.status in ("queued", "running"):
708-
return redirect(_back(here, "Stop it first.", "error"))
733+
return redirect(_back(back if back != f"/external/{asset_id}" else here,
734+
"Stop it first.", "error"))
709735
db.delete(scan)
710736
db.flush()
711737
external.copy_newest(db, asset)
712738
record(db, "external.scan_deleted", actor=user.username, target=host,
713739
detail=f"scan {scan_id}", source_ip=client_ip(request))
714-
return redirect(_back(f"/external/{asset_id}", "Removed that scan."))
740+
return redirect(_back(back, "Removed that scan."))
715741
if action == "stop":
716742
ok = external.stop_scan(scan_id)
717743
message, kind = ("Stopping the scan of " + host + ".", "ok") if ok \
718744
else ("It is not running.", "error")
719745
form = await request.form()
720746
# Back to where the Stop was pressed, and only to a page of ours.
721-
back = str(form.get("back") or "")
722-
if back in ("/external", f"/external/{asset_id}"):
723-
here = back
747+
here = _safe_back(form.get("back"), asset_id, here)
724748
elif action == "triage":
725749
problem = external.start_triage(scan_id, user.username)
726750
message, kind = (problem, "error") if problem else ("Asking the model. This page "

‎app/web/templates/external_site.html‎

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,11 @@
167167
</div>
168168

169169
<div class="card">
170-
<div class="card-head">Dynamic scans</div>
170+
<div class="card-head">Dynamic scans
171+
{% if scan_total %}<span class="faint small" style="font-weight:400">{{ scan_total }} in all, newest first</span>{% endif %}</div>
171172
{% if scan_views %}
172173
<div class="table-scroll"><table>
173-
<thead><tr><th>When</th><th>Server</th><th>Result</th><th>Findings</th><th>AI</th></tr></thead>
174+
<thead><tr><th>When</th><th>Server</th><th>Result</th><th>Findings</th><th>AI</th><th></th></tr></thead>
174175
<tbody>
175176
{% for s in scan_views %}
176177
<tr>
@@ -185,9 +186,33 @@
185186
<td class="nowrap">{% for level in ['critical','high','medium','low'] %}{% if s.counts[level] %}<span class="badge {{ level }}">{{ s.counts[level] }}</span> {% endif %}{% endfor %}
186187
{% if s.counts.info %}<span class="badge info">{{ s.counts.info }}</span>{% endif %}</td>
187188
<td class="small">{% if s.ai.status %}{{ s.ai.status }}{% if s.ai.input_tokens %}<div class="faint">{{ (s.ai.input_tokens + s.ai.output_tokens) | thousands }} tokens{% if s.ai.cost %}, {{ s.ai.cost }}{% endif %}</div>{% endif %}{% endif %}</td>
189+
<td class="nowrap">
190+
{% set back = '/external/' ~ asset.id ~ ('?scans=' ~ scan_page if scan_page > 1 else '') %}
191+
{% if s.status in ('queued', 'running') %}
192+
<form method="post" action="{{ base }}/external/{{ asset.id }}/scans/{{ s.id }}/stop" style="display:inline"
193+
data-confirm="Stop this scan?">
194+
<input type="hidden" name="csrf_token" value="{{ csrf_token }}">
195+
<input type="hidden" name="back" value="{{ back }}">
196+
<button class="btn sm danger" type="submit">Stop</button></form>
197+
{% else %}
198+
<form method="post" action="{{ base }}/external/{{ asset.id }}/scans/{{ s.id }}/delete" style="display:inline"
199+
data-confirm="Delete this scan and its findings? It cannot be undone.">
200+
<input type="hidden" name="csrf_token" value="{{ csrf_token }}">
201+
<input type="hidden" name="back" value="{{ back }}">
202+
<button class="btn sm" type="submit" title="Delete this scan">Delete</button></form>
203+
{% endif %}</td>
188204
</tr>
189205
{% endfor %}
190206
</tbody></table></div>
207+
{% if scan_pages > 1 %}
208+
<div class="card-body" style="display:flex;gap:.4rem;align-items:center">
209+
{% if scan_page > 1 %}<a class="btn sm" href="{{ base }}/external/{{ asset.id }}{{ '?scans=%d' % (scan_page - 1) if scan_page > 2 else '' }}">Newer</a>
210+
{% else %}<span class="btn sm" aria-disabled="true" style="opacity:.45">Newer</span>{% endif %}
211+
<span class="faint small">Page {{ scan_page }} of {{ scan_pages }}</span>
212+
{% if scan_page < scan_pages %}<a class="btn sm" href="{{ base }}/external/{{ asset.id }}?scans={{ scan_page + 1 }}">Older</a>
213+
{% else %}<span class="btn sm" aria-disabled="true" style="opacity:.45">Older</span>{% endif %}
214+
</div>
215+
{% endif %}
191216
{% else %}<div class="empty">No dynamic scan recorded yet.</div>{% endif %}
192217
</div>
193218

0 commit comments

Comments
 (0)