Open-source AI Security Operations Center: alert fusion, LLM-agent triage, MITRE ATT&CK investigation, and a replayable decision ledger for every agent step. Self-hostable, runs with no API keys, MIT licensed. Ships an MCP server for Claude, Cursor and Continue.
Python agentic-aiai-securityalert-triageblue-teamcybersecuritydetection-engineeringincident-responselanggraphllm-agentsmcpmitre-attackmodel-context-protocolpurple-teamsecurity-operationsself-hostedsiemsoarsocthreat-detectionthreat-intelligence
fix(main): repair the six defects the v17 follow-on landed with (#1160)
Beenu Arora7a894e66333a
diff --git a/scripts/chaos/store_outage.py b/scripts/chaos/store_outage.pyindex a3879226e..8e9744f1b 100644--- a/scripts/chaos/store_outage.py+++ b/scripts/chaos/store_outage.py@@ -227,10 +227,58 @@ def grade_outage(return result+def _resolve_container(service: str) -> str | None:+ """The container id for a compose service, or ``None`` if it is not up.++ Asked of compose rather than assembled from a convention. Every service in+ `docker-compose.yml` pins `container_name: aisoc-<service>`, so the bare+ service name this module records never matched a running container and+ every outage reported `could not stop container 'postgres' -- docker+ unavailable?`. All six store-outage jobs failed that way, naming Docker+ for a defect that was ours.++ `docker compose ps -q` is authoritative: it accounts for the pinned name,+ the project prefix when there is no pinned name, and a service that is+ simply not running. The literal fallbacks exist so the script still works+ when invoked outside a compose project.+ """+ try:+ done = subprocess.run(+ ["docker", "compose", "ps", "-q", service],+ capture_output=True,+ text=True,+ timeout=60,+ check=False,+ )+ cid = (done.stdout or "").strip().splitlines()+ if done.returncode == 0 and cid:+ return cid[0]+ except Exception: # noqa: BLE001 - fall through to the literal names+ pass++ for candidate in (f"aisoc-{service}", service):+ try:+ done = subprocess.run(+ ["docker", "inspect", "-f", "{{.Id}}", candidate],+ capture_output=True,+ text=True,+ timeout=30,+ check=False,+ )+ if done.returncode == 0 and (done.stdout or "").strip():+ return candidate+ except Exception: # noqa: BLE001+ continue+ return None++def _container_action(container: str, action: str) -> bool:+ target = _resolve_container(container)+ if target is None:+ return Falsetry:subprocess.run(- ["docker", action, container],+ ["docker", action, target],check=True,capture_output=True,timeout=60,diff --git a/scripts/check_role_grant_scope.py b/scripts/check_role_grant_scope.pyindex 7227fd66a..82f3a0797 100644--- a/scripts/check_role_grant_scope.py+++ b/scripts/check_role_grant_scope.py@@ -153,6 +153,63 @@ def _module_literal(tree: ast.Module, name: str) -> object | None:return None+#: The permission string that means "every permission", as+#: `app/core/role_grants.py` declares it.+WILDCARD = "*"+++def _subscript_assigned_keys(tree: ast.Module, name: str) -> dict[str, ast.expr]:+ """Roles bound by ``NAME["role"] = <expr>`` after the dict literal.++ `_module_literal` only matches an `ast.Name` target, so a role defined+ this way is invisible to it -- and `infosec` is defined exactly this way,+ as a set expression derived from two other roles so the two cannot drift.+ The gate therefore reported it as "grantable but grants nothing" while the+ role resolves to 18 permissions at runtime, which is a false positive on+ working code and is how a gate gets suppressed.+ """+ found: dict[str, ast.expr] = {}+ for node in ast.walk(tree):+ if not isinstance(node, ast.Assign):+ continue+ for target in node.targets:+ if (+ isinstance(target, ast.Subscript)+ and isinstance(target.value, ast.Name)+ and target.value.id == name+ and isinstance(target.slice, ast.Constant)+ and isinstance(target.slice.value, str)+ ):+ found[target.slice.value] = node.value+ return found+++def _mentions_wildcard(expr: ast.expr) -> bool:+ """Whether an expression could produce the wildcard permission.++ Conservative: a literal ``"*"`` anywhere in the subtree counts. A role+ built by *subtracting* from other roles cannot gain a permission none of+ them had, and the callers below resolve the roles it reads, so this only+ has to catch the wildcard being introduced here.+ """+ return any(isinstance(n, ast.Constant) and n.value == WILDCARD for n in ast.walk(expr))+++def _referenced_roles(expr: ast.expr, name: str) -> set[str]:+ """Roles an expression reads out of ``NAME[...]``."""+ out: set[str] = set()+ for n in ast.walk(expr):+ if (+ isinstance(n, ast.Subscript)+ and isinstance(n.value, ast.Name)+ and n.value.id == name+ and isinstance(n.slice, ast.Constant)+ and isinstance(n.slice.value, str)+ ):+ out.add(n.slice.value)+ return out++def _binding(tree: ast.Module, name: str) -> ast.expr | None:"""The expression a module-level name is bound to, literal or not."""for node in ast.walk(tree):@@ -308,8 +365,20 @@ def audit(root: Path) -> tuple[list[str], dict[str, int]]:problems.append(f"{SECURITY} is absent, so the enforced role map could not be read")return problems, counts- enforced = _module_literal(_parse(security), "ROLE_PERMISSIONS")+ security_tree = _parse(security)+ enforced = _module_literal(security_tree, "ROLE_PERMISSIONS")+ # Roles added after the literal, which `_module_literal` cannot see.+ derived = _subscript_assigned_keys(security_tree, "ROLE_PERMISSIONS")if isinstance(enforced, dict):+ for role, expr in derived.items():+ if role in enforced:+ continue+ # A derived role holds the wildcard only if it introduces one or+ # reads a role that already has it. Recorded as the permission+ # list so the wildcard scan below reads it the same way as a+ # literal row.+ inherits_wildcard = any(WILDCARD in (enforced.get(r) or []) for r in _referenced_roles(expr, "ROLE_PERMISSIONS"))+ enforced[role] = [WILDCARD] if (_mentions_wildcard(expr) or inherits_wildcard) else ["<derived>"]counts["roles"] = len(enforced)if not role_grants.is_file():return problems, counts@@ -323,7 +392,7 @@ def audit(root: Path) -> tuple[list[str], dict[str, int]]:return problems, counts# counted above, before the early return- wildcard = {role for role, perms in enforced.items() if "*" in (perms or [])}+ wildcard = {role for role, perms in enforced.items() if WILDCARD in (perms or [])}refused = wildcard | set(explicit)for role in sorted(set(grantable) & wildcard):diff --git a/scripts/check_route_auth.py b/scripts/check_route_auth.pyindex 744abf88c..7b4601547 100644--- a/scripts/check_route_auth.py+++ b/scripts/check_route_auth.py@@ -112,6 +112,12 @@ PUBLIC_ROUTES: dict[str, str] = {"services/fusion/app/api/router.py::metrics": "Prometheus scrape of worker counters; no tenant data, numeric series only",# -- sign-in: the request that establishes a credential cannot carry one --"services/api/app/api/v1/endpoints/auth.py::login": "the sign-in request itself; it is what mints the credential",+ "services/api/app/api/v1/endpoints/auth.py::sso_status": (+ "the login screen asks this before anyone has a credential, to decide whether to "+ "offer the SSO button and whether to keep the password form. Read-only, and the "+ "payload is four fields -- enabled, provider, button label, local-login-enabled -- "+ "with no issuer, client id, endpoint or secret."+ ),"services/api/app/api/v1/endpoints/auth.py::refresh_token": "presents a signed refresh token in the body and is verified there","services/api/app/api/v1/endpoints/waitlist.py::signup": "public sign-up form; rate-limited and writes only to the waitlist table","services/api/app/api/v1/endpoints/passkeys.py::passkey_authenticate_begin": (diff --git a/scripts/competitor_names.toml b/scripts/competitor_names.tomlindex 3dc98f864..fa7828817 100644--- a/scripts/competitor_names.toml+++ b/scripts/competitor_names.toml@@ -110,6 +110,16 @@ paths = ["services/connectors/**"]names = ["Torq", "Tines", "Wazuh"]reason = "Connector modules, the connector registry, event fixtures and connector tests. These vendors ship as first-party integrations."+[[allow]]+# The published spec is generated from the code above, so a connector field+# that legitimately names a first-party integration reappears here on every+# regeneration. `wazuh_locator` on the alert envelope is the deep link back to+# the system the alert came from; excusing the generator's input but not its+# output would make `make openapi` red the build.+paths = ["docs/openapi.yaml"]+names = ["Torq", "Tines", "Wazuh"]+reason = "Generated from services/connectors/** and the connector-derived API models, which are already excused above. The spec is an artefact of that code, not an independent mention."+[[allow]]paths = ["plugins/*/plugin.yaml"]names = ["Torq", "Tines", "Wazuh"]diff --git a/services/agents/app/investigator/forensic_agent.py b/services/agents/app/investigator/forensic_agent.pyindex bc100815f..bded013b2 100644--- a/services/agents/app/investigator/forensic_agent.py+++ b/services/agents/app/investigator/forensic_agent.py@@ -63,8 +63,7 @@ async def _llm_forensic(state: InvestigatorState) -> dict[str, Any]:max_lines=80,max_depth=3,)- evidence_available = bool((state.raw_alert or {}).get("alerts")- or (state.enrichment_cache or {}))+ evidence_available = bool((state.raw_alert or {}).get("alerts") or (state.enrichment_cache or {}))prompt = (f"Alert summary:\n{safe_summary}\n\n"@@ -72,9 +71,11 @@ async def _llm_forensic(state: InvestigatorState) -> dict[str, Any]:f"Recon findings:\n{safe_recon}\n"f"MITRE techniques: {safe_mitre}\n\n"f"Enrichment data (sample):\n{enrichment_blob}"- + ("" if evidence_available else- "\n\nWARNING: no alert payload, no enrichment, no lake results "- "were retrieved. Produce only an inconclusive analysis.")+ + (+ ""+ if evidence_available+ else "\n\nWARNING: no alert payload, no enrichment, no lake results were retrieved. Produce only an inconclusive analysis."+ ))bundle_append = format_bundle_prompt_append(state.context_bundle)if bundle_append:@@ -180,27 +181,31 @@ async def run_forensic(state_dict: dict[str, Any]) -> dict[str, Any]:# cap confidence, and mark the run inconclusive so the report and the# UI cannot present fiction as fact (2026-10 live incident: a run over# an empty raw_alert "found" C:\\Windows\\Temp\\malware.exe).- has_primary = bool((state.raw_alert or {}).get("alerts")- or (state.raw_alert or {}).get("title")- or (state.raw_alert or {}).get("full_log"))+ has_primary = bool(+ (state.raw_alert or {}).get("alerts") or (state.raw_alert or {}).get("title") or (state.raw_alert or {}).get("full_log")+ )has_enriched = bool(state.enrichment_cache)if not has_primary and not has_enriched:- findings = findings.model_copy(update={- "artefacts": [],- "confidence": min(findings.confidence, 0.1),- "root_cause_hypothesis": (- "INCONCLUSIVE - no alert payload or retrieved evidence was "- "available to this investigation; any finding would be "- "speculation."- ),- "summary": (- "Automated forensic analysis inconclusive: zero evidence "- "retrieved. Manual review with the source-console locator on the "- "case is required."- ),- })+ findings = findings.model_copy(+ update={+ "artefacts": [],+ "confidence": min(findings.confidence, 0.1),+ "root_cause_hypothesis": (+ "INCONCLUSIVE - no alert payload or retrieved evidence was "+ "available to this investigation; any finding would be "+ "speculation."+ ),+ "summary": (+ "Automated forensic analysis inconclusive: zero evidence "+ "retrieved. Manual review with the source-console locator on the "+ "case is required."+ ),+ }+ )state.log(- StepKind.WARNING if hasattr(StepKind, "WARNING") else StepKind.DECISION_REASON,+ # StepKind has no WARNING member, so the guarded form this replaced+ # always took the fallback while still failing the type check.+ StepKind.DECISION_REASON,"ForensicAgent","inconclusive: zero evidence retrieved; artefacts suppressed",)diff --git a/services/agents/tests/test_shadow_mode.py b/services/agents/tests/test_shadow_mode.pyindex b84c6d156..72ac574e6 100644--- a/services/agents/tests/test_shadow_mode.py+++ b/services/agents/tests/test_shadow_mode.py@@ -17,6 +17,7 @@ reads. Both are the evaluation answering itself.from __future__ import annotations+import inspectimport uuidfrom typing import Any@@ -215,13 +216,24 @@ class TestTheLedgerLeavesTheAnalystsColumnsAlone:def test_the_disposition_write_is_guarded_by_the_shadow_parameter(self):source = ledger_module.persist_auto_triage.__doc__ or ""assert "shadow" in source- import inspect- body = inspect.getsource(ledger_module.persist_auto_triage)+ body = " ".join(inspect.getsource(ledger_module.persist_auto_triage).split())assert "SET disposition = CASE WHEN $10 THEN disposition ELSE $3 END" in body- assert "status = CASE WHEN $7 AND NOT $10 THEN 'resolved' ELSE status END" in bodyassert "resolved_at = CASE WHEN $7 AND NOT $10 THEN now() ELSE resolved_at END" in body+ # Check the property rather than one known line. The previous form+ # asserted the exact text of the single status branch that existed when+ # it was written, so adding a branch broke it on layout alone -- it+ # would not have failed had the new branch simply omitted the guard,+ # which is the thing that actually matters.+ status_case = body[body.index("status = CASE") : body.index("END, resolved_at")]+ branches = [b.strip() for b in status_case.split("WHEN")[1:] if "THEN" in b]+ assert branches, f"no status branches found to check in: {status_case!r}"+ for branch in branches:+ assert "NOT $10" in branch, (+ f"status branch {branch!r} writes a status without the shadow guard. A shadow run must never move an alert's status."+ )+def test_the_guard_does_not_depend_on_the_caller_forcing_auto_closed_off(self):"""``NOT $10`` is what makes this survive a forgetful future caller.diff --git a/services/api/app/api/v1/endpoints/alerts.py b/services/api/app/api/v1/endpoints/alerts.pyindex e92e1f596..87cd70acd 100644--- a/services/api/app/api/v1/endpoints/alerts.py+++ b/services/api/app/api/v1/endpoints/alerts.py@@ -5,7 +5,7 @@ import loggingimport reimport uuidfrom datetime import UTC, datetime-from typing import Annotated, Literal+from typing import Annotated, Any, Literalfrom fastapi import APIRouter, Depends, Header, HTTPException, Query, statusfrom pydantic import BaseModel, ConfigDict, Field@@ -135,6 +135,20 @@ class AlertDetailResponse(AlertResponse):wazuh_locator: dict = {}+def _mapping_section(source: dict[str, Any], key: str) -> dict[str, Any]:+ """One nested object from a raw event, or an empty dict.++ Written out rather than inlined as+ `source.get(k) if isinstance(source.get(k), dict) else {}`. That form reads+ the key twice and, more to the point, mypy cannot narrow it: the true+ branch keeps `.get()`'s `Any | None`, so every `.get()` on the result was a+ `union-attr` error even though the isinstance guard makes it safe. Seven of+ them, on code that works.+ """+ value = source.get(key)+ return value if isinstance(value, dict) else {}++def _build_wazuh_locator(raw_event: dict) -> dict:"""Extract where-in-the-source-HIDS coordinates from a stored alert payload.@@ -153,11 +167,11 @@ def _build_wazuh_locator(raw_event: dict) -> dict:raw = rdmerged = {**raw, **{k: v for k, v in raw_event.items() if k != "raw_data"}}- rule = raw_event.get("rule") if isinstance(raw_event.get("rule"), dict) else {}- agent = raw_event.get("agent") if isinstance(raw_event.get("agent"), dict) else {}- data = raw_event.get("data") if isinstance(raw_event.get("data"), dict) else {}- finding = raw_event.get("finding") if isinstance(raw_event.get("finding"), dict) else {}- device = raw_event.get("device") if isinstance(raw_event.get("device"), dict) else {}+ rule = _mapping_section(raw_event, "rule")+ agent = _mapping_section(raw_event, "agent")+ data = _mapping_section(raw_event, "data")+ finding = _mapping_section(raw_event, "finding")+ device = _mapping_section(raw_event, "device")candidates = {"wazuh_alert_id": merged.get("alert_id") or rule.get("alert_id") or finding.get("uid") or raw_event.get("id"),diff --git a/services/api/app/api/v1/endpoints/attack_chain.py b/services/api/app/api/v1/endpoints/attack_chain.pyindex 453a6807f..d4f37d8dd 100644--- a/services/api/app/api/v1/endpoints/attack_chain.py+++ b/services/api/app/api/v1/endpoints/attack_chain.py@@ -72,10 +72,9 @@ async def get_attack_chain(# legacy ``cases`` table — ``select(Case)`` 404'd every real case.case_row = (await db.execute(- text(- "SELECT id, tenant_id, alert_ids FROM aisoc_cases "- "WHERE id = :id AND tenant_id = :tenant_id"- ).bindparams(id=case_id, tenant_id=user.tenant_id)+ text("SELECT id, tenant_id, alert_ids FROM aisoc_cases WHERE id = :id AND tenant_id = :tenant_id").bindparams(+ id=case_id, tenant_id=user.tenant_id+ ))).fetchone()if case_row is None:diff --git a/services/api/app/api/v1/endpoints/auth.py b/services/api/app/api/v1/endpoints/auth.pyindex b7a1d777d..c3698bc4d 100644--- a/services/api/app/api/v1/endpoints/auth.py+++ b/services/api/app/api/v1/endpoints/auth.py@@ -111,7 +111,7 @@ async def login(request: LoginRequest, http_request: Request, db: DBSession) -># other attempt here.sso_enabled = (settings_dict().get("SSO_ENABLED") or "false").strip().lower() in {"1", "true", "yes", "on"}local_admin_only = (settings_dict().get("SSO_LOCAL_ADMIN_ONLY") or "false").strip().lower() in {"1", "true", "yes", "on"}- if sso_enabled and local_admin_only and user.role not in WILDCARD_ROLES:+ if sso_enabled and local_admin_only and user.role not in WILDCARD_ROLES():raise HTTPException(status_code=status.HTTP_403_FORBIDDEN,detail="Password sign-in is disabled for this account. Use single sign-on.",diff --git a/services/api/app/api/v1/endpoints/cases.py b/services/api/app/api/v1/endpoints/cases.pyindex 768956622..4b6e3fbd9 100644--- a/services/api/app/api/v1/endpoints/cases.py+++ b/services/api/app/api/v1/endpoints/cases.py@@ -39,7 +39,7 @@ import osimport reimport uuidfrom datetime import UTC, datetime-from typing import Annotated, Any, Literal+from typing import Annotated, Any, Literal, castfrom urllib.parse import quoteimport httpx@@ -104,9 +104,20 @@ def _validate_agents_path(path: str) -> str:# Pydantic schemas# ────────────────────────────────────────────────────────────────────────────-CaseStatus = Literal["new", "triaged", "investigating", "contained", "resolved", "closed",- # legacy/console aliases, normalized to the ladder above in update_case- "open", "in_progress", "pending", "pending_closure", "cancelled"]+CaseStatus = Literal[+ "new",+ "triaged",+ "investigating",+ "contained",+ "resolved",+ "closed",+ # legacy/console aliases, normalized to the ladder above in update_case+ "open",+ "in_progress",+ "pending",+ "pending_closure",+ "cancelled",+]CaseSeverity = Literal["info", "low", "medium", "high", "critical"]# Valid forward-only state transitions@@ -129,8 +140,14 @@ _STATUS_ALIASES: dict[str, str] = {}-def _normalize_status(status: str) -> str:- return _STATUS_ALIASES.get(status, status)+def _normalize_status(status: str) -> CaseStatus:+ """Map a legacy write vocabulary onto the canonical ladder.++ Returns `CaseStatus` rather than `str` because the result is assigned back+ onto a `CaseStatus`-typed field: every alias resolves to a member of the+ ladder, and annotating it as `str` only moved the mismatch to the caller.+ """+ return cast("CaseStatus", _STATUS_ALIASES.get(status, status))def _status_transition_ok(current: str, target: str) -> bool:@@ -139,6 +156,7 @@ def _status_transition_ok(current: str, target: str) -> bool:rejected; reopening goes through the explicit reopen action."""return target in _TRANSITIONS.get(current, set())+#: The canonical ladder, in order, for the forward-walk used by the#: agent-launch advance._LADDER = list(case_status.ALL_STATUSES)@@ -923,6 +941,7 @@ def case_queue_for(case_row: Any, queues: list[Any]) -> Any:queues,)+@router.post("/{case_id}/alerts", response_model=CaseResponse, summary="Link alerts to a case")async def add_alerts(case_id: str, body: AddAlertsRequest, db: DBSession, user: Annotated[AuthUser, Depends(require_permission("cases:write"))]@@ -1562,15 +1581,19 @@ async def case_investigate(try:if case_alert_ids:rows = (- await db.execute(- text(- "SELECT id, title, description, severity, connector_type, "- "affected_ips, affected_hosts, affected_users, event_time, raw_event "- "FROM alerts WHERE id::text = ANY(:ids) AND tenant_id = :tenant_id "- "ORDER BY event_time DESC LIMIT 3"- ).bindparams(ids=case_alert_ids, tenant_id=user.tenant_id)+ (+ await db.execute(+ text(+ "SELECT id, title, description, severity, connector_type, "+ "affected_ips, affected_hosts, affected_users, event_time, raw_event "+ "FROM alerts WHERE id::text = ANY(:ids) AND tenant_id = :tenant_id "+ "ORDER BY event_time DESC LIMIT 3"+ ).bindparams(ids=case_alert_ids, tenant_id=user.tenant_id)+ ))- ).mappings().all()+ .mappings()+ .all()+ )alerts_out = []for r in rows:raw = r["raw_event"] if isinstance(r["raw_event"], dict) else {}@@ -1592,7 +1615,7 @@ async def case_investigate(if alerts_out:raw_alert_payload = {"alerts": alerts_out}except Exception: # noqa: BLE001 - never block launch on enrichment- logger.warning("investigate.launch.raw_alert_enrichment_failed", case_id=str(cid), exc_info=True)+ logger.warning("investigate.launch.raw_alert_enrichment_failed case_id=%s", cid, exc_info=True)# Launching an agent investigation IS an analyst action: advance# new/triaged cases to 'investigating' in the same request so the@@ -1602,22 +1625,21 @@ async def case_investigate(try:st_row = (await db.execute(- text(- "SELECT status FROM aisoc_cases WHERE id = :id AND tenant_id = :tenant_id"- ).bindparams(id=cid, tenant_id=user.tenant_id)+ text("SELECT status FROM aisoc_cases WHERE id = :id AND tenant_id = :tenant_id").bindparams(+ id=cid, tenant_id=user.tenant_id+ ))).fetchone()if st_row and _forward_to_investigating_ok(st_row.status):await db.execute(text(- "UPDATE aisoc_cases SET status = 'investigating', updated_at = :now "- "WHERE id = :id AND tenant_id = :tenant_id"+ "UPDATE aisoc_cases SET status = 'investigating', updated_at = :now WHERE id = :id AND tenant_id = :tenant_id").bindparams(id=cid, tenant_id=user.tenant_id, now=datetime.now(UTC)))await db.commit()except Exception: # noqa: BLE001 — never block the launch on the status bumpawait db.rollback()- logger.warning("investigate.launch.status_advance_failed", case_id=str(cid), exc_info=True)+ logger.warning("investigate.launch.status_advance_failed case_id=%s", cid, exc_info=True)resp = await _agents_proxy("POST",@@ -1651,19 +1673,28 @@ async def list_case_investigations(if resp.status_code < 400:return resp.json()except HTTPException:+ # Deliberate: the fallback below is the handling. The comment lives+ # inside the clause because an explanation sitting after the block+ # reads as commentary on the next statement, and `py/empty-except`+ # cannot see it there either.pass# Agents service has no list route (or is unreachable) — fall back to the# local ledger, which every run persists to anyway.from app.models.investigation import InvestigationRun+rows = (- await db.execute(- select(InvestigationRun)- .where(InvestigationRun.tenant_id == user.tenant_id)- .where(InvestigationRun.case_id.in_([str(cid), str(case_id)]))- .order_by(InvestigationRun.created_at.desc())- .limit(20)+ (+ await db.execute(+ select(InvestigationRun)+ .where(InvestigationRun.tenant_id == user.tenant_id)+ .where(InvestigationRun.case_id.in_([str(cid), str(case_id)]))+ .order_by(InvestigationRun.created_at.desc())+ .limit(20)+ ))- ).scalars().all()+ .scalars()+ .all()+ )return {"runs": [{diff --git a/services/api/app/api/v1/endpoints/insights.py b/services/api/app/api/v1/endpoints/insights.pyindex 573f28b4d..d33f9408f 100644--- a/services/api/app/api/v1/endpoints/insights.py+++ b/services/api/app/api/v1/endpoints/insights.py@@ -68,6 +68,7 @@ def _tid(t):"""Bind a native UUID for raw SQL on uuid-typed columns (asyncpg)."""return t if isinstance(t, uuid.UUID) else uuid.UUID(str(t))+router = APIRouter(prefix="/insights", tags=["insights"])@@ -310,11 +311,7 @@ async def _case_sparkline(# `cases` table, so the sparkline was flat zero for live tenants.rows = (await db.execute(- text(- "SELECT created_at FROM aisoc_cases "- "WHERE tenant_id = :tenant_id "- "AND created_at >= :start AND created_at < :end"- ),+ text("SELECT created_at FROM aisoc_cases WHERE tenant_id = :tenant_id AND created_at >= :start AND created_at < :end"),{"tenant_id": _tid(tenant_id), "start": start, "end": end},)).all()diff --git a/services/api/app/api/v1/endpoints/metrics.py b/services/api/app/api/v1/endpoints/metrics.pyindex 44e48de36..67dda690c 100644--- a/services/api/app/api/v1/endpoints/metrics.py+++ b/services/api/app/api/v1/endpoints/metrics.py@@ -110,8 +110,6 @@ class DashboardMetrics(BaseModel):topMitre: list[MitreTactic]alertsTrend: list[TrendPoint]threatsBySource: list[SourceThreat]- #: Echo of the selected window so the console can confirm what it fetched.- period: str = "24h"# ───────────────────────────── v1.5 Funnel models ─────────────────────────────@@ -453,9 +451,7 @@ async def get_dashboard_metrics(# jsonb_array_elements_text + GROUP BY in the same SELECT 500s on some# Postgres builds.tactic_rows = (- await db.execute(- select(Alert.mitre_tactics).where(and_(Alert.tenant_id == tenant_id, Alert.mitre_tactics.isnot(None), _in_window))- )+ await db.execute(select(Alert.mitre_tactics).where(and_(Alert.tenant_id == tenant_id, Alert.mitre_tactics.isnot(None), _in_window)))).all()tactic_counts: dict[str, int] = {}diff --git a/services/api/app/api/v1/endpoints/sso_connections.py b/services/api/app/api/v1/endpoints/sso_connections.pyindex 4c0a91a90..a58841987 100644--- a/services/api/app/api/v1/endpoints/sso_connections.py+++ b/services/api/app/api/v1/endpoints/sso_connections.py@@ -272,8 +272,13 @@ async def create_sso_connection(action="sso.connection_created",resource="sso_connection",resource_id=str(row["id"]),- changes={"provider": body.provider, "issuer": body.issuer, "enabled": body.enabled,- "default_role": body.default_role, "group_role_mode": body.group_role_mode},+ changes={+ "provider": body.provider,+ "issuer": body.issuer,+ "enabled": body.enabled,+ "default_role": body.default_role,+ "group_role_mode": body.group_role_mode,+ },)await db.commit()return _row_to_out(row)@@ -340,8 +345,12 @@ async def update_sso_connection(action="sso.connection_updated",resource="sso_connection",resource_id=str(connection_id),- changes={"enabled": body.enabled, "default_role": body.default_role,- "group_role_mapping": body.group_role_mapping, "group_role_mode": body.group_role_mode},+ changes={+ "enabled": body.enabled,+ "default_role": body.default_role,+ "group_role_mapping": body.group_role_mapping,+ "group_role_mode": body.group_role_mode,+ },)await db.commit()return _row_to_out(row)diff --git a/services/api/app/api/v1/endpoints/tenants.py b/services/api/app/api/v1/endpoints/tenants.pyindex 7a5a3798c..dcf22c451 100644--- a/services/api/app/api/v1/endpoints/tenants.py+++ b/services/api/app/api/v1/endpoints/tenants.py@@ -289,8 +289,7 @@ async def update_user(remaining = (await db.execute(text(- "SELECT count(*) FROM users "- "WHERE tenant_id = :t AND is_active = TRUE AND role = ANY(:wild) AND id <> :keep"+ "SELECT count(*) FROM users WHERE tenant_id = :t AND is_active = TRUE AND role = ANY(:wild) AND id <> :keep").bindparams(t=str(current_user.tenant_id),wild=sorted(role_grants.wildcard_roles()),@@ -330,7 +329,7 @@ async def update_user(tenant_id=current_user.tenant_id,actor_id=current_user.user_id,actor_email=current_user.email,- api_key_prefix=getattr(current_user, "api_key_prefix", None),+ api_key_prefix=getattr(current_user, "api_key_prefix", None),action="users:role_changed",resource="user",resource_id=str(user_id),diff --git a/services/api/app/auth/oidc.py b/services/api/app/auth/oidc.pyindex e1f564d82..6f0ae1e44 100644--- a/services/api/app/auth/oidc.py+++ b/services/api/app/auth/oidc.py@@ -454,10 +454,9 @@ async def oidc_callback(# and the console moves it into the storage its API client reads.separator = "&" if "#" in redirect_url else "#"response = RedirectResponse(- url=f"{redirect_url}{separator}access_token={session['access_token']}"- f"&refresh_token={session['refresh_token']}",- status_code=302,- )+ url=f"{redirect_url}{separator}access_token={session['access_token']}&refresh_token={session['refresh_token']}",+ status_code=302,+ )response.delete_cookie("oidc_state")return responsediff --git a/services/api/app/auth/sso_provisioning.py b/services/api/app/auth/sso_provisioning.pyindex 9a08ab7f5..4434f63d1 100644--- a/services/api/app/auth/sso_provisioning.py+++ b/services/api/app/auth/sso_provisioning.py@@ -368,8 +368,7 @@ async def provision_user(# silently viewed-later, so "your account does not exist yet" is# actionable rather than mysterious.raise SsoProvisioningError(- "just-in-time provisioning is disabled on this SSO connection; "- "an administrator must create the account first"+ "just-in-time provisioning is disabled on this SSO connection; an administrator must create the account first")user_id = uuid.uuid4()diff --git a/services/api/app/core/permission_cache.py b/services/api/app/core/permission_cache.pyindex a69d5fb85..dadc5a180 100644--- a/services/api/app/core/permission_cache.py+++ b/services/api/app/core/permission_cache.py@@ -214,14 +214,28 @@ async def bump_version(tenant_id: str) -> None:async def _tenant_has_rbac(db: AsyncSession, tenant_id: Any) -> bool:- """Whether this tenant has configured any roles at all.+ """Whether this tenant has actually granted anybody a role.The fact that separates bootstrap from deprovisioning, and the reason- the old fallback was unsafe.+ the old fallback was unsafe: once a tenant is administering access, a+ user with no grant has been *left* without one, and quietly handing them+ their static role back would undo the deprovisioning.++ Measured on grants rather than on the existence of role rows. A role row+ is not evidence that anyone configured anything -- migration 091 bulk+ seeds `infosec` into `roles` for every tenant with one+ ``INSERT ... SELECT FROM tenants``, so counting roles answered True for+ every tenant in the world the moment it ran, including tenants that had+ never provisioned RBAC at all, and the bootstrap fallback was then+ unreachable for every one of those tenants: an `admin` resolved to zero+ permissions and every authorized route answered 403. Grants cannot be seeded that way, because a grant+ names a user."""- from app.models.rbac import Role # noqa: PLC0415+ from app.models.rbac import Role, UserRole # noqa: PLC0415- total = await db.scalar(select(func.count()).select_from(Role).where(Role.tenant_id == tenant_id))+ total = await db.scalar(+ select(func.count()).select_from(UserRole).join(Role, Role.id == UserRole.role_id).where(Role.tenant_id == tenant_id)+ )return bool(total)diff --git a/services/api/app/services/resolution_time.py b/services/api/app/services/resolution_time.pyindex 002aa375b..038880a44 100644--- a/services/api/app/services/resolution_time.py+++ b/services/api/app/services/resolution_time.py@@ -45,6 +45,7 @@ from sqlalchemy.ext.asyncio import AsyncSession# the account is being run now rather than a year ago.MTTR_WINDOW = timedelta(days=30)+def _tid(t):"""Bind a native UUID for raw SQL on uuid-typed columns (asyncpg)."""return t if isinstance(t, uuid.UUID) else uuid.UUID(str(t))diff --git a/services/api/tests/test_bootstrap_fallback_survives_a_seeded_role.py b/services/api/tests/test_bootstrap_fallback_survives_a_seeded_role.pynew file mode 100644index 000000000..611cfc366--- /dev/null+++ b/services/api/tests/test_bootstrap_fallback_survives_a_seeded_role.py@@ -0,0 +1,167 @@+"""A seeded role is not a configured tenant, and an admin must keep its grant.++What broke+----------+`resolve_permissions` falls back to the static `ROLE_PERMISSIONS` map only on+the bootstrap path -- a tenant that has nothing for the database to answer+with. Everywhere else the database is authoritative, so that removing a+user's roles actually removes their access instead of quietly restoring their+static ones.++`_tenant_has_rbac` decided which path a request was on by counting rows in+`roles`. Migration `091_sso_policy.sql` then seeded the new `infosec` role+into *every* tenant with a single statement::++ INSERT INTO roles (tenant_id, name, description, is_system)+ SELECT t.id, 'infosec', ... FROM tenants t++From the moment it ran, every tenant in the world had a role row, so the+predicate answered True everywhere and the bootstrap path stopped existing.+An `admin` whose permissions came from the static map resolved to the empty+set and **every authorized route answered 403** -- on a fresh install, before+anyone had configured anything. The golden-pipeline job caught it as+``the API refused the read (HTTP 403)`` after a successful mint.++Why this test runs against live Postgres+----------------------------------------+The unit tests for this resolver drive a `_FakeSession` whose `scalar()`+returns a `tenant_role_count` the test supplies. That double answers the+count query *as the test imagines it* -- so it cannot distinguish "counts+role rows" from "counts grants", and it has no migrations, which is where the+seed lives. Both of those are exactly the boundary the defect sat on, and+both unit tests passed throughout. Only a database with the real migration+chain applied can tell the difference.++Skips when no database answers so a local run stays green, but cannot skip+where it is meant to run: `integration.yml` sets `MSSP_ISOLATION_REQUIRED=1`+and an unreachable database is then a failure.+"""++from __future__ import annotations++import os+import uuid++import pytest+import pytest_asyncio+from app.core.permission_cache import _tenant_has_rbac, grants, reset_for_tests, resolve_permissions+from sqlalchemy import text+from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine++DSN = os.environ.get("DATABASE_URL", "")+REQUIRED = os.environ.get("MSSP_ISOLATION_REQUIRED", "").strip() not in ("", "0", "false")++TENANT = uuid.UUID("0e110000-0000-0000-0000-00000000000b")+ADMIN = uuid.UUID("0e110000-0000-0000-0000-0000000000a1")+COLLEAGUE = uuid.UUID("0e110000-0000-0000-0000-0000000000a2")+++@pytest_asyncio.fixture+async def db():+ if "postgres" not in DSN and not REQUIRED:+ pytest.skip("needs a live Postgres with the migration chain applied (integration.yml)")+ engine = create_async_engine(DSN)+ try:+ async with engine.connect() as probe:+ await probe.execute(text("SELECT 1"))+ except Exception as exc:+ await engine.dispose()+ if REQUIRED:+ pytest.fail(+ "MSSP_ISOLATION_REQUIRED is set but no database answered at DATABASE_URL — "+ f"the bootstrap-fallback proof did not run: {type(exc).__name__}: {exc}"+ )+ pytest.skip(f"no database at DATABASE_URL ({type(exc).__name__}) — runs in integration.yml")++ factory = async_sessionmaker(engine, expire_on_commit=False)+ async with factory() as session:+ await _seed(session)+ try:+ yield session+ finally:+ await _clear_grants(session)+ await engine.dispose()+++async def _seed(session) -> None:+ """A tenant carrying the seeded role and two users, neither granted anything.++ This is the shape migration 091 leaves behind on a tenant nobody has+ administered: one `roles` row that no operator asked for, and no grants.+ """+ await session.rollback()+ await session.execute(+ text("INSERT INTO tenants (id, name, slug) VALUES (CAST(:t AS uuid), 'bootstrap-proof', :s) ON CONFLICT DO NOTHING"),+ {"t": str(TENANT), "s": f"bootstrap-proof-{TENANT.hex[:8]}"},+ )+ for uid, email in ((ADMIN, "[email protected]"), (COLLEAGUE, "[email protected]")):+ await session.execute(+ text(+ "INSERT INTO users (id, tenant_id, email, username, hashed_password, role, is_active) "+ "VALUES (CAST(:i AS uuid), CAST(:t AS uuid), :e, :e, 'x', 'admin', true) ON CONFLICT DO NOTHING"+ ),+ {"i": str(uid), "t": str(TENANT), "e": email},+ )+ # The seed migration 091 performs, reproduced for this tenant so the test+ # does not depend on having been created before that migration ran.+ await session.execute(+ text(+ "INSERT INTO roles (tenant_id, name, description, is_system) "+ "VALUES (CAST(:t AS uuid), 'infosec', 'seeded by migration 091', TRUE) ON CONFLICT DO NOTHING"+ ),+ {"t": str(TENANT)},+ )+ await session.commit()+ reset_for_tests()+++async def _clear_grants(session) -> None:+ await session.rollback()+ await session.execute(+ text("DELETE FROM user_roles WHERE role_id IN (SELECT id FROM roles WHERE tenant_id = CAST(:t AS uuid))"),+ {"t": str(TENANT)},+ )+ await session.commit()+ reset_for_tests()+++class TestASeededRoleIsNotAConfiguredTenant:+ async def test_the_seeded_role_alone_does_not_make_the_tenant_configured(self, db) -> None:+ """The predicate itself, stated directly.++ A role row exists. Nobody has been granted anything. That is a+ bootstrap tenant, and reading it as a configured one is what took+ the fallback away.+ """+ assert await _tenant_has_rbac(db, TENANT) is False++ async def test_an_admin_keeps_its_permissions_on_a_freshly_migrated_tenant(self, db) -> None:+ """The 403 a user would have hit, as an assertion.++ Before the fix this resolved to `frozenset()` and the console was+ locked out of a deployment nobody had misconfigured.+ """+ resolved = await resolve_permissions(db, tenant_id=TENANT, user_id=ADMIN, static_role="admin")+ assert resolved, "an admin on a freshly migrated tenant resolved to no permissions at all"+ assert grants(resolved, "alerts:read")++ async def test_a_real_grant_still_ends_the_bootstrap_path(self, db) -> None:+ """The hole the predicate exists to close, proven still closed.++ Once anyone in the tenant holds a grant, access is being+ administered, and a user without one has been *left* without one.+ Handing them their static role back would undo the deprovisioning,+ which is the defect the original predicate was written for.+ """+ role_id = await db.scalar(text("SELECT id FROM roles WHERE tenant_id = CAST(:t AS uuid) LIMIT 1"), {"t": str(TENANT)})+ await db.execute(+ text("INSERT INTO user_roles (user_id, role_id) VALUES (CAST(:u AS uuid), :r) ON CONFLICT DO NOTHING"),+ {"u": str(COLLEAGUE), "r": role_id},+ )+ await db.commit()+ reset_for_tests()++ assert await _tenant_has_rbac(db, TENANT) is True+ resolved = await resolve_permissions(db, tenant_id=TENANT, user_id=ADMIN, static_role="admin")+ assert resolved == frozenset(), "a user with no grant was handed their static role back"diff --git a/services/api/tests/test_database_backed_permissions.py b/services/api/tests/test_database_backed_permissions.pyindex 6d4cb981d..36a1149d5 100644--- a/services/api/tests/test_database_backed_permissions.py+++ b/services/api/tests/test_database_backed_permissions.py@@ -59,14 +59,22 @@ class _FakeResult:class _FakeSession:"""A session that answers the two queries the resolver makes.- `granted` is what `user_roles` joins to; `tenant_role_count` is how many- roles the tenant has configured, which is the fact that separates a+ `granted` is what `user_roles` joins to; `tenant_grant_count` is how many+ grants exist anywhere in the tenant, which is the fact that separates afresh tenant from a deprovisioned user.++ Note what this double cannot see. `scalar()` answers the same number for+ any query, so these tests read identically whether the resolver counts+ role rows or grants -- and the distinction turned out to matter: a+ migration that seeds a role into every tenant took the bootstrap path+ away from all of them while every test here still passed.+ `test_bootstrap_fallback_survives_a_seeded_role.py` covers that against a+ real database, because this fake structurally cannot."""- def __init__(self, *, granted: list[str], tenant_role_count: int) -> None:+ def __init__(self, *, granted: list[str], tenant_grant_count: int) -> None:self._granted = granted- self._tenant_role_count = tenant_role_count+ self._tenant_grant_count = tenant_grant_countself.executed = 0async def execute(self, *_args, **_kwargs):@@ -74,7 +82,7 @@ class _FakeSession:return _FakeResult([(name,) for name in self._granted])async def scalar(self, *_args, **_kwargs) -> int:- return self._tenant_role_count+ return self._tenant_grant_countTENANT = uuid.uuid4()@@ -86,7 +94,7 @@ class TestTheDatabaseIsAuthoritative:"""Otherwise the console's RBAC screen is decorative."""assert "lake:admin" not in ROLE_PERMISSIONS["viewer"]resolved = await resolve_permissions(- _FakeSession(granted=["lake:admin"], tenant_role_count=3),+ _FakeSession(granted=["lake:admin"], tenant_grant_count=3),tenant_id=TENANT,user_id=USER,static_role="viewer",@@ -102,7 +110,7 @@ class TestTheDatabaseIsAuthoritative:"""assert "alerts:write" in ROLE_PERMISSIONS["tenant_admin"]resolved = await resolve_permissions(- _FakeSession(granted=[], tenant_role_count=4),+ _FakeSession(granted=[], tenant_grant_count=4),tenant_id=TENANT,user_id=USER,static_role="tenant_admin",@@ -117,7 +125,7 @@ class TestTheDatabaseIsAuthoritative:the defect was that it also covered the case above."""resolved = await resolve_permissions(- _FakeSession(granted=[], tenant_role_count=0),+ _FakeSession(granted=[], tenant_grant_count=0),tenant_id=TENANT,user_id=USER,static_role="tenant_admin",@@ -131,13 +139,13 @@ class TestTheDatabaseIsAuthoritative:tenant and must work; the other was deprovisioned and must not."""fresh = await resolve_permissions(- _FakeSession(granted=[], tenant_role_count=0),+ _FakeSession(granted=[], tenant_grant_count=0),tenant_id=TENANT,user_id=USER,static_role="soc_analyst",)deprovisioned = await resolve_permissions(- _FakeSession(granted=[], tenant_role_count=7),+ _FakeSession(granted=[], tenant_grant_count=7),tenant_id=uuid.uuid4(),user_id=uuid.uuid4(),static_role="soc_analyst",@@ -226,7 +234,7 @@ class TestTheCacheInvalidates:async def test_a_second_resolution_does_not_requery(self) -> None:"""A four-table join per request is what the cache exists to avoid."""- session = _FakeSession(granted=["alerts:read"], tenant_role_count=2)+ session = _FakeSession(granted=["alerts:read"], tenant_grant_count=2)for _ in range(3):await resolve_permissions(session, tenant_id=TENANT, user_id=USER, static_role="viewer")assert session.executed == 1, f"queried {session.executed} times; the cache is not holding"diff --git a/services/api/tests/test_sso_enterprise_policy.py b/services/api/tests/test_sso_enterprise_policy.pyindex ae810973e..1ced4aa56 100644--- a/services/api/tests/test_sso_enterprise_policy.py+++ b/services/api/tests/test_sso_enterprise_policy.py@@ -4,6 +4,7 @@ These tests lock the security properties of the SSO wave, not its shape:least-privilege defaults, fail-closed gates, auditable role changes, and thefeature flag that keeps the whole surface shut until verified."""+from __future__ import annotationsimport pytest@@ -16,13 +17,31 @@ def test_infosec_role_exists_and_is_least_privilege_analyst() -> None:infosec = set(ROLE_PERMISSIONS["infosec"])# The spec floor: analyst work is fully available.- assert {"alerts:read", "alerts:write", "cases:read", "cases:write",- "threat_intel:read", "threat_intel:write", "rules:write",- "playbooks:execute", "lake:query", "reports:read"} <= infosec+ assert {+ "alerts:read",+ "alerts:write",+ "cases:read",+ "cases:write",+ "threat_intel:read",+ "threat_intel:write",+ "rules:write",+ "playbooks:execute",+ "lake:query",+ "reports:read",+ } <= infosec# The ceiling: no user, role, settings, credential, audit or admin door.- forbidden = {"users:write", "users:delete", "roles:write", "roles:delete",- "settings:write", "api_keys:manage", "platform_admin",- "alert_source_raw:read", "audit:delete", "connectors:write"}+ forbidden = {+ "users:write",+ "users:delete",+ "roles:write",+ "roles:delete",+ "settings:write",+ "api_keys:manage",+ "platform_admin",+ "alert_source_raw:read",+ "audit:delete",+ "connectors:write",+ }assert not (infosec & forbidden)@@ -107,15 +126,15 @@ def test_status_endpoint_contract(monkeypatch: pytest.MonkeyPatch) -> None:monkeypatch.delenv("SSO_LOCAL_ADMIN_ONLY", raising=False)monkeypatch.delenv("SSO_LOGIN_LABEL", raising=False)out = asyncio.run(auth_ep.sso_status())- assert out["sso_enabled"] is False # hidden by default- assert out["local_login_enabled"] is True # break-glass stays open+ assert out["sso_enabled"] is False # hidden by default+ assert out["local_login_enabled"] is True # break-glass stays openmonkeypatch.setenv("SSO_ENABLED", "true")monkeypatch.setenv("SSO_LOGIN_LABEL", "Sign in with Acme")out = asyncio.run(auth_ep.sso_status())assert out["sso_enabled"] is Trueassert out["login_label"] == "Sign in with Acme"- assert out["local_login_enabled"] is True # until the operator closes it+ assert out["local_login_enabled"] is True # until the operator closes itmonkeypatch.setenv("SSO_LOCAL_ADMIN_ONLY", "true")out = asyncio.run(auth_ep.sso_status())diff --git a/tests/test_env_placeholder_gate.py b/tests/test_env_placeholder_gate.pyindex bbde0c821..99be4246e 100644--- a/tests/test_env_placeholder_gate.py+++ b/tests/test_env_placeholder_gate.py@@ -68,6 +68,30 @@ DELIBERATE_DEFAULTS: dict[str, str] = {"AISOC_AGENTS_URL": "the compose hostname the console proxies agent calls to","AISOC_REALTIME_URL": "the compose hostname the console proxies WS/SSE to","AISOC_DEMO_MODE": "a boolean; false is the correct value for every deployment that is not the public demo",+ "SSO_ENABLED": (+ "off is the correct default for a feature that cannot work until an "+ "operator has configured a provider and verified the connection. "+ "Shipping it on would render an SSO button that 503s"+ ),+ "SSO_PROVIDER": (+ "which flow the login button points at, not a secret. `oidc` is the "+ "default because it needs no certificate exchange; it is inert while "+ "SSO_ENABLED is false"+ ),+ "SSO_LOGIN_LABEL": (+ "the words on the login button. A default is required because an empty "+ "label renders a button with no text; it is inert while SSO is off"+ ),+ "SSO_LOCAL_ADMIN_ONLY": (+ "off is the safe default: on, it restricts password sign-in to wildcard "+ "roles, which locks every other local account out of a deployment that "+ "has not finished configuring SSO"+ ),+ "OIDC_SCOPES": (+ "the OIDC scopes requested at authorisation. These four are what the "+ "provisioning path reads — `groups` drives role mapping — so an empty "+ "default would ask for nothing and bind no role"+ ),"AISOC_GPU_COUNT": ("a device count, not a secret. 1 is the right value for every host with one ""GPU, and it is only read by `make up-gpu` — the default `make up` ignores it"