Repository navigation
feat(runtime): add per-project Runtime dashboard, live polling, and alert webhooks - #212
SriRamkunamsetty wants to merge 1 commit into
Conversation
vsn411
left a comment
There was a problem hiding this comment.
Thanks, and welcome! The Runtime page is a big step for #72. Sensor health with a stale state after 3 missed heartbeats, coverage, new packages and newly reached functions are exactly what the issue asks for. The webhook payload deliberately leaves out stack samples, and the polling script is CSP-clean.
Some changes are needed in the alert path before we merge, because it currently runs inside the request handlers:
- Ingest blocks the event loop.
post_eventsisasync, but_notify_newly_reachedruns synchronously inside it. It reads everyruntime_evidencerow for the project on each batch with hits, and callsemit_alertonce per row. Each call does anINSERT … ; commit, and possibly a blockingurlopenwith a 5 s timeout. Evidence grows without bound, so a busy sensor will slow down, and every other request waits while the webhook POST runs. Suggestions:- only look at fingerprints that are new in this batch, e.g. a SQL anti-join against
runtime_alerts_sent; - send the webhook off the event loop (
BackgroundTasks, or a small worker thread).
- only look at fingerprints that are new in this batch, e.g. a SQL anti-join against
- A GET can fire a webhook. The
sensor_stalealert is emitted while/projects/{id}/runtime(and the 10 s.jsonpoll) renders. Viewing a page shouldn't have side effects, and with the page closed nobody gets the alert. Detect staleness where heartbeats are processed, or in a periodic check, rather than in the dashboard builder. _claim_alertcommits the caller's connection. Called mid-ingest, it commits whatever the ingest had pending. Let the caller own the transaction.- Don't log the webhook URL.
logger.warning("… failed for %s", url)writes the full URL, and Slack/Teams-style URLs carry their secret token in the path. Log the host only.
Small things:
runtime/api.pynow imports private helpers fromweb/(_vulnerability_key,_is_request_route). Moving them to a shared module would keepruntimeindependent of the web layer.- #213 (yours too) also changes
routes/projects.pyandproject.html, so whichever merges second will need a rebase.
CI hasn't run yet because this is your first PR here. A maintainer will approve the workflow run.
059909c to
3e044da
Compare
|
Addressed all review feedback and rebased onto \main:
|
| secret_url = "https://hooks.example.com/services/SECRET_WORKSPACE/SECRET_TOKEN" | ||
| with caplog.at_level(logging.WARNING, logger="secfoo.runtime.alerts"): | ||
| post_webhook(secret_url, {"event": "sensor_stale"}) | ||
| assert "hooks.example.com" in caplog.text |
vsn411
left a comment
There was a problem hiding this comment.
Thanks for the quick turnaround. Every point from the last review is fixed: webhooks go out through BackgroundTasks, the GET routes have no side effects, the caller owns the transaction, only the host is logged, and the helpers moved out of web/.
One thing left before we merge:
A dead sensor is never reported as stale. detect_stale_sensors only runs inside /heartbeat and /events, so it needs some sensor in the project to still be talking. With a single sensor, the usual case for local and CI runs, nothing arrives once it dies, and sensor_stale never fires. Please add a periodic check that doesn't depend on incoming traffic. For example, a background task started with secfoo serve (FastAPI lifespan) that calls detect_stale_sensors every HEARTBEAT_INTERVAL_SECONDS. Add a test where the only sensor stops sending and the alert still goes out.
Small things, optional:
configured_webhook_url()readsconfig.tomlon everyemit_alertcall. Reading it once per request (and returning early from_notify_newly_reachedwhen no webhook is configured) avoids the per-row disk read and the SCA findings query.- The CodeQL alert in
tests/test_web/test_runtime_dashboard.py:368is a test assertion, not sanitisation. We'll dismiss it, so there's nothing to do there. - #210 is about to merge and also changes
storage/schema.sql, so this needs a small rebase after it lands.
Fixes #72
Summary
/projects/{id}/runtime, linked fromproject.html):src/secfoo/web/runtime_dashboard.pycomputing connected sensor health (healthy vs Stale after missing >= 3 heartbeats), events/min, dropped event totals, observation windows, function execution coverage (app_functions_executed / app_functions_total), routes observed, newly loaded packages, version drift / unscanned / new advisory findings, and newly reached vulnerable functions linking directly to SCA finding details./projects/{id}/runtimeand/projects/{id}/runtime.jsonroutes insrc/secfoo/web/routes/projects.pyplus templatesprojects_runtime.html,project.html, and CSP-clean external scriptsrc/secfoo/web/static/js/runtime-live.js(polling every 10s while the tab is visible).[runtime] alert_webhook):[runtime] alert_webhookinsrc/secfoo/settings.py.src/secfoo/runtime/alerts.py+runtime_alerts_sentdeduplication table inschema.sql, emitting sanitized webhook payloads (withfinding_fingerprint,app,env, and deeplink, never stack samples) forreached_from_traffic(runtime/api.py),new_advisory(runtime/inventory.py), andsensor_stale(runtime_dashboard.py, reset on fresh heartbeat).[runtime] alert_webhookand/projects/<id>/runtimeindocs/REFERENCE.md.tests/test_web/test_runtime_dashboard.py.Verification
ruff check .— 0 errorsbandit -c .bandit -ll -r src/secfoo— 0 medium/high issuespytest tests/test_web/test_runtime_dashboard.py tests/test_runtime tests/test_web/test_no_inline_scripts.py tests/test_web/test_routes.py tests/test_docs_consistency.py -q— 231 passed