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.

test(taxii): 15 live cases against real Postgres, with a negative control

Beenu Arorafdd316a1fd0c
diff --git a/.github/workflows/taxii-live.yml b/.github/workflows/taxii-live.yml
new file mode 100644
index 000000000..921c8a2c3
--- /dev/null
+++ b/.github/workflows/taxii-live.yml
@@ -0,0 +1,175 @@
+# The TAXII 2.1 server, against real Postgres (depth plan 4.2).
+#
+# Why this is a live job rather than part of the general Python suite:
+# every property worth proving here is a boundary condition over real rows
+# with a real ordering. A mocked session answers whatever the test handed
+# it, so the off-by-one that loses a polling client's indicators — two rows
+# written by one feed poll sharing `updated_at` to the microsecond — is
+# invisible to it, and so is a missing tenant predicate on a collection
+# lookup.
+#
+# No `paths:` filter. A cross-tenant read guard must grade every pull
+# request, not only ones that touch a named path.
+name: TAXII server (live)
+
+on:
+ push:
+ branches: [main]
+ pull_request:
+ workflow_dispatch:
+
+concurrency:
+ group: taxii-live-${{ github.ref }}-${{ github.event_name }}-${{ github.event.pull_request.number || github.sha }}
+ cancel-in-progress: ${{ github.event_name == 'pull_request' }}
+
+permissions:
+ contents: read
+
+jobs:
+ taxii-live:
+ name: TAXII pages every indicator exactly once, and only this tenant's
+ runs-on: ubuntu-latest
+ timeout-minutes: 20
+
+ services:
+ postgres:
+ image: public.ecr.aws/docker/library/postgres:16-alpine
+ env:
+ POSTGRES_USER: aisoc
+ POSTGRES_PASSWORD: aisoc_ci_only
+ POSTGRES_DB: aisoc
+ ports: ["5432:5432"]
+ options: >-
+ --health-cmd "pg_isready -U aisoc"
+ --health-interval 5s
+ --health-timeout 5s
+ --health-retries 20
+
+ env:
+ TAXII_DSN: postgresql+asyncpg://aisoc:aisoc_ci_only@localhost:5432/aisoc
+ PGPASSWORD: aisoc_ci_only
+ # Not a bypass environment. The suite constructs its principal
+ # directly, but a tree where `development` is in scope could grow a
+ # path that resolves an uncredentialed caller to a demo admin, and
+ # then the tenant-scoping cases would pass for the wrong reason.
+ ENVIRONMENT: test
+ SECRET_KEY: ci-taxii-signing-secret-at-least-32-chars
+ OTEL_SDK_DISABLED: "true"
+
+ steps:
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
+
+ - uses: actions/setup-python@e9d6f990972a57673cdb72ec29e19d42ba28880f # v6
+ with:
+ python-version: "3.11"
+
+ # The service's own lockfile, not a hand-curated pip list: a suite
+ # that drives real route functions pulls in the real service's whole
+ # import chain.
+ - name: Install api from its lockfile
+ working-directory: services/api
+ run: |
+ set -euo pipefail
+ python -m pip install --quiet --upgrade pip "poetry==2.4.1"
+ poetry config virtualenvs.create false
+ poetry install --no-root --no-interaction
+ python -m pip install --quiet pytest pytest-asyncio
+
+ - name: Apply every migration, in order
+ run: |
+ set -euo pipefail
+ for f in services/api/migrations/*.sql; do
+ psql -h localhost -U aisoc -d aisoc -v ON_ERROR_STOP=1 -q -f "$f"
+ done
+
+ # Migration 110's index is what keeps a collection poll from sorting
+ # the tenant's whole store on every request. Asserted rather than
+ # assumed: a migration that ran and created nothing is the shape this
+ # repository keeps finding.
+ - name: The paging index exists
+ run: |
+ set -euo pipefail
+ found=$(psql -h localhost -U aisoc -d aisoc -tAc \
+ "SELECT count(*) FROM pg_indexes WHERE indexname = 'idx_ioc_taxii_paging'")
+ if [ "$found" != "1" ]; then
+ echo "FAIL: idx_ioc_taxii_paging was not created by migration 110."
+ exit 1
+ fi
+ echo "OK: idx_ioc_taxii_paging is present."
+
+ - name: Pagination, tenant scoping and the refused write
+ working-directory: services/api
+ env:
+ ISOLATION_TAXII_DSN: ${{ env.TAXII_DSN }}
+ DATABASE_URL: ${{ env.TAXII_DSN }}
+ run: python -m pytest ../../tests/isolation/test_taxii_server_live.py -v --tb=short
+
+ # The suite must not be able to pass by not running. A live job whose
+ # tests all skipped reports success and proves nothing, which this
+ # repository has shipped before.
+ - name: The suite actually ran
+ working-directory: services/api
+ env:
+ ISOLATION_TAXII_DSN: ${{ env.TAXII_DSN }}
+ DATABASE_URL: ${{ env.TAXII_DSN }}
+ run: |
+ set -euo pipefail
+ python -m pytest ../../tests/isolation/test_taxii_server_live.py -q 2>&1 | tee /tmp/taxii-run.txt
+ if grep -qE '[0-9]+ skipped' /tmp/taxii-run.txt; then
+ echo "FAIL: a case skipped in the job whose whole purpose is to run it live."
+ exit 1
+ fi
+ if ! grep -qE '[0-9]+ passed' /tmp/taxii-run.txt; then
+ echo "FAIL: no case reported as passed."
+ exit 1
+ fi
+
+ # The negative control.
+ #
+ # Replace the (updated_at, id) row-value cursor with the bare
+ # timestamp comparison it replaced. Two of the fixture's indicators
+ # share updated_at to the microsecond — what one feed poll produces —
+ # so the single-row pagination walk must then lose one. Without this,
+ # a cursor that quietly stopped working would pass the suite above.
+ - name: The gate fails when the cursor compares only the timestamp
+ working-directory: services/api
+ env:
+ ISOLATION_TAXII_DSN: ${{ env.TAXII_DSN }}
+ DATABASE_URL: ${{ env.TAXII_DSN }}
+ run: |
+ set -euo pipefail
+ cp app/api/v1/endpoints/taxii.py /tmp/taxii.bak
+ python - <<'PY'
+ import pathlib, sys
+ p = pathlib.Path("app/api/v1/endpoints/taxii.py")
+ s = p.read_text()
+ # Built line by line rather than as a triple-quoted literal: a
+ # de-indented continuation line inside a YAML block scalar ends
+ # the block, which is a parse error in this file rather than a
+ # failure in the step.
+ needle = "\n".join([
+ " stmt = stmt.where(",
+ " or_(",
+ " ThreatIntelIOC.updated_at > stamp,",
+ " and_(ThreatIntelIOC.updated_at == stamp, ThreatIntelIOC.id > row_id),",
+ " )",
+ " )",
+ ])
+ if needle not in s:
+ print("negative control could not find the row-value cursor predicate; "
+ "the pager has been reshaped and this control no longer proves anything")
+ sys.exit(1)
+ p.write_text(s.replace(needle, " stmt = stmt.where(ThreatIntelIOC.updated_at > stamp)", 1))
+ PY
+ set +e
+ python -m pytest \
+ "../../tests/isolation/test_taxii_server_live.py::TestPagination::test_paging_one_at_a_time_yields_every_indicator_exactly_once" \
+ -q >/dev/null 2>&1
+ rc=$?
+ set -e
+ cp /tmp/taxii.bak app/api/v1/endpoints/taxii.py
+ if [ "$rc" -eq 0 ]; then
+ echo "FAIL: the walk passed with a timestamp-only cursor, so it does not prove the ordering."
+ exit 1
+ fi
+ echo "OK: a timestamp-only cursor loses an indicator and the walk goes red, as it must."
diff --git a/tests/isolation/test_taxii_server_live.py b/tests/isolation/test_taxii_server_live.py
new file mode 100644
index 000000000..9a3decd92
--- /dev/null
+++ b/tests/isolation/test_taxii_server_live.py
@@ -0,0 +1,361 @@
+"""The TAXII 2.1 server against real Postgres (depth 4.2).
+
+Why this suite exists rather than a mocked session
+--------------------------------------------------
+Every defect worth catching here is one a test double cannot see.
+
+* ``added_after`` and the ``next`` cursor are a boundary condition over
+ real rows. A fake session returns whatever the test handed it, so the
+ off-by-one that loses a client's indicators — two rows written by one
+ feed poll sharing ``updated_at`` to the microsecond — is invisible
+ unless real rows with a real ordering are paged.
+* A collection id is the one value a third party is expected to hold, so
+ the tenant predicate is the whole security property. A mocked session
+ answers a cross-tenant lookup happily.
+* The server's own index (``idx_ioc_taxii_paging``) and the migration's
+ RLS policy only exist in a real database.
+
+The suite skips when ``ISOLATION_TAXII_DSN`` is unset, and the workflow
+that owns it sets the variable unconditionally — a skip in a job that is
+supposed to be live is a pass for the wrong reason.
+"""
+
+from __future__ import annotations
+
+import os
+import uuid
+from datetime import UTC, datetime, timedelta
+
+import pytest
+import pytest_asyncio
+
+pytestmark = [
+ pytest.mark.asyncio(loop_scope="module"),
+ pytest.mark.skipif(
+ not os.environ.get("ISOLATION_TAXII_DSN", "").strip(),
+ reason="ISOLATION_TAXII_DSN is not set; this suite needs a live Postgres",
+ ),
+]
+
+
+def _dsn() -> str:
+ value = os.environ.get("ISOLATION_TAXII_DSN", "").strip()
+ if not value:
+ pytest.skip("ISOLATION_TAXII_DSN is not set")
+ if value.startswith("postgresql://"):
+ return value.replace("postgresql://", "postgresql+asyncpg://", 1)
+ return value
+
+
+@pytest_asyncio.fixture(scope="module", loop_scope="module")
+async def engine():
+ from sqlalchemy.ext.asyncio import create_async_engine
+
+ eng = create_async_engine(_dsn())
+ try:
+ yield eng
+ finally:
+ await eng.dispose()
+
+
+@pytest_asyncio.fixture(scope="module", loop_scope="module")
+async def world(engine):
+ """Two tenants, each with a collection and indicators.
+
+ Tenant B exists so every read in this suite is a cross-tenant read
+ that must come back empty. A single-tenant fixture cannot tell a
+ working tenant predicate from a missing one.
+ """
+ from sqlalchemy import text
+
+ tenant_a = uuid.uuid4()
+ tenant_b = uuid.uuid4()
+ base = datetime(2026, 10, 11, 9, 0, 0, tzinfo=UTC)
+
+ async with engine.begin() as conn:
+ for tid, slug in ((tenant_a, "taxii-a"), (tenant_b, "taxii-b")):
+ await conn.execute(
+ text("INSERT INTO tenants (id, name, slug) VALUES (:id, :n, :s)"),
+ {"id": str(tid), "n": slug, "s": f"{slug}-{uuid.uuid4().hex[:8]}"},
+ )
+
+ collections: dict[str, uuid.UUID] = {}
+ for tid, title, key, ioc_filter in (
+ (tenant_a, "Everything", "all", "{}"),
+ (tenant_a, "IPs only", "ips", '{"ioc_type": ["ip"]}'),
+ (tenant_b, "Tenant B", "b", "{}"),
+ ):
+ cid = uuid.uuid4()
+ collections[key] = cid
+ await conn.execute(
+ text(
+ "INSERT INTO taxii_collections (id, tenant_id, alias, title, ioc_filter) VALUES (:id, :t, :a, :ti, CAST(:f AS JSONB))"
+ ),
+ {"id": str(cid), "t": str(tid), "a": key, "ti": title, "f": ioc_filter},
+ )
+
+ # Two of these share updated_at to the microsecond, which is what
+ # one feed poll produces and what a timestamp-only cursor loses.
+ rows = [
+ (tenant_a, "ip", "198.51.100.1", base),
+ (tenant_a, "ip", "198.51.100.2", base),
+ (tenant_a, "domain", "a.example.com", base + timedelta(seconds=1)),
+ (tenant_a, "domain", "b.example.com", base + timedelta(seconds=2)),
+ # Not publishable: no STIX pattern exists for a CVE.
+ (tenant_a, "cve", "CVE-2026-9999", base + timedelta(seconds=3)),
+ (tenant_b, "ip", "203.0.113.9", base),
+ ]
+ ioc_ids: list[uuid.UUID] = []
+ for tid, ioc_type, value, stamp in rows:
+ iid = uuid.uuid4()
+ ioc_ids.append(iid)
+ await conn.execute(
+ text(
+ "INSERT INTO threat_intel_iocs "
+ "(id, tenant_id, ioc_type, value, confidence, severity, tlp, source, "
+ " first_seen, last_seen, is_active, false_positive, created_at, updated_at) "
+ "VALUES (:id, :t, :ty, :v, 70, 'high', 'amber', 'internal', "
+ " :s, :s, true, false, :s, :s)"
+ ),
+ {"id": str(iid), "t": str(tid), "ty": ioc_type, "v": value, "s": stamp},
+ )
+
+ yield {
+ "tenant_a": tenant_a,
+ "tenant_b": tenant_b,
+ "collections": collections,
+ "base": base,
+ }
+
+ async with engine.begin() as conn:
+ for tid in (tenant_a, tenant_b):
+ await conn.execute(text("DELETE FROM tenants WHERE id = :id"), {"id": str(tid)})
+
+
+def _user(tenant_id: uuid.UUID):
+ from app.api.v1.deps import CurrentUser
+
+ return CurrentUser(
+ user_id=uuid.uuid4(),
+ tenant_id=tenant_id,
+ role="threat_hunter",
+ )
+
+
+@pytest_asyncio.fixture(loop_scope="module")
+async def session(engine):
+ from sqlalchemy.ext.asyncio import async_sessionmaker
+
+ maker = async_sessionmaker(engine, expire_on_commit=False)
+ async with maker() as s:
+ yield s
+
+
+async def _objects(session, user, collection_ref, **kwargs):
+ """Call the real route function and decode its TAXII envelope."""
+ import json
+
+ from app.api.v1.endpoints.taxii import API_ROOT, get_objects
+
+ response = await get_objects(
+ api_root=API_ROOT,
+ collection_ref=str(collection_ref),
+ current_user=user,
+ db=session,
+ **kwargs,
+ )
+ assert response.media_type == "application/taxii+json;version=2.1"
+ return json.loads(response.body), response
+
+
+# ---------------------------------------------------------------------------
+# Tenant scoping. A collection id is the one value a third party holds.
+# ---------------------------------------------------------------------------
+
+
+class TestTenantScoping:
+ async def test_another_tenants_collection_is_not_found_by_id(self, session, world):
+ from app.api.v1.endpoints.taxii import API_ROOT, get_collection
+ from fastapi import HTTPException
+
+ with pytest.raises(HTTPException) as exc:
+ await get_collection(
+ api_root=API_ROOT,
+ collection_ref=str(world["collections"]["b"]),
+ current_user=_user(world["tenant_a"]),
+ db=session,
+ )
+ assert exc.value.status_code == 404
+
+ async def test_another_tenants_collection_is_not_found_by_alias(self, session, world):
+ from app.api.v1.endpoints.taxii import API_ROOT, get_collection
+ from fastapi import HTTPException
+
+ with pytest.raises(HTTPException) as exc:
+ await get_collection(
+ api_root=API_ROOT,
+ collection_ref="b",
+ current_user=_user(world["tenant_a"]),
+ db=session,
+ )
+ assert exc.value.status_code == 404
+
+ async def test_listing_shows_only_the_callers_collections(self, session, world):
+ import json
+
+ from app.api.v1.endpoints.taxii import API_ROOT, list_collections
+
+ response = await list_collections(api_root=API_ROOT, current_user=_user(world["tenant_a"]), db=session)
+ titles = {c["title"] for c in json.loads(response.body)["collections"]}
+ assert titles == {"Everything", "IPs only"}
+ assert "Tenant B" not in titles
+
+ async def test_no_other_tenants_indicator_appears_in_a_page(self, session, world):
+ body, _ = await _objects(session, _user(world["tenant_a"]), world["collections"]["all"])
+ patterns = " ".join(o["pattern"] for o in body["objects"])
+ assert "203.0.113.9" not in patterns
+
+
+# ---------------------------------------------------------------------------
+# Projection against real rows.
+# ---------------------------------------------------------------------------
+
+
+class TestProjection:
+ async def test_the_collection_publishes_its_tenants_publishable_indicators(self, session, world):
+ body, _ = await _objects(session, _user(world["tenant_a"]), world["collections"]["all"])
+ # Four publishable rows; the CVE is excluded because no STIX
+ # pattern exists for it.
+ assert len(body["objects"]) == 4
+ assert all(o["type"] == "indicator" for o in body["objects"])
+ assert "CVE-2026-9999" not in " ".join(o["pattern"] for o in body["objects"])
+
+ async def test_a_collection_filter_narrows_what_is_published(self, session, world):
+ body, _ = await _objects(session, _user(world["tenant_a"]), world["collections"]["ips"])
+ assert len(body["objects"]) == 2
+ assert all("ipv4-addr" in o["pattern"] for o in body["objects"])
+
+ async def test_the_envelope_carries_the_headers_a_client_bookmarks(self, session, world):
+ _, response = await _objects(session, _user(world["tenant_a"]), world["collections"]["all"])
+ assert response.headers["X-TAXII-Date-Added-First"].endswith("Z")
+ assert response.headers["X-TAXII-Date-Added-Last"].endswith("Z")
+
+ async def test_an_empty_page_omits_the_boundary_headers(self, session, world):
+ # Setting them to "now" on an empty page makes a client record a
+ # boundary no row was ever written at, so anything landing in
+ # between is skipped forever.
+ future = (world["base"] + timedelta(days=365)).isoformat()
+ _, response = await _objects(
+ session,
+ _user(world["tenant_a"]),
+ world["collections"]["all"],
+ added_after=future,
+ )
+ assert "X-TAXII-Date-Added-Last" not in response.headers
+
+
+# ---------------------------------------------------------------------------
+# Pagination. The part that silently loses indicators.
+# ---------------------------------------------------------------------------
+
+
+class TestPagination:
+ async def test_paging_one_at_a_time_yields_every_indicator_exactly_once(self, session, world):
+ """The whole point of the (updated_at, id) cursor.
+
+ Two of these rows share ``updated_at`` to the microsecond. With a
+ timestamp-only cursor, paging at size 1 either repeats one of them
+ forever or skips it — this walk is what distinguishes the two.
+ """
+ seen: list[str] = []
+ token = None
+ for _ in range(10):
+ body, _r = await _objects(
+ session,
+ _user(world["tenant_a"]),
+ world["collections"]["all"],
+ limit=1,
+ next_token=token,
+ )
+ seen.extend(o["id"] for o in body["objects"])
+ if not body["more"]:
+ break
+ token = body["next"]
+ else:
+ pytest.fail("pagination did not terminate")
+
+ assert len(seen) == 4, f"walked {len(seen)} objects in single-row pages"
+ assert len(set(seen)) == 4, "an object was returned twice"
+
+ async def test_more_is_false_on_the_last_page(self, session, world):
+ body, _ = await _objects(session, _user(world["tenant_a"]), world["collections"]["all"], limit=100)
+ assert body["more"] is False
+ assert "next" not in body
+
+ async def test_added_after_excludes_what_the_client_already_has(self, session, world):
+ boundary = (world["base"] + timedelta(seconds=1)).isoformat()
+ body, _ = await _objects(
+ session,
+ _user(world["tenant_a"]),
+ world["collections"]["all"],
+ added_after=boundary,
+ )
+ # Only the row at base+2s is strictly after the boundary among the
+ # publishable ones.
+ assert len(body["objects"]) == 1
+
+ async def test_a_corrupt_next_token_is_a_400_not_a_silent_restart(self, session, world):
+ from fastapi import HTTPException
+
+ with pytest.raises(HTTPException) as exc:
+ await _objects(
+ session,
+ _user(world["tenant_a"]),
+ world["collections"]["all"],
+ next_token="2026-10-11T09:00:00+00:00|" + str(uuid.uuid4()) + "|deadbeef",
+ )
+ assert exc.value.status_code == 400
+
+ async def test_the_manifest_pages_by_the_same_cursor_as_objects(self, session, world):
+ import json
+
+ from app.api.v1.endpoints.taxii import API_ROOT, get_manifest
+
+ response = await get_manifest(
+ api_root=API_ROOT,
+ collection_ref=str(world["collections"]["all"]),
+ current_user=_user(world["tenant_a"]),
+ db=session,
+ )
+ manifest = json.loads(response.body)
+ body, _ = await _objects(session, _user(world["tenant_a"]), world["collections"]["all"])
+ # Two orderings would let a client hold a manifest entry it can
+ # never reach through objects.
+ assert [e["id"] for e in manifest["objects"]] == [o["id"] for o in body["objects"]]
+
+
+# ---------------------------------------------------------------------------
+# Posture.
+# ---------------------------------------------------------------------------
+
+
+class TestWritesAreRefused:
+ async def test_a_write_is_403_and_says_why(self, session, world):
+ import json
+
+ from app.api.v1.endpoints.taxii import API_ROOT, refuse_write
+
+ response = await refuse_write(api_root=API_ROOT, collection_ref=str(world["collections"]["all"]))
+ assert response.status_code == 403
+ body = json.loads(response.body)
+ assert "does not accept" in body["description"]
+
+ async def test_every_collection_advertises_can_write_false(self, session, world):
+ import json
+
+ from app.api.v1.endpoints.taxii import API_ROOT, list_collections
+
+ response = await list_collections(api_root=API_ROOT, current_user=_user(world["tenant_a"]), db=session)
+ for collection in json.loads(response.body)["collections"]:
+ assert collection["can_write"] is False