Skip to content

armgit / AiSOC

Public Imported from GitHub
0

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.

fix: the mypy findings that were bugs, and pin the one unpinned type target

Beenu Arorab84beb47883f
diff --git a/packages/sdk-py/pyproject.toml b/packages/sdk-py/pyproject.toml
index 42a3e47ba..d1fa2a011 100644
--- a/packages/sdk-py/pyproject.toml
+++ b/packages/sdk-py/pyproject.toml
@@ -43,3 +43,10 @@ line-length = 100
[tool.mypy]
strict = true
+# Pinned, like the other five trees that declare [tool.mypy]. Without it
+# mypy resolves the standard library for whatever interpreter it happens to
+# run on, so this tree's share of the recorded baseline moved whenever CI's
+# Python did — and the ratchet would red on a workflow change that touched
+# none of this code. 3.11 is the interpreter every service image ships and
+# the one CI now runs.
+python_version = "3.11"
diff --git a/scripts/check_toolchain_pins.py b/scripts/check_toolchain_pins.py
index c43e3ee87..c021fd75d 100644
--- a/scripts/check_toolchain_pins.py
+++ b/scripts/check_toolchain_pins.py
@@ -331,6 +331,8 @@ class Scan:
ci_dirs: dict[str, tuple[str, str]] = field(default_factory=dict)
# service name -> (context, dockerfile), from the publish matrix.
published: dict[str, tuple[str, str]] = field(default_factory=dict)
+ # Manifests declaring [tool.mypy] without a `python_version`.
+ untargeted_mypy: list[str] = field(default_factory=list)
def by_runtime(self, runtime: str) -> list[Pin]:
return [p for p in self.pins if p.runtime == runtime]
@@ -966,6 +968,11 @@ def scan(root: Path) -> Scan:
if isinstance(declared, str):
result.pins.append(Pin("python", re.sub(r"^[^\d]*", "", declared), rel, "floor", f"python {declared}"))
_parse_python_targets(raw, rel, result)
+ # A tree that asks to be type-checked and does not say against which
+ # Python gets the interpreter mypy happens to run on.
+ mypy_config = data.get("tool", {}).get("mypy")
+ if isinstance(mypy_config, dict) and "python_version" not in mypy_config:
+ result.untargeted_mypy.append(rel)
# `ruff.toml` at the root configures every tree that has no local table.
ruff_config = root / "ruff.toml"
@@ -1123,6 +1130,17 @@ def check_python_tooling_target(data: Scan) -> list[str]:
f"`python_version` names ({', '.join(sorted(declared))}) — the static tools were "
f"left behind by a Dockerfile bump (ship -> target)"
)
+ # A declared-but-untargeted tree is the same defect with nothing to
+ # compare: five of the six trees declaring [tool.mypy] pin 3.11 and one
+ # did not, so its share of the recorded baseline followed whatever
+ # interpreter CI ran and a workflow change could red the ratchet without
+ # touching the code.
+ for path in sorted(data.untargeted_mypy):
+ problems.append(
+ f"{path} declares [tool.mypy] with no `python_version`, so it is checked against "
+ f"whichever interpreter the job happens to run — pin it to the shipped "
+ f"{', '.join(sorted(shipped))} (target -> ship)"
+ )
return problems
@@ -1799,6 +1817,11 @@ def self_test() -> int:
path = root / "services" / "py-svc" / "pyproject.toml"
path.write_text(path.read_text().replace('python_version = "3.11"', 'python_version = "3.10"'), encoding="utf-8")
+ def drift_mypy_without_a_target(root: Path) -> None:
+ """A tree asking to be type-checked without saying against which Python."""
+ path = root / "services" / "py-svc" / "pyproject.toml"
+ path.write_text(path.read_text().replace('python_version = "3.11"', "strict = true"), encoding="utf-8")
+
def drift_no_gofmt_at_all(root: Path) -> None:
"""The measured bug: `go vet` and `go build`, and no formatting check."""
path = root / ".github" / "workflows" / "ci.yml"
@@ -1923,6 +1946,7 @@ def self_test() -> int:
("python: image ahead of CI", drift_python_image_ahead_of_ci, "`python` is pinned 2 different ways"),
("ruff targets another interpreter", drift_ruff_target, "target -> ship"),
("mypy targets another interpreter", drift_mypy_target, "target -> ship"),
+ ("mypy declared with no target at all", drift_mypy_without_a_target, "no `python_version`"),
("no gofmt anywhere", drift_no_gofmt_at_all, "module -> format"),
("gofmt scoped off the modules", drift_gofmt_scoped_off_the_modules, "format -> module"),
("gofmt -l that cannot fail", drift_gofmt_that_cannot_fail, "can only ever pass"),
diff --git a/scripts/mypy_baseline.json b/scripts/mypy_baseline.json
index 171dd289f..72ea9f351 100644
--- a/scripts/mypy_baseline.json
+++ b/scripts/mypy_baseline.json
@@ -108,30 +108,15 @@
"app/api/v1/endpoints/community.py": {
"import-untyped": 1
},
- "app/api/v1/endpoints/detection_loop.py": {
- "call-arg": 1
- },
- "app/api/v1/endpoints/hunts.py": {
- "call-arg": 1
- },
"app/api/v1/endpoints/identity_timeline.py": {
"assignment": 1
},
- "app/api/v1/endpoints/knowledge_base.py": {
- "call-arg": 1
- },
"app/api/v1/endpoints/metrics.py": {
"return-value": 2
},
- "app/api/v1/endpoints/phishing.py": {
- "call-arg": 1
- },
"app/api/v1/endpoints/plugins.py": {
"assignment": 8
},
- "app/api/v1/endpoints/translation.py": {
- "call-arg": 1
- },
"app/auth/saml.py": {
"var-annotated": 1
},
@@ -153,9 +138,6 @@
"app/services/detections/sigma_import.py": {
"import-untyped": 1
},
- "app/services/effective_permissions/posture_loader.py": {
- "return-value": 1
- },
"app/services/event_warehouse/registry.py": {
"list-item": 2
},
@@ -169,8 +151,7 @@
"assignment": 1
},
"app/services/playbook_step_dispatch.py": {
- "arg-type": 8,
- "union-attr": 14
+ "arg-type": 8
},
"app/services/plugin_manager.py": {
"import-untyped": 1
@@ -179,16 +160,7 @@
"import-untyped": 1
},
"app/services/siem_writeback.py": {
- "arg-type": 1,
- "union-attr": 6
- },
- "app/services/sla.py": {
- "arg-type": 1,
- "assignment": 1,
- "call-overload": 3,
- "dict-item": 1,
- "index": 1,
- "no-redef": 1
+ "arg-type": 1
},
"tests/api/v1/endpoints/test_alerts_detail_envelope.py": {
"func-returns-value": 1
diff --git a/services/api/app/api/v1/endpoints/detection_loop.py b/services/api/app/api/v1/endpoints/detection_loop.py
index f42c3fe0d..810e856f9 100644
--- a/services/api/app/api/v1/endpoints/detection_loop.py
+++ b/services/api/app/api/v1/endpoints/detection_loop.py
@@ -127,7 +127,8 @@ async def _llm_draft_sigma(
)
return json.loads(body["choices"][0]["message"]["content"])
except LLMContractViolation as exc:
- logger.warning("detection_loop.llm_contract_violation", reason=exc.reason)
+ # %-style: stdlib Logger, not structlog. See translation.py.
+ logger.warning("detection_loop.llm_contract_violation reason=%s", exc.reason)
return _template_fallback(current_sigma, alert_fields, analyst_note)
except Exception:
return _template_fallback(current_sigma, alert_fields, analyst_note)
diff --git a/services/api/app/api/v1/endpoints/hunts.py b/services/api/app/api/v1/endpoints/hunts.py
index 69d561fcb..61cb6e24d 100644
--- a/services/api/app/api/v1/endpoints/hunts.py
+++ b/services/api/app/api/v1/endpoints/hunts.py
@@ -163,7 +163,8 @@ async def _generate_queries(hypothesis: str, mitre: str | None) -> dict[str, str
)
return json.loads(body["choices"][0]["message"]["content"])
except LLMContractViolation as exc:
- logger.warning("hunts.llm_contract_violation", reason=exc.reason)
+ # %-style: stdlib Logger, not structlog. See translation.py.
+ logger.warning("hunts.llm_contract_violation reason=%s", exc.reason)
return None
except Exception:
return None
diff --git a/services/api/app/api/v1/endpoints/knowledge_base.py b/services/api/app/api/v1/endpoints/knowledge_base.py
index 8c6b8d319..1f1a6e133 100644
--- a/services/api/app/api/v1/endpoints/knowledge_base.py
+++ b/services/api/app/api/v1/endpoints/knowledge_base.py
@@ -136,7 +136,8 @@ async def _synthesise(question: str, chunks: list[KBChunk]) -> str | None:
)
return str(body["choices"][0]["message"]["content"]).strip()
except LLMContractViolation as exc:
- logger.warning("knowledge_base.llm_contract_violation", reason=exc.reason)
+ # %-style: stdlib Logger, not structlog. See translation.py.
+ logger.warning("knowledge_base.llm_contract_violation reason=%s", exc.reason)
return None
except Exception:
return None
diff --git a/services/api/app/api/v1/endpoints/phishing.py b/services/api/app/api/v1/endpoints/phishing.py
index 994c045c1..a336f34de 100644
--- a/services/api/app/api/v1/endpoints/phishing.py
+++ b/services/api/app/api/v1/endpoints/phishing.py
@@ -132,7 +132,11 @@ async def _triage(artifact_kind: str, content: str, urls: list[str]) -> TriageRe
# Degrading to heuristic triage is the right outcome, but it must be
# visible: this `except` used to swallow everything, so a refused
# prompt was indistinguishable from a missing API key.
- logger.warning("phishing.llm_contract_violation", reason=exc.reason)
+ # %-style: `logger` is the stdlib one, which raises TypeError on an
+ # unknown keyword — and a raise inside an `except` is not caught by
+ # the sibling handler below. The line added to stop this path
+ # degrading silently was itself throwing.
+ logger.warning("phishing.llm_contract_violation reason=%s", exc.reason)
return None
except Exception:
return None
diff --git a/services/api/app/api/v1/endpoints/translation.py b/services/api/app/api/v1/endpoints/translation.py
index 8fb08651a..15a42b1af 100644
--- a/services/api/app/api/v1/endpoints/translation.py
+++ b/services/api/app/api/v1/endpoints/translation.py
@@ -142,7 +142,14 @@ async def _llm_translate(req: TranslateRequest) -> dict[str, Any] | None:
)
return json.loads(body["choices"][0]["message"]["content"])
except LLMContractViolation as exc:
- logger.warning("translation.llm_contract_violation", reason=exc.reason)
+ # %-style, not a `reason=` keyword: `logger` here is
+ # `logging.getLogger`, not structlog, and the stdlib Logger rejects
+ # unknown keywords with a TypeError. An exception raised inside an
+ # `except` block is not caught by a sibling handler, so the
+ # `except Exception` below never saw it and the TypeError escaped —
+ # the one path whose whole job is to degrade gracefully was the one
+ # that raised.
+ logger.warning("translation.llm_contract_violation reason=%s", exc.reason)
return None
except Exception:
return None
diff --git a/services/api/app/services/effective_permissions/posture_loader.py b/services/api/app/services/effective_permissions/posture_loader.py
index dd6d13e39..74ebfa7f4 100644
--- a/services/api/app/services/effective_permissions/posture_loader.py
+++ b/services/api/app/services/effective_permissions/posture_loader.py
@@ -164,4 +164,11 @@ class HttpResourceConfigFetcher:
logger.info("posture_loader.fetch_non_200", connector_id=connector_id, status=resp.status_code)
return {}
body = resp.json()
- return body.get("config") if isinstance(body, dict) else {}
+ # `body.get("config")` is None when a 200 response carries no
+ # `config` key, and this is declared to return a dict — so the
+ # caller got None where it expected something to iterate. The
+ # non-200 branch above already returns `{}` for "nothing to load";
+ # a 200 with nothing in it means the same thing and must not be
+ # spelled differently.
+ config = body.get("config") if isinstance(body, dict) else None
+ return config if isinstance(config, dict) else {}
diff --git a/services/api/app/services/playbook_step_dispatch.py b/services/api/app/services/playbook_step_dispatch.py
index 0b17dfd43..1b02a2a71 100644
--- a/services/api/app/services/playbook_step_dispatch.py
+++ b/services/api/app/services/playbook_step_dispatch.py
@@ -295,8 +295,16 @@ def _interpret(capability: str, vendor_id: str, body: dict[str, Any], *, dry_run
lose an action that is genuinely in flight; reporting the second as
executed would be the fake success this replaces.
"""
- result = body.get("result") if isinstance(body.get("result"), dict) else {}
- details = result.get("details") if isinstance(result.get("details"), dict) else {}
+ # Fetched once and then tested, rather than `x.get(k) if
+ # isinstance(x.get(k), dict) else {}`: that form calls `get` twice, so
+ # the value the guard inspects is not the value that gets used. It
+ # happens to be safe for a plain dict and it is not a property anything
+ # enforces, which is why the fourteen `union-attr` findings under it
+ # were real reports about a guard that does not guard.
+ raw_result = body.get("result")
+ result: dict[str, Any] = raw_result if isinstance(raw_result, dict) else {}
+ raw_details = result.get("details")
+ details: dict[str, Any] = raw_details if isinstance(raw_details, dict) else {}
status = str(result.get("status") or "")
summary = str(result.get("summary") or "")
diff --git a/services/api/app/services/siem_writeback.py b/services/api/app/services/siem_writeback.py
index 43c64cf58..1f83ae09d 100644
--- a/services/api/app/services/siem_writeback.py
+++ b/services/api/app/services/siem_writeback.py
@@ -497,8 +497,13 @@ def _interpret(link: AlertSourceLink, body: dict[str, Any], *, dry_run: bool) ->
a SIMULATED status, a refusal and a dry run all produce ``written=False``,
and none of them may be reported as a write that happened.
"""
- result = body.get("result") if isinstance(body.get("result"), dict) else {}
- details = result.get("details") if isinstance(result.get("details"), dict) else {}
+ # Fetched once and then tested; the `x.get(k) if isinstance(x.get(k),
+ # dict)` form calls `get` twice, so the guard inspects a different call's
+ # result from the one that is used.
+ raw_result = body.get("result")
+ result: dict[str, Any] = raw_result if isinstance(raw_result, dict) else {}
+ raw_details = result.get("details")
+ details: dict[str, Any] = raw_details if isinstance(raw_details, dict) else {}
status = str(result.get("status") or "")
written = bool(details.get("written"))
action = str(details.get("writeback_action") or "refuse")
diff --git a/services/api/app/services/sla.py b/services/api/app/services/sla.py
index 337301272..9bf367577 100644
--- a/services/api/app/services/sla.py
+++ b/services/api/app/services/sla.py
@@ -28,34 +28,32 @@ DEFAULT_KPI_BAR_TARGETS: dict[str, float] = {
"mitre_subtechnique_tagging_min_pct": 60.0,
}
-# Default per-severity SLA targets (minutes) used as the fallback when a tenant
-# has not provided overrides via ``tenant_sla_config``. The queue's `sla_due_at`
-# expression also reads ``DEFAULT_SLA_TARGETS["info"]["mttd_target"]`` as a
-# catch-all for severities that fall outside the standard four-tier ladder.
-DEFAULT_SLA_TARGETS: dict[str, dict[str, int]] = {
- "critical": {"mttd_target": 15, "mttr_target": 60, "mttc_target": 120},
- "high": {"mttd_target": 30, "mttr_target": 120, "mttc_target": 240},
- "medium": {"mttd_target": 60, "mttr_target": 240, "mttc_target": 480},
- "low": {"mttd_target": 120, "mttr_target": 480, "mttc_target": 1440},
- "info": {"mttd_target": 240, "mttr_target": 960, "mttc_target": 2880},
-}
-
-
-# Default per-severity SLA targets (minutes). These mirror the seed values
-# in migrations 007 (critical/high/medium/low) and 040 (info) — `critical`
+# Default per-severity SLA targets (minutes), used when a tenant has no
+# override row in ``tenant_sla_config``. They mirror the seed values in
+# migrations 007 (critical/high/medium/low) and 040 (info) — `critical`
# carries the v1.5 P1 ≤15 min MTTD promise (W2 in the v1.5 SOC Console
-# Parity plan), and `info` is the widest band because info-tier alerts
-# are background context, not work items the analyst is on the clock for.
+# Parity plan), and `info` is the widest band because info-tier alerts are
+# background context, not work items the analyst is on the clock for.
+#
+# The queue's `sla_due_at` expression also reads
+# ``DEFAULT_SLA_TARGETS["info"]["mttd_target"]`` as the catch-all for
+# severities outside the standard four-tier ladder, so this row decides a
+# deadline even for alerts that never match a tier.
#
-# Lifted to a module-level constant so it can be asserted against in
-# tests; the row is also the source of truth used by ``compute_sla_metrics``
-# when a tenant hasn't overridden it via ``tenant_sla_config``.
+# This was declared twice, and the two bodies disagreed: the second — the
+# one that actually won, being later in the file — put `info` at
+# (480, 1440, 2880) against the first's (240, 960, 2880), and migration 040
+# seeds (240, 1440, 4320). So all three disagreed, and a tenant's info-tier
+# deadline depended on whether it had a seeded row (240 min) or fell back to
+# Python (480 min). The migration is the source of truth here because it is
+# what is in the database; the duplicate is gone and the values now match
+# it. Found by mypy's `no-redef`, which had never been run.
DEFAULT_SLA_TARGETS: dict[str, dict[str, int]] = {
"critical": {"mttd_target": 15, "mttr_target": 60, "mttc_target": 120},
"high": {"mttd_target": 30, "mttr_target": 120, "mttc_target": 240},
"medium": {"mttd_target": 60, "mttr_target": 240, "mttc_target": 480},
"low": {"mttd_target": 120, "mttr_target": 480, "mttc_target": 1440},
- "info": {"mttd_target": 480, "mttr_target": 1440, "mttc_target": 2880},
+ "info": {"mttd_target": 240, "mttr_target": 1440, "mttc_target": 4320},
}
@@ -88,8 +86,10 @@ def merge_tenant_settings_patch(existing: dict | None, patch: dict) -> dict:
async def load_kpi_bar_targets(db: AsyncSession, tenant_id: uuid.UUID) -> dict[str, float]:
row = await db.execute(select(Tenant.settings).where(Tenant.id == tenant_id))
raw = row.scalar_one_or_none() or {}
- kb = (raw or {}).get("kpi_bar") if isinstance((raw or {}).get("kpi_bar"), dict) else {}
- return merge_kpi_bar_dict(kb, {})
+ # Fetched once and then tested; the two-`get` form guards one call's
+ # result and uses another's.
+ kb = raw.get("kpi_bar")
+ return merge_kpi_bar_dict(kb if isinstance(kb, dict) else {}, {})
async def patch_tenant_kpi_bar_targets(
@@ -103,8 +103,8 @@ async def patch_tenant_kpi_bar_targets(
if tenant is None:
raise ValueError("tenant not found")
settings = dict(tenant.settings or {})
- prev_bar = settings.get("kpi_bar") if isinstance(settings.get("kpi_bar"), dict) else {}
- merged_bar = merge_kpi_bar_dict(prev_bar, patch)
+ prev_bar = settings.get("kpi_bar")
+ merged_bar = merge_kpi_bar_dict(prev_bar if isinstance(prev_bar, dict) else {}, patch)
settings["kpi_bar"] = merged_bar
tenant.settings = settings
tenant.updated_at = datetime.now(UTC)
@@ -154,8 +154,15 @@ async def _fetch_alert_events(
def _compute_durations(
events: list[AlertSLAEvent],
-) -> dict[str, int | None]:
- """Compute MTTD/MTTR/MTTC in minutes for a single alert lifecycle."""
+) -> dict[str, Any]:
+ """Compute MTTD/MTTR/MTTC in minutes for a single alert lifecycle.
+
+ The returned mapping is heterogeneous: three `int | None` durations and
+ a `str` severity. It was annotated `dict[str, int | None]`, so every
+ caller of `d["severity"]` was typed `int | None` — which is how
+ `buckets.setdefault(sev, ...)` on a `dict[str, ...]` came to be four
+ separate findings about a key that is always a string.
+ """
by_type: dict[str, datetime] = {e.event_type: e.occurred_at for e in events}
detected_at = by_type.get("detected")