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(wave6): retract what is not built, and remove routes that cannot run (#1119)
Beenu Arora76570487f556
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 9d27fb26e..b90e04309 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -7,6 +7,77 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0## [Unreleased]+### BREAKING++- **`POST /api/v1/detection-loop/suggest`, `GET /api/v1/detection-loop/suggestions`+ and `GET /api/v1/detection-loop/suggestions/{suggestion_id}` are removed**,+ along with the `SuggestRequest`, `SuggestionResponse` and+ `SuggestionListResponse` schemas. Every generated SDK client loses those three+ operations.++ **Migration: none is needed, because none of them has ever worked.** They+ queried `aisoc_alerts` and `aisoc_detection_rules`, which no migration+ creates, and `alerts.evidence`, which does not exist either -- so repointing+ at the real table was not an option. Any caller was receiving an error. The+ governed equivalent is `POST /api/v1/detection-proposals`, which writes to a+ real table and carries separation of duties.++### Removed++- **`POST /api/v1/detection-loop/suggest` and its two sibling routes.** They+ queried `aisoc_alerts` and `aisoc_detection_rules`, which no migration+ creates, plus `alerts.evidence`, which does not exist either -- so a rename+ could not have fixed them. Their own test built "a fake `aisoc_alerts` row+ exposing the columns the endpoint reads", so three routes passed CI for as+ long as they shipped while being unable to succeed on any deployment, and+ they held their drafts in a process-global dict no second replica could see.+ No console surface called them.++### Fixed++- **Three more routes were reading a table that does not exist.** Emptying the+ `KNOWN_MISSING_TABLES` debt list in `scripts/check_raw_sql_columns.py` --+ which had recorded the two names above -- immediately surfaced the business+ context preview, the case timeline's linked-alert hydration and the identity+ timeline, all three naming `aisoc_alerts`. The identity timeline also named+ `evidence` and `mitre_technique` where the real columns are `raw_event` and+ `mitre_techniques`, so it had reported **no alerts for every identity on+ every deployment**. The business-context preview swallowed its error and+ returned `[]` under a comment blaming "test envs without the schema", so an+ analyst asking to preview a rule against their last 50 alerts silently got+ the console's built-in illustrative samples instead. All three now name+ `alerts` and its real columns, and the debt list is empty and gated.+- **A proposal whose body is only comments can no longer be promoted.**+ `POST /api/v1/detection/tuning/auto-suggest` opened proposals whose+ `rule_body` was a header, a rationale and a `TODO(analyst)`, with a null+ `base_rule_id` -- which takes the "new rule" branch in+ `/detection-proposals/{id}/decide`, so an approver clicking through would+ have written a rule with no detection logic into the engine. `/decide` now+ refuses such a body, and the auto-tuner no longer seeds the queue by default.++### Changed++- **Retracted claims for schema that nothing reads.** Migration 084's+ detection lifecycle (`environment`, `shadow_until`, `detection_rule_versions`,+ `detection_shadow_matches`) and migration 087's enterprise IAM+ (`workload_identities`, `privilege_grants`, `permission_conditions`,+ `narrow_by_conditions`) each have **zero readers** in `services/`. A rule set+ to `dev` still raises alerts, a future `shadow_until` still pages, and there+ is no rollback route. Separation of duties on detection proposals is real and+ the claim for it stands. The docs page, README and changelog now say which+ half is which.+- **Three docs-portal overclaims corrected.** Qdrant holds the MITRE technique+ corpus for lookup, which is a reference index and not agent memory; there is+ no coverage advisor that recommends rules for uncovered techniques and no+ one-click generation route; and `GET /taxii/collections` calls `_demo_only()`+ and returns a fixed list, so TAXII collection management is demo-only and the+ intel sharing is one-way.+- **The phishing playbook no longer claims a retraction it cannot perform.**+ Its "Retract phishing email fleet-wide" step posts to `${EMAIL_GATEWAY_URL}`,+ which no compose file or `.env.example` sets, under `on_failure: continue`.+ The step and the playbook description now say the message is not retracted+ when the gateway is unconfigured.+### Fixed- **The MCP client could not complete a single real call, and every tool it@@ -197,11 +268,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0ladder addresses.- **Legal hold, data residency, field-level access and per-subject deletion.** A hold outranksretention unconditionally.-- **Workload identities, API-key rotation in place, time-boxed privilege grants and ABAC- conditions.** Conditions narrow and never grant, and a condition the request carries no data for- denies rather than passes.-- **Detection lifecycle**: dev/staging/production, shadow mode independent of environment, and- version history so a promotion can be rolled back.+- **Workload identities, time-boxed privilege grants and ABAC conditions: schema only.**+ Migration 087 creates `workload_identities`, `privilege_grants` and+ `permission_conditions`, and **nothing reads any of them**;+ `narrow_by_conditions` has no caller. API-key rotation in place does work.+ The condition semantics described here -- narrow and never grant, deny when+ the request carries no data for a condition -- are the design the evaluator+ implements, not behaviour any route reaches today.+- **Detection lifecycle: separation of duties works, the rest is schema only.**+ `POST /api/v1/detection-proposals/{id}/decide` refuses an approval from the+ rule's own author, which is real. The `environment`, `shadow_until`,+ `detection_rule_versions` and `detection_shadow_matches` that migration 084+ adds are **read by nothing**: a rule set to `dev` still raises alerts, a+ future `shadow_until` still pages, and there is no rollback route.- **Chaos coverage** for actions as well as events, and for ClickHouse, Neo4j, Qdrant, Redis andKafka, on a weekly trigger — the existing harness had run live once, by hand.- **Load profiles** — ramp, burst, backpressure and 24/72-hour soak. No `--duration` flag existed,diff --git a/FIX_PASS_PROGRESS.md b/FIX_PASS_PROGRESS.mdindex d557dca40..4b7600f89 100644--- a/FIX_PASS_PROGRESS.md+++ b/FIX_PASS_PROGRESS.md@@ -121,11 +121,16 @@ conditional `numpy` pins collide into an unresolvable install. Parse with## Wave 6: Retract what is not built-- [ ] **6.1** The v16 detection lifecycle-- [ ] **6.2** Enterprise IAM-- [ ] **6.3** Overclaims in `apps/docs/docs/intro.md`-- [ ] **6.4** The phishing playbook's fleet-wide retraction-- [ ] **6.5** Two broken detection-tuning routes+- [x] **6.1** The v16 detection lifecycle+ - Retracted. `detection_rule_versions`, `detection_shadow_matches` and `shadow_until` have zero readers in `services/`; the only `rollback` in the detection endpoints is a DB transaction rollback. Docs page, README and CHANGELOG now mark the four sections schema-only. Separation of duties is real and its claim stands.+- [x] **6.2** Enterprise IAM+ - Retracted. `workload_identities`, `privilege_grants`, `permission_conditions` have zero readers; `narrow_by_conditions` has zero callers. README and CHANGELOG narrowed; API-key rotation, which does work, kept.+- [x] **6.3** Overclaims in `apps/docs/docs/intro.md`+ - Retracted three intro.md overclaims: Qdrant holds the MITRE corpus for lookup (not agent memory), no coverage advisor or one-click generation route exists, and `GET /taxii/collections` calls `_demo_only()`.+- [x] **6.4** The phishing playbook's fleet-wide retraction+ - Corrected. The retraction step posts to an unset `EMAIL_GATEWAY_URL` under `on_failure: continue`; step name and playbook description now say the message is not retracted when unconfigured. Both copies identical.+- [x] **6.5** Two broken detection-tuning routes+ - Removed `detection_loop.py` (3 routes) -- `aisoc_alerts`, `aisoc_detection_rules` and `alerts.evidence` all absent, so a rename could not work; its test mocked the missing row. Emptying the gate debt list surfaced **three more** callers (business context preview, case timeline, identity timeline), all repointed at `alerts` with real column names. Added a guard refusing promotion of a comment-only body and defaulted the auto-tuner off. `KNOWN_MISSING_TABLES` is now empty and gated.## Wave 7: Close the booksdiff --git a/README.md b/README.mdindex 7fdff703f..3e9649c2b 100644--- a/README.md+++ b/README.md@@ -226,9 +226,9 @@ Agents triage alerts and investigate incidents. What they can and cannot do:Secrets are generated per deployment and never committed; connector credentials are encrypted at rest. Services connect to Postgres as a DML-only role, so row-level security actually applies, and tenant isolation is enforced at the query layer in every store. RBAC gates every mutating route, ingest is authenticated, and the default install sends no prompt anywhere — the model runs beside it.-SAML and OIDC sign-in with per-connection tenant and group mapping, SCIM provisioning, attribute-conditions that narrow a permission, and time-boxed elevation instead of standing admin-([setup](apps/docs/docs/operations/enterprise-sso.md)).+SAML and OIDC sign-in with per-connection tenant and group mapping, and SCIM provisioning+([setup](apps/docs/docs/operations/enterprise-sso.md)). Attribute conditions and time-boxed+elevation are schema only: migration 087 creates the tables and no code reads them yet.**A service with no credential refuses to serve rather than serving unauthenticated.** The [changelog](CHANGELOG.md) records each fix; report via [SECURITY.md](SECURITY.md).diff --git a/apps/docs/docs/intro.md b/apps/docs/docs/intro.mdindex 8030a0241..914ed3600 100644--- a/apps/docs/docs/intro.md+++ b/apps/docs/docs/intro.md@@ -52,12 +52,12 @@ for the full list.- **Public eval harness** — alert reduction, measured against `RawAlert.correlation_key()` on a fixed noisy stream (a legacy in-test suite grouping on different dimensions is retained for continuity and does not describe this product), plus MITRE-tactic, investigation-completeness, and response-quality substrate self-consistency gates. Reproducible with one command and run in CI on every PR. The [eval harness page](./benchmark) documents what each suite does and does not measure.- **Ambient Copilot** — context-aware next-action suggestions on every alert, case, rule, and playbook page; one click runs the right agent tool with the right payload.- **Responder PWA** — installable mobile route at `/responder/*` with passkey-only login, on-call rotation, approvals queue, VAPID Web Push, and offline shell.-- **LangGraph multi-agent investigation** — orchestrator, recon, forensic, responder, and report-writer agents grounded in MITRE ATT&CK with Qdrant RAG memory. Includes domain-specific agents for phishing triage, identity threat analysis, cloud misconfiguration detection, and insider threat scoring.+- **LangGraph multi-agent investigation** — orchestrator, recon, forensic, responder, and report-writer agents grounded in MITRE ATT&CK, whose technique corpus is embedded in Qdrant for lookup. That is a reference index, not agent memory: nothing persists what an investigation learned. Includes domain-specific agents for phishing triage, identity threat analysis, cloud misconfiguration detection, and insider threat scoring.- **Autonomous alert triage** — LLM-based auto-triage agent classifies alerts as true positive, false positive, or benign with confidence scoring. High-confidence false positives are auto-closed; uncertain alerts escalate to human analysts.- **Conversational investigation chat** — multi-turn NL interface for querying alerts, cases, and threat intel with quick actions and a persistent investigation context panel.-- **MITRE ATT&CK coverage advisor** — identifies detection gaps across tactics, recommends new rules, and enables one-click detection generation for uncovered techniques.+- **MITRE ATT&CK coverage matrix** — reports which techniques the executable rule corpus tags, as tag coverage rather than detection efficacy. It does **not** recommend rules and there is no one-click generation for an uncovered technique; no such route exists. `POST /api/v1/nl-detection` turns a plain-English description you supply into candidate rules, which is a different thing from advising you what is missing.- **Alert noise tuning** — closed-loop dashboard driven by analyst TP/FP verdicts, with auto-tune toggles per rule and monthly noise trend visualization.-- **STIX/TAXII publishing** — bidirectional threat intel sharing with STIX 2.1 bundle creation and TAXII collection management. Optional [STIX → MISP push](./integrations/misp-push) republishes high-confidence indicators and bundles to a MISP server with `dry-run` / `health` endpoints for fail-closed staging rollouts.+- **STIX bundle creation**, with an optional push to MISP. **TAXII collection management is demo-only**: `GET /taxii/collections` calls `_demo_only()` and returns a fixed list, so there is no TAXII server a peer can subscribe to and the sharing is one-way. Optional [STIX → MISP push](./integrations/misp-push) republishes high-confidence indicators and bundles to a MISP server with `dry-run` / `health` endpoints for fail-closed staging rollouts.- **Automated compliance evidence** — continuous collection from connected sources across SOC 2, ISO 27001, NIST CSF, PCI-DSS, HIPAA, and DORA frameworks.- **AI-generated incident reports** — one-click PDF/MD export of case investigation summaries directly from the case detail view.- **Real-time fusion** — Kafka spine with sub-second alert ingestion, Bloom-filter dedup on 10M+ IOCs, ML scoring (LightGBM + Isolation Forest).diff --git a/apps/docs/docs/operations/detection-lifecycle.md b/apps/docs/docs/operations/detection-lifecycle.mdindex 3cd59c50f..92717ca8f 100644--- a/apps/docs/docs/operations/detection-lifecycle.md+++ b/apps/docs/docs/operations/detection-lifecycle.md@@ -56,60 +56,53 @@ POST /api/v1/detection-proposals/{id}/evaluate-ruleThe request body is optional. Omit it and the proposal's own fixturesare replayed; supply fixtures to override them for one run.-## Environments-| Environment | Evaluates | Raises alerts | For |-|---|---|---|---|-| `dev` | Yes | No | A rule nobody has reviewed |-| `staging` | Yes | No | A reviewed rule earning confidence on live traffic |-| `production` | Yes | Yes | Live detection |+:::warning Schema only: not yet read by anything-## Shadow mode+Everything from here to "Related" describes **columns and tables migration+087 creates and no code reads**. Verified on a fully-migrated database:+`detection_rule_versions` and `detection_shadow_matches` have zero readers in+`services/`, `shadow_until` has zero, and the only `rollback` in the detection+endpoints is a database transaction rollback.-`shadow_until` is a timestamp, and it is **independent of-environment** — a production rule can be shadowed during a tuning-change without being demoted and losing its history.+So a rule set to `dev` still raises alerts, a `shadow_until` in the future+still pages, and there is no route that rolls a rule back. The schema is kept+because the design is settled and the migration has shipped; the behaviour is+not claimed until the PR that wires it lands, under the parity plan.-While it is set and in the future, the rule is evaluated and its-matches recorded, and it raises nothing.+**What does work is above:** separation of duties on+`POST /api/v1/detection-proposals/{id}/decide`, which compares the caller+against `proposed_by_id` and refuses an approval from the same person.-Matches land in `detection_shadow_matches`, a separate table rather-than a flag on `alerts`. That is the whole point: no query and no-person can mistake a shadow match for an alert.+:::-```sql-SELECT rule_id, count(*) AS would_have_fired- FROM detection_shadow_matches- WHERE matched_at > now() - interval '24 hours'- GROUP BY rule_id- ORDER BY would_have_fired DESC;-```+## Environments (schema only)++| Environment | Column accepts | Evaluates | Raises alerts |+|---|---|---|---|+| `dev` | yes | yes | **yes, today** |+| `staging` | yes | yes | **yes, today** |+| `production` | yes | yes | yes |-A rule at the top of that list would have been your noisiest detection.+The engine does not read `environment`, so all three behave identically.-## Versions and rollback+## Shadow mode (schema only)-Every body a rule has ever had live is kept in-`detection_rule_versions`. Promotion used to overwrite in place, so-"roll back the rule we shipped on Tuesday" had no answer other than-reconstructing it from a pull request — which is exactly the question-asked when a detection starts firing on everything overnight.+`shadow_until` is a timestamp column. Nothing reads it, and+`detection_shadow_matches` is never written, so a rule with a future+`shadow_until` raises alerts exactly as it would without one.-Versions are numbered with a **monotonic integer, not a timestamp**.-Two promotions in the same second are ordinary during a tuning session,-and "the version before this one" has to have exactly one answer.+## Versions and rollback (schema only)-A rollback writes a **new version carrying an old body** rather than-deleting rows, so the history of what was live when stays intact and-`rolled_back_from` records where the body came from.+`detection_rule_versions` exists and nothing writes to it, so a promotion+still overwrites in place and "roll back the rule we shipped on Tuesday" still+has no answer other than reconstructing it from a pull request.-## Ownership and expiry+## Ownership and expiry (schema only)-| Field | Why |-|---|---|-| `owner_email`, `owner_team` | A rule written for an incident three years ago was indistinguishable from one somebody maintains |-| `expires_at` | Nullable. A rule with no expiry is permanent, which is a legitimate choice — what was missing was the ability to say otherwise |-| `review_due_at` | A prompt to re-check a rule that is still correct but no longer load-bearing |+`owner_email`, `owner_team`, `expires_at` and `review_due_at` are columns a+caller can set. Nothing reads them, so an expiry does not expire a rule and a+review date prompts nobody.## Relateddiff --git a/apps/web/public/marketplace/index.json b/apps/web/public/marketplace/index.jsonindex 04d78d188..747fe4c41 100644--- a/apps/web/public/marketplace/index.json+++ b/apps/web/public/marketplace/index.json@@ -1,7 +1,7 @@{"$schema": "https://example.com/schemas/marketplace/v1.json","version": "1.0.0",- "generated": "2026-10-03T15:14:21Z",+ "generated": "2026-10-04T15:38:08Z","categories": [{"id": "playbooks",@@ -236777,7 +236777,7 @@"id": "phishing-investigation-v1","type": "playbook","name": "Phishing: Email Investigation & Remediation",- "description": "Triggered by a phishing alert from email gateway or user report. Extracts IOCs, retracts the message fleet-wide, blocks sender domain, and resets any credentials that may have been compromised.",+ "description": "Triggered by a phishing alert from an email gateway or a user report. Extracts IOCs, blocks the sender domain, and resets credentials that may have been compromised. The retraction step requires EMAIL_GATEWAY_URL, which no compose file or .env.example sets, so on a default deployment the message is NOT retracted and the step is skipped.","version": "1.0.0","author": "Beenu - [email protected]","tags": [@@ -236799,7 +236799,7 @@"pack": true,"enabled": true,"path": "playbooks/packs/v1/phishing/phishing-investigation.playbook.json",- "sha256": "f68e72606a9ce930c2c0bfa33bff93fbce949fe5d0a8a6e5629319f65c0e8dec"+ "sha256": "73f354344458fe6ed95f28835b5cc40c045c67716f5c56f909b6df3b8406ef7a"},{"id": "phishing-response-v1",diff --git a/docs/openapi.yaml b/docs/openapi.yamlindex d47684709..f360248fa 100644--- a/docs/openapi.yaml+++ b/docs/openapi.yaml@@ -3845,7 +3845,7 @@ paths:required: falseschema:type: boolean- default: true+ default: falsetitle: Create Proposalsresponses:'200':@@ -12863,104 +12863,6 @@ paths:$ref: '#/components/schemas/HTTPValidationError'security:- HTTPBearer: []- /api/v1/detection-loop/suggest:- post:- tags:- - detection_rules- - detection_loop- summary: Draft Sigma improvement for a FP alert- description: 'Retrieve the triggering rule + alert evidence, then draft a Sigma- improvement.--- Tenant isolation: the alert and rule lookups are scoped to ``user.tenant_id``-- so a caller cannot pull another tenant''s evidence by guessing an ``alert_id``.-- ``TenantDBSession`` also sets the Postgres RLS context for defense in depth.'- operationId: suggest_fp_fix_api_v1_detection_loop_suggest_post- requestBody:- content:- application/json:- schema:- $ref: '#/components/schemas/SuggestRequest'- required: true- responses:- '201':- description: Successful Response- content:- application/json:- schema:- $ref: '#/components/schemas/SuggestionResponse'- '422':- description: Validation Error- content:- application/json:- schema:- $ref: '#/components/schemas/HTTPValidationError'- security:- - HTTPBearer: []- /api/v1/detection-loop/suggestions:- get:- tags:- - detection_rules- - detection_loop- summary: List LLM-drafted Sigma suggestions- description: 'List suggestions drafted by *this* tenant only.--- Tenant isolation: ``_SUGGESTIONS`` is process-wide and shared across all-- tenants. Filtering on the stored ``tenant_id`` ensures one tenant never-- sees another tenant''s drafts, rule names, or evidence-derived rationale.'- operationId: list_suggestions_api_v1_detection_loop_suggestions_get- responses:- '200':- description: Successful Response- content:- application/json:- schema:- $ref: '#/components/schemas/SuggestionListResponse'- security:- - HTTPBearer: []- /api/v1/detection-loop/suggestions/{suggestion_id}:- get:- tags:- - detection_rules- - detection_loop- summary: Get one Sigma suggestion- description: 'Return one suggestion if and only if it belongs to the caller''s- tenant.--- Tenant isolation: a cross-tenant lookup returns 404 (not 403) to avoid-- leaking the existence of a suggestion that belongs to another tenant.'- operationId: get_suggestion_api_v1_detection_loop_suggestions__suggestion_id__get- security:- - HTTPBearer: []- parameters:- - name: suggestion_id- in: path- required: true- schema:- type: string- format: uuid- title: Suggestion Id- responses:- '200':- description: Successful Response- content:- application/json:- schema:- $ref: '#/components/schemas/SuggestionResponse'- '422':- description: Validation Error- content:- application/json:- schema:- $ref: '#/components/schemas/HTTPValidationError'/api/v1/nl-query/translate:post:tags:@@ -32591,84 +32493,6 @@ components:required:- urltitle: SubmitUrlRequest- SuggestRequest:- properties:- alert_id:- type: string- format: uuid- title: Alert Id- description: ID of the FP-flagged alert.- analyst_note:- anyOf:- - type: string- - type: 'null'- title: Analyst Note- description: Free-text note explaining why this is a FP.- type: object- required:- - alert_id- title: SuggestRequest- SuggestionListResponse:- properties:- suggestions:- items:- $ref: '#/components/schemas/SuggestionResponse'- type: array- title: Suggestions- total:- type: integer- title: Total- type: object- required:- - suggestions- - total- title: SuggestionListResponse- SuggestionResponse:- properties:- suggestion_id:- type: string- format: uuid- title: Suggestion Id- alert_id:- type: string- format: uuid- title: Alert Id- base_rule_id:- anyOf:- - type: string- format: uuid- - type: 'null'- title: Base Rule Id- draft_rule_name:- type: string- title: Draft Rule Name- draft_sigma_yaml:- type: string- title: Draft Sigma Yaml- rationale:- type: string- title: Rationale- proposal_id:- anyOf:- - type: string- format: uuid- - type: 'null'- title: Proposal Id- created_at:- type: string- format: date-time- title: Created At- type: object- required:- - suggestion_id- - alert_id- - base_rule_id- - draft_rule_name- - draft_sigma_yaml- - rationale- - proposal_id- - created_at- title: SuggestionResponseSuppressRequest:properties:reason:diff --git a/marketplace/index.json b/marketplace/index.jsonindex 04d78d188..747fe4c41 100644--- a/marketplace/index.json+++ b/marketplace/index.json@@ -1,7 +1,7 @@{"$schema": "https://example.com/schemas/marketplace/v1.json","version": "1.0.0",- "generated": "2026-10-03T15:14:21Z",+ "generated": "2026-10-04T15:38:08Z","categories": [{"id": "playbooks",@@ -236777,7 +236777,7 @@"id": "phishing-investigation-v1","type": "playbook","name": "Phishing: Email Investigation & Remediation",- "description": "Triggered by a phishing alert from email gateway or user report. Extracts IOCs, retracts the message fleet-wide, blocks sender domain, and resets any credentials that may have been compromised.",+ "description": "Triggered by a phishing alert from an email gateway or a user report. Extracts IOCs, blocks the sender domain, and resets credentials that may have been compromised. The retraction step requires EMAIL_GATEWAY_URL, which no compose file or .env.example sets, so on a default deployment the message is NOT retracted and the step is skipped.","version": "1.0.0","author": "Beenu - [email protected]","tags": [@@ -236799,7 +236799,7 @@"pack": true,"enabled": true,"path": "playbooks/packs/v1/phishing/phishing-investigation.playbook.json",- "sha256": "f68e72606a9ce930c2c0bfa33bff93fbce949fe5d0a8a6e5629319f65c0e8dec"+ "sha256": "73f354344458fe6ed95f28835b5cc40c045c67716f5c56f909b6df3b8406ef7a"},{"id": "phishing-response-v1",diff --git a/playbooks/packs/v1/phishing/phishing-investigation.playbook.json b/playbooks/packs/v1/phishing/phishing-investigation.playbook.jsonindex e5637937c..eba28a8eb 100644--- a/playbooks/packs/v1/phishing/phishing-investigation.playbook.json+++ b/playbooks/packs/v1/phishing/phishing-investigation.playbook.json@@ -1,7 +1,7 @@{"id": "phishing-investigation-v1","name": "Phishing: Email Investigation & Remediation",- "description": "Triggered by a phishing alert from email gateway or user report. Extracts IOCs, retracts the message fleet-wide, blocks sender domain, and resets any credentials that may have been compromised.",+ "description": "Triggered by a phishing alert from an email gateway or a user report. Extracts IOCs, blocks the sender domain, and resets credentials that may have been compromised. The retraction step requires EMAIL_GATEWAY_URL, which no compose file or .env.example sets, so on a default deployment the message is NOT retracted and the step is skipped.","version": "1.0.0","author": "Beenu - [email protected]","tags": [@@ -13,8 +13,14 @@],"trigger": {"on": "alert",- "severity": ["high", "critical"],- "tags": ["phishing", "email-security"]+ "severity": [+ "high",+ "critical"+ ],+ "tags": [+ "phishing",+ "email-security"+ ]},"enabled": true,"dry_run_support": true,@@ -70,7 +76,7 @@},{"id": "retract",- "name": "Retract phishing email fleet-wide",+ "name": "Retract the message via the tenant's email gateway (needs EMAIL_GATEWAY_URL; no-op when unset)","type": "http","params": {"url": "${EMAIL_GATEWAY_URL}/v1/messages/retract",@@ -84,13 +90,18 @@},{"id": "approval_block_domain",- "name": "Approval gate — block sender domain",+ "name": "Approval gate \u2014 block sender domain","type": "approval","params": {"message": "Block sender domain '{{inputs.sender_domain}}' at email gateway and DNS firewall?",- "approvers": ["soc-lead", "security-manager"],+ "approvers": [+ "soc-lead",+ "security-manager"+ ],"timeout_seconds": 900,- "auto_approve_severity": ["critical"]+ "auto_approve_severity": [+ "critical"+ ]},"on_failure": "abort","retry_max": 0,@@ -126,11 +137,13 @@},{"id": "approval_reset",- "name": "Approval gate — force password reset (if click detected)",+ "name": "Approval gate \u2014 force password reset (if click detected)","type": "approval","params": {"message": "Recipient '{{inputs.recipient_user}}' may have clicked link. Force password reset + MFA?",- "approvers": ["soc-lead"],+ "approvers": [+ "soc-lead"+ ],"timeout_seconds": 600,"condition_skip": "context.click_detected == false"},@@ -170,7 +183,7 @@"type": "notify","params": {"channel": "slack",- "message_template": "Phishing alert: {{inputs.sender_domain}} → {{inputs.recipient_user}}. Message retracted: {{context.retract_success}}. Domain blocked: {{context.domain_blocked}}.",+ "message_template": "Phishing alert: {{inputs.sender_domain}} \u2192 {{inputs.recipient_user}}. Message retracted: {{context.retract_success}}. Domain blocked: {{context.domain_blocked}}.","webhook_env": "SLACK_SOC_WEBHOOK"},"on_failure": "continue",@@ -185,7 +198,10 @@"priority": "P2","title_template": "Phishing: {{inputs.sender_domain}} targeting {{inputs.recipient_user}}","queue": "soc",- "labels": ["phishing", "email"]+ "labels": [+ "phishing",+ "email"+ ]},"on_failure": "continue","retry_max": 0,diff --git a/scripts/check_python_route_state.py b/scripts/check_python_route_state.pyindex ab0fb32c7..35ddf490f 100644--- a/scripts/check_python_route_state.py+++ b/scripts/check_python_route_state.py@@ -154,13 +154,6 @@ ALLOWED_GLOBALS: dict[tuple[str, str], str] = {"services/api/app/api/v1/endpoints/community.py","_community_playbooks",): "Same module, same gap.",- (- "services/api/app/api/v1/endpoints/detection_loop.py",- "_SUGGESTIONS",- ): (- "Drafted detection suggestions awaiting review. Parity plan 6.2 makes a draft a "- "governed DRAFT proposal with a row, which is where this gets a table."- ),}diff --git a/scripts/check_raw_sql_columns.py b/scripts/check_raw_sql_columns.pyindex 68cb4582a..3dec8f8e4 100644--- a/scripts/check_raw_sql_columns.py+++ b/scripts/check_raw_sql_columns.py@@ -248,14 +248,18 @@ PARSER_ARTEFACTS: dict[tuple[str, str], str] = {#: An entry here is a debt with an owner, not an exemption. Both of these are#: fix-pass item 6.5, which either points the two detection-tuning routes at#: the real tables or removes them.-KNOWN_MISSING_TABLES: dict[str, str] = {- "aisoc_alerts": (- "No migration creates this. The detection-loop suggestion route and the business-context "- "reader query it, so both return nothing on every deployment. Fix-pass item 6.5 removes "- "this entry by pointing them at `alerts` or removing the routes."- ),- "aisoc_detection_rules": ("No migration creates this. Same two routes, same item 6.5."),-}+#: Tables a statement names that no migration creates and that are not+#: foreign either: a defect, recorded with the item that removes it.+#:+#: **Empty, and it must stay empty.** It held `aisoc_alerts` and+#: `aisoc_detection_rules`. `detection_loop.py` queried both plus+#: `alerts.evidence`, none of which exist, and its own test built "a fake+#: `aisoc_alerts` row exposing the columns the endpoint reads" -- so three+#: routes passed CI for as long as they shipped while being unable to succeed+#: anywhere. Fix-pass item 6.5 removed those routes rather than renaming the+#: table, because the column does not exist either, and repointed the+#: business-context preview at `alerts` with the real column names.+KNOWN_MISSING_TABLES: dict[str, str] = {}#: Statements whose table or column list is assembled at runtime, and so#: cannot be compared against anything, with the reason.diff --git a/scripts/export_openapi.py b/scripts/export_openapi.pyindex a278ff9b8..50f8242d2 100755--- a/scripts/export_openapi.py+++ b/scripts/export_openapi.py@@ -61,6 +61,80 @@ def write_yaml(schema: dict, dest: Path) -> None:print(f"OpenAPI schema written to {dest.relative_to(REPO_ROOT)}")+def _locked_version(package: str) -> str | None:+ """What `services/api/poetry.lock` pins `package` to, if it can be read."""+ lock = REPO_ROOT / "services" / "api" / "poetry.lock"+ if not lock.exists():+ return None+ wanted = f'name = "{package}"'+ lines = lock.read_text(encoding="utf-8").splitlines()+ for i, line in enumerate(lines):+ if line.strip() == wanted:+ for follow in lines[i + 1 : i + 4]:+ stripped = follow.strip()+ if stripped.startswith("version = "):+ return stripped.split('"')[1]+ return None+++def _report_generator_versions() -> None:+ """Name a toolchain mismatch, because it is the usual cause and the+ message above cannot fix it.++ The schema is generated by pydantic and FastAPI, and both change their+ output between minors -- pydantic 2.13 emits `additionalProperties: true`+ on bare `dict` responses where 2.8 did not, which is 252 lines of diff+ across this file and nothing to do with anyone's change. Told only to+ "run export_openapi.py", a contributor runs the command that produced the+ rejected file and gets the same result, which is the least useful place a+ gate can leave somebody.+ """+ for package, module in (("pydantic", "pydantic"), ("fastapi", "fastapi")):+ locked = _locked_version(package)+ try:+ installed = __import__(module).VERSION if package == "pydantic" else __import__(module).__version__+ except Exception: # pragma: no cover - a missing generator fails earlier+ continue+ if locked and installed != locked:+ print(+ f" {package} {installed} is installed and services/api/poetry.lock pins {locked}.\n"+ f" That alone changes the generated schema. Install the locked version first:\n"+ f" pip install '{package}=={locked}'",+ file=sys.stderr,+ )+++def _report_first_differences(existing: dict, schema: dict, limit: int = 8) -> None:+ """Print the first few paths that differ, so the cause is visible here.++ A gate that says only "these two documents differ" over a 30,000-line file+ leaves the reader to reconstruct the diff by hand.+ """++ def walk(a: object, b: object, trail: str) -> list[str]:+ if isinstance(a, dict) and isinstance(b, dict):+ out: list[str] = []+ for key in sorted(set(a) | set(b)):+ if key not in a:+ out.append(f"{trail}.{key}: only in the generated schema")+ elif key not in b:+ out.append(f"{trail}.{key}: only in the committed file")+ else:+ out.extend(walk(a[key], b[key], f"{trail}.{key}"))+ if len(out) >= limit:+ break+ return out+ if a != b:+ return [f"{trail}: committed {a!r} vs generated {b!r}"]+ return []++ differences = walk(existing, schema, "")[:limit]+ if differences:+ print(" First differences:", file=sys.stderr)+ for line in differences:+ print(f" {line}"[:200], file=sys.stderr)++def check_yaml(schema: dict, dest: Path) -> bool:"""Return True if file matches generated schema."""if not dest.exists():@@ -75,6 +149,8 @@ def check_yaml(schema: dict, dest: Path) -> bool:f"{dest.relative_to(REPO_ROOT)} is out of date.\n Run: python scripts/export_openapi.py then commit the result.",file=sys.stderr,)+ _report_generator_versions()+ _report_first_differences(existing, schema)return Falseprint(f"{dest.relative_to(REPO_ROOT)} is up to date.")diff --git a/scripts/mypy_baseline.json b/scripts/mypy_baseline.jsonindex bfc8a3c18..43ee34077 100644--- a/scripts/mypy_baseline.json+++ b/scripts/mypy_baseline.json@@ -405,9 +405,6 @@"tests/test_credential_vault_envelope.py": {"misc": 1},- "tests/test_detection_loop_tenant_isolation.py": {- "misc": 1- },"tests/test_effective_permissions_aws.py": {"attr-defined": 1},diff --git a/services/agents/app/playbook/packs/v1/phishing/phishing-investigation.playbook.json b/services/agents/app/playbook/packs/v1/phishing/phishing-investigation.playbook.jsonindex e5637937c..eba28a8eb 100644--- a/services/agents/app/playbook/packs/v1/phishing/phishing-investigation.playbook.json+++ b/services/agents/app/playbook/packs/v1/phishing/phishing-investigation.playbook.json@@ -1,7 +1,7 @@{"id": "phishing-investigation-v1","name": "Phishing: Email Investigation & Remediation",- "description": "Triggered by a phishing alert from email gateway or user report. Extracts IOCs, retracts the message fleet-wide, blocks sender domain, and resets any credentials that may have been compromised.",+ "description": "Triggered by a phishing alert from an email gateway or a user report. Extracts IOCs, blocks the sender domain, and resets credentials that may have been compromised. The retraction step requires EMAIL_GATEWAY_URL, which no compose file or .env.example sets, so on a default deployment the message is NOT retracted and the step is skipped.","version": "1.0.0","author": "Beenu - [email protected]","tags": [@@ -13,8 +13,14 @@],"trigger": {"on": "alert",- "severity": ["high", "critical"],- "tags": ["phishing", "email-security"]+ "severity": [+ "high",+ "critical"+ ],+ "tags": [+ "phishing",+ "email-security"+ ]},"enabled": true,"dry_run_support": true,@@ -70,7 +76,7 @@},{"id": "retract",- "name": "Retract phishing email fleet-wide",+ "name": "Retract the message via the tenant's email gateway (needs EMAIL_GATEWAY_URL; no-op when unset)","type": "http","params": {"url": "${EMAIL_GATEWAY_URL}/v1/messages/retract",@@ -84,13 +90,18 @@},{"id": "approval_block_domain",- "name": "Approval gate — block sender domain",+ "name": "Approval gate \u2014 block sender domain","type": "approval","params": {"message": "Block sender domain '{{inputs.sender_domain}}' at email gateway and DNS firewall?",- "approvers": ["soc-lead", "security-manager"],+ "approvers": [+ "soc-lead",+ "security-manager"+ ],"timeout_seconds": 900,- "auto_approve_severity": ["critical"]+ "auto_approve_severity": [+ "critical"+ ]},"on_failure": "abort","retry_max": 0,@@ -126,11 +137,13 @@},{"id": "approval_reset",- "name": "Approval gate — force password reset (if click detected)",+ "name": "Approval gate \u2014 force password reset (if click detected)","type": "approval","params": {"message": "Recipient '{{inputs.recipient_user}}' may have clicked link. Force password reset + MFA?",- "approvers": ["soc-lead"],+ "approvers": [+ "soc-lead"+ ],"timeout_seconds": 600,"condition_skip": "context.click_detected == false"},@@ -170,7 +183,7 @@"type": "notify","params": {"channel": "slack",- "message_template": "Phishing alert: {{inputs.sender_domain}} → {{inputs.recipient_user}}. Message retracted: {{context.retract_success}}. Domain blocked: {{context.domain_blocked}}.",+ "message_template": "Phishing alert: {{inputs.sender_domain}} \u2192 {{inputs.recipient_user}}. Message retracted: {{context.retract_success}}. Domain blocked: {{context.domain_blocked}}.","webhook_env": "SLACK_SOC_WEBHOOK"},"on_failure": "continue",@@ -185,7 +198,10 @@"priority": "P2","title_template": "Phishing: {{inputs.sender_domain}} targeting {{inputs.recipient_user}}","queue": "soc",- "labels": ["phishing", "email"]+ "labels": [+ "phishing",+ "email"+ ]},"on_failure": "continue","retry_max": 0,diff --git a/services/api/app/api/v1/endpoints/business_context.py b/services/api/app/api/v1/endpoints/business_context.pyindex faf3df19b..82689e5d0 100644--- a/services/api/app/api/v1/endpoints/business_context.py+++ b/services/api/app/api/v1/endpoints/business_context.py@@ -355,20 +355,34 @@ async def _fetch_sample_alerts() -> list[dict[str, Any]]:"""Pull the last ``limit`` alerts for the tenant.- The dry-run preview surfaces "what would happen to my last 50- alerts?" so the analyst gets a tangible feel for the rule before- saving. Returns an empty list when the alerts table isn't reachable- (test envs without the schema, demo Fly.io stack) — the UI then- falls back to its built-in illustrative samples.+ The dry-run preview surfaces "what would happen to my last 50 alerts?" so+ the analyst gets a tangible feel for the rule before saving.++ This used to read ``aisoc_alerts``, a table no migration creates, naming+ five columns the real ``alerts`` table does not have either. The ``except``+ below swallowed the error and returned ``[]`` under a comment blaming+ "test envs without the schema", so on **every** deployment the preview fell+ through to the UI's built-in illustrative samples while telling the analyst+ it had used their own alerts.++ The column mapping is the part worth reading: the real table stores the+ source as ``connector_type`` and the entities as jsonb arrays+ (``affected_ips`` and friends), so the single-valued fields the rule+ grammar references are taken from the first element rather than invented."""try:result = await db.execute(text("""- SELECT id, severity, title, source, src_ip, hostname, username,- tags, metadata- FROM aisoc_alerts- WHERE tenant_id = :tenant_id+ SELECT id, severity, title,+ connector_type AS source,+ affected_ips->>0 AS src_ip,+ affected_hosts->>0 AS hostname,+ affected_users->>0 AS username,+ tags,+ raw_event AS metadata+ FROM alerts+ WHERE tenant_id = CAST(:tenant_id AS uuid)ORDER BY created_at DESCLIMIT :limit"""diff --git a/services/api/app/api/v1/endpoints/cases.py b/services/api/app/api/v1/endpoints/cases.pyindex e688bcc7a..595a295a7 100644--- a/services/api/app/api/v1/endpoints/cases.py+++ b/services/api/app/api/v1/endpoints/cases.py@@ -1000,7 +1000,9 @@ async def case_timeline(case_id: str, db: DBSession, user: AuthUser) -> Timeline# nothing and a poisoned ``alert_ids`` array would otherwise# hydrate another tenant's alert title into this timeline.await db.execute(- text("SELECT id, title, severity, created_at FROM aisoc_alerts WHERE id = :id AND tenant_id = :tenant_id").bindparams(+ text(+ "SELECT id, title, severity, created_at FROM alerts WHERE id = :id AND tenant_id = CAST(:tenant_id AS uuid)"+ ).bindparams(id=alert_id,tenant_id=str(user.tenant_id),)diff --git a/services/api/app/api/v1/endpoints/detection_loop.py b/services/api/app/api/v1/endpoints/detection_loop.pydeleted file mode 100644index 8e3fa83cf..000000000--- a/services/api/app/api/v1/endpoints/detection_loop.py+++ /dev/null@@ -1,335 +0,0 @@-"""Closed-loop detection engineering (tier2-detection-loop).--When an analyst marks an alert as a false-positive (FP), this module:--1. Retrieves the triggering detection rule and the alert's raw evidence.-2. Invokes an LLM to draft a Sigma YAML improvement that would suppress- the FP without widening the exclusion too broadly.-3. Creates a ``DetectionRuleProposal`` via the existing DAC lifecycle so the- suggestion is eval-gated before promotion.-4. Returns the draft proposal ID and the diff for the analyst to review.--Endpoints-----------* ``POST /detection-loop/suggest`` Trigger FP → Sigma draft.-* ``GET /detection-loop/suggestions`` List LLM-drafted suggestions.-* ``GET /detection-loop/suggestions/{id}`` Detail of one suggestion.--Authorization---------------``POST /suggest`` requires ``rules:read``. The route reads the triggering-rule's body out of ``aisoc_detection_rules`` and returns a draft of it, so-the floor is that the caller may read detection content — which a ``viewer``-may not, and which is what made this route a small disclosure of the-tenant's detection logic as well as an ungated LLM spend. The draft is-persisted as a proposal by the DAC lifecycle, which is gated on-``rules:write``; drafting needs the read, promoting needs the write.-"""--from __future__ import annotations--import json-import logging-import textwrap-import uuid-from datetime import UTC, datetime-from typing import Annotated, Any--from fastapi import APIRouter, Depends, HTTPException, status-from pydantic import BaseModel, Field-from sqlalchemy import text--from app.api.v1.deps import AuthUser, require_permission-from app.core.config import settings-from app.db.rls import TenantDBSession-from app.services.llm_safety import LLMContractViolation, safe_chat_completions_request-from app.services.model_aliases import resolve_model_alias--logger = logging.getLogger(__name__)--router = APIRouter(prefix="/detection-loop", tags=["detection_rules", "detection_loop"])--# ────────────────────────────────────────────────────────────────────────────-# Pydantic schemas-# ────────────────────────────────────────────────────────────────────────────---class SuggestRequest(BaseModel):- alert_id: uuid.UUID = Field(..., description="ID of the FP-flagged alert.")- analyst_note: str | None = Field(None, description="Free-text note explaining why this is a FP.")---class SuggestionResponse(BaseModel):- suggestion_id: uuid.UUID- alert_id: uuid.UUID- base_rule_id: uuid.UUID | None- draft_rule_name: str- draft_sigma_yaml: str- rationale: str- proposal_id: uuid.UUID | None- created_at: datetime---class SuggestionListResponse(BaseModel):- suggestions: list[SuggestionResponse]- total: int---# ────────────────────────────────────────────────────────────────────────────-# LLM helper-# ────────────────────────────────────────────────────────────────────────────--_SYS_PROMPT = textwrap.dedent(- """- You are a senior detection engineer reviewing a false-positive alert.- The user will provide:- - The Sigma rule that triggered the alert (YAML).- - Key fields from the alert that fired.- - An analyst note explaining why this is benign.-- Your task is to produce a *minimal, targeted* Sigma rule modification that- suppresses this class of false-positive without unduly widening the exclusion.- Prefer:- - Adding a `filter` condition rather than removing detection logic.- - Scoping exclusions to specific processes, users, or source paths when- the evidence supports it.- - Keeping the rule's ATT&CK technique tags unchanged.-- Respond in JSON only with this schema:- {- "rule_name": "...", // may append '-v2' or '-fp-fix'- "sigma_yaml": "...", // full updated Sigma YAML- "rationale": "..." // ≤ 3 sentences explaining the change- }- """-).strip()---async def _llm_draft_sigma(- current_sigma: str,- alert_fields: dict[str, Any],- analyst_note: str,-) -> dict[str, Any]:- """Call LLM to draft a Sigma improvement. Returns parsed JSON or template."""- api_key = getattr(settings, "OPENAI_API_KEY", None) or getattr(settings, "LLM_API_KEY", None)- if not api_key:- return _template_fallback(current_sigma, alert_fields, analyst_note)-- user_msg = json.dumps(- {- "current_sigma_rule": current_sigma,- "alert_fields": alert_fields,- "analyst_note": analyst_note,- },- indent=2,- )- try:- # T2.3 — `user_msg` embeds `alert_fields`, i.e. raw alert data, which- # is the shape the contract exists to stop leaving the deployment.- body = await safe_chat_completions_request(- api_key=api_key,- # The gateway alias, not a hosted model id. Hardcoding- # `gpt-4o-mini` meant this route could only work against a- # hosted provider: on CORE it reaches LiteLLM, which knows the- # `aisoc-*` aliases and not that id, so every call 400'd with- # `Invalid model name` and fell through to the deterministic- # path. In air-gap mode there is no hosted provider at all.- model=resolve_model_alias("detection"),- messages=[- {"role": "system", "content": _SYS_PROMPT},- {"role": "user", "content": user_msg},- ],- timeout=30.0,- response_format={"type": "json_object"},- )- return json.loads(body["choices"][0]["message"]["content"])- except LLMContractViolation as exc:- # %-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)---def _template_fallback(current_sigma: str, alert_fields: dict[str, Any], analyst_note: str) -> dict[str, Any]:- """Return a structured template when no LLM key is configured."""- proc = alert_fields.get("process_name") or alert_fields.get("Image") or "UNKNOWN"- user = alert_fields.get("user") or alert_fields.get("User") or "UNKNOWN"- return {- "rule_name": "fp-exclusion-draft",- "sigma_yaml": textwrap.dedent(- f"""\- # AUTO-DRAFTED FP EXCLUSION — review before promoting- # Original analyst note: {analyst_note}- # Alert fields: {json.dumps(alert_fields, default=str)[:200]}- filter:- - process_name: '{proc}'- - User: '{user}'- condition: selection and not filter- """- ),- "rationale": (- f"Auto-generated exclusion for process '{proc}' / user '{user}'. "- "Review and tighten before promoting. "- "Analyst note: " + (analyst_note or "none provided")- ),- }---# ────────────────────────────────────────────────────────────────────────────-# In-memory store (replace with DB table in prod)-# ────────────────────────────────────────────────────────────────────────────--_SUGGESTIONS: dict[uuid.UUID, dict[str, Any]] = {}---# ────────────────────────────────────────────────────────────────────────────-# Endpoints-# ────────────────────────────────────────────────────────────────────────────--- "/suggest",- response_model=SuggestionResponse,- status_code=status.HTTP_201_CREATED,- summary="Draft Sigma improvement for a FP alert",-)-async def suggest_fp_fix(- body: SuggestRequest,- db: TenantDBSession,- user: Annotated[AuthUser, Depends(require_permission("rules:read"))],-) -> SuggestionResponse:- """Retrieve the triggering rule + alert evidence, then draft a Sigma improvement.-- Tenant isolation: the alert and rule lookups are scoped to ``user.tenant_id``- so a caller cannot pull another tenant's evidence by guessing an ``alert_id``.- ``TenantDBSession`` also sets the Postgres RLS context for defense in depth.- """- # 1. Load alert — scoped to caller's tenant. A cross-tenant alert_id 404s- # before any evidence or rule body is read.- row = await db.execute(- text("SELECT rule_id, evidence, tenant_id FROM aisoc_alerts WHERE id = :aid AND tenant_id = :tenant_id").bindparams(- aid=body.alert_id, tenant_id=user.tenant_id- )- )- alert_row = row.fetchone()- if not alert_row:- raise HTTPException(status_code=404, detail="Alert not found")-- rule_id = alert_row.rule_id- evidence: dict[str, Any] = alert_row.evidence or {}- # Trust the caller's tenant for downstream writes — never echo a value- # read from the database back into an authorization decision.- tenant_id: uuid.UUID = user.tenant_id-- # 2. Load rule body if available — also tenant-scoped so an alert in tenant- # A can never resolve a rule body owned by tenant B (e.g. via stale data).- current_sigma = "# Rule body not found\n"- if rule_id:- rule_row = await db.execute(- text("SELECT rule_body FROM aisoc_detection_rules WHERE id = :rid AND tenant_id = :tenant_id").bindparams(- rid=rule_id, tenant_id=user.tenant_id- )- )- rule_data = rule_row.fetchone()- if rule_data:- current_sigma = rule_data.rule_body-- # 3. Draft via LLM- draft = await _llm_draft_sigma(- current_sigma=current_sigma,- alert_fields=evidence,- analyst_note=body.analyst_note or "",- )-- suggestion_id = uuid.uuid4()- now = datetime.now(UTC)-- # 4. Auto-create a DAC proposal- proposal_id: uuid.UUID | None = None- try:- proposal_id = uuid.uuid4()- await db.execute(- text(- """- INSERT INTO detection_rule_proposals- (id, tenant_id, base_rule_id, name, description,- rule_language, rule_body, category, severity, confidence,- status, source, created_at, updated_at)- VALUES- (:id, :tid, :rid, :name, :desc,- 'sigma', :body, 'fp-fix', 'low', 70,- 'draft', 'detection-loop', :now, :now)- """- ).bindparams(- id=proposal_id,- tid=tenant_id,- rid=rule_id,- name=draft.get("rule_name", "fp-exclusion-draft"),- desc=draft.get("rationale", ""),- body=draft.get("sigma_yaml", ""),- now=now,- )- )- await db.commit()- except Exception:- proposal_id = None- await db.rollback()-- result = SuggestionResponse(- suggestion_id=suggestion_id,- alert_id=body.alert_id,- base_rule_id=rule_id,- draft_rule_name=draft.get("rule_name", "fp-exclusion-draft"),- draft_sigma_yaml=draft.get("sigma_yaml", ""),- rationale=draft.get("rationale", ""),- proposal_id=proposal_id,- created_at=now,- )- # Tag the in-memory record with the caller's tenant so list/detail reads- # can filter cross-tenant access. ``tenant_id`` is *not* part of the- # response schema — it is internal metadata used only for isolation.- stored = result.model_dump()- stored["tenant_id"] = tenant_id- _SUGGESTIONS[suggestion_id] = stored- return result--- "/suggestions",- response_model=SuggestionListResponse,- summary="List LLM-drafted Sigma suggestions",-)-async def list_suggestions(user: AuthUser) -> SuggestionListResponse:- """List suggestions drafted by *this* tenant only.-- Tenant isolation: ``_SUGGESTIONS`` is process-wide and shared across all- tenants. Filtering on the stored ``tenant_id`` ensures one tenant never- sees another tenant's drafts, rule names, or evidence-derived rationale.- """- items = [- SuggestionResponse(**{k: v for k, v in stored.items() if k != "tenant_id"})- for stored in _SUGGESTIONS.values()- if str(stored.get("tenant_id")) == str(user.tenant_id)- ]- return SuggestionListResponse(suggestions=items, total=len(items))--- "/suggestions/{suggestion_id}",- response_model=SuggestionResponse,- summary="Get one Sigma suggestion",-)-async def get_suggestion(- suggestion_id: uuid.UUID,- user: AuthUser,-) -> SuggestionResponse:- """Return one suggestion if and only if it belongs to the caller's tenant.-- Tenant isolation: a cross-tenant lookup returns 404 (not 403) to avoid- leaking the existence of a suggestion that belongs to another tenant.- """- item = _SUGGESTIONS.get(suggestion_id)- if not item or str(item.get("tenant_id")) != str(user.tenant_id):- raise HTTPException(status_code=404, detail="Suggestion not found")- return SuggestionResponse(**{k: v for k, v in item.items() if k != "tenant_id"})diff --git a/services/api/app/api/v1/endpoints/detection_proposals.py b/services/api/app/api/v1/endpoints/detection_proposals.pyindex fbdcff2f5..bf2665d9e 100644--- a/services/api/app/api/v1/endpoints/detection_proposals.py+++ b/services/api/app/api/v1/endpoints/detection_proposals.py@@ -387,6 +387,30 @@ def _evaluate_eval_report(# ────────────────────────────────────────────────────────────────────────────+def body_has_detection_logic(rule_body: str | None) -> bool:+ """Whether this body could match anything at all.++ The auto-tuner opened proposals whose `rule_body` was **entirely+ comments** -- a header, a rationale and a `TODO(analyst)` -- with a null+ `base_rule_id`. A null base takes the "new rule" branch below, so an+ approver clicking through would have written a rule with no detection+ logic into the engine: a rule that matches nothing, published as though it+ detects something.++ Comment-stripping only. This deliberately does not try to validate Sigma:+ a parser here would be a second opinion that can disagree with the+ compiler, and the question being asked is narrower than "is this valid",+ it is "is there anything here but prose".+ """+ if not rule_body:+ return False+ for line in rule_body.splitlines():+ stripped = line.strip()+ if stripped and not stripped.startswith("#"):+ return True+ return False++@router.get("", response_model=list[ProposalResponse])async def list_proposals(current_user: Annotated[AuthUser, Depends(require_permission("rules:read"))],@@ -1017,6 +1041,19 @@ async def promote_proposal(detail=f"Proposal must be approved before promotion (current status: {proposal.status})",)+ # Before either branch. An approved proposal whose body is all prose is a+ # rule that matches nothing, and promoting one publishes a detection that+ # cannot fire. The auto-tuner produced exactly that shape.+ if not body_has_detection_logic(proposal.rule_body):+ raise HTTPException(+ status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,+ detail=(+ "This proposal's body contains no detection logic, only comments. "+ "Promoting it would publish a rule that matches nothing. Attach a real "+ "rule body and fixtures before approving."+ ),+ )+if proposal.base_rule_id is not None:# Edit of an existing rule — update in place, bump version.rule_q = await db.execute(diff --git a/services/api/app/api/v1/endpoints/identity_timeline.py b/services/api/app/api/v1/endpoints/identity_timeline.pyindex be12bb09e..7fa17ebed 100644--- a/services/api/app/api/v1/endpoints/identity_timeline.py+++ b/services/api/app/api/v1/endpoints/identity_timeline.py@@ -122,9 +122,16 @@ async def build_timeline(alert_rows = await db.execute(text("""- SELECT id, created_at, severity, title, evidence, mitre_technique- FROM aisoc_alerts- WHERE tenant_id = :tenant_id+ -- `aisoc_alerts` exists in no migration, and neither did+ -- `evidence` or `mitre_technique` on the real table: the+ -- columns are `raw_event` and `mitre_techniques`. So this+ -- query could not run anywhere, and the identity timeline+ -- reported no alerts for every identity on every deployment.+ SELECT id, created_at, severity, title,+ raw_event AS evidence,+ mitre_techniques AS mitre_technique+ FROM alerts+ WHERE tenant_id = CAST(:tenant_id AS uuid)AND created_at BETWEEN :from_ts AND :to_tsAND (evidence::text ILIKE :patdiff --git a/services/api/app/api/v1/endpoints/rule_tuning.py b/services/api/app/api/v1/endpoints/rule_tuning.pyindex a0375aac3..2de942b01 100644--- a/services/api/app/api/v1/endpoints/rule_tuning.py+++ b/services/api/app/api/v1/endpoints/rule_tuning.py@@ -79,7 +79,12 @@ async def auto_suggest_tuning(current_user: Annotated[AuthUser, Depends(require_permission("rules:write"))],db: TenantDBSession,window_days: int = Query(default=30, ge=1, le=180),- create_proposals: bool = Query(default=True),+ # Default off. A stub proposal is only dangerous once it is in the+ # governed queue, and this defaulted to True, so calling the endpoint+ # at all seeded the queue with comment-only bodies nobody intended to+ # promote. `/decide` now refuses such a body outright; this stops them+ # being created in the first place.+ create_proposals: bool = Query(default=False),) -> AutoSuggestResponse:"""Derive human-approval-gated tuning suggestions from disposition history.diff --git a/services/api/app/api/v1/router.py b/services/api/app/api/v1/router.pyindex 0b970a239..693c1f1c3 100644--- a/services/api/app/api/v1/router.py+++ b/services/api/app/api/v1/router.py@@ -27,7 +27,6 @@ from app.api.v1.endpoints import (data_lifecycle,deployment,detection_compat,- detection_loop,detection_proposals,detection_rules,easm,@@ -255,7 +254,6 @@ api_router.include_router(business_context.router)api_router.include_router(nl_detection.router)# Closed-loop detection engineering: FP → LLM Sigma draft → DAC proposal (Tier 2)-api_router.include_router(detection_loop.router)# Natural-language query → ES|QL / SPL / KQL translation + execution (Tier 2)api_router.include_router(nl_query.router)diff --git a/services/api/app/data/marketplace/index.json b/services/api/app/data/marketplace/index.jsonindex 04d78d188..747fe4c41 100644--- a/services/api/app/data/marketplace/index.json+++ b/services/api/app/data/marketplace/index.json@@ -1,7 +1,7 @@{"$schema": "https://example.com/schemas/marketplace/v1.json","version": "1.0.0",- "generated": "2026-10-03T15:14:21Z",+ "generated": "2026-10-04T15:38:08Z","categories": [{"id": "playbooks",@@ -236777,7 +236777,7 @@"id": "phishing-investigation-v1","type": "playbook","name": "Phishing: Email Investigation & Remediation",- "description": "Triggered by a phishing alert from email gateway or user report. Extracts IOCs, retracts the message fleet-wide, blocks sender domain, and resets any credentials that may have been compromised.",+ "description": "Triggered by a phishing alert from an email gateway or a user report. Extracts IOCs, blocks the sender domain, and resets credentials that may have been compromised. The retraction step requires EMAIL_GATEWAY_URL, which no compose file or .env.example sets, so on a default deployment the message is NOT retracted and the step is skipped.","version": "1.0.0","author": "Beenu - [email protected]","tags": [@@ -236799,7 +236799,7 @@"pack": true,"enabled": true,"path": "playbooks/packs/v1/phishing/phishing-investigation.playbook.json",- "sha256": "f68e72606a9ce930c2c0bfa33bff93fbce949fe5d0a8a6e5629319f65c0e8dec"+ "sha256": "73f354344458fe6ed95f28835b5cc40c045c67716f5c56f909b6df3b8406ef7a"},{"id": "phishing-response-v1",diff --git a/services/api/tests/test_detection_loop_tenant_isolation.py b/services/api/tests/test_detection_loop_tenant_isolation.pydeleted file mode 100644index 47d9a0fa0..000000000--- a/services/api/tests/test_detection_loop_tenant_isolation.py+++ /dev/null@@ -1,432 +0,0 @@-"""Tenant-isolation tests for /detection-loop (security fix).--Sibling of :mod:`tests.test_alerts_tenant_isolation` — exercises the-detection-loop endpoints exposed by-:mod:`app.api.v1.endpoints.detection_loop` and asserts that the-cross-tenant attack vectors closed by the security fix stay closed.--The contract being protected------------------------------* ``POST /detection-loop/suggest`` must scope the **alert lookup** and- the **rule body lookup** on the caller's ``tenant_id``. A cross-tenant- ``alert_id`` must 404 *before* any evidence or rule body is read, and- no detection-rule-proposal row may be inserted.-* The auto-created ``detection_rule_proposals`` row must bind the- **caller's** ``tenant_id`` — never one derived from a database read,- so a poisoned ``aisoc_alerts.tenant_id`` cannot redirect the write.-* ``GET /detection-loop/suggestions`` and- ``GET /detection-loop/suggestions/{id}`` operate over a process-wide- in-memory ``_SUGGESTIONS`` dict. Filtering on the stored ``tenant_id``- must hide other tenants' drafts. A cross-tenant GET-by-id must 404- (not 403) so we don't leak existence.-* The ``tenant_id`` we tag onto the in-memory record is *internal- metadata* — it must not appear in any response body.--These tests call the endpoint functions directly with a mocked-:class:`AsyncSession`, the same pattern used in-:mod:`tests.test_alerts_tenant_isolation`.-"""--from __future__ import annotations--import re-import uuid-from typing import Any-from unittest.mock import AsyncMock, MagicMock--import pytest-from app.api.v1.deps import CurrentUser-from app.api.v1.endpoints import detection_loop as detection_loop_module-from app.api.v1.endpoints.detection_loop import (- SuggestionResponse,- SuggestRequest,- get_suggestion,- list_suggestions,- suggest_fp_fix,-)-from fastapi import HTTPException--# ────────────────────────────────────────────────────────────────────────────-# Fixtures / helpers-# ────────────────────────────────────────────────────────────────────────────---def _user(tenant_id: uuid.UUID | None = None) -> CurrentUser:- """Build a CurrentUser without touching JWT plumbing or the DB."""- return CurrentUser(- user_id=uuid.uuid4(),- tenant_id=tenant_id or uuid.uuid4(),- role="soc_analyst",- email="[email protected]",- )---def _alert_row(tenant_id: uuid.UUID, rule_id: uuid.UUID | None = None, evidence: dict[str, Any] | None = None) -> MagicMock:- """Build a fake ``aisoc_alerts`` row exposing the columns the endpoint reads."""- row = MagicMock()- row.rule_id = rule_id- row.evidence = evidence or {"process_name": "powershell.exe", "User": "svc-ci"}- row.tenant_id = tenant_id- return row---def _rule_row(body: str = "title: Stub\nlogsource:\n product: windows\n") -> MagicMock:- row = MagicMock()- row.rule_body = body- return row---def _mk_db(rows: list[Any]) -> MagicMock:- """Mock AsyncSession that returns queued rows one execute() at a time.-- Mirrors the helper in ``test_alerts_tenant_isolation`` so SQL + bind- parameter assertions work the same way: each ``execute(clause)`` call- pushes ``(str(clause), clause.compile().params)`` onto ``db.executed``.-- The queued payloads serve as ``result.fetchone()`` returns — the- detection-loop endpoint uses ``row.fetchone()`` rather than- ``scalars().all()``, so the mock returns the raw payload (``None`` for- the cross-tenant 404 path).- """- db = MagicMock()- db.executed: list[tuple[str, dict[str, Any]]] = []- iterator = iter(rows)-- async def _execute(clause: Any, *args: Any, **kwargs: Any) -> MagicMock:- sql = str(clause)- try:- params = dict(clause.compile().params) if hasattr(clause, "compile") else {}- except Exception: # pragma: no cover — defensive: never crash the mock.- params = {}- db.executed.append((sql, params))- try:- payload = next(iterator)- except StopIteration:- payload = None- result = MagicMock()- result.fetchone = MagicMock(return_value=payload)- return result-- db.execute = AsyncMock(side_effect=_execute)- db.commit = AsyncMock()- db.rollback = AsyncMock()- return db---def _find_select(executed: list[tuple[str, dict[str, Any]]], table: str) -> tuple[str, dict[str, Any]] | None:- """Return the first executed SELECT against ``table`` (case-insensitive), or None."""- for sql, params in executed:- normalized = re.sub(r"\s+", " ", sql).lower()- if normalized.startswith("select") and table.lower() in normalized:- return sql, params- return None---def _find_insert(executed: list[tuple[str, dict[str, Any]]], table: str) -> tuple[str, dict[str, Any]] | None:- """Return the first executed INSERT into ``table``, or None."""- for sql, params in executed:- normalized = re.sub(r"\s+", " ", sql).lower()- if "insert into" in normalized and table.lower() in normalized:- return sql, params- return None--[email protected](autouse=True)-def _clear_suggestions_store() -> Any:- """The ``_SUGGESTIONS`` dict is process-wide; reset it per test."""- detection_loop_module._SUGGESTIONS.clear()- yield- detection_loop_module._SUGGESTIONS.clear()---def _stub_llm(monkeypatch: pytest.MonkeyPatch) -> Any:- """Replace the LLM call so tests stay hermetic and fast.-- The endpoint dispatches to ``_llm_draft_sigma`` which, in the- template-fallback path, would still need ``alert_fields`` to be a- real dict. Returning the canned dict here keeps the assertion- surface focused on tenant scoping rather than LLM contract drift.- """- canned = {- "rule_name": "fp-exclusion-draft",- "sigma_yaml": "filter:\n - process_name: 'powershell.exe'\ncondition: selection and not filter\n",- "rationale": "Auto-generated exclusion for the FP analyst flagged.",- }- monkeypatch.setattr(- detection_loop_module,- "_llm_draft_sigma",- AsyncMock(return_value=canned),- )- return canned---# ────────────────────────────────────────────────────────────────────────────-# POST /detection-loop/suggest-# ────────────────────────────────────────────────────────────────────────────---async def test_suggest_cross_tenant_alert_id_returns_404(_stub_llm: dict[str, Any]) -> None:- """A caller in tenant A cannot pull tenant B's alert evidence by id.-- Pre-fix: ``SELECT ... WHERE id = :aid`` returned the row regardless- of tenant, then the endpoint echoed ``evidence`` (and a rule_id- that resolved against ``aisoc_detection_rules`` cross-tenant).-- Post-fix: the SELECT is scoped to ``user.tenant_id``, so a- cross-tenant id returns no row and we 404 before any other read- or any proposal INSERT runs.- """- tenant_a = _user()- # SELECT scoped to tenant A's id returns nothing — the row exists in- # tenant B but our WHERE clause now filters it out.- db = _mk_db([None])-- with pytest.raises(HTTPException) as exc:- await suggest_fp_fix(- body=SuggestRequest(alert_id=uuid.uuid4()),- db=db,- user=tenant_a,- )-- assert exc.value.status_code == 404-- # The alert SELECT must have been tenant-scoped.- alert_select = _find_select(db.executed, "aisoc_alerts")- assert alert_select is not None, "expected a SELECT against aisoc_alerts"- sql, params = alert_select- assert "tenant_id" in re.sub(r"\s+", " ", sql).lower(), f"alert SELECT not tenant-scoped: {sql}"- assert params.get("tenant_id") == tenant_a.tenant_id, (- f"alert SELECT did not bind the caller's tenant_id; params={params}; expected={tenant_a.tenant_id}"- )-- # Critical: no rule lookup and no proposal INSERT must have run.- assert _find_select(db.executed, "aisoc_detection_rules") is None, "rule body lookup must not run when the alert lookup 404s"- assert _find_insert(db.executed, "detection_rule_proposals") is None, "no proposal must be inserted when the alert lookup 404s"- # And nothing must land in the in-memory store.- assert detection_loop_module._SUGGESTIONS == {}---async def test_suggest_same_tenant_scopes_alert_rule_and_proposal(_stub_llm: dict[str, Any]) -> None:- """Same-tenant suggest: alert + rule SELECTs and the proposal INSERT all bind the caller's tenant."""- user = _user()- rule_id = uuid.uuid4()- alert = _alert_row(user.tenant_id, rule_id=rule_id)- rule = _rule_row()- # Three executes: alert SELECT, rule SELECT, proposal INSERT.- db = _mk_db([alert, rule, None])-- response = await suggest_fp_fix(- body=SuggestRequest(alert_id=uuid.uuid4(), analyst_note="benign automation"),- db=db,- user=user,- )-- assert isinstance(response, SuggestionResponse)- assert response.base_rule_id == rule_id-- # Alert SELECT — tenant-scoped on caller's tenant.- alert_select = _find_select(db.executed, "aisoc_alerts")- assert alert_select is not None- sql, params = alert_select- assert "tenant_id" in re.sub(r"\s+", " ", sql).lower()- assert params.get("tenant_id") == user.tenant_id-- # Rule SELECT — also tenant-scoped on caller's tenant.- rule_select = _find_select(db.executed, "aisoc_detection_rules")- assert rule_select is not None, "expected a SELECT against aisoc_detection_rules"- sql, params = rule_select- assert "tenant_id" in re.sub(r"\s+", " ", sql).lower()- assert params.get("tenant_id") == user.tenant_id-- # Proposal INSERT — the ``tid`` bind must equal the *caller's* tenant.- proposal_insert = _find_insert(db.executed, "detection_rule_proposals")- assert proposal_insert is not None, "expected an INSERT into detection_rule_proposals"- _sql, params = proposal_insert- assert params.get("tid") == user.tenant_id, (- f"proposal INSERT did not bind the caller's tenant; params={params}; expected={user.tenant_id}"- )-- # The in-memory record must be tagged with the caller's tenant- # (used by list/get for cross-tenant filtering) and must hold the- # full response.- stored = detection_loop_module._SUGGESTIONS[response.suggestion_id]- assert stored["tenant_id"] == user.tenant_id---async def test_suggest_proposal_insert_binds_caller_tenant_not_db_row_tenant(_stub_llm: dict[str, Any]) -> None:- """Defence in depth: even if a poisoned alert row leaks past RLS, the proposal binds caller's tenant.-- The fix replaces ``tid=alert_row.tenant_id`` with ``tid=user.tenant_id``.- This test forges an alert row whose ``tenant_id`` differs from the- caller's (a hypothetical "RLS bypassed" scenario), confirms the- endpoint still reaches the INSERT (because we mocked the SELECT to- return the row), and asserts the INSERT binds the *caller's*- tenant — not the value read from the database.- """- user = _user()- poisoned_row_tenant = uuid.uuid4()- rule_id = uuid.uuid4()- forged_alert = _alert_row(poisoned_row_tenant, rule_id=rule_id)- rule = _rule_row()- db = _mk_db([forged_alert, rule, None])-- response = await suggest_fp_fix(- body=SuggestRequest(alert_id=uuid.uuid4()),- db=db,- user=user,- )-- proposal_insert = _find_insert(db.executed, "detection_rule_proposals")- assert proposal_insert is not None- _sql, params = proposal_insert- # The critical assertion: never trust a value we read back from the DB- # for an authorization decision on a write.- assert params.get("tid") == user.tenant_id- assert params.get("tid") != poisoned_row_tenant-- # And the in-memory tag also follows the caller, not the row.- stored = detection_loop_module._SUGGESTIONS[response.suggestion_id]- assert stored["tenant_id"] == user.tenant_id---async def test_suggest_alert_without_rule_id_skips_rule_select(_stub_llm: dict[str, Any]) -> None:- """If the alert has no ``rule_id`` we must not blindly query the rules table.-- Guards against a regression where someone adds a tenant_id-less- rule lookup back. Also locks down that the proposal INSERT still- fires (with ``base_rule_id=NULL``) so suggest_fp_fix remains usable- for orphan alerts.- """- user = _user()- alert = _alert_row(user.tenant_id, rule_id=None)- # Only two executes: alert SELECT and proposal INSERT.- db = _mk_db([alert, None])-- response = await suggest_fp_fix(- body=SuggestRequest(alert_id=uuid.uuid4()),- db=db,- user=user,- )-- assert response.base_rule_id is None- assert _find_select(db.executed, "aisoc_detection_rules") is None- proposal_insert = _find_insert(db.executed, "detection_rule_proposals")- assert proposal_insert is not None- assert proposal_insert[1].get("tid") == user.tenant_id---# ────────────────────────────────────────────────────────────────────────────-# GET /detection-loop/suggestions-# ────────────────────────────────────────────────────────────────────────────---def _seed_suggestion(tenant_id: uuid.UUID, rule_name: str = "fp-fix-draft") -> uuid.UUID:- """Insert a stub row directly into ``_SUGGESTIONS`` for list/get tests."""- sid = uuid.uuid4()- from datetime import UTC, datetime # noqa: PLC0415 — keep helper hermetic.-- detection_loop_module._SUGGESTIONS[sid] = {- "suggestion_id": sid,- "alert_id": uuid.uuid4(),- "base_rule_id": uuid.uuid4(),- "draft_rule_name": rule_name,- "draft_sigma_yaml": "filter:\n - User: 'svc-ci'\n",- "rationale": "test",- "proposal_id": uuid.uuid4(),- "created_at": datetime.now(UTC),- "tenant_id": tenant_id,- }- return sid---async def test_list_suggestions_only_returns_caller_tenant() -> None:- """A list call must return only the caller-tenant's suggestions, never others'."""- tenant_a = _user()- tenant_b = _user()- a1 = _seed_suggestion(tenant_a.tenant_id, "a-1")- a2 = _seed_suggestion(tenant_a.tenant_id, "a-2")- # Seed a tenant-B suggestion solely to prove it doesn't leak into tenant A's- # list response below; we never need its id here.- _seed_suggestion(tenant_b.tenant_id, "b-1")-- resp = await list_suggestions(user=tenant_a)-- assert resp.total == 2- returned_ids = {item.suggestion_id for item in resp.suggestions}- assert returned_ids == {a1, a2}, "tenant A's list returned the wrong set — possible cross-tenant leak"---async def test_list_suggestions_does_not_leak_internal_tenant_id_field() -> None:- """The internal ``tenant_id`` tag must not appear in the response body."""- user = _user()- _seed_suggestion(user.tenant_id)-- resp = await list_suggestions(user=user)-- assert resp.total == 1- item = resp.suggestions[0]- # ``SuggestionResponse`` does not declare ``tenant_id`` and pydantic- # will not expose attributes that aren't declared. Verify defensively- # in case someone adds the field to the schema later.- assert not hasattr(item, "tenant_id"), "internal tenant_id metadata must not be part of the API response"- dumped = item.model_dump()- assert "tenant_id" not in dumped---async def test_list_suggestions_empty_when_no_caller_tenant_rows() -> None:- """A caller in a fresh tenant must see an empty list — not other tenants' work."""- tenant_a = _user()- other = _user()- _seed_suggestion(other.tenant_id, "other-1")- _seed_suggestion(other.tenant_id, "other-2")-- resp = await list_suggestions(user=tenant_a)-- assert resp.total == 0- assert resp.suggestions == []---# ────────────────────────────────────────────────────────────────────────────-# GET /detection-loop/suggestions/{id}-# ────────────────────────────────────────────────────────────────────────────---async def test_get_suggestion_cross_tenant_returns_404() -> None:- """Looking up another tenant's suggestion by id must 404 — not 403, not 200."""- tenant_a = _user()- tenant_b = _user()- target = _seed_suggestion(tenant_b.tenant_id, "b-only")-- with pytest.raises(HTTPException) as exc:- await get_suggestion(suggestion_id=target, user=tenant_a)-- # 404 (not 403) avoids leaking the existence of suggestions owned- # by other tenants.- assert exc.value.status_code == 404---async def test_get_suggestion_unknown_id_returns_404() -> None:- """A non-existent suggestion id must 404, not 500 or 200."""- user = _user()- # Store contains rows for *other* tenants — exercise the "key missing"- # branch separately from the "wrong tenant" branch.- _seed_suggestion(uuid.uuid4())-- with pytest.raises(HTTPException) as exc:- await get_suggestion(suggestion_id=uuid.uuid4(), user=user)-- assert exc.value.status_code == 404---async def test_get_suggestion_same_tenant_returns_item() -> None:- """A caller can read its own suggestion and the response excludes ``tenant_id``."""- user = _user()- sid = _seed_suggestion(user.tenant_id, "mine")-- resp = await get_suggestion(suggestion_id=sid, user=user)-- assert resp.suggestion_id == sid- assert resp.draft_rule_name == "mine"- dumped = resp.model_dump()- assert "tenant_id" not in dumpeddiff --git a/services/api/tests/test_detection_tuning_is_not_promotable_as_a_stub.py b/services/api/tests/test_detection_tuning_is_not_promotable_as_a_stub.pynew file mode 100644index 000000000..bb9d80dc6--- /dev/null+++ b/services/api/tests/test_detection_tuning_is_not_promotable_as_a_stub.py@@ -0,0 +1,99 @@+"""A proposal with no detection logic cannot be promoted into the engine.++Fix pass item 6.5. See `plans/aisoc_fix_pass_plan.plan.md`.++What was wrong+--------------+`POST /api/v1/detection/tuning/auto-suggest` opens proposals in+`detection_rule_proposals` whose `rule_body` is **entirely comments**:++ # Auto-tuner proposal for rule <id>+ # Action: <action>+ # <rationale>+ # FP rate 40% over 25 decided alerts+ # TODO(analyst): review + attach fixtures before promotion.++and whose `base_rule_id` is `NULL`. A null base takes the "new rule" branch in+`POST /detection-proposals/{id}/decide`, so an approver clicking through would+**write a rule with no detection logic into the engine** -- a rule that matches+nothing, published as though it detects something.++It also defaulted `create_proposals=True`, so simply calling the endpoint+seeded the governed queue with stubs.++What this file asserts+----------------------+The guard, on the real helper: a body whose every non-blank line is a comment+is refused, and a body with real logic is not. Plus the default on the query+parameter, because a queue that fills itself with stubs is the thing that made+the first defect reachable.+"""++from __future__ import annotations++import inspect++import pytest+++class TestABodyWithNoLogicIsRefused:+ @pytest.mark.parametrize(+ "body",+ [+ "# Auto-tuner proposal for rule abc\n# Action: narrow\n# TODO(analyst): review\n",+ " \n# only a comment\n\n",+ "",+ " ",+ "#\n#\n#\n",+ ],+ )+ def test_a_comment_only_body_carries_no_detection_logic(self, body: str) -> None:+ from app.api.v1.endpoints.detection_proposals import body_has_detection_logic++ assert body_has_detection_logic(body) is False, f"{body!r} was treated as a promotable rule"++ @pytest.mark.parametrize(+ "body",+ [+ "detection:\n selection:\n EventID: 4625\n condition: selection\n",+ "# a comment, then logic\ntitle: Failed logon\ndetection:\n condition: selection\n",+ ],+ )+ def test_a_body_with_real_logic_is_promotable(self, body: str) -> None:+ """The negative control. A guard that refused everything would make the+ whole proposal surface unusable, which is a worse outcome than the+ defect."""+ from app.api.v1.endpoints.detection_proposals import body_has_detection_logic++ assert body_has_detection_logic(body) is True+++class TestTheAutoTunerDoesNotSeedTheQueueByDefault:+ def test_create_proposals_defaults_to_false(self) -> None:+ """A stub proposal is only dangerous once it is in the queue.++ The parameter defaulted to `True`, so calling the endpoint at all+ filled the governed queue with bodies nobody intended to promote.+ """+ from app.api.v1.endpoints.rule_tuning import auto_suggest_tuning++ default = inspect.signature(auto_suggest_tuning).parameters["create_proposals"].default+ actual = getattr(default, "default", default)++ assert actual is False, f"create_proposals defaults to {actual!r}, so the queue seeds itself"+++class TestTheDecideRouteConsultsTheGuard:+ def test_promotion_checks_for_detection_logic(self) -> None:+ """Read as source, deliberately, and the reason is narrow.++ Driving the promotion branch needs an approved proposal, a tenant+ session and a rule row, which is the live suite's job. What can be+ pinned cheaply is that the promotion path calls the guard at all --+ the defect was that nothing did.+ """+ from app.api.v1.endpoints import detection_proposals++ source = inspect.getsource(detection_proposals)++ assert "body_has_detection_logic(" in source, "the promotion path does not consult the guard"diff --git a/services/api/tests/test_hunting_route_permissions.py b/services/api/tests/test_hunting_route_permissions.pyindex 57513280f..d6005e988 100644--- a/services/api/tests/test_hunting_route_permissions.py+++ b/services/api/tests/test_hunting_route_permissions.py@@ -35,7 +35,7 @@ from unittest.mock import AsyncMock, MagicMockimport pytestfrom app.api.v1.deps import CurrentUser, get_current_user-from app.api.v1.endpoints import detection_loop, graph, hunts, nl_detection, nl_query, saved_hunts, translation+from app.api.v1.endpoints import graph, hunts, nl_detection, nl_query, saved_hunts, translationfrom app.db.database import get_dbfrom fastapi import FastAPIfrom fastapi.testclient import TestClient@@ -73,7 +73,6 @@ GATED: list[tuple[Any, str, str, str, str, dict[str, Any] | None]] = ["/api/v1/translation/translate",{"rule": "title: x", "source_format": "sigma", "target_formats": ["spl"]},),- (detection_loop, "suggest_fp_fix", "rules:read", "POST", "/api/v1/detection-loop/suggest", {"alert_id": str(uuid.uuid4())}),]#: A role holding each permission. `soc_analyst` for `lake:query` on purpose:@@ -186,11 +185,6 @@ class TestUnentitledCallersAreRefused:status, _ = _request(graph, "viewer", "POST", "/api/v1/graph/investigate/query", {"tool": "host_processes", "args": {}})assert status == 403- def test_a_viewer_cannot_read_detection_logic_through_a_draft(self) -> None:- """`POST /detection-loop/suggest` returns the rule body it drafts from."""- status, _ = _request(detection_loop, "viewer", "POST", "/api/v1/detection-loop/suggest", {"alert_id": str(uuid.uuid4())})- assert status == 403-class TestEntitledCallersStillWork:"""The half that catches a permission applied too strongly."""@@ -241,7 +235,7 @@ class TestWiring:assert _permissions_on(_route_for(module, handler)) == [permission]@pytest.mark.parametrize(- "module", [hunts, saved_hunts, nl_query, nl_detection, translation, detection_loop], ids=lambda m: m.__name__.rsplit(".", 1)[-1]+ "module", [hunts, saved_hunts, nl_query, nl_detection, translation], ids=lambda m: m.__name__.rsplit(".", 1)[-1])def test_no_state_changing_route_in_these_modules_is_unguarded(self, module: Any) -> None:"""These six are now fully gated, so the invariant is "all of them".diff --git a/services/api/tests/test_llm_safety.py b/services/api/tests/test_llm_safety.pyindex 63defab22..39335843a 100644--- a/services/api/tests/test_llm_safety.py+++ b/services/api/tests/test_llm_safety.py@@ -122,7 +122,6 @@ GUARDED_CALL_SITES = ("api/v1/endpoints/knowledge_base.py","api/v1/endpoints/hunts.py","api/v1/endpoints/nl_detection.py",- "api/v1/endpoints/detection_loop.py","services/alert_explain.py",)diff --git a/services/api/tests/test_tenant_scope_and_auth_holes.py b/services/api/tests/test_tenant_scope_and_auth_holes.pyindex ac25fb454..be36a22f4 100644--- a/services/api/tests/test_tenant_scope_and_auth_holes.py+++ b/services/api/tests/test_tenant_scope_and_auth_holes.py@@ -194,7 +194,7 @@ class TestPlaybookRoutesRequirePermission:class TestCaseTimelineAlertHydrationIsScoped:def test_linked_alert_lookup_binds_a_tenant(self) -> None:source = Path(cases_module.__file__).read_text()- matches = [ln for ln in source.splitlines() if "FROM aisoc_alerts WHERE id" in ln]+ matches = [ln for ln in source.splitlines() if "FROM alerts WHERE id" in ln]assert matches, "the linked-alert hydration query moved; re-point this test"for line in matches:assert "tenant_id" in line, f"unscoped alert read: {line.strip()}"