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.

feat(threatintel): Phase 1.3 Qdrant tenant isolation + isolation harness (#418)

Beenu Arora2e7157e7c2a8
diff --git a/.github/workflows/isolation.yml b/.github/workflows/isolation.yml
new file mode 100644
index 000000000..187bb5a4a
--- /dev/null
+++ b/.github/workflows/isolation.yml
@@ -0,0 +1,36 @@
+name: Tenant Isolation
+
+# Phase 1.3: cross-store tenant-isolation gate. The offline layer asserts every
+# datastore read path constructs a tenant scope (a search as tenant A can never
+# include tenant B). The live-container replay lands with Phase 3's integration
+# tier. Table-driven (tests/isolation/stores.py) so a new store cannot ship
+# without an isolation entry.
+
+on:
+ push:
+ branches: [main, develop]
+ paths:
+ - "tests/isolation/**"
+ - "services/threatintel/app/storage/qdrant.py"
+ pull_request:
+ paths:
+ - "tests/isolation/**"
+ - "services/threatintel/app/storage/qdrant.py"
+
+jobs:
+ isolation:
+ name: Cross-store tenant isolation (offline)
+ runs-on: ubuntu-latest
+
+ steps:
+ - uses: actions/checkout@v7
+
+ - uses: actions/setup-python@v6
+ with:
+ python-version: "3.12"
+
+ - name: Install deps
+ run: pip install --quiet qdrant-client structlog pytest pytest-asyncio
+
+ - name: Run isolation suite
+ run: python -m pytest tests/isolation -v --tb=short
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8c869fc8c..6976e2e28 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Security
+- **Phase 1.3 — cross-store tenant isolation (Qdrant + harness).** Closed the Qdrant leak the reality audit flagged: `services/threatintel/app/storage/qdrant.py` had no tenant scoping at all (global collections, no filter, no `tenant_id` in payloads). Added `tenant_scope_filter` + tenant-stamped payloads + tenant-scoped point ids so a search as tenant A can never surface tenant B's private vectors, while global feed intel stays shared under a `SHARED_TENANT` sentinel (backward-compatible with the feed pipeline). Stood up a table-driven `tests/isolation/` suite (registry in `stores.py` so a new store cannot ship without an isolation entry) and a new `.github/workflows/isolation.yml` gate running the offline layer on every PR. Neo4j/Redis/ClickHouse/Kafka live-container replay is registered as `container_pending` for Phase 3's integration tier.
+
- **Phase 1.2 — memory-poisoning defenses for the override-learning loop.** New pure `services/api/app/services/memory_poisoning.py`: provenance on every memory write (`MemoryProvenance` — no anonymous memory), trust weighting (verified human outranks autonomous closure) with age decay so lessons must be re-confirmed, a `PoisoningDetector` that flags a burst of same-signature false-positive dispositions from low-trust authors, and blast-radius controls for retroactive re-disposition (`plan_redisposition` + `compute_confirmation_token`: capped batches, explicit confirmation token over the exact alert set, quarantine on flagged signatures). Wired into `override_learning.py` (poisoning-resistant signature key now includes the entity-independent severity band; provenance stamped on writes; `apply_redisposition` requires the token and enforces the cap) and the `/feedback` endpoints (preview returns the token + quarantine state; apply rejects stale/tampered/over-cap batches with 409). The farming-attack eval (`services/api/tests/test_memory_poisoning.py::test_farming_attack_then_real_attack_is_not_auto_closed`) gates the api job: a poisoned signature is flagged and its retroactive apply quarantined, so the real intrusion is not auto-closed.
- **Phase 1.1 — prompt-injection structural containment + detection.** New `services/agents/app/prompting/envelope.py`: per-run cryptographic-nonce evidence fence (`EvidenceEnvelope`, `make_nonce`, `system_rule`) so injected text cannot forge the closing delimiter to break out of the data block, and a `PromptInjectionGuard` that scans untrusted evidence for instruction-shaped content (role markers, "ignore previous", secret/prompt exfiltration, SOAR tool-name mentions, base64- and zero-width-obfuscated directives) and, on a high-severity hit, signals demotion of the case autonomy tier to L0. Added `services/agents/tests/test_prompt_envelope.py` (25 tests across every ingest path) and gated it plus the previously-ungated `test_prompt_sanitizer.py` in the CI agents job (fixed its stale agent-wiring expectations — the investigator agents sanitise via `sanitize_text` / `sanitize_iterable_of_strings` / `format_bundle_prompt_append`). Threat model: `docs/security/agent-threat-model.md`.
diff --git a/docs/audit/CLAIM_TO_GATE_MATRIX.md b/docs/audit/CLAIM_TO_GATE_MATRIX.md
index d71113696..035dcdc97 100644
--- a/docs/audit/CLAIM_TO_GATE_MATRIX.md
+++ b/docs/audit/CLAIM_TO_GATE_MATRIX.md
@@ -27,7 +27,7 @@ Statuses: `GATED` (a CI job fails when the claim stops being true) · `PARTIAL`
| Plugin SDK Python/TS/Go | README L79, L193 | `ci.yml :: sdk-*` | PARTIAL (build/test gated; contract-drift vs `docs/openapi.yaml` ungated) | Phase 11 |
| Prompt-injection resistance | (implied by agent claims) | `ci.yml :: python-test` (agents) runs `test_prompt_sanitizer.py` + `test_prompt_envelope.py` | PARTIAL (unit-level nonce envelope + guard gated; 150-payload adversarial eval + tool-call provenance in Phase 4 Tier 2) | Phase 4 |
| Cross-tenant isolation (Postgres) | (implied by multi-tenant) | `cross-tenant-rbac.yml` (nightly, 3 endpoints) + `ci.yml` | PARTIAL (Postgres only, compiled-SQL not live DB) | Phase 1.3 |
-| Cross-tenant isolation (Qdrant/Neo4j/Redis/ClickHouse/Kafka) | (implied by multi-tenant) | none | NO GATE | Phase 1.3 |
+| Cross-tenant isolation (Qdrant/Neo4j/Redis/ClickHouse/Kafka) | (implied by multi-tenant) | `isolation.yml` (offline: Qdrant search always tenant-scoped + registry gate) | PARTIAL (Qdrant offline-gated; Neo4j/Redis/ClickHouse/Kafka live-container replay in Phase 3) | Phase 3 |
| SAST | README badge (CodeQL) | `codeql.yml` | GATED | - |
| Dependency CVE scanning | (implied by security) | `security-audit.yml` | GATED | - |
| OpenSSF Scorecard | README badge L13 | `scorecard.yml` | GATED | - |
@@ -39,7 +39,7 @@ Statuses: `GATED` (a CI job fails when the claim stops being true) · `PARTIAL`
## Summary
- GATED: 9
-- PARTIAL: 12
-- NO GATE: 6 (Phase 1.1 moved prompt-injection resistance from NO GATE to PARTIAL)
+- PARTIAL: 13
+- NO GATE: 5 (Phase 1.1 moved prompt-injection resistance to PARTIAL; Phase 1.3 moved non-Postgres tenant isolation to PARTIAL)
The Definition of Done requires zero `NO GATE` and zero unclosed `PARTIAL`. Each row's "Closes in" column is the binding commitment.
diff --git a/services/threatintel/app/storage/qdrant.py b/services/threatintel/app/storage/qdrant.py
index fa6a3fc17..48aa6b344 100644
--- a/services/threatintel/app/storage/qdrant.py
+++ b/services/threatintel/app/storage/qdrant.py
@@ -13,6 +13,9 @@ import structlog
from qdrant_client import AsyncQdrantClient
from qdrant_client.models import (
Distance,
+ FieldCondition,
+ Filter,
+ MatchAny,
PointStruct,
VectorParams,
)
@@ -23,6 +26,29 @@ _IOC_COLLECTION = "threatintel_iocs"
_ACTOR_COLLECTION = "threatintel_actors"
_EMBEDDING_DIM = 1536 # text-embedding-3-small / ada-002
+# Sentinel tenant for globally-shared feed intel (STIX/TAXII/MISP), which is
+# public and intentionally visible to every tenant. Tenant-submitted or private
+# intel is stored under the owning tenant id and is never returned to others.
+SHARED_TENANT = "shared"
+
+
+def tenant_scope_filter(tenant_id: str | None, *, include_shared: bool = True) -> Filter | None:
+ """Build the Qdrant payload filter that scopes a search to a tenant.
+
+ - ``tenant_id=None`` -> no filter (privileged/global read; callers must gate).
+ - ``tenant_id=X`` -> matches points owned by X, plus the shared feed intel
+ when ``include_shared`` (the common analyst case).
+
+ Every tenant-facing search path MUST pass a concrete ``tenant_id`` so a
+ query as tenant A can never surface tenant B's private vectors.
+ """
+ if tenant_id is None:
+ return None
+ allowed = [tenant_id]
+ if include_shared and tenant_id != SHARED_TENANT:
+ allowed.append(SHARED_TENANT)
+ return Filter(must=[FieldCondition(key="tenant_id", match=MatchAny(any=allowed))])
+
class QdrantStore:
"""
@@ -49,20 +75,24 @@ class QdrantStore:
)
logger.info("Created Qdrant collection", collection=col)
- async def upsert_iocs(self, iocs: list[dict[str, Any]]) -> None:
- """Embed and upsert IOC documents into Qdrant for semantic search."""
+ async def upsert_iocs(self, iocs: list[dict[str, Any]], *, tenant_id: str = SHARED_TENANT) -> None:
+ """Embed and upsert IOC documents into Qdrant for semantic search.
+
+ ``tenant_id`` defaults to :data:`SHARED_TENANT` (global feed intel). It
+ is stamped into every payload and folded into the point id so the same
+ IOC submitted by two tenants does not collide or overwrite.
+ """
if not iocs:
return
texts = [self._ioc_to_text(ioc) for ioc in iocs]
vectors = await self._embed(texts)
- # Use hash of value+type for stable integer IDs
points = [
PointStruct(
- id=abs(int(hashlib.md5(f"{ioc['type']}:{ioc['value']}".encode()).hexdigest(), 16)) % (2**63),
+ id=self._point_id(tenant_id, f"{ioc['type']}:{ioc['value']}"),
vector=vectors[idx],
- payload=ioc,
+ payload={**ioc, "tenant_id": tenant_id},
)
for idx, ioc in enumerate(iocs)
]
@@ -71,23 +101,21 @@ class QdrantStore:
collection_name=_IOC_COLLECTION,
points=points,
)
- logger.debug("Qdrant IOCs upserted", count=len(points))
+ logger.debug("Qdrant IOCs upserted", count=len(points), tenant_id=tenant_id)
- async def upsert_actors(self, actors: list[dict[str, Any]]) -> None:
- """Embed and upsert threat actor documents."""
+ async def upsert_actors(self, actors: list[dict[str, Any]], *, tenant_id: str = SHARED_TENANT) -> None:
+ """Embed and upsert threat actor documents (tenant-scoped payload + id)."""
if not actors:
return
texts = [f"{a.get('name', '')} {a.get('description', '')}" for a in actors]
vectors = await self._embed(texts)
- import hashlib
-
points = [
PointStruct(
- id=abs(int(hashlib.md5(actor.get("name", "").encode()).hexdigest(), 16)) % (2**63),
+ id=self._point_id(tenant_id, actor.get("name", "")),
vector=vectors[idx],
- payload=actor,
+ payload={**actor, "tenant_id": tenant_id},
)
for idx, actor in enumerate(actors)
]
@@ -102,16 +130,31 @@ class QdrantStore:
query: str,
collection: str = _IOC_COLLECTION,
limit: int = 10,
+ *,
+ tenant_id: str | None = None,
+ include_shared: bool = True,
) -> list[dict[str, Any]]:
- """Semantic similarity search over threat intel."""
+ """Semantic similarity search over threat intel, scoped to a tenant.
+
+ Tenant-facing callers MUST pass ``tenant_id`` so a search as tenant A
+ can never surface tenant B's private vectors. ``tenant_id=None`` is a
+ privileged/global read and callers are responsible for gating it.
+ """
vectors = await self._embed([query])
results = await self._client.search(
collection_name=collection,
query_vector=vectors[0],
limit=limit,
+ query_filter=tenant_scope_filter(tenant_id, include_shared=include_shared),
)
return [r.payload for r in results if r.payload]
+ @staticmethod
+ def _point_id(tenant_id: str, natural_key: str) -> int:
+ """Stable, tenant-scoped 63-bit point id (no cross-tenant collisions)."""
+ raw = f"{tenant_id}:{natural_key}".encode()
+ return abs(int(hashlib.md5(raw, usedforsecurity=False).hexdigest(), 16)) % (2**63)
+
# ─── Private helpers ──────────────────────────────────────────────────────
async def _embed(self, texts: list[str]) -> list[list[float]]:
diff --git a/tests/isolation/README.md b/tests/isolation/README.md
new file mode 100644
index 000000000..a934a5d35
--- /dev/null
+++ b/tests/isolation/README.md
@@ -0,0 +1,28 @@
+# Cross-store tenant-isolation suite
+
+Phase 1.3 of the world-class program. Postgres row-level security covers one of
+six datastores; every other store is a potential silent cross-tenant leak. This
+suite is table-driven (see `stores.py`) so a new datastore or a new read path
+cannot ship without an isolation entry.
+
+## Two layers
+
+1. **Offline (gated on every PR, `.github/workflows/isolation.yml`).** Asserts
+ that each store's read path *constructs* a tenant scope — e.g. that
+ `QdrantStore.semantic_search` always passes a tenant filter, that a search as
+ tenant A can never include tenant B in its filter, and that writes stamp
+ `tenant_id` with non-colliding, tenant-scoped ids. These run without any live
+ datastore by mocking the client.
+2. **Live-container replay (Phase 3 `integration.yml`).** Seeds tenant A + B in
+ real containers (Qdrant, Neo4j, Redis, ClickHouse, Kafka) and asserts a read
+ as A returns zero B rows/vectors/nodes/keys. Promoted to a required check on
+ `main` when the integration tier lands.
+
+## Coverage
+
+See `stores.py::STORES`. Each store is one of:
+
+- `offline_gated` — query-construction isolation asserted here, every PR.
+- `rls` — enforced by Postgres RLS + query-layer filters (tested in
+ `services/api/tests/test_*_tenant_isolation.py`).
+- `container_pending` — needs the live-container replay in Phase 3.
diff --git a/tests/isolation/stores.py b/tests/isolation/stores.py
new file mode 100644
index 000000000..9e94ef41e
--- /dev/null
+++ b/tests/isolation/stores.py
@@ -0,0 +1,53 @@
+"""Registry of datastores and their tenant-isolation coverage.
+
+Table-driven so a new store or read path cannot ship without an entry. The
+registry test (`test_registry.py`) fails if any store is left `unset`.
+"""
+
+from __future__ import annotations
+
+from dataclasses import dataclass
+
+
+@dataclass(frozen=True)
+class StoreCoverage:
+ name: str
+ # one of: offline_gated | rls | container_pending | unset
+ status: str
+ note: str
+
+
+STORES: tuple[StoreCoverage, ...] = (
+ StoreCoverage(
+ "postgres",
+ "rls",
+ "migrations/002_rls.sql + query-layer WHERE tenant_id; services/api/tests/test_*_tenant_isolation.py",
+ ),
+ StoreCoverage(
+ "qdrant",
+ "offline_gated",
+ "tests/isolation/test_qdrant_isolation.py — search always tenant-scoped; writes stamp tenant_id",
+ ),
+ StoreCoverage(
+ "neo4j",
+ "container_pending",
+ "write-time tenant_id tagging exists (ingest/internal/graph/writer.go); live-replay in Phase 3",
+ ),
+ StoreCoverage(
+ "clickhouse",
+ "container_pending",
+ "lake_sql.rewrite_for_tenant injects tenant predicate; live-replay in Phase 3",
+ ),
+ StoreCoverage(
+ "redis",
+ "container_pending",
+ "cache-key namespacing; live-replay in Phase 3",
+ ),
+ StoreCoverage(
+ "kafka",
+ "container_pending",
+ "topic/consumer-group scoping; live-replay in Phase 3",
+ ),
+)
+
+VALID_STATUSES = {"offline_gated", "rls", "container_pending"}
diff --git a/tests/isolation/test_qdrant_isolation.py b/tests/isolation/test_qdrant_isolation.py
new file mode 100644
index 000000000..60903c975
--- /dev/null
+++ b/tests/isolation/test_qdrant_isolation.py
@@ -0,0 +1,109 @@
+"""Offline Qdrant tenant-isolation assertions (Phase 1.3).
+
+Loads the QdrantStore module by path (self-contained; only needs qdrant-client)
+and mocks the async client, so the isolation contract is asserted with no live
+Qdrant. The live-container replay lands with Phase 3's integration tier.
+"""
+
+from __future__ import annotations
+
+import importlib.util
+from pathlib import Path
+from unittest.mock import AsyncMock, MagicMock
+
+import pytest
+
+_QDRANT_PATH = Path(__file__).resolve().parents[2] / "services" / "threatintel" / "app" / "storage" / "qdrant.py"
+_spec = importlib.util.spec_from_file_location("aisoc_qdrant_store_under_test", _QDRANT_PATH)
+assert _spec and _spec.loader
+_qdrant = importlib.util.module_from_spec(_spec)
+_spec.loader.exec_module(_qdrant)
+
+QdrantStore = _qdrant.QdrantStore
+tenant_scope_filter = _qdrant.tenant_scope_filter
+SHARED_TENANT = _qdrant.SHARED_TENANT
+
+
+def _match_any_values(f) -> set[str]:
+ """Pull the allowed tenant_id values out of a tenant scope Filter."""
+ assert f is not None
+ cond = f.must[0]
+ assert cond.key == "tenant_id"
+ return set(cond.match.any)
+
+
+# ── Filter construction ──────────────────────────────────────────────────────
+
+
+def test_none_tenant_is_unfiltered_privileged_read():
+ assert tenant_scope_filter(None) is None
+
+
+def test_tenant_filter_includes_self_and_shared_only():
+ values = _match_any_values(tenant_scope_filter("tenant-A"))
+ assert values == {"tenant-A", SHARED_TENANT}
+ assert "tenant-B" not in values # the core isolation property
+
+
+def test_tenant_filter_can_exclude_shared():
+ values = _match_any_values(tenant_scope_filter("tenant-A", include_shared=False))
+ assert values == {"tenant-A"}
+
+
+# ── Search is always tenant-scoped ───────────────────────────────────────────
+
+
+async def test_search_as_A_cannot_include_B_in_its_filter():
+ client = MagicMock()
+ client.search = AsyncMock(return_value=[])
+ store = QdrantStore(client)
+
+ await store.semantic_search(" transfer to attacker", tenant_id="tenant-A")
+
+ client.search.assert_awaited_once()
+ query_filter = client.search.await_args.kwargs["query_filter"]
+ allowed = _match_any_values(query_filter)
+ assert "tenant-A" in allowed
+ assert "tenant-B" not in allowed # A can never read B
+
+
+async def test_search_without_tenant_is_flagged_as_privileged():
+ client = MagicMock()
+ client.search = AsyncMock(return_value=[])
+ store = QdrantStore(client)
+ await store.semantic_search("q", tenant_id=None)
+ assert client.search.await_args.kwargs["query_filter"] is None
+
+
+# ── Writes stamp tenant + avoid cross-tenant collisions ──────────────────────
+
+
+async def test_upsert_stamps_tenant_and_scopes_point_ids():
+ client = MagicMock()
+ client.upsert = AsyncMock()
+ store = QdrantStore(client)
+
+ ioc = {"type": "ip", "value": "203.0.113.5", "description": "c2"}
+ await store.upsert_iocs([dict(ioc)], tenant_id="tenant-A")
+ await store.upsert_iocs([dict(ioc)], tenant_id="tenant-B")
+
+ points_a = client.upsert.await_args_list[0].kwargs["points"]
+ points_b = client.upsert.await_args_list[1].kwargs["points"]
+
+ assert points_a[0].payload["tenant_id"] == "tenant-A"
+ assert points_b[0].payload["tenant_id"] == "tenant-B"
+ # Same IOC under two tenants must NOT collide (else B overwrites A).
+ assert points_a[0].id != points_b[0].id
+
+
+async def test_default_upsert_is_shared_feed_intel():
+ client = MagicMock()
+ client.upsert = AsyncMock()
+ store = QdrantStore(client)
+ await store.upsert_iocs([{"type": "domain", "value": "evil.example"}])
+ points = client.upsert.await_args.kwargs["points"]
+ assert points[0].payload["tenant_id"] == SHARED_TENANT
diff --git a/tests/isolation/test_registry.py b/tests/isolation/test_registry.py
new file mode 100644
index 000000000..742f659df
--- /dev/null
+++ b/tests/isolation/test_registry.py
@@ -0,0 +1,26 @@
+"""The registry gate: every datastore must declare isolation coverage."""
+
+from __future__ import annotations
+
+import sys
+from pathlib import Path
+
+sys.path.insert(0, str(Path(__file__).resolve().parent))
+
+from stores import STORES, VALID_STATUSES # noqa: E402
+
+# The six datastores AiSOC runs. A new store added to the stack must be added
+# here (and get a real isolation entry) or this test fails.
+EXPECTED_STORES = {"postgres", "qdrant", "neo4j", "clickhouse", "redis", "kafka"}
+
+
+def test_every_store_has_a_valid_coverage_status():
+ for store in STORES:
+ assert store.status in VALID_STATUSES, f"{store.name} has unset/invalid isolation status: {store.status}"
+ assert store.note.strip(), f"{store.name} isolation entry needs a note"
+
+
+def test_all_known_stores_are_registered():
+ registered = {s.name for s in STORES}
+ missing = EXPECTED_STORES - registered
+ assert not missing, f"datastores missing an isolation entry: {sorted(missing)}"