Skip to content

armgit / AiSOC

Public Imported from GitHub
0

Open-source AI Security Operations Center: alert fusion, LLM-agent triage, MITRE ATT&CK investigation, and a replayable decision ledger for every agent step. Self-hostable, runs with no API keys, MIT licensed. Ships an MCP server for Claude, Cursor and Continue.

fix(auth)!: require an explicit opt-in before serving an anonymous caller (#1072)

Beenu Arora4f4ba326c6b8
diff --git a/.env.example b/.env.example
index 807454972..92bdee4fc 100644
--- a/.env.example
+++ b/.env.example
@@ -437,4 +437,4 @@ REALTIME_INTERNAL_TOKEN=
# Bearer the agents service presents to the ChatOps fan-out (slack-bot).
# Unset means no ChatOps notification is sent; the approval is still durable
# and visible in the console.
-# AISOC_INTERNAL_TOKEN=
+AISOC_INTERNAL_TOKEN=
diff --git a/.github/workflows/golden-pipeline.yml b/.github/workflows/golden-pipeline.yml
index ad9cb03ea..ed40f18f7 100644
--- a/.github/workflows/golden-pipeline.yml
+++ b/.github/workflows/golden-pipeline.yml
@@ -165,6 +165,24 @@ jobs:
fi
echo "confirmed: a second mint reused the existing push token"
+ # The alert reads need a credential too. They used to succeed with none,
+ # because an uncredentialed request resolved to a demo administrator in
+ # a development-class environment and every documented path produced
+ # one — so this workflow was reading the API as an anonymous admin
+ # without anybody having decided that. Minted through the shipped
+ # command, for the same reason the ingest token is.
+ - name: Mint the API credential the alert reads need
+ run: |
+ set -euo pipefail
+ docker compose run --rm -T api python -m app.scripts.bootstrap_admin >/dev/null 2>&1 || true
+ token="$(docker compose run --rm -T api python -m app.scripts.mint_api_token --quiet)"
+ if [ -z "$token" ]; then
+ echo "::error::mint_api_token printed nothing"
+ exit 1
+ fi
+ echo "::add-mask::$token"
+ echo "AISOC_API_TOKEN=$token" >> "$GITHUB_ENV"
+
- name: Golden pipeline — one real event end to end
run: python3 tests/e2e/golden_pipeline/run_golden_pipeline.py
diff --git a/CHANGELOG.md b/CHANGELOG.md
index df42aa6f4..b8a782e08 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,22 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
+### BREAKING
+
+- **An unauthenticated request is no longer an administrator, and `AISOC_DEV_MODE` no longer admits an uncredentialed caller.** If you rely on either, here is what changes and how to get it back.
+
+ | What used to work | What happens now | How to restore it |
+ |---|---|---|
+ | `ENVIRONMENT=development` (the compose and `.env.example` default) served a request with no `Authorization` header as `admin` | `401` | Set `AISOC_DEV_AUTH_BYPASS=1`, or use `infra/compose/docker-compose.dev.yml` |
+ | The same, while the stack published a non-loopback address | `401` even with the flag set | Bind to `127.0.0.1`, or authenticate |
+ | `AISOC_DEV_MODE=1` with no `SECRET_KEY` and no service token admitted an uncredentialed caller to the nine services carrying `require_console_or_service_auth` | `503` | Set `AISOC_DEV_AUTH_BYPASS=1` as well, and keep the published addresses on loopback |
+ | The same on `require_service_auth` in `ueba`, `honeytokens` and `purple-team` | `503`, with no flag that re-enables it | Set `AISOC_SERVICE_TOKEN`. `make env` generates one |
+ | `POST /internal/approval-card` on `slack-bot` with `AISOC_INTERNAL_TOKEN` unset | `401` | `make env` now generates `AISOC_INTERNAL_TOKEN` |
+ | `POST /v1/push/subscribe`, `/unsubscribe` and `/test` on `realtime` with no credential | `401` | Call through the API's `/api/v1/push/*` proxy, which authenticates you and stamps the token |
+ | Those push routes taking the tenant from a `tenant_id` query parameter, or defaulting to the string `'default'` | `400` unless `X-Tenant-Id` is present | The proxy stamps it from your session |
+
+ **The demo tenant also moved**, from `00000000-0000-0000-0000-000000000001` to `00000000-0000-0000-0000-0000000000de`. A stack that ran the demo seed and wants its old demo rows back should re-run `make demo`. The canonical tenant id is unchanged.
+
### Security
- **Ten newly-disclosed advisories in two transitive dependencies.** `axios` moved to `>=1.20.0` through a `pnpm.overrides` entry (six high-severity advisories against the 1.x line, all patched in 1.20.0: GHSA-c29m-xwm3-cm6r, GHSA-mghh-pgcx-3jjj, GHSA-3pq3-5fj3-cg6v, GHSA-542g-h47m-68v8, GHSA-m8m8-qj5v-23w3, GHSA-r4gj-5m52-g5wh). A seventh, GHSA-x97p-jq2g-jp4f, covers 0.28.0 to 0.34.0, a line nothing in this tree resolves, so the bound is written against 1.x rather than spanning both.
@@ -27,6 +43,39 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
The gate that keeps it closed is wired into `ci.yml` beside the sample-data gate, and runs `--self-test` first. Three blind spots in its own first draft are pinned as cases, each of which had made it silently miss real findings: a generic type argument (`useSWR<Role[]>(...)`) hid both settings SWR sites, a multi-line arrow fetcher was truncated at the first newline so nine calls on the honeytokens and purple-team pages read as "imported, judged elsewhere", and an inline fetcher whose URL is the SWR key has no API path at the call site to match on at all.
+- **An anonymous caller was an administrator in the operator's own tenant, on every documented deployment.** Three things had to be true at once and all three were, by default:
+
+ 1. `ENVIRONMENT` defaulted to `development` in `docker-compose.yml`, in `.env.example`, and therefore in the `.env` that `scripts/ensure_env.py` copies verbatim on first run. `development` is in `AUTH_BYPASS_ENVIRONMENTS`, so `get_current_user` resolved a request with no bearer token to a demo user whose role is `admin`.
+ 2. `dev_auth.DEMO_TENANT_ID` and `bootstrap_admin.DEFAULT_TENANT_ID` were **byte-identical** (`…0001`). So that administrator was not sandboxed in a demo tenant; it was an administrator of the tenant `make bootstrap` had just put the operator's real account into, with their alerts and their vault-encrypted connector credentials in it.
+ 3. `apps/docs/docs/deployment/single-host.md` and `walkthrough.mdx` instruct `AISOC_CONSOLE_BIND_ADDR=0.0.0.0`, and neither page mentions `ENVIRONMENT`, `production` or `docker-compose.prod.yml` anywhere.
+
+ An environment name cannot carry that weight. It is a label chosen for logging verbosity and docs URLs, it defaults to a dev-class value, and nothing about setting it says "and make this host anonymous". So the shim now needs `AISOC_DEV_AUTH_BYPASS`, whose only meaning is the bypass and which no compose file or template sets, and it is refused outright when the deployment publishes an address that is not loopback. The API cannot work that out alone — it binds `0.0.0.0` inside a container — so compose passes the published addresses in through `AISOC_PUBLISHED_BIND_ADDRS`. Every refusal names the condition that withheld it, in the log, because silence is how this stayed invisible: an operator who expected the bypass and one who did not both saw the same 401.
+
+ The demo identity was given a tenant of its own, and `bootstrap_admin` now refuses to create a real account in it and will not adopt it as the oldest existing tenant. The canonical id stays where it is, because ten modules pin it, including the ingest-token minter and the agents ledger.
+
+ Measured, in both directions: 36 cases in `services/api/tests/test_anonymous_bypass_requires_optin.py`, of which 35 fail at `079fdb69` and all 36 pass here. The tenant-sharing assertion fails pre-fix as `assert UUID('…0001') != UUID('…0001')`.
+
+- **The same shape in nine sibling services, plus slack-bot and realtime.** `AISOC_DEV_MODE` defaults to `1` on ten services in `docker-compose.yml`, and it selects table autocreate, docs URLs, the metrics gate, log formatting, the GraphiQL UI and an ephemeral vault key as well. A flag that means nine things cannot be refused for one of them.
+
+ - The nine vendored `require_console_or_service_auth` copies admitted a caller with no credential at all whenever neither `SECRET_KEY` nor a service token was configured. They now need `AISOC_DEV_AUTH_BYPASS` too, refuse on a published non-loopback address, and scope the admitted principal to the demo tenant rather than the canonical one. Fixed in `services/fusion/app/security/tenant_scope.py` and propagated by `scripts/sync_vendored_tenant_scope.py`, so all nine move together.
+ - `require_service_auth` in `ueba`, `honeytokens` and `purple-team` had the same exemption. It is **removed outright**, with no replacement flag, following the precedent `services/actions/app/security/authz.py` already set: a service-to-service dependency has no browser to keep usable, so a 503 naming the variable to set is the whole of what is needed.
+ - `slack-bot`'s `POST /internal/approval-card` treated an unset `AISOC_INTERNAL_TOKEN` as "no auth needed" under `AISOC_DEV_MODE`, and **nothing generated that token** — it appeared only commented out in `.env.example`. So the exemption was not a convenience, it was the only state a stock install ran in, on a route that posts into a workspace channel. `scripts/ensure_env.py` now generates the token, which is what makes requiring it unconditionally viable.
+ - `realtime`'s three `POST /v1/push/*` routes carried only a rate limiter, and `tenantOf` read a request header, then a `tenant_id` **query parameter**, then the literal string `'default'`. Any caller who could reach the port could enrol a push endpoint against any tenant, unsubscribe another tenant's devices, or make the service send a notification. They now require the same internal token `/internal/*` requires, which makes the API's `/api/v1/push/*` proxy the only way in — the arrangement the module's comments already assumed.
+
+ Two adjacent defects surfaced while fixing it. The proxy stamped `X-AiSOC-Internal-Token` while `requireInternal` reads `x-internal-token`, so **the token it sent was never checked**; the receiver now accepts both spellings and the proxy sends both. And `userOf` preferred a caller-supplied body field over the stamped `X-User-Id`, under a comment saying the API gateway "is expected to" validate it — it does not, so a caller could enrol a push endpoint against another user in their own tenant. The precedence is now header first.
+
+ The `'default'` fallback deserves naming separately: it is not a tenant. Migration 001 seeds the canonical tenant with that *slug* and the demo seed renames it, so every subscription that reached the fallback was filed under a Redis key belonging to nobody, silently. `tenantOf` now throws and the routes answer 400.
+
+- **The golden pipeline was reading the API as an anonymous administrator.** `tests/e2e/golden_pipeline/run_golden_pipeline.py` drives one real event through the spine and then asks the API whether it became an alert. That read carried no credential and succeeded, because an uncredentialed request resolves to a demo administrator in a development-class environment and every documented path produced one — so the project's only end-to-end claim of "it works" was being made through the bypass, without anybody having decided that.
+
+ The harness now carries a bearer token, minted by `app.scripts.mint_api_token` — the sibling of `mint_ingest_token`, and a shipped command for the same reason: the step that calls it also covers a path an operator can follow (`make api-token`). It creates nothing, refuses when no account exists, and refuses the demo tenant outright, because a real token scoped to the tenant the shim hands anonymous callers would make the bypass reachable from outside the bypass.
+
+ A refused read is now reported as refused. The first version spent 90 seconds on a 401 and then suggested checking Kafka, the fusion consumer and the alerts table — three things that were all working.
+
+ Two things caught by gates rather than by the suite: `scripts/check_tenant_query_predicates.py` flagged the first draft for selecting "the most privileged active account anywhere", which is unscoped against a table with no row-level security and a vague target on a multi-tenant deployment; and every test passed while `DEFAULT_TENANT_ID` was not imported at all, because each one supplied a tenant explicitly and none reached the default branch. Ruff found it.
+
+- **A test-isolation trap worth recording, because it cost a diagnosis.** The first draft of the new API test file called `importlib.reload` on `app.api.v1.deps`, copying an older test in the same directory. That rebinds every function object in the module, so the `dependency_overrides` other test modules keyed on the old objects stop matching and their routes fall through to real authentication: **146 tests failed across three unrelated files, every one of which passes in isolation.** The older test got away with it only because its filename sorts near the end of the suite. Neither file reloads anything now, and neither needs to — the shim reads `os.environ` at call time precisely so that it does not.
+
## [14.0.0] - 2026-09-29
### BREAKING
diff --git a/Makefile b/Makefile
index 237e52135..6a3451304 100644
--- a/Makefile
+++ b/Makefile
@@ -45,7 +45,7 @@ CONSOLE_URL = $(shell sed -n 's/^AISOC_CONSOLE_URL=//p' .env 2>/dev/null | tail
console_url = $(if $(strip $(CONSOLE_URL)),$(strip $(CONSOLE_URL)),http://localhost:3000)
.PHONY: help install env up up-full pull down restart status doctor smoke demo logs clean \
- bootstrap ingest-token test test-unit test-integration test-e2e stats papers \
+ bootstrap ingest-token api-token test test-unit test-integration test-e2e stats papers \
papers-install demo-script
help:
@@ -264,6 +264,13 @@ doctor:
ingest-token:
@$(COMPOSE) run --rm -T api python -m app.scripts.mint_ingest_token $(ARGS)
+# A short-lived bearer for the account `make bootstrap` created, so a script
+# can read the API the way the console does. The sibling of `ingest-token`:
+# reading used to need no credential at all, because an uncredentialed request
+# resolved to a demo administrator and every documented path produced one.
+api-token:
+ @$(COMPOSE) run --rm -T api python -m app.scripts.mint_api_token $(ARGS)
+
# The golden pipeline. One real event, through the real spine, observed from
# outside. This is the only claim of "it works" the project makes.
#
@@ -275,7 +282,11 @@ smoke:
echo "Could not mint an ingest token — is the stack up? Try 'make doctor'."; \
exit 1; \
}; \
- AISOC_INGEST_TOKEN="$$token" $(PYTHON) tests/e2e/golden_pipeline/run_golden_pipeline.py
+ api="$$($(COMPOSE) run --rm -T api python -m app.scripts.mint_api_token --quiet)" || { \
+ echo "Could not mint an API token — has 'make bootstrap' run? Try 'make doctor'."; \
+ exit 1; \
+ }; \
+ AISOC_INGEST_TOKEN="$$token" AISOC_API_TOKEN="$$api" $(PYTHON) tests/e2e/golden_pipeline/run_golden_pipeline.py
demo:
@echo "Loading synthetic demo data. Every row is tagged is_synthetic=true"
diff --git a/README.md b/README.md
index 1a0c0c4b7..2046f3239 100644
--- a/README.md
+++ b/README.md
@@ -57,7 +57,7 @@ Needs Docker Compose v2 with **8 GB memory and 20 GB free disk in the Docker VM*
was measured against. The first run downloads a ~2 GB language model into a named volume; only
`make clean` fetches it again.
-`make up` also creates `.env` and generates the **eleven** secrets in it — the credential vault,
+`make up` also creates `.env` and generates the **twelve** secrets in it — the credential vault,
the session signing key, the five service-to-service credentials and the four datastore passwords — then
creates an administrator and prints its password. That password is generated on your machine, shown once, and stored nowhere:
copy it, or mint a new one with `make bootstrap ARGS=--reset-password`.
diff --git a/ROADMAP.md b/ROADMAP.md
index 35d113119..047911149 100644
--- a/ROADMAP.md
+++ b/ROADMAP.md
@@ -44,7 +44,7 @@ A proof-first, security-first program to make every README claim gate-backed, cl
- **Phase D (breadth):** D1 eight connectors (QRadar/Exabeam/Securonix/Devo/Netskope/Windows-Sysmon/Zeek-Suricata/syslog-CEF) · D2 AI/LLM-usage audit connector + `llm-*` detections + hot/cold lake tiering · D3 live-vendor mock-server smoke.
- **Phase E (prove it):** E1 CI-gated benchmark scoreboard tied to a deterministic live-agent MITRE-accuracy run.
-The claim-to-gate matrix stands at **242 rows — 242 GATED / 0 PARTIAL / 0 NO GATE** — **every product claim is backed by a failing test**, none is a named deferral either, and the ratchet (`MAX_NO_GATE=0`) forbids any regression. Every PARTIAL row was closed by building the gate it named, never by relabelling. The last two rested on the live-agent eval workflow, which this file said had simply never run: the real reason was that its live path imported a class that exists nowhere in `services/agents`, so its first dispatch failed in 92 seconds and every later one would have. With that fixed the workflow measures, and the groundedness floor is **0.40 over a deterministic 10-incident slice**, derived from ten runs across two environments rather than chosen — seven local runs returning 0.5561 to four decimal places, three GitHub-runner runs returning 0.5821, 0.5329 and 0.5933. Every one is a locally-served `qwen2.5:0.5b`; no hosted provider has been exercised and the floor describes none. Count the table rows with `python3 scripts/check_claim_gate_matrix.py` rather than trusting a figure quoted in prose — this line has gone stale before. The six lettered deferrals are scoped in [`docs/audit/DEFERRED_SUBPHASES.md`](docs/audit/DEFERRED_SUBPHASES.md).
+The claim-to-gate matrix stands at **243 rows — 243 GATED / 0 PARTIAL / 0 NO GATE** — **every product claim is backed by a failing test**, none is a named deferral either, and the ratchet (`MAX_NO_GATE=0`) forbids any regression. Every PARTIAL row was closed by building the gate it named, never by relabelling. The last two rested on the live-agent eval workflow, which this file said had simply never run: the real reason was that its live path imported a class that exists nowhere in `services/agents`, so its first dispatch failed in 92 seconds and every later one would have. With that fixed the workflow measures, and the groundedness floor is **0.40 over a deterministic 10-incident slice**, derived from ten runs across two environments rather than chosen — seven local runs returning 0.5561 to four decimal places, three GitHub-runner runs returning 0.5821, 0.5329 and 0.5933. Every one is a locally-served `qwen2.5:0.5b`; no hosted provider has been exercised and the floor describes none. Count the table rows with `python3 scripts/check_claim_gate_matrix.py` rather than trusting a figure quoted in prose — this line has gone stale before. The six lettered deferrals are scoped in [`docs/audit/DEFERRED_SUBPHASES.md`](docs/audit/DEFERRED_SUBPHASES.md).
## v4.0 — Shipped
diff --git a/SECURITY_BATCH_PROGRESS.md b/SECURITY_BATCH_PROGRESS.md
index c0dacfe61..56fef105c 100644
--- a/SECURITY_BATCH_PROGRESS.md
+++ b/SECURITY_BATCH_PROGRESS.md
@@ -147,8 +147,14 @@ anonymous principal is an `admin` in the same tenant the real operator is
bootstrapped into.
- [x] **S1c** Console credential on every call ([#1069](https://github.com/beenuar/AiSOC/pull/1069))
-- [ ] **S1** Production-class defaults on every documented path
-- [ ] **S1b** The nine vendored shims, slack-bot and realtime push
+- [x] **S1a** The bypass needs an explicit opt-in, refuses a reachable bind address, and the demo
+ identity has its own tenant ([#1070](https://github.com/beenuar/AiSOC/pull/1072))
+- [x] **S1b** The nine vendored shims, the three `require_service_auth` copies, slack-bot and
+ realtime push ([#1070](https://github.com/beenuar/AiSOC/pull/1072))
+- [ ] **S1d** Production-class defaults on every documented path: `install.sh`, `install.ps1`,
+ `make up`, the dev overlay, the single-host guide, the walkthrough, `make smoke` asserting an
+ anonymous write returns 401, and the gate that resolves the environment each documented path
+ produces
## S2 to S13
@@ -188,3 +194,19 @@ caught it on the first attempt here, which is what it is for.
The batch order is dependency-driven, not severity-driven. S1c had to land
before S1 because closing the anonymous path turns 61 console calls into 401s,
and the console had to be able to authenticate first.
+
+S1 split into S1a/S1b (the code that decides whether to admit an
+uncredentialed caller, across all eleven services at once so the vendored
+copies and their cross-references move together) and S1d (the deployment
+paths). They are separable because S1a/S1b close the hole and S1d makes the
+documented experience match; shipping the first without the second leaves a
+correct default and a stale guide, which is the safe order.
+
+Two things found while fixing S1b that neither brief raised:
+
+* the API's push proxy stamped `X-AiSOC-Internal-Token` while realtime's
+ `requireInternal` reads `x-internal-token`, so the token it sent was never
+ checked by anything;
+* `userOf` in `services/realtime/src/push.ts` preferred a caller-supplied body
+ field over the stamped `X-User-Id`, under a comment asserting that the API
+ gateway validates it. It does not.
diff --git a/apps/docs/docs/compliance/evidence-pack.md b/apps/docs/docs/compliance/evidence-pack.md
index b62132f97..d0ae3c6cc 100644
--- a/apps/docs/docs/compliance/evidence-pack.md
+++ b/apps/docs/docs/compliance/evidence-pack.md
@@ -33,7 +33,7 @@ and says so.
| Prompt-injection resistance | Three adversarial corpora: 32 payloads scanned in isolation and 71 incident pairs whose payloads are placed in attacker-controllable fields, both against a clean twin with a recall floor and a false-positive ceiling, plus 34 held-out pairs authored after the guard was frozen and carrying no floor at all, on which the guard measures 7.1% | `services/agents/tests/adversarial/` |
| Claim-to-gate traceability | Every product claim mapped to the CI job that fails when it stops being true | `docs/audit/CLAIM_TO_GATE_MATRIX.md` |
-The last row is the one worth reading first. It is the honest index: 242 rows
+The last row is the one worth reading first. It is the honest index: 243 rows
`GATED`, 0 `PARTIAL`, and a ratchet that refuses to let a row sit at
`NO GATE`. Re-read the figure from the table rather than from here — a count
copied into prose goes stale in silence, which is why
diff --git a/apps/docs/docs/deployment/docker.md b/apps/docs/docs/deployment/docker.md
index 81500d969..f1022a497 100644
--- a/apps/docs/docs/deployment/docker.md
+++ b/apps/docs/docs/deployment/docker.md
@@ -117,7 +117,7 @@ COMPOSE_PROFILES=connectors,osquery,slack docker compose up -d
## Production
```bash
-make env # generates the eleven secrets the stack needs
+make env # generates the twelve secrets the stack needs
docker compose -f docker-compose.yml -f docker-compose.prod.yml up -d
```
diff --git a/docs/audit/CLAIM_TO_GATE_MATRIX.md b/docs/audit/CLAIM_TO_GATE_MATRIX.md
index ecaed61fa..05ac9a80f 100644
--- a/docs/audit/CLAIM_TO_GATE_MATRIX.md
+++ b/docs/audit/CLAIM_TO_GATE_MATRIX.md
@@ -250,10 +250,11 @@ Statuses: `GATED` (a CI job fails when the claim stops being true) · `PARTIAL`
| Every CI job that grades a service installs what that service's image ships, so CI cannot run a different engine than production | `scripts/check_ci_install_parity.py` (module docstring); `RELEASES.md` | `reproducible-builds.yml :: pin-agreement` (`check_ci_install_parity.py` — for every job running a service's *whole* pytest suite, three directions: a runtime dependency guarded by `except ImportError` that CI does not install, so CI grades the fallback and the image grades the real path; a package CI installs that none of the job's services declares; and a version range disjoint from, wider than, or unbounded against the intersection of what those services declare. 16 self-test cases inject each direction, two controls must not fire, and two refusals cover a tree with nothing to compare. It reported 210 divergences against the commit before it landed) | GATED | The three whole-suite jobs derive their install list from the manifests through `service_requirements.py`, which re-resolves inside the declared ranges rather than installing the locked set — so CI can land on a different patch release than the image. Narrower than a hand-written list, and stated in the script's docstring. Closing it means installing from the lock in CI, at the cost of the pip wheel cache the matrix was built around |
| A published Helm chart version names one chart forever | `apps/docs/docs/deployment/kubernetes.md` (The chart's version); `infra/helm/aisoc/Chart.yaml` | `helm.yml :: version` + `release.yml :: chart-publish` (`check_chart_version.py` — four checks that need no network: `appVersion` moved while the chart's own `version` did not, any tracked chart file moved with no version bump (with Chart.yaml's own `version:` masked, so the bump cannot justify itself), a version that moved backward, and one chart version declared at two release tags with differing content. Plus one that asks GHCR: the version about to be pushed already exists holding content this tree does not. The registry comparison is on **unpacked files**, because `helm package` is not byte-reproducible — `Chart.yaml` is compared parsed since helm re-serialises it, and the tracked subchart `.tgz` are expanded the way helm expands them, a mapping measured exact against the published 5.9.2 at 146 files. On a release it runs with `--require-network`, before `helm push`, because the push overwrites silently and there is no after. 16 self-test cases, and the gate refuses a checkout with no tags rather than concluding no release ever declared a version) | GATED | **Proven against the pre-fix tree**: at `2db6ce73` (the v13.0.0 release commit) it exits 1 naming `appversion-moved-alone` — `v12.3.2` -> `v13.0.0` with `version` held at 5.9.2 — and exits 0 at `9d3225c6` after the bump to 6.0.0. **Two things it deliberately does not decide.** Whether a bump is the right *size*: nothing in a diff knows if a renamed `values.yaml` key breaks somebody's values file, so the gate enforces forward, never how far. And the overwrite that already happened: republishing replaced the bytes, so the registry's 5.9.2 and the tree that overwrote it are now byte-identical — measured, 146 files, zero differences — which is why the retro-detection reads git rather than the registry, and why the pre-fix proof runs through the baseline check. `5.9.2` and `5.2.0` are recorded in `KNOWN_COLLISIONS` as unrepairable, checked in both directions so an entry that stops being detectable fails |
| Every console call to an AiSOC API carries a credential | `apps/web/src/lib/api.ts` (the credentialed client); `SECURITY.md` (authentication) | `ci.yml :: Python — Lint & Type-check` (`check_console_auth_headers.py` — parses every `fetch(` and `useSWR(` call under `apps/web/src` and requires the credential to be visible on the same expression, so hiding it behind a local `const headers` a few lines up does not count. `X-Tenant-Id` alone and `credentials: 'include'` are both findings, because neither authenticates against a bearer-token API. 15 self-test cases, of which three pin blind spots in the gate's own first draft — a generic type argument hid both real SWR sites, a multi-line arrow fetcher was truncated at the first newline, and an inline fetcher whose URL is the SWR key has no API path at the call site — and four are controls that must not fire. It refuses a tree with no console sources, and separately refuses one where the console makes no API call at all, since a broken matcher and a console that stopped calling the API print the same clean line) | GATED | **Proven against the pre-fix tree**: 61 findings across 21 files at `079fdb69` (45 bare fetches, 16 SWR fetchers), 0 after. One allowlisted exception, the public `/r/<slug>` replay permalink, which is unauthenticated by design and is kept safe by `replay_redaction.py` rather than by a header; the allowlist may only shrink and a stale entry fails the gate. One limit stated rather than implied: the gate reads the console, so it proves the credential is sent, not that the server requires it — `check_route_auth.py` and `check_route_authz.py` are the other half |
+| An unauthenticated request is never an administrator, and never acts in a real tenant | `SECURITY.md` (authentication); `apps/docs/docs/deployment/single-host.md` | `ci.yml :: Python — Tests` (`services/api/tests/test_anonymous_bypass_requires_optin.py`, 36 cases: the four names in `AUTH_BYPASS_ENVIRONMENTS` each refuse on their own; the explicit `AISOC_DEV_AUTH_BYPASS` opt-in admits; five truthy and five falsy spellings of it; the flag does not re-enable the shim outside a dev environment; five reachable published addresses refuse and five loopback forms admit; one reachable address among several refuses; the demo tenant is not the bootstrap tenant and the canonical id did not move; and every refusal names the condition that withheld it) + the nine vendored copies of `services/fusion/tests/test_tenant_scope.py` kept in lockstep by `sync_vendored_tenant_scope.py --check` + `services/realtime/test/push-tenant.test.ts` | GATED | **Proven against the pre-fix tree**: 35 of 36 API cases fail at `079fdb69` and all 36 pass after, with the tenant-sharing assertion failing as `assert UUID('…0001') != UUID('…0001')`; 4 of 8 realtime cases fail pre-fix, the other 4 being controls that must pass both ways. Two limits stated rather than implied. The loopback check is only as good as what compose passes in `AISOC_PUBLISHED_BIND_ADDRS`: an unset list is treated as a local run, because refusing on an empty list would break `uvicorn app.main:app` for contributors, which is the case the shim exists for — the deployment-path gate is what stops a published stack leaving it unset. And a hostname that is not obviously loopback is treated as reachable, so a DNS name resolving to 127.0.0.1 refuses the bypass; that is the safe direction for an undecidable question, and it is a false refusal, never a false admission |
## Summary
-- GATED: 242
+- GATED: 243
- PARTIAL: 0
- NO GATE: 0 (**every claim is backed by a failing test, and none is now a named deferral either.** The last NO GATE — the weekly benchmark scoreboard running live against `main` — closed in Phase E1: `scripts/check_scoreboard.py` ties the published scoreboard to a deterministic per-PR live-agent MITRE-accuracy run, while the funded weekly `wet-eval.yml` appends the LLM-tier rows. The last two PARTIAL rows closed with `scripts/check_live_agent_floor.py` and a floor derived from ten runs across two environments, not by relabelling. Recount with `scripts/check_claim_gate_matrix.py` rather than trusting these three numbers: they move weekly, and a count copied into prose goes stale in silence.)
diff --git a/scripts/ensure_env.py b/scripts/ensure_env.py
index d291d6c69..7fe0448bc 100644
--- a/scripts/ensure_env.py
+++ b/scripts/ensure_env.py
@@ -136,6 +136,17 @@ GENERATED: dict[str, tuple[Callable[[], str], str]] = {
lambda: secrets.token_urlsafe(32),
"Bearer for realtime's /internal/* fan-out (API/agents -> realtime). Sent as x-internal-token.",
),
+ # Service-to-service bearer for slack-bot's POST /internal/approval-card,
+ # a route that posts into a workspace channel. Its guard treated an unset
+ # value as "no auth needed" whenever AISOC_DEV_MODE was set, and compose
+ # defaults that flag to 1 while this variable appeared only commented out
+ # in `.env.example` — so nothing generated it and the exemption was the
+ # only state a stock install ran in. Generating it lets the guard require
+ # it unconditionally.
+ "AISOC_INTERNAL_TOKEN": (
+ lambda: secrets.token_urlsafe(32),
+ "Bearer for slack-bot's /internal/approval-card (agents -> slack-bot). Sent as X-AiSOC-Internal-Token.",
+ ),
# The datastore passwords. `.env.example` shipped these as
# `aisoc_dev_secret` / `aisoc_app_dev_secret` / `redis_dev_secret` /
# `clickhouse_dev_secret`, and `docker-compose.prod.yml` guarded each one
diff --git a/services/actions/app/security/tenant_scope.py b/services/actions/app/security/tenant_scope.py
index edd831e65..8067c7301 100644
--- a/services/actions/app/security/tenant_scope.py
+++ b/services/actions/app/security/tenant_scope.py
@@ -62,6 +62,7 @@ import base64
import binascii
import hashlib
import hmac
+import ipaddress
import json
import logging
import os
@@ -92,8 +93,29 @@ INSECURE_SECRET_DEFAULTS = frozenset(
)
#: Deterministic demo tenant, matching ``DEMO_TENANT_ID`` in
-#: services/api/app/api/v1/dev_auth.py. Only ever used under AISOC_DEV_MODE.
-DEV_TENANT_ID = uuid.UUID("00000000-0000-0000-0000-000000000001")
+#: services/api/app/api/v1/dev_auth.py. Only ever reached when
+#: :func:`auth_bypass_refusal` returns ``None``.
+#:
+#: This used to be ``…0001``, the canonical tenant that migration 001 seeds
+#: and that ``bootstrap_admin`` puts the real administrator into, so an
+#: unauthenticated caller was scoped to the operator's own data. The canonical
+#: id stays where it is because ten modules pin it; the demo identity moved.
+DEV_TENANT_ID = uuid.UUID("00000000-0000-0000-0000-0000000000de")
+
+#: The environment variable whose only job is to enable the anonymous shim.
+#:
+#: Deliberately not ``AISOC_DEV_MODE``. That flag already selects table
+#: autocreate, docs URLs, the metrics gate, log formatting, the GraphiQL UI
+#: and an ephemeral vault key, and ``docker-compose.yml`` defaults it to ``1``
+#: on ten services. A flag that means nine things cannot be refused for one
+#: of them, so the bypass gets a variable that means exactly one thing and
+#: that no compose file or template sets.
+DEV_AUTH_BYPASS_VAR = "AISOC_DEV_AUTH_BYPASS"
+
+#: Where the deployment publishes this service, comma-separated. Supplied by
+#: compose, because a container binds ``0.0.0.0`` and cannot otherwise know
+#: whether anyone but the operator's own machine can reach it.
+PUBLISHED_BIND_VAR = "AISOC_PUBLISHED_BIND_ADDRS"
#: Header a trusted service uses to declare which tenant it is acting for.
TENANT_HEADER = "X-AiSOC-Tenant-ID"
@@ -200,6 +222,86 @@ def _dev_mode() -> bool:
return os.getenv("AISOC_DEV_MODE", "").strip().lower() in _TRUTHY
+def _is_loopback(address: str) -> bool:
+ """Whether ``address`` reaches only the machine this service runs on."""
+ host = address.strip().lower()
+ if not host:
+ return True
+ if host.startswith("["):
+ host = host.partition("]")[0].lstrip("[")
+ elif host.count(":") == 1:
+ host = host.partition(":")[0]
+ if host in {"localhost", "::1", "ip6-localhost"}:
+ return True
+ try:
+ return ipaddress.ip_address(host).is_loopback
+ except ValueError:
+ # A hostname that is not obviously loopback. When the question cannot
+ # be decided, the safe answer is the one that refuses the bypass.
+ return False
+
+
+def auth_bypass_refusal() -> str | None:
+ """Why an uncredentialed caller must not be admitted, or ``None``.
+
+ Three conditions, where there used to be one. ``AISOC_DEV_MODE`` alone
+ admitted a caller with no credential at all whenever neither a console
+ secret nor a service token was configured — and ``docker-compose.yml``
+ defaults that flag to ``1`` on ten services while generating no service
+ token, so a hand-copied ``.env`` and a plain ``docker compose up`` left
+ this open.
+
+ Returns a sentence for the log, so an operator who expected the bypass
+ learns which condition withheld it instead of seeing an unexplained 503.
+ """
+ if not _dev_mode():
+ return "AISOC_DEV_MODE is not set"
+ if os.getenv(DEV_AUTH_BYPASS_VAR, "").strip().lower() not in _TRUTHY:
+ return (
+ f"{DEV_AUTH_BYPASS_VAR} is not set. AISOC_DEV_MODE selects several "
+ "unrelated development behaviours and defaults to 1 in compose, so "
+ "it no longer enables the anonymous path on its own. Set "
+ f"{DEV_AUTH_BYPASS_VAR}=1 deliberately, or use "
+ "infra/compose/docker-compose.dev.yml, which sets it"
+ )
+ published = [a for a in os.getenv(PUBLISHED_BIND_VAR, "").split(",") if a.strip()]
+ reachable = [a.strip() for a in published if not _is_loopback(a)]
+ if reachable:
+ return (
+ f"{DEV_AUTH_BYPASS_VAR} is set, but this deployment publishes "
+ f"{', '.join(reachable)}, which is not loopback. Bind to 127.0.0.1, "
+ "or configure SECRET_KEY and a service token"
+ )
+ return None
+
+
+#: Refusals and activations already logged, so a hot path does not emit one
+#: line per request. Keyed on the reason rather than counted, so a deployment
+#: whose refusal *changes* still says so once.
+_BYPASS_LOGGED: set[str] = set()
+
+
+def _bypass_permitted() -> bool:
+ """Whether to admit an uncredentialed caller, logging the decision once."""
+ refusal = auth_bypass_refusal()
+ if refusal is None:
+ if "__active__" not in _BYPASS_LOGGED:
+ _BYPASS_LOGGED.add("__active__")
+ logger.warning(
+ "ANONYMOUS ACCESS IS ENABLED on %s. Requests with no credential "
+ "resolve to the demo tenant %s. This is a development "
+ "convenience and must not be used where anyone else can reach "
+ "this host.",
+ SERVICE_NAME,
+ DEV_TENANT_ID,
+ )
+ return True
+ if refusal not in _BYPASS_LOGGED:
+ _BYPASS_LOGGED.add(refusal)
+ logger.info("anonymous access refused on %s: %s", SERVICE_NAME, refusal)
+ return False
+
+
def resolve_console_secret() -> str:
"""The HS256 secret console tokens are signed with, or "" when unusable."""
secret = (os.getenv("SECRET_KEY") or "").strip()
@@ -302,7 +404,7 @@ async def require_console_or_service_auth(
service_token = resolve_service_token()
if not console_secret and not service_token:
- if _dev_mode():
+ if _bypass_permitted():
return TenantPrincipal(
tenant_ids=frozenset({DEV_TENANT_ID}),
subject="dev",
diff --git a/services/actions/tests/test_tenant_scope.py b/services/actions/tests/test_tenant_scope.py
index adc488b19..3ba17f28d 100644
--- a/services/actions/tests/test_tenant_scope.py
+++ b/services/actions/tests/test_tenant_scope.py
@@ -269,6 +269,8 @@ async def test_dev_mode_only_applies_with_nothing_configured(monkeypatch: pytest
every tenant-scoped route.
"""
monkeypatch.setenv("AISOC_DEV_MODE", "1")
+ monkeypatch.setenv("AISOC_DEV_AUTH_BYPASS", "1")
+ monkeypatch.delenv("AISOC_PUBLISHED_BIND_ADDRS", raising=False)
monkeypatch.setenv("SECRET_KEY", SECRET)
monkeypatch.setenv("AISOC_SERVICE_TOKEN", SERVICE_TOKEN)
with pytest.raises(HTTPException) as exc:
@@ -277,10 +279,79 @@ async def test_dev_mode_only_applies_with_nothing_configured(monkeypatch: pytest
monkeypatch.setenv("SECRET_KEY", "")
monkeypatch.setenv("AISOC_SERVICE_TOKEN", "")
+ ts._BYPASS_LOGGED.clear()
principal = await ts.require_console_or_service_auth(authorization=None, x_aisoc_tenant_id=None)
assert principal.tenant_ids == frozenset({ts.DEV_TENANT_ID})
+async def test_dev_mode_alone_no_longer_admits_an_uncredentialed_caller(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ """The defect, stated as a test.
+
+ `AISOC_DEV_MODE` selects table autocreate, docs URLs, the metrics gate,
+ log formatting, the GraphiQL UI and an ephemeral vault key. It also
+ defaults to `1` on ten services in `docker-compose.yml`, while nothing
+ generates a service token. So a hand-copied `.env` and a plain
+ `docker compose up` admitted a caller with no credential at all, and the
+ only thing that had to be true for it was a flag nobody set for this
+ purpose.
+ """
+ monkeypatch.setenv("AISOC_DEV_MODE", "1")
+ monkeypatch.delenv("AISOC_DEV_AUTH_BYPASS", raising=False)
+ monkeypatch.delenv("AISOC_PUBLISHED_BIND_ADDRS", raising=False)
+ monkeypatch.setenv("SECRET_KEY", "")
+ monkeypatch.setenv("AISOC_SERVICE_TOKEN", "")
+ ts._BYPASS_LOGGED.clear()
+ with pytest.raises(HTTPException) as exc:
+ await ts.require_console_or_service_auth(authorization=None, x_aisoc_tenant_id=None)
+ assert exc.value.status_code == 503
+
+
+async def test_the_bypass_is_refused_on_a_reachable_published_address(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ """An anonymous principal is a convenience only on loopback.
+
+ The service binds `0.0.0.0` inside its container and cannot work out for
+ itself whether anyone else can reach it, so compose passes the published
+ addresses in. Both directions, because a check that only ever refuses is
+ indistinguishable from one that removed the shim.
+ """
+ monkeypatch.setenv("AISOC_DEV_MODE", "1")
+ monkeypatch.setenv("AISOC_DEV_AUTH_BYPASS", "1")
+ monkeypatch.setenv("SECRET_KEY", "")
+ monkeypatch.setenv("AISOC_SERVICE_TOKEN", "")
+
+ for published in ("0.0.0.0", "192.168.1.10", "127.0.0.1,0.0.0.0:8080", "aisoc.example.com"):
+ monkeypatch.setenv("AISOC_PUBLISHED_BIND_ADDRS", published)
+ ts._BYPASS_LOGGED.clear()
+ with pytest.raises(HTTPException) as exc:
+ await ts.require_console_or_service_auth(authorization=None, x_aisoc_tenant_id=None)
+ assert exc.value.status_code == 503, published
+
+ for published in ("127.0.0.1", "localhost:8080", "::1", "[::1]:9000"):
+ monkeypatch.setenv("AISOC_PUBLISHED_BIND_ADDRS", published)
+ ts._BYPASS_LOGGED.clear()
+ principal = await ts.require_console_or_service_auth(authorization=None, x_aisoc_tenant_id=None)
+ assert principal.tenant_ids == frozenset({ts.DEV_TENANT_ID}), published
+
+
+def test_the_dev_tenant_is_not_the_canonical_tenant() -> None:
+ """The crux, and it was byte-identical.
+
+ `DEV_TENANT_ID` was `…0001`, the tenant migration 001 seeds and that
+ `bootstrap_admin` puts the real administrator into. So an unauthenticated
+ caller admitted by the shim was scoped to the operator's own data rather
+ than to a demo tenant.
+ """
+ import uuid as _uuid
+
+ assert ts.DEV_TENANT_ID != _uuid.UUID("00000000-0000-0000-0000-000000000001")
+
+
@pytest.mark.asyncio
async def test_a_per_service_token_override_wins(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv(f"AISOC_{ts.SERVICE_NAME}_SERVICE_TOKEN", "service-specific")
diff --git a/services/agents/app/security/tenant_scope.py b/services/agents/app/security/tenant_scope.py
index 028e86e14..95477a53c 100644
--- a/services/agents/app/security/tenant_scope.py
+++ b/services/agents/app/security/tenant_scope.py
@@ -62,6 +62,7 @@ import base64
import binascii
import hashlib
import hmac
+import ipaddress
import json
import logging
import os
@@ -92,8 +93,29 @@ INSECURE_SECRET_DEFAULTS = frozenset(
)
#: Deterministic demo tenant, matching ``DEMO_TENANT_ID`` in
-#: services/api/app/api/v1/dev_auth.py. Only ever used under AISOC_DEV_MODE.
-DEV_TENANT_ID = uuid.UUID("00000000-0000-0000-0000-000000000001")
+#: services/api/app/api/v1/dev_auth.py. Only ever reached when
+#: :func:`auth_bypass_refusal` returns ``None``.
+#:
+#: This used to be ``…0001``, the canonical tenant that migration 001 seeds
+#: and that ``bootstrap_admin`` puts the real administrator into, so an
+#: unauthenticated caller was scoped to the operator's own data. The canonical
+#: id stays where it is because ten modules pin it; the demo identity moved.
+DEV_TENANT_ID = uuid.UUID("00000000-0000-0000-0000-0000000000de")
+
+#: The environment variable whose only job is to enable the anonymous shim.
+#:
+#: Deliberately not ``AISOC_DEV_MODE``. That flag already selects table
+#: autocreate, docs URLs, the metrics gate, log formatting, the GraphiQL UI
+#: and an ephemeral vault key, and ``docker-compose.yml`` defaults it to ``1``
+#: on ten services. A flag that means nine things cannot be refused for one
+#: of them, so the bypass gets a variable that means exactly one thing and
+#: that no compose file or template sets.
+DEV_AUTH_BYPASS_VAR = "AISOC_DEV_AUTH_BYPASS"
+
+#: Where the deployment publishes this service, comma-separated. Supplied by
+#: compose, because a container binds ``0.0.0.0`` and cannot otherwise know
+#: whether anyone but the operator's own machine can reach it.
+PUBLISHED_BIND_VAR = "AISOC_PUBLISHED_BIND_ADDRS"
#: Header a trusted service uses to declare which tenant it is acting for.
TENANT_HEADER = "X-AiSOC-Tenant-ID"
@@ -200,6 +222,86 @@ def _dev_mode() -> bool:
return os.getenv("AISOC_DEV_MODE", "").strip().lower() in _TRUTHY
+def _is_loopback(address: str) -> bool:
+ """Whether ``address`` reaches only the machine this service runs on."""
+ host = address.strip().lower()
+ if not host:
+ return True
+ if host.startswith("["):
+ host = host.partition("]")[0].lstrip("[")
+ elif host.count(":") == 1:
+ host = host.partition(":")[0]
+ if host in {"localhost", "::1", "ip6-localhost"}:
+ return True
+ try:
+ return ipaddress.ip_address(host).is_loopback
+ except ValueError:
+ # A hostname that is not obviously loopback. When the question cannot
+ # be decided, the safe answer is the one that refuses the bypass.
+ return False
+
+
+def auth_bypass_refusal() -> str | None:
+ """Why an uncredentialed caller must not be admitted, or ``None``.
+
+ Three conditions, where there used to be one. ``AISOC_DEV_MODE`` alone
+ admitted a caller with no credential at all whenever neither a console
+ secret nor a service token was configured — and ``docker-compose.yml``
+ defaults that flag to ``1`` on ten services while generating no service
+ token, so a hand-copied ``.env`` and a plain ``docker compose up`` left
+ this open.
+
+ Returns a sentence for the log, so an operator who expected the bypass
+ learns which condition withheld it instead of seeing an unexplained 503.
+ """
+ if not _dev_mode():
+ return "AISOC_DEV_MODE is not set"
+ if os.getenv(DEV_AUTH_BYPASS_VAR, "").strip().lower() not in _TRUTHY:
+ return (
+ f"{DEV_AUTH_BYPASS_VAR} is not set. AISOC_DEV_MODE selects several "
+ "unrelated development behaviours and defaults to 1 in compose, so "
+ "it no longer enables the anonymous path on its own. Set "
+ f"{DEV_AUTH_BYPASS_VAR}=1 deliberately, or use "
+ "infra/compose/docker-compose.dev.yml, which sets it"
+ )
+ published = [a for a in os.getenv(PUBLISHED_BIND_VAR, "").split(",") if a.strip()]
+ reachable = [a.strip() for a in published if not _is_loopback(a)]
+ if reachable:
+ return (
+ f"{DEV_AUTH_BYPASS_VAR} is set, but this deployment publishes "
+ f"{', '.join(reachable)}, which is not loopback. Bind to 127.0.0.1, "
+ "or configure SECRET_KEY and a service token"
+ )
+ return None
+
+
+#: Refusals and activations already logged, so a hot path does not emit one
+#: line per request. Keyed on the reason rather than counted, so a deployment
+#: whose refusal *changes* still says so once.
+_BYPASS_LOGGED: set[str] = set()
+
+
+def _bypass_permitted() -> bool:
+ """Whether to admit an uncredentialed caller, logging the decision once."""
+ refusal = auth_bypass_refusal()
+ if refusal is None:
+ if "__active__" not in _BYPASS_LOGGED:
+ _BYPASS_LOGGED.add("__active__")
+ logger.warning(
+ "ANONYMOUS ACCESS IS ENABLED on %s. Requests with no credential "
+ "resolve to the demo tenant %s. This is a development "
+ "convenience and must not be used where anyone else can reach "
+ "this host.",
+ SERVICE_NAME,
+ DEV_TENANT_ID,
+ )
+ return True
+ if refusal not in _BYPASS_LOGGED:
+ _BYPASS_LOGGED.add(refusal)
+ logger.info("anonymous access refused on %s: %s", SERVICE_NAME, refusal)
+ return False
+
+
def resolve_console_secret() -> str:
"""The HS256 secret console tokens are signed with, or "" when unusable."""
secret = (os.getenv("SECRET_KEY") or "").strip()
@@ -302,7 +404,7 @@ async def require_console_or_service_auth(
service_token = resolve_service_token()
if not console_secret and not service_token:
- if _dev_mode():
+ if _bypass_permitted():
return TenantPrincipal(
tenant_ids=frozenset({DEV_TENANT_ID}),
subject="dev",
diff --git a/services/agents/tests/test_tenant_scope.py b/services/agents/tests/test_tenant_scope.py
index adc488b19..3ba17f28d 100644
--- a/services/agents/tests/test_tenant_scope.py
+++ b/services/agents/tests/test_tenant_scope.py
@@ -269,6 +269,8 @@ async def test_dev_mode_only_applies_with_nothing_configured(monkeypatch: pytest
every tenant-scoped route.
"""
monkeypatch.setenv("AISOC_DEV_MODE", "1")
+ monkeypatch.setenv("AISOC_DEV_AUTH_BYPASS", "1")
+ monkeypatch.delenv("AISOC_PUBLISHED_BIND_ADDRS", raising=False)
monkeypatch.setenv("SECRET_KEY", SECRET)
monkeypatch.setenv("AISOC_SERVICE_TOKEN", SERVICE_TOKEN)
with pytest.raises(HTTPException) as exc:
@@ -277,10 +279,79 @@ async def test_dev_mode_only_applies_with_nothing_configured(monkeypatch: pytest
monkeypatch.setenv("SECRET_KEY", "")
monkeypatch.setenv("AISOC_SERVICE_TOKEN", "")
+ ts._BYPASS_LOGGED.clear()
principal = await ts.require_console_or_service_auth(authorization=None, x_aisoc_tenant_id=None)
assert principal.tenant_ids == frozenset({ts.DEV_TENANT_ID})
+async def test_dev_mode_alone_no_longer_admits_an_uncredentialed_caller(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ """The defect, stated as a test.
+
+ `AISOC_DEV_MODE` selects table autocreate, docs URLs, the metrics gate,
+ log formatting, the GraphiQL UI and an ephemeral vault key. It also
+ defaults to `1` on ten services in `docker-compose.yml`, while nothing
+ generates a service token. So a hand-copied `.env` and a plain
+ `docker compose up` admitted a caller with no credential at all, and the
+ only thing that had to be true for it was a flag nobody set for this
+ purpose.
+ """
+ monkeypatch.setenv("AISOC_DEV_MODE", "1")
+ monkeypatch.delenv("AISOC_DEV_AUTH_BYPASS", raising=False)
+ monkeypatch.delenv("AISOC_PUBLISHED_BIND_ADDRS", raising=False)
+ monkeypatch.setenv("SECRET_KEY", "")
+ monkeypatch.setenv("AISOC_SERVICE_TOKEN", "")
+ ts._BYPASS_LOGGED.clear()
+ with pytest.raises(HTTPException) as exc:
+ await ts.require_console_or_service_auth(authorization=None, x_aisoc_tenant_id=None)
+ assert exc.value.status_code == 503
+
+
+async def test_the_bypass_is_refused_on_a_reachable_published_address(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ """An anonymous principal is a convenience only on loopback.
+
+ The service binds `0.0.0.0` inside its container and cannot work out for
+ itself whether anyone else can reach it, so compose passes the published
+ addresses in. Both directions, because a check that only ever refuses is
+ indistinguishable from one that removed the shim.
+ """
+ monkeypatch.setenv("AISOC_DEV_MODE", "1")
+ monkeypatch.setenv("AISOC_DEV_AUTH_BYPASS", "1")
+ monkeypatch.setenv("SECRET_KEY", "")
+ monkeypatch.setenv("AISOC_SERVICE_TOKEN", "")
+
+ for published in ("0.0.0.0", "192.168.1.10", "127.0.0.1,0.0.0.0:8080", "aisoc.example.com"):
+ monkeypatch.setenv("AISOC_PUBLISHED_BIND_ADDRS", published)
+ ts._BYPASS_LOGGED.clear()
+ with pytest.raises(HTTPException) as exc:
+ await ts.require_console_or_service_auth(authorization=None, x_aisoc_tenant_id=None)
+ assert exc.value.status_code == 503, published
+
+ for published in ("127.0.0.1", "localhost:8080", "::1", "[::1]:9000"):
+ monkeypatch.setenv("AISOC_PUBLISHED_BIND_ADDRS", published)
+ ts._BYPASS_LOGGED.clear()
+ principal = await ts.require_console_or_service_auth(authorization=None, x_aisoc_tenant_id=None)
+ assert principal.tenant_ids == frozenset({ts.DEV_TENANT_ID}), published
+
+
+def test_the_dev_tenant_is_not_the_canonical_tenant() -> None:
+ """The crux, and it was byte-identical.
+
+ `DEV_TENANT_ID` was `…0001`, the tenant migration 001 seeds and that
+ `bootstrap_admin` puts the real administrator into. So an unauthenticated
+ caller admitted by the shim was scoped to the operator's own data rather
+ than to a demo tenant.
+ """
+ import uuid as _uuid
+
+ assert ts.DEV_TENANT_ID != _uuid.UUID("00000000-0000-0000-0000-000000000001")
+
+
@pytest.mark.asyncio
async def test_a_per_service_token_override_wins(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv(f"AISOC_{ts.SERVICE_NAME}_SERVICE_TOKEN", "service-specific")
diff --git a/services/api/app/api/v1/dev_auth.py b/services/api/app/api/v1/dev_auth.py
index e120e496a..4789ec79f 100644
--- a/services/api/app/api/v1/dev_auth.py
+++ b/services/api/app/api/v1/dev_auth.py
@@ -1,32 +1,61 @@
"""Development authentication bypass.
-When ``ENV`` names a dev-class environment and a request arrives without
-a bearer token, we resolve a deterministic demo user. This makes the web
-console usable without seeding users + logging in for every contributor
-running the stack locally.
+When every condition in :func:`app.core.config.auth_bypass_refusal` is met
+and a request arrives without a bearer token, we resolve a deterministic demo
+user. This makes the web console usable without seeding users and logging in
+for every contributor running the stack locally.
The demo IDs are also used by ``services/api/app/scripts/seed_demo.py``
so the data the UI sees actually belongs to the user that the API hands
back.
-Production safety: this module reads ``ENV`` from the live process
-environment at call time (not from the cached :class:`Settings`
-singleton) so a test that does ``monkeypatch.setenv("ENV", "production")``
-mid-suite immediately stops handing back the demo user. The canonical
-allow-list lives in ``app.core.config.AUTH_BYPASS_ENVIRONMENTS`` and
-deliberately excludes ``"test"`` — test suites must seed an
-``Authorization`` header rather than rely on the bypass, or genuine auth
-regressions get masked.
+Three conditions, not one
+-------------------------
+Naming a dev-class environment used to be sufficient, and that is what made
+this a vulnerability rather than a convenience: ``ENVIRONMENT`` defaulted to
+``development`` in ``docker-compose.yml`` and in the ``.env`` the setup script
+writes, so every stock ``docker compose up`` served an uncredentialed request
+as an administrator. The bypass now additionally needs
+``AISOC_DEV_AUTH_BYPASS`` set, which no compose file or template sets, and it
+is refused outright when the deployment publishes a non-loopback address.
+
+The demo tenant is not the operator's tenant
+--------------------------------------------
+``DEMO_TENANT_ID`` used to be ``…0001``, byte-identical to
+``bootstrap_admin.DEFAULT_TENANT_ID``. So the anonymous principal was an
+administrator *in the tenant the real operator had just been bootstrapped
+into*, acting on their alerts and their connector credentials. The demo
+identity now has a tenant of its own, and ``bootstrap_admin`` refuses to
+create a real account in it.
+
+This module reads the environment from ``os.environ`` at call time (not from
+the cached :class:`Settings` singleton) so a test that does
+``monkeypatch.setenv("ENV", "production")`` mid-suite immediately stops
+handing back the demo user. The canonical allow-list lives in
+``app.core.config.AUTH_BYPASS_ENVIRONMENTS`` and deliberately excludes
+``"test"`` — test suites must seed an ``Authorization`` header rather than
+rely on the bypass, or genuine auth regressions get masked.
"""
from __future__ import annotations
+import logging
import uuid
-from app.core.config import current_env_from_os, is_auth_bypass_env
+from app.core.config import auth_bypass_refusal
+
+logger = logging.getLogger(__name__)
# Deterministic demo IDs — kept in sync with seed_demo.py
-DEMO_TENANT_ID: uuid.UUID = uuid.UUID("00000000-0000-0000-0000-000000000001")
+#
+# The demo tenant ends `…00de` and is the demo's alone. It used to be
+# `…0001`, which is the canonical tenant that migration 001 seeds and that
+# `bootstrap_admin` puts the real administrator into — so an anonymous caller
+# was an administrator over the operator's own alerts and connector
+# credentials. The canonical id stays where it is, because ten modules depend
+# on it including the ingest-token minter and the agents ledger; it is the
+# demo identity that moved.
+DEMO_TENANT_ID: uuid.UUID = uuid.UUID("00000000-0000-0000-0000-0000000000de")
DEMO_USER_ID: uuid.UUID = uuid.UUID("00000000-0000-0000-0000-000000000002")
# Deterministic, demo-only credentials. ``example.com`` is reserved by
# RFC 2606 for exactly this: it is valid to ``pydantic.EmailStr`` (unlike
@@ -43,11 +72,37 @@ DEMO_USER_PASSWORD: str = "aisoc-demo"
DEMO_USER_ROLE: str = "admin"
+#: Refusals already logged, so a hot path does not emit one line per request.
+#: Keyed on the reason rather than counted, so a deployment whose refusal
+#: *changes* (someone sets the flag, but on a published address) says so.
+_REFUSALS_LOGGED: set[str] = set()
+
+
def is_dev_mode() -> bool:
- """True if the running environment permits the auth bypass.
+ """True if this process may serve an uncredentialed request as the demo user.
Reads the environment from ``os.environ`` at call time via
- :func:`app.core.config.current_env_from_os` so tests that
+ :func:`app.core.config.auth_bypass_refusal` so tests that
``monkeypatch.setenv(...)`` mid-suite see the change immediately.
+
+ A refusal is logged once per distinct reason. Silence here was part of the
+ original problem: an operator had no way to tell a deployment that had the
+ bypass active from one that did not, because the only difference was a 401
+ they never saw.
"""
- return is_auth_bypass_env(current_env_from_os())
+ refusal = auth_bypass_refusal()
+ if refusal is None:
+ if "__active__" not in _REFUSALS_LOGGED:
+ _REFUSALS_LOGGED.add("__active__")
+ logger.warning(
+ "ANONYMOUS ACCESS IS ENABLED. Requests with no bearer token resolve "
+ "to the demo administrator in tenant %s. This is a development "
+ "convenience and must not be used where anyone else can reach this "
+ "host.",
+ DEMO_TENANT_ID,
+ )
+ return True
+ if refusal not in _REFUSALS_LOGGED:
+ _REFUSALS_LOGGED.add(refusal)
+ logger.info("anonymous access refused: %s", refusal)
+ return False
diff --git a/services/api/app/api/v1/endpoints/push.py b/services/api/app/api/v1/endpoints/push.py
index 2360205f8..63f4fe671 100644
--- a/services/api/app/api/v1/endpoints/push.py
+++ b/services/api/app/api/v1/endpoints/push.py
@@ -53,7 +53,12 @@ async def _proxy(
url = f"{settings.REALTIME_BASE_URL.rstrip('/')}{path}"
headers: dict[str, str] = {"Accept": "application/json"}
if settings.REALTIME_INTERNAL_TOKEN:
+ # Both spellings: realtime's requireInternal reads
+ # `x-internal-token`, which is what the agents service sends, and
+ # this proxy historically sent only the AiSOC-prefixed form — so
+ # the token it stamped was never actually checked.
headers["X-AiSOC-Internal-Token"] = settings.REALTIME_INTERNAL_TOKEN
+ headers["X-Internal-Token"] = settings.REALTIME_INTERNAL_TOKEN
if user is not None:
# Mirror what the realtime push module expects (x-tenant-id /
# x-user-id headers). We also send the AiSOC-prefixed variants so
diff --git a/services/api/app/core/config.py b/services/api/app/core/config.py
index 6e1f29395..06179b39b 100644
--- a/services/api/app/core/config.py
+++ b/services/api/app/core/config.py
@@ -4,6 +4,7 @@ AiSOC — open-source AI Security Operations Center
MIT License
"""
+import ipaddress
import logging
import os
import warnings
@@ -93,10 +94,93 @@ def is_auth_bypass_env(value: str | None) -> bool:
Same canonical set as :func:`is_dev_env` minus ``"test"`` — see the
module-level rationale.
+
+ Naming a dev-class environment is now necessary and **not sufficient**:
+ :func:`auth_bypass_refusal` has to return ``None`` as well.
"""
return _normalize_env(value) in AUTH_BYPASS_ENVIRONMENTS
+# ------------------------------------------------------------------
+# The anonymous shim needs an explicit opt-in, not just an environment name
+# ------------------------------------------------------------------
+# ``ENVIRONMENT`` defaulted to ``development`` in ``docker-compose.yml``, in
+# ``.env.example``, and therefore in the ``.env`` that ``ensure_env.py``
+# copies from it. So every stock ``docker compose up`` served an
+# uncredentialed request as an administrator, and the single-host guide told
+# operators to publish that console on ``0.0.0.0``.
+#
+# An environment name cannot carry that weight on its own. It is a label
+# chosen for logging verbosity and docs URLs, it defaults to a dev-class
+# value, and nothing about setting it says "and make this host anonymous".
+# So the shim now needs a flag whose only meaning is the bypass, which no
+# compose file and no template sets, plus a bind address that keeps the
+# consequence on the operator's own machine.
+
+#: The environment variable whose only job is to enable the anonymous shim.
+#: Deliberately not ``AISOC_DEV_MODE``: that flag already means eight other
+#: things across nine services, and a flag that means many things cannot be
+#: refused for one of them.
+DEV_AUTH_BYPASS_VAR: Final[str] = "AISOC_DEV_AUTH_BYPASS"
+
+#: Where the deployment publishes the console and the API, comma-separated.
+#: Supplied by compose so the API can answer "is anyone but this machine able
+#: to reach me", which it otherwise cannot know: it binds inside a container
+#: and sees only ``0.0.0.0``.
+PUBLISHED_BIND_VAR: Final[str] = "AISOC_PUBLISHED_BIND_ADDRS"
+
+_TRUTHY: Final[frozenset[str]] = frozenset({"1", "true", "yes", "on"})
+
+
+def _is_loopback(address: str) -> bool:
+ """Whether ``address`` reaches only the machine the stack runs on."""
+ host = address.strip().lower()
+ if not host:
+ return True
+ # Strip a port, and the brackets an IPv6 host:port form carries.
+ if host.startswith("["):
+ host = host.partition("]")[0].lstrip("[")
+ elif host.count(":") == 1:
+ host = host.partition(":")[0]
+ if host in {"localhost", "::1", "ip6-localhost"}:
+ return True
+ try:
+ return ipaddress.ip_address(host).is_loopback
+ except ValueError:
+ # A hostname that is not obviously loopback. Treat as reachable: the
+ # safe answer when the question cannot be decided is the one that
+ # refuses the bypass.
+ return False
+
+
+def auth_bypass_refusal(env: str | None = None) -> str | None:
+ """Why the anonymous shim must not serve this request, or ``None``.
+
+ Returns a sentence for the log, so an operator who expected the bypass
+ learns which condition withheld it rather than seeing an unexplained 401.
+ """
+ resolved = _normalize_env(env if env is not None else current_env_from_os())
+ if resolved not in AUTH_BYPASS_ENVIRONMENTS:
+ return f"ENVIRONMENT is {resolved or 'unset'!r}, which is not one of {sorted(AUTH_BYPASS_ENVIRONMENTS)}"
+ if os.getenv(DEV_AUTH_BYPASS_VAR, "").strip().lower() not in _TRUTHY:
+ return (
+ f"{DEV_AUTH_BYPASS_VAR} is not set. Naming a development "
+ "environment no longer enables the anonymous shim on its own; set "
+ f"{DEV_AUTH_BYPASS_VAR}=1 deliberately, or use "
+ "infra/compose/docker-compose.dev.yml, which sets it"
+ )
+ published = [a for a in os.getenv(PUBLISHED_BIND_VAR, "").split(",") if a.strip()]
+ reachable = [a.strip() for a in published if not _is_loopback(a)]
+ if reachable:
+ return (
+ f"{DEV_AUTH_BYPASS_VAR} is set, but this deployment publishes "
+ f"{', '.join(reachable)}, which is not loopback. An anonymous "
+ "administrator on a reachable address is not a developer "
+ "convenience. Bind to 127.0.0.1, or authenticate"
+ )
+ return None
+
+
def current_env_from_os() -> str:
"""Read the current environment from ``os.environ`` at call time.
diff --git a/services/api/app/scripts/bootstrap_admin.py b/services/api/app/scripts/bootstrap_admin.py
index 322e2e725..0455e26c3 100644
--- a/services/api/app/scripts/bootstrap_admin.py
+++ b/services/api/app/scripts/bootstrap_admin.py
@@ -49,6 +49,7 @@ import uuid
from email_validator import EmailNotValidError, validate_email
from sqlalchemy import func, select
+from app.api.v1.dev_auth import DEMO_TENANT_ID
from app.core.security import get_password_hash
from app.db.database import AsyncSessionLocal
from app.models.tenant import Tenant, User
@@ -56,6 +57,14 @@ from app.models.tenant import Tenant, User
# Matches the tenant seeded by migration 001. Reused so a bootstrap on an
# already-migrated database adopts that tenant rather than creating a second
# one that none of the seeded rows belong to.
+#
+# This must never equal `dev_auth.DEMO_TENANT_ID`. The two used to be the same
+# UUID, and that is what turned the development auth bypass into a
+# vulnerability rather than a convenience: the anonymous principal the API
+# hands back was an administrator in whichever tenant this script had just
+# created the operator's real account in, acting on their alerts and their
+# connector credentials. `_ensure_tenant` refuses to proceed if they are ever
+# made equal again, rather than trusting a comment to hold.
DEFAULT_TENANT_ID = uuid.UUID("00000000-0000-0000-0000-000000000001")
# `.internal` is reserved by ICANN for private use and — unlike `.local`,
@@ -144,7 +153,20 @@ def resolve_password(args: argparse.Namespace) -> tuple[str, bool]:
async def _ensure_tenant(session) -> Tenant:
- """Adopt the migration-seeded tenant, else any existing one, else create it."""
+ """Adopt the migration-seeded tenant, else any existing one, else create it.
+
+ Never the demo tenant. The bypass hands an uncredentialed caller an
+ administrator in `DEMO_TENANT_ID`, so a real account sharing that tenant
+ is a real account anyone can act as.
+ """
+ if DEFAULT_TENANT_ID == DEMO_TENANT_ID:
+ raise RuntimeError(
+ "the bootstrap tenant and the demo tenant are the same "
+ f"({DEFAULT_TENANT_ID}). The development auth bypass makes an "
+ "uncredentialed caller an administrator of the demo tenant, so a "
+ "real account must not live there. Refusing to bootstrap."
+ )
+
result = await session.execute(select(Tenant).where(Tenant.id == DEFAULT_TENANT_ID))
tenant = result.scalar_one_or_none()
if tenant is not None:
@@ -152,8 +174,10 @@ async def _ensure_tenant(session) -> Tenant:
# A deployment that was provisioned some other way already has a tenant;
# attaching the administrator to a brand-new one would make them an admin
- # of nothing, with every existing alert invisible.
- result = await session.execute(select(Tenant).order_by(Tenant.created_at).limit(1))
+ # of nothing, with every existing alert invisible. The demo tenant is
+ # excluded from that adoption: `seed_demo` creates it, so on a stack where
+ # the demo ran first it can easily be the oldest row.
+ result = await session.execute(select(Tenant).where(Tenant.id != DEMO_TENANT_ID).order_by(Tenant.created_at).limit(1))
tenant = result.scalar_one_or_none()
if tenant is not None:
return tenant
diff --git a/services/api/app/scripts/mint_api_token.py b/services/api/app/scripts/mint_api_token.py
new file mode 100644
index 000000000..78f35014a
--- /dev/null
+++ b/services/api/app/scripts/mint_api_token.py
@@ -0,0 +1,214 @@
+"""Mint a short-lived bearer token for an existing administrator.
+
+ docker compose run --rm api python -m app.scripts.mint_api_token
+ # or, from the repo root:
+ make api-token
+
+Prints an access token for the account ``bootstrap_admin`` created, so a
+script can read ``/api/v1/alerts`` the way the console does.
+
+Why this exists
+---------------
+``make smoke`` drives one real event through the pipeline and then asks the
+API whether it became an alert. That read used to succeed with no credential
+at all, because the API resolved an uncredentialed request to a demo
+administrator in a development-class environment, and every documented way to
+start the stack produced one. Closing that turned the last stage of the golden
+pipeline into a 401.
+
+The harness therefore needs a credential, and it should obtain one the way a
+person would rather than being handed a bypass. This is the sibling of
+``mint_ingest_token``: a shipped command, so the step that calls it also
+covers a path an operator can follow.
+
+What it will not do
+-------------------
+Create an account, change a password, or grant a role. It mints a token for a
+principal that already exists, and fails with an actionable message when none
+does. A minting command that could create its own administrator would be a
+second bootstrap path with none of the checks the first one has.
+
+It never mints for the demo tenant. That tenant is what the development auth
+shim hands an anonymous caller, so a *real* token scoped to it would make the
+bypass reachable from outside the bypass.
+
+This is not a dev-mode shortcut and does not consult ``AISOC_DEV_MODE``.
+"""
+
+from __future__ import annotations
+
+import argparse
+import asyncio
+import sys
+import uuid
+from datetime import timedelta
+
+from sqlalchemy import select
+
+from app.api.v1.dev_auth import DEMO_TENANT_ID
+from app.core.security import create_access_token
+from app.db.database import AsyncSessionLocal
+from app.models.tenant import Tenant, User
+from app.scripts.bootstrap_admin import DEFAULT_TENANT_ID
+
+#: Short by design. This is a credential printed to a terminal and pasted into
+#: an environment variable by a test harness, not a session someone keeps.
+DEFAULT_TTL_MINUTES = 30
+
+#: Roles a token may be minted for, most privileged first. The read the golden
+#: pipeline performs needs `alerts:read`, which every one of these carries.
+_PREFERRED_ROLES = ("admin", "platform_admin", "tenant_admin", "soc_lead")
+
+
+async def _resolve_tenant(session, tenant_ref: str | None) -> uuid.UUID:
+ """Which tenant to mint inside. Never the demo one.
+
+ Every user query below carries this as a predicate. `users` has no
+ row-level security — excluded by design, because the login lookup has to
+ find an account before a tenant is known — so a predicate is the only
+ control there is, and `scripts/check_tenant_query_predicates.py` enforces
+ it. The first draft of this command selected "the most privileged active
+ account anywhere", which is both unscoped and a vague target on a
+ multi-tenant deployment; the gate caught it.
+ """
+ if tenant_ref:
+ try:
+ wanted = uuid.UUID(tenant_ref)
+ except ValueError:
+ raise SystemExit(f"--tenant must be a UUID, got {tenant_ref!r}") from None
+ if wanted == DEMO_TENANT_ID:
+ raise SystemExit(
+ "that is the demo tenant, which the development auth shim already hands to "
+ "anonymous callers. Refusing to mint a real token for it."
+ )
+ return wanted
+
+ # The tenant `bootstrap_admin` uses, and the one it falls back to when
+ # that row is absent: the oldest that is not the demo tenant.
+ result = await session.execute(select(Tenant).where(Tenant.id == DEFAULT_TENANT_ID))
+ if result.scalar_one_or_none() is not None:
+ return DEFAULT_TENANT_ID
+
+ result = await session.execute(select(Tenant).where(Tenant.id != DEMO_TENANT_ID).order_by(Tenant.created_at).limit(1))
+ tenant = result.scalar_one_or_none()
+ if tenant is None:
+ raise SystemExit("no tenant to mint inside.\nCreate one with: docker compose run --rm api python -m app.scripts.bootstrap_admin")
+ return tenant.id
+
+
+async def _resolve_user(session, email: str | None, tenant_id: uuid.UUID) -> User:
+ """The account to mint for inside `tenant_id`: the one named, else the
+ best-privileged one."""
+ if email:
+ result = await session.execute(
+ select(User).where(
+ User.email == email,
+ User.tenant_id == tenant_id,
+ User.is_active == True, # noqa: E712
+ )
+ )
+ user = result.scalar_one_or_none()
+ if user is None:
+ raise SystemExit(
+ f"no active account with the address {email!r} in tenant {tenant_id}.\n"
+ "Create one with: docker compose run --rm api python -m app.scripts.bootstrap_admin"
+ )
+ return user
+
+ result = await session.execute(
+ select(User).where(
+ User.tenant_id == tenant_id,
+ User.is_active == True, # noqa: E712
+ )
+ )
+ users = list(result.scalars())
+ if not users:
+ raise SystemExit(
+ f"no active account in tenant {tenant_id} to mint for.\n"
+ "Create one with: docker compose run --rm api python -m app.scripts.bootstrap_admin"
+ )
+
+ def rank(candidate: User) -> tuple[int, str]:
+ role = (candidate.role or "").strip()
+ position = _PREFERRED_ROLES.index(role) if role in _PREFERRED_ROLES else len(_PREFERRED_ROLES)
+ return (position, str(candidate.created_at))
+
+ return sorted(users, key=rank)[0]
+
+
+async def mint(*, email: str | None, tenant_ref: str | None, ttl_minutes: int) -> tuple[str, User]:
+ async with AsyncSessionLocal() as session:
+ tenant_id = await _resolve_tenant(session, tenant_ref)
+ user = await _resolve_user(session, email, tenant_id)
+ token = create_access_token(
+ {"sub": str(user.id), "type": "access"},
+ expires_delta=timedelta(minutes=ttl_minutes),
+ )
+ return token, user
+
+
+def _print_human(token: str, user: User, *, ttl_minutes: int) -> None:
+ print()
+ print(" Access token minted.")
+ print()
+ print(f" account {user.email} ({user.role})")
+ print(f" tenant {user.tenant_id}")
+ print(f" expires in {ttl_minutes} minutes")
+ print()
+ print(f" {token}")
+ print()
+ print(" Use it as a bearer token:")
+ print()
+ print(f' curl -H "Authorization: Bearer $TOKEN" {"http://localhost:8000"}/api/v1/alerts')
+ print()
+
+
+def build_parser() -> argparse.ArgumentParser:
+ parser = argparse.ArgumentParser(
+ prog="mint_api_token",
+ description="Mint a short-lived bearer token for an existing administrator.",
+ )
+ parser.add_argument(
+ "--email",
+ default=None,
+ help="the account to mint for; defaults to the most privileged active one",
+ )
+ parser.add_argument(
+ "--tenant",
+ default=None,
+ help="tenant UUID to mint inside; defaults to the tenant bootstrap_admin uses",
+ )
+ parser.add_argument(
+ "--ttl-minutes",
+ type=int,
+ default=DEFAULT_TTL_MINUTES,
+ help=f"how long the token is valid (default {DEFAULT_TTL_MINUTES})",
+ )
+ parser.add_argument(
+ "--quiet",
+ action="store_true",
+ help='print only the token, for AISOC_API_TOKEN="$(... --quiet)"',
+ )
+ return parser
+
+
+async def _run(args: argparse.Namespace) -> int:
+ if args.ttl_minutes < 1:
+ print("--ttl-minutes must be at least 1", file=sys.stderr)
+ return 2
+ token, user = await mint(email=args.email, tenant_ref=args.tenant, ttl_minutes=args.ttl_minutes)
+ if args.quiet:
+ # Nothing but the token on stdout, so a shell can capture it with
+ # AISOC_API_TOKEN="$(... --quiet)" without parsing.
+ print(token)
+ else:
+ _print_human(token, user, ttl_minutes=args.ttl_minutes)
+ return 0
+
+
+def main() -> int:
+ return asyncio.run(_run(build_parser().parse_args()))
+
+
+if __name__ == "__main__":
+ raise SystemExit(main())
diff --git a/services/api/tests/test_anonymous_bypass_requires_optin.py b/services/api/tests/test_anonymous_bypass_requires_optin.py
new file mode 100644
index 000000000..75f010f9b
--- /dev/null
+++ b/services/api/tests/test_anonymous_bypass_requires_optin.py
@@ -0,0 +1,250 @@
+"""Naming a development environment does not make a host anonymous.
+
+The defect this pins was not that the bypass existed. It was that the only
+thing standing between an uncredentialed request and an administrator was the
+string `development`, which:
+
+* `docker-compose.yml` set as the default for `ENVIRONMENT`;
+* `.env.example` wrote into `.env`, which `scripts/ensure_env.py` copies
+ verbatim on first run;
+* the single-host guide never mentioned, while telling operators to publish
+ the console on `0.0.0.0`.
+
+So every stock `docker compose up` served a credential-free request as
+`admin`, and the tenant that admin belonged to was byte-identical to the one
+`bootstrap_admin` had just put the operator's real account into.
+
+Three conditions now gate the shim, and each is asserted in both directions.
+One-directional coverage is what let this ship: the existing suite asserted
+that `production` refuses, and nothing asserted that `development` alone
+should not be enough.
+"""
+
+from __future__ import annotations
+
+import uuid
+
+import pytest
+from fastapi import Depends, FastAPI
+from fastapi.testclient import TestClient
+
+
+def _probe_app(get_current_user) -> FastAPI: # noqa: ANN001 - dependency callable
+ """A one-route app whose only job is to resolve the current user.
+
+ The dependency is a default value rather than `Annotated[..., Depends(...)]`
+ because this module uses postponed annotations: the annotation would be a
+ string, FastAPI would resolve it against this module's globals where the
+ dependency is a local, and `user` would degrade to a query parameter —
+ giving 422 on every request and testing nothing about authentication.
+ """
+ app = FastAPI()
+
+ @app.get("/probe")
+ async def probe(user=Depends(get_current_user)): # noqa: ANN001, B008
+ return {
+ "user_id": str(getattr(user, "user_id", None)),
+ "tenant_id": str(getattr(user, "tenant_id", None)),
+ "role": getattr(user, "role", None),
+ }
+
+ return app
+
+
+def client_with(monkeypatch: pytest.MonkeyPatch):
+ """Build a probe client under a named environment, flag and bind address.
+
+ Deliberately does **not** `importlib.reload` anything. The shim reads
+ `os.environ` at call time precisely so that it does not need to, and a
+ reload here is actively harmful: it rebinds every function object in
+ `deps`, so the `dependency_overrides` other test modules keyed on the old
+ objects stop matching and their routes fall through to real authentication.
+
+ That is not hypothetical. The first draft of this file reloaded both
+ modules, copying an older test that does the same, and took 146 unrelated
+ tests down with it across `test_content_route_permissions`,
+ `test_discarded_permission_dependencies` and `test_platform_route_permissions`
+ — every one of which passes in isolation. The older test got away with it
+ only because its filename sorts near the end of the suite.
+ """
+
+ def _build(
+ environment: str = "development",
+ *,
+ bypass: str | None = None,
+ published: str | None = None,
+ ) -> TestClient:
+ # `current_env_from_os` reads ENV first, so both are set; a stale ENV
+ # is precisely what shadowed an operator's ENVIRONMENT on ingest.
+ monkeypatch.setenv("ENVIRONMENT", environment)
+ monkeypatch.delenv("ENV", raising=False)
+ for var, value in (
+ ("AISOC_DEV_AUTH_BYPASS", bypass),
+ ("AISOC_PUBLISHED_BIND_ADDRS", published),
+ ):
+ if value is None:
+ monkeypatch.delenv(var, raising=False)
+ else:
+ monkeypatch.setenv(var, value)
+
+ from app.api.v1 import deps, dev_auth
+
+ # The refusal log is deduplicated per reason for the life of the
+ # process, and each case wants its own reason recorded.
+ dev_auth._REFUSALS_LOGGED.clear()
+ return TestClient(_probe_app(deps.get_current_user), raise_server_exceptions=False)
+
+ return _build
+
+
+class TestTheEnvironmentNameIsNotEnough:
+ def test_development_alone_refuses_an_unauthenticated_request(self, client_with) -> None:
+ """The defect, stated as a test.
+
+ This is the exact configuration every documented deployment produced:
+ `ENVIRONMENT=development` from the compose default, no explicit
+ opt-in anywhere, and an administrator handed to anyone who asked.
+ """
+ response = client_with("development").get("/probe")
+ assert response.status_code == 401, (
+ "an uncredentialed request was served under ENVIRONMENT=development "
+ "with no explicit opt-in. That is the default every `docker compose "
+ "up` and every `.env` written by ensure_env.py produced"
+ )
+
+ @pytest.mark.parametrize("environment", ["development", "dev", "local", "demo"])
+ def test_no_bypass_environment_is_sufficient_on_its_own(self, client_with, environment: str) -> None:
+ """All four names in AUTH_BYPASS_ENVIRONMENTS, not just the default one."""
+ assert client_with(environment).get("/probe").status_code == 401
+
+ def test_the_explicit_flag_enables_it(self, client_with) -> None:
+ """The other direction, so this cannot pass on a build with no shim."""
+ response = client_with("development", bypass="1").get("/probe")
+ assert response.status_code == 200
+ assert response.json()["role"] == "admin"
+
+ @pytest.mark.parametrize("truthy", ["1", "true", "TRUE", "yes", "on"])
+ def test_the_flag_accepts_the_documented_spellings(self, client_with, truthy: str) -> None:
+ assert client_with("development", bypass=truthy).get("/probe").status_code == 200
+
+ @pytest.mark.parametrize("falsy", ["0", "false", "no", "off", ""])
+ def test_the_flag_is_not_enabled_by_any_other_value(self, client_with, falsy: str) -> None:
+ """`AISOC_DEV_MODE=0` style values must read as off, not as "set"."""
+ assert client_with("development", bypass=falsy).get("/probe").status_code == 401
+
+ @pytest.mark.parametrize("environment", ["production", "staging", "prod"])
+ def test_the_flag_does_not_re_enable_it_outside_a_dev_environment(self, client_with, environment: str) -> None:
+ """Belt and braces: the flag narrows, it never widens.
+
+ `staging` is included deliberately. It is not in the allow-list, and a
+ reader could reasonably assume any named environment is safe.
+ """
+ assert client_with(environment, bypass="1").get("/probe").status_code == 401
+
+
+class TestTheBypassIsRefusedOnAReachableAddress:
+ """An anonymous administrator is a developer convenience only on loopback.
+
+ The API cannot work this out for itself: it binds inside a container and
+ sees `0.0.0.0` regardless of what the host published. So compose passes
+ the published addresses in, and the shim refuses when any of them is
+ reachable from somewhere else. That is the half the single-host guide
+ needed, since it instructs `AISOC_CONSOLE_BIND_ADDR=0.0.0.0`.
+ """
+
+ @pytest.mark.parametrize("address", ["0.0.0.0", "192.168.1.10", "10.0.0.4", "[::]", "aisoc.example.com"])
+ def test_a_reachable_published_address_refuses_the_bypass(self, client_with, address: str) -> None:
+ response = client_with("development", bypass="1", published=address).get("/probe")
+ assert response.status_code == 401, f"the bypass served a request while the deployment published {address}"
+
+ @pytest.mark.parametrize("address", ["127.0.0.1", "localhost", "::1", "127.0.0.1:3000", "[::1]:8000"])
+ def test_a_loopback_published_address_still_permits_it(self, client_with, address: str) -> None:
+ assert client_with("development", bypass="1", published=address).get("/probe").status_code == 200
+
+ def test_one_reachable_address_among_several_is_enough_to_refuse(self, client_with) -> None:
+ """The console on loopback and the API published is still published."""
+ response = client_with("development", bypass="1", published="127.0.0.1:3000,0.0.0.0:8000").get("/probe")
+ assert response.status_code == 401
+
+ def test_an_unset_bind_list_does_not_refuse(self, client_with) -> None:
+ """A deployment that tells us nothing is treated as a local run.
+
+ Refusing on an empty list would break `python -m uvicorn app.main:app`
+ for contributors, which is the case the shim exists for. The
+ deployment-path gate is what stops a published stack leaving it
+ unset.
+ """
+ assert client_with("development", bypass="1", published=None).get("/probe").status_code == 200
+
+
+class TestTheDemoIdentityHasItsOwnTenant:
+ def test_the_demo_tenant_is_not_the_bootstrap_tenant(self) -> None:
+ """The crux of the finding, and it was byte-identical.
+
+ `dev_auth.DEMO_TENANT_ID` and `bootstrap_admin.DEFAULT_TENANT_ID` were
+ both `…0001`, so the anonymous administrator operated inside the
+ tenant holding the operator's real alerts and vault-encrypted
+ connector credentials.
+ """
+ from app.api.v1.dev_auth import DEMO_TENANT_ID
+ from app.scripts.bootstrap_admin import DEFAULT_TENANT_ID
+
+ assert DEMO_TENANT_ID != DEFAULT_TENANT_ID, (
+ "the anonymous demo principal shares a tenant with the real administrator this deployment bootstraps"
+ )
+
+ def test_the_canonical_tenant_id_did_not_move(self) -> None:
+ """It is the demo identity that moved, and it had to be.
+
+ Ten modules pin `…0001` as the canonical tenant, including the
+ ingest-token minter, the agents ledger, and the dev principal in nine
+ vendored `tenant_scope` copies. Moving that instead would have
+ orphaned every row migration 001 seeds.
+ """
+ from app.scripts.bootstrap_admin import DEFAULT_TENANT_ID
+
+ assert DEFAULT_TENANT_ID == uuid.UUID("00000000-0000-0000-0000-000000000001")
+
+ def test_the_bypass_serves_the_demo_tenant_not_the_operators(self, client_with) -> None:
+ from app.api.v1.dev_auth import DEMO_TENANT_ID
+
+ response = client_with("development", bypass="1").get("/probe")
+ assert response.status_code == 200
+ assert response.json()["tenant_id"] == str(DEMO_TENANT_ID)
+
+
+class TestTheRefusalSaysWhy:
+ """A 401 with no explanation is how this stayed invisible.
+
+ An operator who expected the bypass, and a contributor who expected it,
+ both see the same 401. The reason has to be available somewhere, and the
+ one place both will look is the service log.
+ """
+
+ def test_each_refusal_names_the_condition_that_withheld_it(self, monkeypatch) -> None:
+ from app.core.config import auth_bypass_refusal
+
+ monkeypatch.setenv("ENVIRONMENT", "production")
+ monkeypatch.delenv("ENV", raising=False)
+ assert "not one of" in (auth_bypass_refusal() or "")
+
+ monkeypatch.setenv("ENVIRONMENT", "development")
+ monkeypatch.delenv("AISOC_DEV_AUTH_BYPASS", raising=False)
+ assert "AISOC_DEV_AUTH_BYPASS is not set" in (auth_bypass_refusal() or "")
+
+ monkeypatch.setenv("AISOC_DEV_AUTH_BYPASS", "1")
+ monkeypatch.setenv("AISOC_PUBLISHED_BIND_ADDRS", "0.0.0.0:3000")
+ refusal = auth_bypass_refusal() or ""
+ assert "not loopback" in refusal
+ assert "0.0.0.0:3000" in refusal
+
+ monkeypatch.setenv("AISOC_PUBLISHED_BIND_ADDRS", "127.0.0.1:3000")
+ assert auth_bypass_refusal() is None
+
+ def test_activation_is_logged_as_a_warning(self, client_with, caplog) -> None:
+ with caplog.at_level("WARNING"):
+ client_with("development", bypass="1").get("/probe")
+ assert any("ANONYMOUS ACCESS IS ENABLED" in record.message for record in caplog.records), (
+ "a deployment with anonymous access active must say so at startup"
+ )
diff --git a/services/api/tests/test_dev_mode_unification.py b/services/api/tests/test_dev_mode_unification.py
index 93e0748b5..a809d870e 100644
--- a/services/api/tests/test_dev_mode_unification.py
+++ b/services/api/tests/test_dev_mode_unification.py
@@ -379,6 +379,12 @@ def test_dev_auth_is_dev_mode_reads_live_env(monkeypatch):
monkeypatch.setenv("ENV", "development")
monkeypatch.delenv("ENVIRONMENT", raising=False)
+ # The environment name is necessary and no longer sufficient: the shim
+ # also needs an explicit opt-in that no compose file sets. What this test
+ # is about is that the answer tracks a mid-test change rather than being
+ # cached, so the flag is held on for both halves.
+ monkeypatch.setenv("AISOC_DEV_AUTH_BYPASS", "1")
+ monkeypatch.delenv("AISOC_PUBLISHED_BIND_ADDRS", raising=False)
assert dev_auth.is_dev_mode() is True
monkeypatch.setenv("ENV", "production")
@@ -400,6 +406,8 @@ def test_dev_auth_is_dev_mode_honors_environment_fallback(monkeypatch):
monkeypatch.delenv("ENV", raising=False)
monkeypatch.setenv("ENVIRONMENT", "development")
+ monkeypatch.setenv("AISOC_DEV_AUTH_BYPASS", "1")
+ monkeypatch.delenv("AISOC_PUBLISHED_BIND_ADDRS", raising=False)
assert dev_auth.is_dev_mode() is True
monkeypatch.setenv("ENVIRONMENT", "production")
diff --git a/services/api/tests/test_mint_api_token.py b/services/api/tests/test_mint_api_token.py
new file mode 100644
index 000000000..433011aa6
--- /dev/null
+++ b/services/api/tests/test_mint_api_token.py
@@ -0,0 +1,186 @@
+"""The API-token minter creates nothing and never mints for the demo tenant.
+
+`make smoke` drives one real event through the pipeline and then asks the API
+whether it became an alert. That read used to succeed with no credential,
+because an uncredentialed request resolved to a demo administrator in a
+development-class environment and every documented way to start the stack
+produced one. So the golden pipeline was reading the API as an anonymous
+administrator, without anybody having decided that.
+
+This command is how the harness gets a real credential instead. Two properties
+matter more than the happy path:
+
+* it mints for an account that already exists and refuses otherwise, because a
+ minting command that could create its own administrator would be a second
+ bootstrap path with none of the checks the first one has;
+* it never mints for the demo tenant, because that tenant is what the
+ development auth shim hands an anonymous caller — a real token scoped to it
+ would make the bypass reachable from outside the bypass.
+"""
+
+from __future__ import annotations
+
+import uuid
+from datetime import UTC, datetime, timedelta
+
+import pytest
+from app.api.v1.dev_auth import DEMO_TENANT_ID
+from app.core.security import decode_token
+from app.scripts import mint_api_token
+from app.scripts.bootstrap_admin import DEFAULT_TENANT_ID
+
+
+class _User:
+ """Enough of the ORM row for the resolver, which reads four fields."""
+
+ def __init__(self, *, email: str, role: str, tenant_id: uuid.UUID, created_at: datetime | None = None):
+ self.id = uuid.uuid4()
+ self.email = email
+ self.role = role
+ self.tenant_id = tenant_id
+ self.is_active = True
+ self.created_at = created_at or datetime.now(UTC)
+
+
+class _Result:
+ """Rows are `object` because this double serves both resolvers: one asks
+ for tenants and the other for users."""
+
+ def __init__(self, rows: list[object]):
+ self._rows = rows
+
+ def scalar_one_or_none(self) -> object | None:
+ return self._rows[0] if self._rows else None
+
+ def scalars(self) -> list[object]:
+ return self._rows
+
+
+class _Session:
+ """Returns a fixed row set, and records that nothing was written."""
+
+ def __init__(self, rows: list[object]):
+ self._rows = rows
+ self.added: list[object] = []
+ self.commits = 0
+
+ async def execute(self, _statement) -> _Result: # noqa: ANN001
+ return _Result(list(self._rows))
+
+ def add(self, obj: object) -> None:
+ self.added.append(obj)
+
+ async def commit(self) -> None:
+ self.commits += 1
+
+
+async def test_it_refuses_when_no_account_exists() -> None:
+ session = _Session([])
+ with pytest.raises(SystemExit) as exit_info:
+ await mint_api_token._resolve_user(session, None, DEFAULT_TENANT_ID)
+ message = str(exit_info.value)
+ assert "no active account" in message
+ assert "bootstrap_admin" in message, "the refusal has to say what to run"
+
+
+async def test_it_creates_nothing() -> None:
+ """A minting command that could create an administrator would be a second
+ bootstrap path with none of the checks the first one has."""
+ session = _Session([_User(email="[email protected]", role="admin", tenant_id=DEFAULT_TENANT_ID)])
+ await mint_api_token._resolve_user(session, None, DEFAULT_TENANT_ID)
+ assert session.added == []
+ assert session.commits == 0
+
+
+async def test_it_refuses_the_demo_tenant_outright() -> None:
+ """Asking for it by UUID is refused before any account is looked up.
+
+ That tenant is what the development auth shim hands an anonymous caller,
+ so a real token scoped to it would make the bypass reachable from outside
+ the bypass.
+ """
+ session = _Session([])
+ with pytest.raises(SystemExit) as exit_info:
+ await mint_api_token._resolve_tenant(session, str(DEMO_TENANT_ID))
+ assert "demo tenant" in str(exit_info.value)
+
+
+async def test_it_defaults_to_the_tenant_bootstrap_admin_uses() -> None:
+ """The path with no `--tenant`, which had no test at first.
+
+ Every assertion here passed while `DEFAULT_TENANT_ID` was not even
+ imported, because each one supplied a tenant explicitly and so never
+ reached this branch. Ruff found the undefined name; the suite did not.
+ """
+
+ class _TenantRow:
+ id = DEFAULT_TENANT_ID
+
+ session = _Session([_TenantRow()])
+ assert await mint_api_token._resolve_tenant(session, None) == DEFAULT_TENANT_ID
+
+
+async def test_it_refuses_when_there_is_no_tenant_at_all() -> None:
+ session = _Session([])
+ with pytest.raises(SystemExit) as exit_info:
+ await mint_api_token._resolve_tenant(session, None)
+ assert "no tenant to mint inside" in str(exit_info.value)
+
+
+async def test_a_non_uuid_tenant_is_rejected() -> None:
+ session = _Session([])
+ with pytest.raises(SystemExit) as exit_info:
+ await mint_api_token._resolve_tenant(session, "default")
+ assert "must be a UUID" in str(exit_info.value)
+
+
+async def test_it_refuses_an_address_that_does_not_exist() -> None:
+ session = _Session([])
+ with pytest.raises(SystemExit) as exit_info:
+ await mint_api_token._resolve_user(session, "[email protected]", DEFAULT_TENANT_ID)
+ assert "[email protected]" in str(exit_info.value)
+
+
+async def test_it_prefers_the_most_privileged_account() -> None:
+ """Not merely the oldest. The read the harness performs needs
+ `alerts:read`, and a viewer created first would not carry it."""
+ viewer = _User(
+ role="viewer",
+ tenant_id=DEFAULT_TENANT_ID,
+ created_at=datetime.now(UTC) - timedelta(days=30),
+ )
+ admin = _User(email="[email protected]", role="admin", tenant_id=DEFAULT_TENANT_ID)
+ session = _Session([viewer, admin])
+ assert (await mint_api_token._resolve_user(session, None, DEFAULT_TENANT_ID)).email == "[email protected]"
+
+
+def test_the_token_carries_the_claims_the_api_verifies() -> None:
+ """`get_current_user` reads `sub` and requires `type == "access"`.
+
+ Asserted against the real `decode_token`, so a change to the signing
+ algorithm or the secret resolution fails here rather than at the first
+ 401 in an end-to-end run.
+ """
+ user_id = uuid.uuid4()
+ from app.core.security import create_access_token
+
+ token = create_access_token({"sub": str(user_id), "type": "access"}, expires_delta=timedelta(minutes=30))
+ payload = decode_token(token)
+ assert payload["sub"] == str(user_id)
+ assert payload["type"] == "access"
+ assert payload["exp"] > datetime.now(UTC).timestamp()
+
+
+def test_the_default_ttl_is_short() -> None:
+ """This is pasted into an environment variable by a test harness, not a
+ session somebody keeps."""
+ assert mint_api_token.DEFAULT_TTL_MINUTES <= 60
diff --git a/services/api/tests/test_production_environment_requires_auth.py b/services/api/tests/test_production_environment_requires_auth.py
index 8056020a1..6d7b2aaeb 100644
--- a/services/api/tests/test_production_environment_requires_auth.py
+++ b/services/api/tests/test_production_environment_requires_auth.py
@@ -20,8 +20,6 @@ product and a silently broken developer experience.
from __future__ import annotations
-import importlib
-
import pytest
from fastapi import Depends, FastAPI
from fastapi.testclient import TestClient
@@ -47,19 +45,29 @@ def _probe_app(get_current_user) -> FastAPI: # noqa: ANN001 - dependency callab
@pytest.fixture
def client_in(monkeypatch: pytest.MonkeyPatch):
- def _build(environment: str) -> TestClient:
+ def _build(environment: str, *, bypass: str | None = None) -> TestClient:
# `current_env_from_os` reads ENV first, so both are set; a stale ENV
# is precisely what shadowed an operator's ENVIRONMENT on ingest.
monkeypatch.setenv("ENVIRONMENT", environment)
monkeypatch.delenv("ENV", raising=False)
+ monkeypatch.delenv("AISOC_PUBLISHED_BIND_ADDRS", raising=False)
+ if bypass is None:
+ monkeypatch.delenv("AISOC_DEV_AUTH_BYPASS", raising=False)
+ else:
+ monkeypatch.setenv("AISOC_DEV_AUTH_BYPASS", bypass)
+
from app.api.v1 import deps, dev_auth
- # Both, and in this order. Reloading `dev_auth` alone leaves
- # `deps.get_current_user` closed over the previous module, and the
- # route then rejects on validation (422) instead of exercising the
- # authentication path this test is about.
- importlib.reload(dev_auth)
- importlib.reload(deps)
+ # This fixture used to `importlib.reload` both modules. It does not
+ # any more, and neither should anything else here: a reload rebinds
+ # every function object in `deps`, so the `dependency_overrides` that
+ # other modules keyed on the old objects stop matching and their
+ # routes fall through to real authentication. It went unnoticed
+ # because this filename sorts near the end of the suite; a new file
+ # doing the same thing took 146 unrelated tests down with it. The
+ # shim reads `os.environ` at call time, so setting the variables is
+ # sufficient and always was.
+ dev_auth._REFUSALS_LOGGED.clear()
return TestClient(_probe_app(deps.get_current_user), raise_server_exceptions=False)
return _build
@@ -80,8 +88,20 @@ class TestTheBypassIsEnvironmentGated:
reader could reasonably assume any named environment is safe."""
assert client_in(environment).get("/probe").status_code == 401
- def test_development_still_resolves_a_user(self, client_in) -> None:
+ def test_development_alone_is_no_longer_enough(self, client_in) -> None:
+ """Naming the environment stopped being sufficient.
+
+ This assertion used to read the other way, and that is the defect:
+ `ENVIRONMENT` defaulted to `development` in `docker-compose.yml` and
+ in the `.env` the setup script writes, so the only thing between an
+ uncredentialed request and an administrator was a string nobody had
+ chosen deliberately. The shim now needs `AISOC_DEV_AUTH_BYPASS`,
+ which no compose file or template sets.
+ """
+ assert client_in("development").get("/probe").status_code == 401
+
+ def test_development_with_the_explicit_optin_still_resolves_a_user(self, client_in) -> None:
"""The other direction, so this cannot pass on a build with no shim."""
- response = client_in("development").get("/probe")
+ response = client_in("development", bypass="1").get("/probe")
assert response.status_code == 200
assert response.json()["role"] == "admin"
diff --git a/services/connectors/app/security/tenant_scope.py b/services/connectors/app/security/tenant_scope.py
index 28a4a76fb..51f7a21cf 100644
--- a/services/connectors/app/security/tenant_scope.py
+++ b/services/connectors/app/security/tenant_scope.py
@@ -62,6 +62,7 @@ import base64
import binascii
import hashlib
import hmac
+import ipaddress
import json
import logging
import os
@@ -92,8 +93,29 @@ INSECURE_SECRET_DEFAULTS = frozenset(
)
#: Deterministic demo tenant, matching ``DEMO_TENANT_ID`` in
-#: services/api/app/api/v1/dev_auth.py. Only ever used under AISOC_DEV_MODE.
-DEV_TENANT_ID = uuid.UUID("00000000-0000-0000-0000-000000000001")
+#: services/api/app/api/v1/dev_auth.py. Only ever reached when
+#: :func:`auth_bypass_refusal` returns ``None``.
+#:
+#: This used to be ``…0001``, the canonical tenant that migration 001 seeds
+#: and that ``bootstrap_admin`` puts the real administrator into, so an
+#: unauthenticated caller was scoped to the operator's own data. The canonical
+#: id stays where it is because ten modules pin it; the demo identity moved.
+DEV_TENANT_ID = uuid.UUID("00000000-0000-0000-0000-0000000000de")
+
+#: The environment variable whose only job is to enable the anonymous shim.
+#:
+#: Deliberately not ``AISOC_DEV_MODE``. That flag already selects table
+#: autocreate, docs URLs, the metrics gate, log formatting, the GraphiQL UI
+#: and an ephemeral vault key, and ``docker-compose.yml`` defaults it to ``1``
+#: on ten services. A flag that means nine things cannot be refused for one
+#: of them, so the bypass gets a variable that means exactly one thing and
+#: that no compose file or template sets.
+DEV_AUTH_BYPASS_VAR = "AISOC_DEV_AUTH_BYPASS"
+
+#: Where the deployment publishes this service, comma-separated. Supplied by
+#: compose, because a container binds ``0.0.0.0`` and cannot otherwise know
+#: whether anyone but the operator's own machine can reach it.
+PUBLISHED_BIND_VAR = "AISOC_PUBLISHED_BIND_ADDRS"
#: Header a trusted service uses to declare which tenant it is acting for.
TENANT_HEADER = "X-AiSOC-Tenant-ID"
@@ -200,6 +222,86 @@ def _dev_mode() -> bool:
return os.getenv("AISOC_DEV_MODE", "").strip().lower() in _TRUTHY
+def _is_loopback(address: str) -> bool:
+ """Whether ``address`` reaches only the machine this service runs on."""
+ host = address.strip().lower()
+ if not host:
+ return True
+ if host.startswith("["):
+ host = host.partition("]")[0].lstrip("[")
+ elif host.count(":") == 1:
+ host = host.partition(":")[0]
+ if host in {"localhost", "::1", "ip6-localhost"}:
+ return True
+ try:
+ return ipaddress.ip_address(host).is_loopback
+ except ValueError:
+ # A hostname that is not obviously loopback. When the question cannot
+ # be decided, the safe answer is the one that refuses the bypass.
+ return False
+
+
+def auth_bypass_refusal() -> str | None:
+ """Why an uncredentialed caller must not be admitted, or ``None``.
+
+ Three conditions, where there used to be one. ``AISOC_DEV_MODE`` alone
+ admitted a caller with no credential at all whenever neither a console
+ secret nor a service token was configured — and ``docker-compose.yml``
+ defaults that flag to ``1`` on ten services while generating no service
+ token, so a hand-copied ``.env`` and a plain ``docker compose up`` left
+ this open.
+
+ Returns a sentence for the log, so an operator who expected the bypass
+ learns which condition withheld it instead of seeing an unexplained 503.
+ """
+ if not _dev_mode():
+ return "AISOC_DEV_MODE is not set"
+ if os.getenv(DEV_AUTH_BYPASS_VAR, "").strip().lower() not in _TRUTHY:
+ return (
+ f"{DEV_AUTH_BYPASS_VAR} is not set. AISOC_DEV_MODE selects several "
+ "unrelated development behaviours and defaults to 1 in compose, so "
+ "it no longer enables the anonymous path on its own. Set "
+ f"{DEV_AUTH_BYPASS_VAR}=1 deliberately, or use "
+ "infra/compose/docker-compose.dev.yml, which sets it"
+ )
+ published = [a for a in os.getenv(PUBLISHED_BIND_VAR, "").split(",") if a.strip()]
+ reachable = [a.strip() for a in published if not _is_loopback(a)]
+ if reachable:
+ return (
+ f"{DEV_AUTH_BYPASS_VAR} is set, but this deployment publishes "
+ f"{', '.join(reachable)}, which is not loopback. Bind to 127.0.0.1, "
+ "or configure SECRET_KEY and a service token"
+ )
+ return None
+
+
+#: Refusals and activations already logged, so a hot path does not emit one
+#: line per request. Keyed on the reason rather than counted, so a deployment
+#: whose refusal *changes* still says so once.
+_BYPASS_LOGGED: set[str] = set()
+
+
+def _bypass_permitted() -> bool:
+ """Whether to admit an uncredentialed caller, logging the decision once."""
+ refusal = auth_bypass_refusal()
+ if refusal is None:
+ if "__active__" not in _BYPASS_LOGGED:
+ _BYPASS_LOGGED.add("__active__")
+ logger.warning(
+ "ANONYMOUS ACCESS IS ENABLED on %s. Requests with no credential "
+ "resolve to the demo tenant %s. This is a development "
+ "convenience and must not be used where anyone else can reach "
+ "this host.",
+ SERVICE_NAME,
+ DEV_TENANT_ID,
+ )
+ return True
+ if refusal not in _BYPASS_LOGGED:
+ _BYPASS_LOGGED.add(refusal)
+ logger.info("anonymous access refused on %s: %s", SERVICE_NAME, refusal)
+ return False
+
+
def resolve_console_secret() -> str:
"""The HS256 secret console tokens are signed with, or "" when unusable."""
secret = (os.getenv("SECRET_KEY") or "").strip()
@@ -302,7 +404,7 @@ async def require_console_or_service_auth(
service_token = resolve_service_token()
if not console_secret and not service_token:
- if _dev_mode():
+ if _bypass_permitted():
return TenantPrincipal(
tenant_ids=frozenset({DEV_TENANT_ID}),
subject="dev",
diff --git a/services/connectors/tests/test_tenant_scope.py b/services/connectors/tests/test_tenant_scope.py
index adc488b19..3ba17f28d 100644
--- a/services/connectors/tests/test_tenant_scope.py
+++ b/services/connectors/tests/test_tenant_scope.py
@@ -269,6 +269,8 @@ async def test_dev_mode_only_applies_with_nothing_configured(monkeypatch: pytest
every tenant-scoped route.
"""
monkeypatch.setenv("AISOC_DEV_MODE", "1")
+ monkeypatch.setenv("AISOC_DEV_AUTH_BYPASS", "1")
+ monkeypatch.delenv("AISOC_PUBLISHED_BIND_ADDRS", raising=False)
monkeypatch.setenv("SECRET_KEY", SECRET)
monkeypatch.setenv("AISOC_SERVICE_TOKEN", SERVICE_TOKEN)
with pytest.raises(HTTPException) as exc:
@@ -277,10 +279,79 @@ async def test_dev_mode_only_applies_with_nothing_configured(monkeypatch: pytest
monkeypatch.setenv("SECRET_KEY", "")
monkeypatch.setenv("AISOC_SERVICE_TOKEN", "")
+ ts._BYPASS_LOGGED.clear()
principal = await ts.require_console_or_service_auth(authorization=None, x_aisoc_tenant_id=None)
assert principal.tenant_ids == frozenset({ts.DEV_TENANT_ID})
+async def test_dev_mode_alone_no_longer_admits_an_uncredentialed_caller(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ """The defect, stated as a test.
+
+ `AISOC_DEV_MODE` selects table autocreate, docs URLs, the metrics gate,
+ log formatting, the GraphiQL UI and an ephemeral vault key. It also
+ defaults to `1` on ten services in `docker-compose.yml`, while nothing
+ generates a service token. So a hand-copied `.env` and a plain
+ `docker compose up` admitted a caller with no credential at all, and the
+ only thing that had to be true for it was a flag nobody set for this
+ purpose.
+ """
+ monkeypatch.setenv("AISOC_DEV_MODE", "1")
+ monkeypatch.delenv("AISOC_DEV_AUTH_BYPASS", raising=False)
+ monkeypatch.delenv("AISOC_PUBLISHED_BIND_ADDRS", raising=False)
+ monkeypatch.setenv("SECRET_KEY", "")
+ monkeypatch.setenv("AISOC_SERVICE_TOKEN", "")
+ ts._BYPASS_LOGGED.clear()
+ with pytest.raises(HTTPException) as exc:
+ await ts.require_console_or_service_auth(authorization=None, x_aisoc_tenant_id=None)
+ assert exc.value.status_code == 503
+
+
+async def test_the_bypass_is_refused_on_a_reachable_published_address(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ """An anonymous principal is a convenience only on loopback.
+
+ The service binds `0.0.0.0` inside its container and cannot work out for
+ itself whether anyone else can reach it, so compose passes the published
+ addresses in. Both directions, because a check that only ever refuses is
+ indistinguishable from one that removed the shim.
+ """
+ monkeypatch.setenv("AISOC_DEV_MODE", "1")
+ monkeypatch.setenv("AISOC_DEV_AUTH_BYPASS", "1")
+ monkeypatch.setenv("SECRET_KEY", "")
+ monkeypatch.setenv("AISOC_SERVICE_TOKEN", "")
+
+ for published in ("0.0.0.0", "192.168.1.10", "127.0.0.1,0.0.0.0:8080", "aisoc.example.com"):
+ monkeypatch.setenv("AISOC_PUBLISHED_BIND_ADDRS", published)
+ ts._BYPASS_LOGGED.clear()
+ with pytest.raises(HTTPException) as exc:
+ await ts.require_console_or_service_auth(authorization=None, x_aisoc_tenant_id=None)
+ assert exc.value.status_code == 503, published
+
+ for published in ("127.0.0.1", "localhost:8080", "::1", "[::1]:9000"):
+ monkeypatch.setenv("AISOC_PUBLISHED_BIND_ADDRS", published)
+ ts._BYPASS_LOGGED.clear()
+ principal = await ts.require_console_or_service_auth(authorization=None, x_aisoc_tenant_id=None)
+ assert principal.tenant_ids == frozenset({ts.DEV_TENANT_ID}), published
+
+
+def test_the_dev_tenant_is_not_the_canonical_tenant() -> None:
+ """The crux, and it was byte-identical.
+
+ `DEV_TENANT_ID` was `…0001`, the tenant migration 001 seeds and that
+ `bootstrap_admin` puts the real administrator into. So an unauthenticated
+ caller admitted by the shim was scoped to the operator's own data rather
+ than to a demo tenant.
+ """
+ import uuid as _uuid
+
+ assert ts.DEV_TENANT_ID != _uuid.UUID("00000000-0000-0000-0000-000000000001")
+
+
@pytest.mark.asyncio
async def test_a_per_service_token_override_wins(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv(f"AISOC_{ts.SERVICE_NAME}_SERVICE_TOKEN", "service-specific")
diff --git a/services/fusion/app/security/tenant_scope.py b/services/fusion/app/security/tenant_scope.py
index 567a5e1de..c70e2ad91 100644
--- a/services/fusion/app/security/tenant_scope.py
+++ b/services/fusion/app/security/tenant_scope.py
@@ -62,6 +62,7 @@ import base64
import binascii
import hashlib
import hmac
+import ipaddress
import json
import logging
import os
@@ -92,8 +93,29 @@ INSECURE_SECRET_DEFAULTS = frozenset(
)
#: Deterministic demo tenant, matching ``DEMO_TENANT_ID`` in
-#: services/api/app/api/v1/dev_auth.py. Only ever used under AISOC_DEV_MODE.
-DEV_TENANT_ID = uuid.UUID("00000000-0000-0000-0000-000000000001")
+#: services/api/app/api/v1/dev_auth.py. Only ever reached when
+#: :func:`auth_bypass_refusal` returns ``None``.
+#:
+#: This used to be ``…0001``, the canonical tenant that migration 001 seeds
+#: and that ``bootstrap_admin`` puts the real administrator into, so an
+#: unauthenticated caller was scoped to the operator's own data. The canonical
+#: id stays where it is because ten modules pin it; the demo identity moved.
+DEV_TENANT_ID = uuid.UUID("00000000-0000-0000-0000-0000000000de")
+
+#: The environment variable whose only job is to enable the anonymous shim.
+#:
+#: Deliberately not ``AISOC_DEV_MODE``. That flag already selects table
+#: autocreate, docs URLs, the metrics gate, log formatting, the GraphiQL UI
+#: and an ephemeral vault key, and ``docker-compose.yml`` defaults it to ``1``
+#: on ten services. A flag that means nine things cannot be refused for one
+#: of them, so the bypass gets a variable that means exactly one thing and
+#: that no compose file or template sets.
+DEV_AUTH_BYPASS_VAR = "AISOC_DEV_AUTH_BYPASS"
+
+#: Where the deployment publishes this service, comma-separated. Supplied by
+#: compose, because a container binds ``0.0.0.0`` and cannot otherwise know
+#: whether anyone but the operator's own machine can reach it.
+PUBLISHED_BIND_VAR = "AISOC_PUBLISHED_BIND_ADDRS"
#: Header a trusted service uses to declare which tenant it is acting for.
TENANT_HEADER = "X-AiSOC-Tenant-ID"
@@ -200,6 +222,86 @@ def _dev_mode() -> bool:
return os.getenv("AISOC_DEV_MODE", "").strip().lower() in _TRUTHY
+def _is_loopback(address: str) -> bool:
+ """Whether ``address`` reaches only the machine this service runs on."""
+ host = address.strip().lower()
+ if not host:
+ return True
+ if host.startswith("["):
+ host = host.partition("]")[0].lstrip("[")
+ elif host.count(":") == 1:
+ host = host.partition(":")[0]
+ if host in {"localhost", "::1", "ip6-localhost"}:
+ return True
+ try:
+ return ipaddress.ip_address(host).is_loopback
+ except ValueError:
+ # A hostname that is not obviously loopback. When the question cannot
+ # be decided, the safe answer is the one that refuses the bypass.
+ return False
+
+
+def auth_bypass_refusal() -> str | None:
+ """Why an uncredentialed caller must not be admitted, or ``None``.
+
+ Three conditions, where there used to be one. ``AISOC_DEV_MODE`` alone
+ admitted a caller with no credential at all whenever neither a console
+ secret nor a service token was configured — and ``docker-compose.yml``
+ defaults that flag to ``1`` on ten services while generating no service
+ token, so a hand-copied ``.env`` and a plain ``docker compose up`` left
+ this open.
+
+ Returns a sentence for the log, so an operator who expected the bypass
+ learns which condition withheld it instead of seeing an unexplained 503.
+ """
+ if not _dev_mode():
+ return "AISOC_DEV_MODE is not set"
+ if os.getenv(DEV_AUTH_BYPASS_VAR, "").strip().lower() not in _TRUTHY:
+ return (
+ f"{DEV_AUTH_BYPASS_VAR} is not set. AISOC_DEV_MODE selects several "
+ "unrelated development behaviours and defaults to 1 in compose, so "
+ "it no longer enables the anonymous path on its own. Set "
+ f"{DEV_AUTH_BYPASS_VAR}=1 deliberately, or use "
+ "infra/compose/docker-compose.dev.yml, which sets it"
+ )
+ published = [a for a in os.getenv(PUBLISHED_BIND_VAR, "").split(",") if a.strip()]
+ reachable = [a.strip() for a in published if not _is_loopback(a)]
+ if reachable:
+ return (
+ f"{DEV_AUTH_BYPASS_VAR} is set, but this deployment publishes "
+ f"{', '.join(reachable)}, which is not loopback. Bind to 127.0.0.1, "
+ "or configure SECRET_KEY and a service token"
+ )
+ return None
+
+
+#: Refusals and activations already logged, so a hot path does not emit one
+#: line per request. Keyed on the reason rather than counted, so a deployment
+#: whose refusal *changes* still says so once.
+_BYPASS_LOGGED: set[str] = set()
+
+
+def _bypass_permitted() -> bool:
+ """Whether to admit an uncredentialed caller, logging the decision once."""
+ refusal = auth_bypass_refusal()
+ if refusal is None:
+ if "__active__" not in _BYPASS_LOGGED:
+ _BYPASS_LOGGED.add("__active__")
+ logger.warning(
+ "ANONYMOUS ACCESS IS ENABLED on %s. Requests with no credential "
+ "resolve to the demo tenant %s. This is a development "
+ "convenience and must not be used where anyone else can reach "
+ "this host.",
+ SERVICE_NAME,
+ DEV_TENANT_ID,
+ )
+ return True
+ if refusal not in _BYPASS_LOGGED:
+ _BYPASS_LOGGED.add(refusal)
+ logger.info("anonymous access refused on %s: %s", SERVICE_NAME, refusal)
+ return False
+
+
def resolve_console_secret() -> str:
"""The HS256 secret console tokens are signed with, or "" when unusable."""
secret = (os.getenv("SECRET_KEY") or "").strip()
@@ -302,7 +404,7 @@ async def require_console_or_service_auth(
service_token = resolve_service_token()
if not console_secret and not service_token:
- if _dev_mode():
+ if _bypass_permitted():
return TenantPrincipal(
tenant_ids=frozenset({DEV_TENANT_ID}),
subject="dev",
diff --git a/services/fusion/tests/test_tenant_scope.py b/services/fusion/tests/test_tenant_scope.py
index adc488b19..3ba17f28d 100644
--- a/services/fusion/tests/test_tenant_scope.py
+++ b/services/fusion/tests/test_tenant_scope.py
@@ -269,6 +269,8 @@ async def test_dev_mode_only_applies_with_nothing_configured(monkeypatch: pytest
every tenant-scoped route.
"""
monkeypatch.setenv("AISOC_DEV_MODE", "1")
+ monkeypatch.setenv("AISOC_DEV_AUTH_BYPASS", "1")
+ monkeypatch.delenv("AISOC_PUBLISHED_BIND_ADDRS", raising=False)
monkeypatch.setenv("SECRET_KEY", SECRET)
monkeypatch.setenv("AISOC_SERVICE_TOKEN", SERVICE_TOKEN)
with pytest.raises(HTTPException) as exc:
@@ -277,10 +279,79 @@ async def test_dev_mode_only_applies_with_nothing_configured(monkeypatch: pytest
monkeypatch.setenv("SECRET_KEY", "")
monkeypatch.setenv("AISOC_SERVICE_TOKEN", "")
+ ts._BYPASS_LOGGED.clear()
principal = await ts.require_console_or_service_auth(authorization=None, x_aisoc_tenant_id=None)
assert principal.tenant_ids == frozenset({ts.DEV_TENANT_ID})
+async def test_dev_mode_alone_no_longer_admits_an_uncredentialed_caller(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ """The defect, stated as a test.
+
+ `AISOC_DEV_MODE` selects table autocreate, docs URLs, the metrics gate,
+ log formatting, the GraphiQL UI and an ephemeral vault key. It also
+ defaults to `1` on ten services in `docker-compose.yml`, while nothing
+ generates a service token. So a hand-copied `.env` and a plain
+ `docker compose up` admitted a caller with no credential at all, and the
+ only thing that had to be true for it was a flag nobody set for this
+ purpose.
+ """
+ monkeypatch.setenv("AISOC_DEV_MODE", "1")
+ monkeypatch.delenv("AISOC_DEV_AUTH_BYPASS", raising=False)
+ monkeypatch.delenv("AISOC_PUBLISHED_BIND_ADDRS", raising=False)
+ monkeypatch.setenv("SECRET_KEY", "")
+ monkeypatch.setenv("AISOC_SERVICE_TOKEN", "")
+ ts._BYPASS_LOGGED.clear()
+ with pytest.raises(HTTPException) as exc:
+ await ts.require_console_or_service_auth(authorization=None, x_aisoc_tenant_id=None)
+ assert exc.value.status_code == 503
+
+
+async def test_the_bypass_is_refused_on_a_reachable_published_address(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ """An anonymous principal is a convenience only on loopback.
+
+ The service binds `0.0.0.0` inside its container and cannot work out for
+ itself whether anyone else can reach it, so compose passes the published
+ addresses in. Both directions, because a check that only ever refuses is
+ indistinguishable from one that removed the shim.
+ """
+ monkeypatch.setenv("AISOC_DEV_MODE", "1")
+ monkeypatch.setenv("AISOC_DEV_AUTH_BYPASS", "1")
+ monkeypatch.setenv("SECRET_KEY", "")
+ monkeypatch.setenv("AISOC_SERVICE_TOKEN", "")
+
+ for published in ("0.0.0.0", "192.168.1.10", "127.0.0.1,0.0.0.0:8080", "aisoc.example.com"):
+ monkeypatch.setenv("AISOC_PUBLISHED_BIND_ADDRS", published)
+ ts._BYPASS_LOGGED.clear()
+ with pytest.raises(HTTPException) as exc:
+ await ts.require_console_or_service_auth(authorization=None, x_aisoc_tenant_id=None)
+ assert exc.value.status_code == 503, published
+
+ for published in ("127.0.0.1", "localhost:8080", "::1", "[::1]:9000"):
+ monkeypatch.setenv("AISOC_PUBLISHED_BIND_ADDRS", published)
+ ts._BYPASS_LOGGED.clear()
+ principal = await ts.require_console_or_service_auth(authorization=None, x_aisoc_tenant_id=None)
+ assert principal.tenant_ids == frozenset({ts.DEV_TENANT_ID}), published
+
+
+def test_the_dev_tenant_is_not_the_canonical_tenant() -> None:
+ """The crux, and it was byte-identical.
+
+ `DEV_TENANT_ID` was `…0001`, the tenant migration 001 seeds and that
+ `bootstrap_admin` puts the real administrator into. So an unauthenticated
+ caller admitted by the shim was scoped to the operator's own data rather
+ than to a demo tenant.
+ """
+ import uuid as _uuid
+
+ assert ts.DEV_TENANT_ID != _uuid.UUID("00000000-0000-0000-0000-000000000001")
+
+
@pytest.mark.asyncio
async def test_a_per_service_token_override_wins(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv(f"AISOC_{ts.SERVICE_NAME}_SERVICE_TOKEN", "service-specific")
diff --git a/services/honeytokens/app/security/service_auth.py b/services/honeytokens/app/security/service_auth.py
index 450a73dd2..c53b241d1 100644
--- a/services/honeytokens/app/security/service_auth.py
+++ b/services/honeytokens/app/security/service_auth.py
@@ -44,12 +44,6 @@ from fastapi import Header, HTTPException, status
#: Set by each vendored copy so the per-service override resolves.
SERVICE_NAME = "HONEYTOKENS"
-_TRUTHY = {"1", "true", "yes", "on"}
-
-
-def _dev_mode() -> bool:
- return os.getenv("AISOC_DEV_MODE", "").strip().lower() in _TRUTHY
-
def resolve_token() -> str:
"""Per-service override wins, then the shared platform token."""
@@ -63,8 +57,13 @@ async def require_service_auth(authorization: str | None = Header(default=None))
"""FastAPI dependency. Fails closed unless explicitly in dev mode."""
token = resolve_token()
if not token:
- if _dev_mode():
- return
+ # No dev-mode exemption, matching services/actions/app/security/authz.py.
+ # This branch used to return early when AISOC_DEV_MODE was set, and
+ # docker-compose.yml defaults that flag to 1 on ten services while
+ # nothing generates a service token — so the exemption was not a
+ # developer convenience, it was the state every stock install ran in.
+ # A service-to-service dependency has no browser to keep usable, so
+ # 503 with an actionable message is the whole of what is needed.
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail=(
diff --git a/services/honeytokens/app/security/tenant_scope.py b/services/honeytokens/app/security/tenant_scope.py
index fe2084ad6..5cf66870f 100644
--- a/services/honeytokens/app/security/tenant_scope.py
+++ b/services/honeytokens/app/security/tenant_scope.py
@@ -62,6 +62,7 @@ import base64
import binascii
import hashlib
import hmac
+import ipaddress
import json
import logging
import os
@@ -92,8 +93,29 @@ INSECURE_SECRET_DEFAULTS = frozenset(
)
#: Deterministic demo tenant, matching ``DEMO_TENANT_ID`` in
-#: services/api/app/api/v1/dev_auth.py. Only ever used under AISOC_DEV_MODE.
-DEV_TENANT_ID = uuid.UUID("00000000-0000-0000-0000-000000000001")
+#: services/api/app/api/v1/dev_auth.py. Only ever reached when
+#: :func:`auth_bypass_refusal` returns ``None``.
+#:
+#: This used to be ``…0001``, the canonical tenant that migration 001 seeds
+#: and that ``bootstrap_admin`` puts the real administrator into, so an
+#: unauthenticated caller was scoped to the operator's own data. The canonical
+#: id stays where it is because ten modules pin it; the demo identity moved.
+DEV_TENANT_ID = uuid.UUID("00000000-0000-0000-0000-0000000000de")
+
+#: The environment variable whose only job is to enable the anonymous shim.
+#:
+#: Deliberately not ``AISOC_DEV_MODE``. That flag already selects table
+#: autocreate, docs URLs, the metrics gate, log formatting, the GraphiQL UI
+#: and an ephemeral vault key, and ``docker-compose.yml`` defaults it to ``1``
+#: on ten services. A flag that means nine things cannot be refused for one
+#: of them, so the bypass gets a variable that means exactly one thing and
+#: that no compose file or template sets.
+DEV_AUTH_BYPASS_VAR = "AISOC_DEV_AUTH_BYPASS"
+
+#: Where the deployment publishes this service, comma-separated. Supplied by
+#: compose, because a container binds ``0.0.0.0`` and cannot otherwise know
+#: whether anyone but the operator's own machine can reach it.
+PUBLISHED_BIND_VAR = "AISOC_PUBLISHED_BIND_ADDRS"
#: Header a trusted service uses to declare which tenant it is acting for.
TENANT_HEADER = "X-AiSOC-Tenant-ID"
@@ -200,6 +222,86 @@ def _dev_mode() -> bool:
return os.getenv("AISOC_DEV_MODE", "").strip().lower() in _TRUTHY
+def _is_loopback(address: str) -> bool:
+ """Whether ``address`` reaches only the machine this service runs on."""
+ host = address.strip().lower()
+ if not host:
+ return True
+ if host.startswith("["):
+ host = host.partition("]")[0].lstrip("[")
+ elif host.count(":") == 1:
+ host = host.partition(":")[0]
+ if host in {"localhost", "::1", "ip6-localhost"}:
+ return True
+ try:
+ return ipaddress.ip_address(host).is_loopback
+ except ValueError:
+ # A hostname that is not obviously loopback. When the question cannot
+ # be decided, the safe answer is the one that refuses the bypass.
+ return False
+
+
+def auth_bypass_refusal() -> str | None:
+ """Why an uncredentialed caller must not be admitted, or ``None``.
+
+ Three conditions, where there used to be one. ``AISOC_DEV_MODE`` alone
+ admitted a caller with no credential at all whenever neither a console
+ secret nor a service token was configured — and ``docker-compose.yml``
+ defaults that flag to ``1`` on ten services while generating no service
+ token, so a hand-copied ``.env`` and a plain ``docker compose up`` left
+ this open.
+
+ Returns a sentence for the log, so an operator who expected the bypass
+ learns which condition withheld it instead of seeing an unexplained 503.
+ """
+ if not _dev_mode():
+ return "AISOC_DEV_MODE is not set"
+ if os.getenv(DEV_AUTH_BYPASS_VAR, "").strip().lower() not in _TRUTHY:
+ return (
+ f"{DEV_AUTH_BYPASS_VAR} is not set. AISOC_DEV_MODE selects several "
+ "unrelated development behaviours and defaults to 1 in compose, so "
+ "it no longer enables the anonymous path on its own. Set "
+ f"{DEV_AUTH_BYPASS_VAR}=1 deliberately, or use "
+ "infra/compose/docker-compose.dev.yml, which sets it"
+ )
+ published = [a for a in os.getenv(PUBLISHED_BIND_VAR, "").split(",") if a.strip()]
+ reachable = [a.strip() for a in published if not _is_loopback(a)]
+ if reachable:
+ return (
+ f"{DEV_AUTH_BYPASS_VAR} is set, but this deployment publishes "
+ f"{', '.join(reachable)}, which is not loopback. Bind to 127.0.0.1, "
+ "or configure SECRET_KEY and a service token"
+ )
+ return None
+
+
+#: Refusals and activations already logged, so a hot path does not emit one
+#: line per request. Keyed on the reason rather than counted, so a deployment
+#: whose refusal *changes* still says so once.
+_BYPASS_LOGGED: set[str] = set()
+
+
+def _bypass_permitted() -> bool:
+ """Whether to admit an uncredentialed caller, logging the decision once."""
+ refusal = auth_bypass_refusal()
+ if refusal is None:
+ if "__active__" not in _BYPASS_LOGGED:
+ _BYPASS_LOGGED.add("__active__")
+ logger.warning(
+ "ANONYMOUS ACCESS IS ENABLED on %s. Requests with no credential "
+ "resolve to the demo tenant %s. This is a development "
+ "convenience and must not be used where anyone else can reach "
+ "this host.",
+ SERVICE_NAME,
+ DEV_TENANT_ID,
+ )
+ return True
+ if refusal not in _BYPASS_LOGGED:
+ _BYPASS_LOGGED.add(refusal)
+ logger.info("anonymous access refused on %s: %s", SERVICE_NAME, refusal)
+ return False
+
+
def resolve_console_secret() -> str:
"""The HS256 secret console tokens are signed with, or "" when unusable."""
secret = (os.getenv("SECRET_KEY") or "").strip()
@@ -302,7 +404,7 @@ async def require_console_or_service_auth(
service_token = resolve_service_token()
if not console_secret and not service_token:
- if _dev_mode():
+ if _bypass_permitted():
return TenantPrincipal(
tenant_ids=frozenset({DEV_TENANT_ID}),
subject="dev",
diff --git a/services/honeytokens/tests/test_tenant_scope.py b/services/honeytokens/tests/test_tenant_scope.py
index adc488b19..3ba17f28d 100644
--- a/services/honeytokens/tests/test_tenant_scope.py
+++ b/services/honeytokens/tests/test_tenant_scope.py
@@ -269,6 +269,8 @@ async def test_dev_mode_only_applies_with_nothing_configured(monkeypatch: pytest
every tenant-scoped route.
"""
monkeypatch.setenv("AISOC_DEV_MODE", "1")
+ monkeypatch.setenv("AISOC_DEV_AUTH_BYPASS", "1")
+ monkeypatch.delenv("AISOC_PUBLISHED_BIND_ADDRS", raising=False)
monkeypatch.setenv("SECRET_KEY", SECRET)
monkeypatch.setenv("AISOC_SERVICE_TOKEN", SERVICE_TOKEN)
with pytest.raises(HTTPException) as exc:
@@ -277,10 +279,79 @@ async def test_dev_mode_only_applies_with_nothing_configured(monkeypatch: pytest
monkeypatch.setenv("SECRET_KEY", "")
monkeypatch.setenv("AISOC_SERVICE_TOKEN", "")
+ ts._BYPASS_LOGGED.clear()
principal = await ts.require_console_or_service_auth(authorization=None, x_aisoc_tenant_id=None)
assert principal.tenant_ids == frozenset({ts.DEV_TENANT_ID})
+async def test_dev_mode_alone_no_longer_admits_an_uncredentialed_caller(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ """The defect, stated as a test.
+
+ `AISOC_DEV_MODE` selects table autocreate, docs URLs, the metrics gate,
+ log formatting, the GraphiQL UI and an ephemeral vault key. It also
+ defaults to `1` on ten services in `docker-compose.yml`, while nothing
+ generates a service token. So a hand-copied `.env` and a plain
+ `docker compose up` admitted a caller with no credential at all, and the
+ only thing that had to be true for it was a flag nobody set for this
+ purpose.
+ """
+ monkeypatch.setenv("AISOC_DEV_MODE", "1")
+ monkeypatch.delenv("AISOC_DEV_AUTH_BYPASS", raising=False)
+ monkeypatch.delenv("AISOC_PUBLISHED_BIND_ADDRS", raising=False)
+ monkeypatch.setenv("SECRET_KEY", "")
+ monkeypatch.setenv("AISOC_SERVICE_TOKEN", "")
+ ts._BYPASS_LOGGED.clear()
+ with pytest.raises(HTTPException) as exc:
+ await ts.require_console_or_service_auth(authorization=None, x_aisoc_tenant_id=None)
+ assert exc.value.status_code == 503
+
+
+async def test_the_bypass_is_refused_on_a_reachable_published_address(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ """An anonymous principal is a convenience only on loopback.
+
+ The service binds `0.0.0.0` inside its container and cannot work out for
+ itself whether anyone else can reach it, so compose passes the published
+ addresses in. Both directions, because a check that only ever refuses is
+ indistinguishable from one that removed the shim.
+ """
+ monkeypatch.setenv("AISOC_DEV_MODE", "1")
+ monkeypatch.setenv("AISOC_DEV_AUTH_BYPASS", "1")
+ monkeypatch.setenv("SECRET_KEY", "")
+ monkeypatch.setenv("AISOC_SERVICE_TOKEN", "")
+
+ for published in ("0.0.0.0", "192.168.1.10", "127.0.0.1,0.0.0.0:8080", "aisoc.example.com"):
+ monkeypatch.setenv("AISOC_PUBLISHED_BIND_ADDRS", published)
+ ts._BYPASS_LOGGED.clear()
+ with pytest.raises(HTTPException) as exc:
+ await ts.require_console_or_service_auth(authorization=None, x_aisoc_tenant_id=None)
+ assert exc.value.status_code == 503, published
+
+ for published in ("127.0.0.1", "localhost:8080", "::1", "[::1]:9000"):
+ monkeypatch.setenv("AISOC_PUBLISHED_BIND_ADDRS", published)
+ ts._BYPASS_LOGGED.clear()
+ principal = await ts.require_console_or_service_auth(authorization=None, x_aisoc_tenant_id=None)
+ assert principal.tenant_ids == frozenset({ts.DEV_TENANT_ID}), published
+
+
+def test_the_dev_tenant_is_not_the_canonical_tenant() -> None:
+ """The crux, and it was byte-identical.
+
+ `DEV_TENANT_ID` was `…0001`, the tenant migration 001 seeds and that
+ `bootstrap_admin` puts the real administrator into. So an unauthenticated
+ caller admitted by the shim was scoped to the operator's own data rather
+ than to a demo tenant.
+ """
+ import uuid as _uuid
+
+ assert ts.DEV_TENANT_ID != _uuid.UUID("00000000-0000-0000-0000-000000000001")
+
+
@pytest.mark.asyncio
async def test_a_per_service_token_override_wins(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv(f"AISOC_{ts.SERVICE_NAME}_SERVICE_TOKEN", "service-specific")
diff --git a/services/osquery-tls/app/security/tenant_scope.py b/services/osquery-tls/app/security/tenant_scope.py
index abe343959..ba6a32987 100644
--- a/services/osquery-tls/app/security/tenant_scope.py
+++ b/services/osquery-tls/app/security/tenant_scope.py
@@ -62,6 +62,7 @@ import base64
import binascii
import hashlib
import hmac
+import ipaddress
import json
import logging
import os
@@ -92,8 +93,29 @@ INSECURE_SECRET_DEFAULTS = frozenset(
)
#: Deterministic demo tenant, matching ``DEMO_TENANT_ID`` in
-#: services/api/app/api/v1/dev_auth.py. Only ever used under AISOC_DEV_MODE.
-DEV_TENANT_ID = uuid.UUID("00000000-0000-0000-0000-000000000001")
+#: services/api/app/api/v1/dev_auth.py. Only ever reached when
+#: :func:`auth_bypass_refusal` returns ``None``.
+#:
+#: This used to be ``…0001``, the canonical tenant that migration 001 seeds
+#: and that ``bootstrap_admin`` puts the real administrator into, so an
+#: unauthenticated caller was scoped to the operator's own data. The canonical
+#: id stays where it is because ten modules pin it; the demo identity moved.
+DEV_TENANT_ID = uuid.UUID("00000000-0000-0000-0000-0000000000de")
+
+#: The environment variable whose only job is to enable the anonymous shim.
+#:
+#: Deliberately not ``AISOC_DEV_MODE``. That flag already selects table
+#: autocreate, docs URLs, the metrics gate, log formatting, the GraphiQL UI
+#: and an ephemeral vault key, and ``docker-compose.yml`` defaults it to ``1``
+#: on ten services. A flag that means nine things cannot be refused for one
+#: of them, so the bypass gets a variable that means exactly one thing and
+#: that no compose file or template sets.
+DEV_AUTH_BYPASS_VAR = "AISOC_DEV_AUTH_BYPASS"
+
+#: Where the deployment publishes this service, comma-separated. Supplied by
+#: compose, because a container binds ``0.0.0.0`` and cannot otherwise know
+#: whether anyone but the operator's own machine can reach it.
+PUBLISHED_BIND_VAR = "AISOC_PUBLISHED_BIND_ADDRS"
#: Header a trusted service uses to declare which tenant it is acting for.
TENANT_HEADER = "X-AiSOC-Tenant-ID"
@@ -200,6 +222,86 @@ def _dev_mode() -> bool:
return os.getenv("AISOC_DEV_MODE", "").strip().lower() in _TRUTHY
+def _is_loopback(address: str) -> bool:
+ """Whether ``address`` reaches only the machine this service runs on."""
+ host = address.strip().lower()
+ if not host:
+ return True
+ if host.startswith("["):
+ host = host.partition("]")[0].lstrip("[")
+ elif host.count(":") == 1:
+ host = host.partition(":")[0]
+ if host in {"localhost", "::1", "ip6-localhost"}:
+ return True
+ try:
+ return ipaddress.ip_address(host).is_loopback
+ except ValueError:
+ # A hostname that is not obviously loopback. When the question cannot
+ # be decided, the safe answer is the one that refuses the bypass.
+ return False
+
+
+def auth_bypass_refusal() -> str | None:
+ """Why an uncredentialed caller must not be admitted, or ``None``.
+
+ Three conditions, where there used to be one. ``AISOC_DEV_MODE`` alone
+ admitted a caller with no credential at all whenever neither a console
+ secret nor a service token was configured — and ``docker-compose.yml``
+ defaults that flag to ``1`` on ten services while generating no service
+ token, so a hand-copied ``.env`` and a plain ``docker compose up`` left
+ this open.
+
+ Returns a sentence for the log, so an operator who expected the bypass
+ learns which condition withheld it instead of seeing an unexplained 503.
+ """
+ if not _dev_mode():
+ return "AISOC_DEV_MODE is not set"
+ if os.getenv(DEV_AUTH_BYPASS_VAR, "").strip().lower() not in _TRUTHY:
+ return (
+ f"{DEV_AUTH_BYPASS_VAR} is not set. AISOC_DEV_MODE selects several "
+ "unrelated development behaviours and defaults to 1 in compose, so "
+ "it no longer enables the anonymous path on its own. Set "
+ f"{DEV_AUTH_BYPASS_VAR}=1 deliberately, or use "
+ "infra/compose/docker-compose.dev.yml, which sets it"
+ )
+ published = [a for a in os.getenv(PUBLISHED_BIND_VAR, "").split(",") if a.strip()]
+ reachable = [a.strip() for a in published if not _is_loopback(a)]
+ if reachable:
+ return (
+ f"{DEV_AUTH_BYPASS_VAR} is set, but this deployment publishes "
+ f"{', '.join(reachable)}, which is not loopback. Bind to 127.0.0.1, "
+ "or configure SECRET_KEY and a service token"
+ )
+ return None
+
+
+#: Refusals and activations already logged, so a hot path does not emit one
+#: line per request. Keyed on the reason rather than counted, so a deployment
+#: whose refusal *changes* still says so once.
+_BYPASS_LOGGED: set[str] = set()
+
+
+def _bypass_permitted() -> bool:
+ """Whether to admit an uncredentialed caller, logging the decision once."""
+ refusal = auth_bypass_refusal()
+ if refusal is None:
+ if "__active__" not in _BYPASS_LOGGED:
+ _BYPASS_LOGGED.add("__active__")
+ logger.warning(
+ "ANONYMOUS ACCESS IS ENABLED on %s. Requests with no credential "
+ "resolve to the demo tenant %s. This is a development "
+ "convenience and must not be used where anyone else can reach "
+ "this host.",
+ SERVICE_NAME,
+ DEV_TENANT_ID,
+ )
+ return True
+ if refusal not in _BYPASS_LOGGED:
+ _BYPASS_LOGGED.add(refusal)
+ logger.info("anonymous access refused on %s: %s", SERVICE_NAME, refusal)
+ return False
+
+
def resolve_console_secret() -> str:
"""The HS256 secret console tokens are signed with, or "" when unusable."""
secret = (os.getenv("SECRET_KEY") or "").strip()
@@ -302,7 +404,7 @@ async def require_console_or_service_auth(
service_token = resolve_service_token()
if not console_secret and not service_token:
- if _dev_mode():
+ if _bypass_permitted():
return TenantPrincipal(
tenant_ids=frozenset({DEV_TENANT_ID}),
subject="dev",
diff --git a/services/osquery-tls/tests/test_tenant_scope.py b/services/osquery-tls/tests/test_tenant_scope.py
index adc488b19..3ba17f28d 100644
--- a/services/osquery-tls/tests/test_tenant_scope.py
+++ b/services/osquery-tls/tests/test_tenant_scope.py
@@ -269,6 +269,8 @@ async def test_dev_mode_only_applies_with_nothing_configured(monkeypatch: pytest
every tenant-scoped route.
"""
monkeypatch.setenv("AISOC_DEV_MODE", "1")
+ monkeypatch.setenv("AISOC_DEV_AUTH_BYPASS", "1")
+ monkeypatch.delenv("AISOC_PUBLISHED_BIND_ADDRS", raising=False)
monkeypatch.setenv("SECRET_KEY", SECRET)
monkeypatch.setenv("AISOC_SERVICE_TOKEN", SERVICE_TOKEN)
with pytest.raises(HTTPException) as exc:
@@ -277,10 +279,79 @@ async def test_dev_mode_only_applies_with_nothing_configured(monkeypatch: pytest
monkeypatch.setenv("SECRET_KEY", "")
monkeypatch.setenv("AISOC_SERVICE_TOKEN", "")
+ ts._BYPASS_LOGGED.clear()
principal = await ts.require_console_or_service_auth(authorization=None, x_aisoc_tenant_id=None)
assert principal.tenant_ids == frozenset({ts.DEV_TENANT_ID})
+async def test_dev_mode_alone_no_longer_admits_an_uncredentialed_caller(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ """The defect, stated as a test.
+
+ `AISOC_DEV_MODE` selects table autocreate, docs URLs, the metrics gate,
+ log formatting, the GraphiQL UI and an ephemeral vault key. It also
+ defaults to `1` on ten services in `docker-compose.yml`, while nothing
+ generates a service token. So a hand-copied `.env` and a plain
+ `docker compose up` admitted a caller with no credential at all, and the
+ only thing that had to be true for it was a flag nobody set for this
+ purpose.
+ """
+ monkeypatch.setenv("AISOC_DEV_MODE", "1")
+ monkeypatch.delenv("AISOC_DEV_AUTH_BYPASS", raising=False)
+ monkeypatch.delenv("AISOC_PUBLISHED_BIND_ADDRS", raising=False)
+ monkeypatch.setenv("SECRET_KEY", "")
+ monkeypatch.setenv("AISOC_SERVICE_TOKEN", "")
+ ts._BYPASS_LOGGED.clear()
+ with pytest.raises(HTTPException) as exc:
+ await ts.require_console_or_service_auth(authorization=None, x_aisoc_tenant_id=None)
+ assert exc.value.status_code == 503
+
+
+async def test_the_bypass_is_refused_on_a_reachable_published_address(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ """An anonymous principal is a convenience only on loopback.
+
+ The service binds `0.0.0.0` inside its container and cannot work out for
+ itself whether anyone else can reach it, so compose passes the published
+ addresses in. Both directions, because a check that only ever refuses is
+ indistinguishable from one that removed the shim.
+ """
+ monkeypatch.setenv("AISOC_DEV_MODE", "1")
+ monkeypatch.setenv("AISOC_DEV_AUTH_BYPASS", "1")
+ monkeypatch.setenv("SECRET_KEY", "")
+ monkeypatch.setenv("AISOC_SERVICE_TOKEN", "")
+
+ for published in ("0.0.0.0", "192.168.1.10", "127.0.0.1,0.0.0.0:8080", "aisoc.example.com"):
+ monkeypatch.setenv("AISOC_PUBLISHED_BIND_ADDRS", published)
+ ts._BYPASS_LOGGED.clear()
+ with pytest.raises(HTTPException) as exc:
+ await ts.require_console_or_service_auth(authorization=None, x_aisoc_tenant_id=None)
+ assert exc.value.status_code == 503, published
+
+ for published in ("127.0.0.1", "localhost:8080", "::1", "[::1]:9000"):
+ monkeypatch.setenv("AISOC_PUBLISHED_BIND_ADDRS", published)
+ ts._BYPASS_LOGGED.clear()
+ principal = await ts.require_console_or_service_auth(authorization=None, x_aisoc_tenant_id=None)
+ assert principal.tenant_ids == frozenset({ts.DEV_TENANT_ID}), published
+
+
+def test_the_dev_tenant_is_not_the_canonical_tenant() -> None:
+ """The crux, and it was byte-identical.
+
+ `DEV_TENANT_ID` was `…0001`, the tenant migration 001 seeds and that
+ `bootstrap_admin` puts the real administrator into. So an unauthenticated
+ caller admitted by the shim was scoped to the operator's own data rather
+ than to a demo tenant.
+ """
+ import uuid as _uuid
+
+ assert ts.DEV_TENANT_ID != _uuid.UUID("00000000-0000-0000-0000-000000000001")
+
+
@pytest.mark.asyncio
async def test_a_per_service_token_override_wins(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv(f"AISOC_{ts.SERVICE_NAME}_SERVICE_TOKEN", "service-specific")
diff --git a/services/purple-team/app/security/service_auth.py b/services/purple-team/app/security/service_auth.py
index 6d83544f0..8dc31a09c 100644
--- a/services/purple-team/app/security/service_auth.py
+++ b/services/purple-team/app/security/service_auth.py
@@ -44,12 +44,6 @@ from fastapi import Header, HTTPException, status
#: Set by each vendored copy so the per-service override resolves.
SERVICE_NAME = "PURPLE_TEAM"
-_TRUTHY = {"1", "true", "yes", "on"}
-
-
-def _dev_mode() -> bool:
- return os.getenv("AISOC_DEV_MODE", "").strip().lower() in _TRUTHY
-
def resolve_token() -> str:
"""Per-service override wins, then the shared platform token."""
@@ -63,8 +57,13 @@ async def require_service_auth(authorization: str | None = Header(default=None))
"""FastAPI dependency. Fails closed unless explicitly in dev mode."""
token = resolve_token()
if not token:
- if _dev_mode():
- return
+ # No dev-mode exemption, matching services/actions/app/security/authz.py.
+ # This branch used to return early when AISOC_DEV_MODE was set, and
+ # docker-compose.yml defaults that flag to 1 on ten services while
+ # nothing generates a service token — so the exemption was not a
+ # developer convenience, it was the state every stock install ran in.
+ # A service-to-service dependency has no browser to keep usable, so
+ # 503 with an actionable message is the whole of what is needed.
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail=(
diff --git a/services/purple-team/app/security/tenant_scope.py b/services/purple-team/app/security/tenant_scope.py
index 9b8f1f77d..1f5910c4e 100644
--- a/services/purple-team/app/security/tenant_scope.py
+++ b/services/purple-team/app/security/tenant_scope.py
@@ -62,6 +62,7 @@ import base64
import binascii
import hashlib
import hmac
+import ipaddress
import json
import logging
import os
@@ -92,8 +93,29 @@ INSECURE_SECRET_DEFAULTS = frozenset(
)
#: Deterministic demo tenant, matching ``DEMO_TENANT_ID`` in
-#: services/api/app/api/v1/dev_auth.py. Only ever used under AISOC_DEV_MODE.
-DEV_TENANT_ID = uuid.UUID("00000000-0000-0000-0000-000000000001")
+#: services/api/app/api/v1/dev_auth.py. Only ever reached when
+#: :func:`auth_bypass_refusal` returns ``None``.
+#:
+#: This used to be ``…0001``, the canonical tenant that migration 001 seeds
+#: and that ``bootstrap_admin`` puts the real administrator into, so an
+#: unauthenticated caller was scoped to the operator's own data. The canonical
+#: id stays where it is because ten modules pin it; the demo identity moved.
+DEV_TENANT_ID = uuid.UUID("00000000-0000-0000-0000-0000000000de")
+
+#: The environment variable whose only job is to enable the anonymous shim.
+#:
+#: Deliberately not ``AISOC_DEV_MODE``. That flag already selects table
+#: autocreate, docs URLs, the metrics gate, log formatting, the GraphiQL UI
+#: and an ephemeral vault key, and ``docker-compose.yml`` defaults it to ``1``
+#: on ten services. A flag that means nine things cannot be refused for one
+#: of them, so the bypass gets a variable that means exactly one thing and
+#: that no compose file or template sets.
+DEV_AUTH_BYPASS_VAR = "AISOC_DEV_AUTH_BYPASS"
+
+#: Where the deployment publishes this service, comma-separated. Supplied by
+#: compose, because a container binds ``0.0.0.0`` and cannot otherwise know
+#: whether anyone but the operator's own machine can reach it.
+PUBLISHED_BIND_VAR = "AISOC_PUBLISHED_BIND_ADDRS"
#: Header a trusted service uses to declare which tenant it is acting for.
TENANT_HEADER = "X-AiSOC-Tenant-ID"
@@ -200,6 +222,86 @@ def _dev_mode() -> bool:
return os.getenv("AISOC_DEV_MODE", "").strip().lower() in _TRUTHY
+def _is_loopback(address: str) -> bool:
+ """Whether ``address`` reaches only the machine this service runs on."""
+ host = address.strip().lower()
+ if not host:
+ return True
+ if host.startswith("["):
+ host = host.partition("]")[0].lstrip("[")
+ elif host.count(":") == 1:
+ host = host.partition(":")[0]
+ if host in {"localhost", "::1", "ip6-localhost"}:
+ return True
+ try:
+ return ipaddress.ip_address(host).is_loopback
+ except ValueError:
+ # A hostname that is not obviously loopback. When the question cannot
+ # be decided, the safe answer is the one that refuses the bypass.
+ return False
+
+
+def auth_bypass_refusal() -> str | None:
+ """Why an uncredentialed caller must not be admitted, or ``None``.
+
+ Three conditions, where there used to be one. ``AISOC_DEV_MODE`` alone
+ admitted a caller with no credential at all whenever neither a console
+ secret nor a service token was configured — and ``docker-compose.yml``
+ defaults that flag to ``1`` on ten services while generating no service
+ token, so a hand-copied ``.env`` and a plain ``docker compose up`` left
+ this open.
+
+ Returns a sentence for the log, so an operator who expected the bypass
+ learns which condition withheld it instead of seeing an unexplained 503.
+ """
+ if not _dev_mode():
+ return "AISOC_DEV_MODE is not set"
+ if os.getenv(DEV_AUTH_BYPASS_VAR, "").strip().lower() not in _TRUTHY:
+ return (
+ f"{DEV_AUTH_BYPASS_VAR} is not set. AISOC_DEV_MODE selects several "
+ "unrelated development behaviours and defaults to 1 in compose, so "
+ "it no longer enables the anonymous path on its own. Set "
+ f"{DEV_AUTH_BYPASS_VAR}=1 deliberately, or use "
+ "infra/compose/docker-compose.dev.yml, which sets it"
+ )
+ published = [a for a in os.getenv(PUBLISHED_BIND_VAR, "").split(",") if a.strip()]
+ reachable = [a.strip() for a in published if not _is_loopback(a)]
+ if reachable:
+ return (
+ f"{DEV_AUTH_BYPASS_VAR} is set, but this deployment publishes "
+ f"{', '.join(reachable)}, which is not loopback. Bind to 127.0.0.1, "
+ "or configure SECRET_KEY and a service token"
+ )
+ return None
+
+
+#: Refusals and activations already logged, so a hot path does not emit one
+#: line per request. Keyed on the reason rather than counted, so a deployment
+#: whose refusal *changes* still says so once.
+_BYPASS_LOGGED: set[str] = set()
+
+
+def _bypass_permitted() -> bool:
+ """Whether to admit an uncredentialed caller, logging the decision once."""
+ refusal = auth_bypass_refusal()
+ if refusal is None:
+ if "__active__" not in _BYPASS_LOGGED:
+ _BYPASS_LOGGED.add("__active__")
+ logger.warning(
+ "ANONYMOUS ACCESS IS ENABLED on %s. Requests with no credential "
+ "resolve to the demo tenant %s. This is a development "
+ "convenience and must not be used where anyone else can reach "
+ "this host.",
+ SERVICE_NAME,
+ DEV_TENANT_ID,
+ )
+ return True
+ if refusal not in _BYPASS_LOGGED:
+ _BYPASS_LOGGED.add(refusal)
+ logger.info("anonymous access refused on %s: %s", SERVICE_NAME, refusal)
+ return False
+
+
def resolve_console_secret() -> str:
"""The HS256 secret console tokens are signed with, or "" when unusable."""
secret = (os.getenv("SECRET_KEY") or "").strip()
@@ -302,7 +404,7 @@ async def require_console_or_service_auth(
service_token = resolve_service_token()
if not console_secret and not service_token:
- if _dev_mode():
+ if _bypass_permitted():
return TenantPrincipal(
tenant_ids=frozenset({DEV_TENANT_ID}),
subject="dev",
diff --git a/services/purple-team/tests/test_service_auth.py b/services/purple-team/tests/test_service_auth.py
index 67b56e135..eb7426ccb 100644
--- a/services/purple-team/tests/test_service_auth.py
+++ b/services/purple-team/tests/test_service_auth.py
@@ -49,10 +49,22 @@ async def test_unconfigured_in_production_fails_closed():
@pytest.mark.asyncio
-async def test_unconfigured_in_dev_mode_is_allowed(monkeypatch: pytest.MonkeyPatch):
- """`docker compose up` with no token still works locally."""
+async def test_unconfigured_in_dev_mode_is_refused(monkeypatch: pytest.MonkeyPatch):
+ """The exemption is gone, and this assertion used to read the other way.
+
+ `docker-compose.yml` defaults `AISOC_DEV_MODE` to 1 on ten services while
+ nothing generates a service token, so "unconfigured and in dev mode" was
+ not a local convenience, it was the state every stock install ran in. This
+ is a service-to-service dependency with no browser to keep usable, so 503
+ with an actionable message is the whole of what is needed — the same
+ posture `services/actions/app/security/authz.py` already took.
+ """
monkeypatch.setenv("AISOC_DEV_MODE", "1")
- assert await _call(None) is None
+ monkeypatch.delenv("AISOC_SERVICE_TOKEN", raising=False)
+ with pytest.raises(HTTPException) as exc:
+ await _call(None)
+ assert exc.value.status_code == 503
+ assert "AISOC_SERVICE_TOKEN" in exc.value.detail
@pytest.mark.asyncio
diff --git a/services/purple-team/tests/test_tenant_scope.py b/services/purple-team/tests/test_tenant_scope.py
index adc488b19..3ba17f28d 100644
--- a/services/purple-team/tests/test_tenant_scope.py
+++ b/services/purple-team/tests/test_tenant_scope.py
@@ -269,6 +269,8 @@ async def test_dev_mode_only_applies_with_nothing_configured(monkeypatch: pytest
every tenant-scoped route.
"""
monkeypatch.setenv("AISOC_DEV_MODE", "1")
+ monkeypatch.setenv("AISOC_DEV_AUTH_BYPASS", "1")
+ monkeypatch.delenv("AISOC_PUBLISHED_BIND_ADDRS", raising=False)
monkeypatch.setenv("SECRET_KEY", SECRET)
monkeypatch.setenv("AISOC_SERVICE_TOKEN", SERVICE_TOKEN)
with pytest.raises(HTTPException) as exc:
@@ -277,10 +279,79 @@ async def test_dev_mode_only_applies_with_nothing_configured(monkeypatch: pytest
monkeypatch.setenv("SECRET_KEY", "")
monkeypatch.setenv("AISOC_SERVICE_TOKEN", "")
+ ts._BYPASS_LOGGED.clear()
principal = await ts.require_console_or_service_auth(authorization=None, x_aisoc_tenant_id=None)
assert principal.tenant_ids == frozenset({ts.DEV_TENANT_ID})
+async def test_dev_mode_alone_no_longer_admits_an_uncredentialed_caller(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ """The defect, stated as a test.
+
+ `AISOC_DEV_MODE` selects table autocreate, docs URLs, the metrics gate,
+ log formatting, the GraphiQL UI and an ephemeral vault key. It also
+ defaults to `1` on ten services in `docker-compose.yml`, while nothing
+ generates a service token. So a hand-copied `.env` and a plain
+ `docker compose up` admitted a caller with no credential at all, and the
+ only thing that had to be true for it was a flag nobody set for this
+ purpose.
+ """
+ monkeypatch.setenv("AISOC_DEV_MODE", "1")
+ monkeypatch.delenv("AISOC_DEV_AUTH_BYPASS", raising=False)
+ monkeypatch.delenv("AISOC_PUBLISHED_BIND_ADDRS", raising=False)
+ monkeypatch.setenv("SECRET_KEY", "")
+ monkeypatch.setenv("AISOC_SERVICE_TOKEN", "")
+ ts._BYPASS_LOGGED.clear()
+ with pytest.raises(HTTPException) as exc:
+ await ts.require_console_or_service_auth(authorization=None, x_aisoc_tenant_id=None)
+ assert exc.value.status_code == 503
+
+
+async def test_the_bypass_is_refused_on_a_reachable_published_address(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ """An anonymous principal is a convenience only on loopback.
+
+ The service binds `0.0.0.0` inside its container and cannot work out for
+ itself whether anyone else can reach it, so compose passes the published
+ addresses in. Both directions, because a check that only ever refuses is
+ indistinguishable from one that removed the shim.
+ """
+ monkeypatch.setenv("AISOC_DEV_MODE", "1")
+ monkeypatch.setenv("AISOC_DEV_AUTH_BYPASS", "1")
+ monkeypatch.setenv("SECRET_KEY", "")
+ monkeypatch.setenv("AISOC_SERVICE_TOKEN", "")
+
+ for published in ("0.0.0.0", "192.168.1.10", "127.0.0.1,0.0.0.0:8080", "aisoc.example.com"):
+ monkeypatch.setenv("AISOC_PUBLISHED_BIND_ADDRS", published)
+ ts._BYPASS_LOGGED.clear()
+ with pytest.raises(HTTPException) as exc:
+ await ts.require_console_or_service_auth(authorization=None, x_aisoc_tenant_id=None)
+ assert exc.value.status_code == 503, published
+
+ for published in ("127.0.0.1", "localhost:8080", "::1", "[::1]:9000"):
+ monkeypatch.setenv("AISOC_PUBLISHED_BIND_ADDRS", published)
+ ts._BYPASS_LOGGED.clear()
+ principal = await ts.require_console_or_service_auth(authorization=None, x_aisoc_tenant_id=None)
+ assert principal.tenant_ids == frozenset({ts.DEV_TENANT_ID}), published
+
+
+def test_the_dev_tenant_is_not_the_canonical_tenant() -> None:
+ """The crux, and it was byte-identical.
+
+ `DEV_TENANT_ID` was `…0001`, the tenant migration 001 seeds and that
+ `bootstrap_admin` puts the real administrator into. So an unauthenticated
+ caller admitted by the shim was scoped to the operator's own data rather
+ than to a demo tenant.
+ """
+ import uuid as _uuid
+
+ assert ts.DEV_TENANT_ID != _uuid.UUID("00000000-0000-0000-0000-000000000001")
+
+
@pytest.mark.asyncio
async def test_a_per_service_token_override_wins(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv(f"AISOC_{ts.SERVICE_NAME}_SERVICE_TOKEN", "service-specific")
diff --git a/services/realtime/src/index.ts b/services/realtime/src/index.ts
index 8355c1579..bfe2d83f1 100644
--- a/services/realtime/src/index.ts
+++ b/services/realtime/src/index.ts
@@ -666,10 +666,29 @@ function asyncRoute(
};
}
+// `public-key` is the VAPID public key and is public by definition.
app.get('/v1/push/public-key', pushManager.publicKeyHandler);
-app.post('/v1/push/subscribe', pushRateLimit, asyncRoute(pushManager.subscribeHandler));
-app.post('/v1/push/unsubscribe', pushRateLimit, asyncRoute(pushManager.unsubscribeHandler));
-app.post('/v1/push/test', pushRateLimit, asyncRoute(pushManager.testNotifyHandler));
+
+// The other three carried only a rate limiter, and took the tenant from a
+// request header, then a query parameter, then the literal string 'default'.
+// So any caller who could reach the port could enrol a push endpoint against
+// any tenant, unsubscribe another tenant's devices, or make this service send
+// a notification. They are reached through the API's `/api/v1/push/*` proxy,
+// which authenticates the analyst and stamps the tenant, so the same internal
+// token `/internal/*` requires is the right contract: it makes the proxy the
+// only way in, which is what the module already assumed.
+app.post('/v1/push/subscribe', pushRateLimit, asyncRoute(async (req, res) => {
+ if (!requireInternal(req, res)) return;
+ await pushManager.subscribeHandler(req, res);
+}));
+app.post('/v1/push/unsubscribe', pushRateLimit, asyncRoute(async (req, res) => {
+ if (!requireInternal(req, res)) return;
+ await pushManager.unsubscribeHandler(req, res);
+}));
+app.post('/v1/push/test', pushRateLimit, asyncRoute(async (req, res) => {
+ if (!requireInternal(req, res)) return;
+ await pushManager.testNotifyHandler(req, res);
+}));
// --- Internal broadcast endpoint (called by other services) ---
// POST /internal/agent-event
@@ -694,7 +713,12 @@ function requireInternal(req: express.Request, res: express.Response): boolean {
});
return false;
}
- const auth = req.headers['x-internal-token'];
+ // Both spellings. `x-internal-token` is what the agents service and the
+ // API's approval route send; `x-aisoc-internal-token` is what the API's
+ // push proxy and the chatops notifier send, and what slack-bot reads. One
+ // receiver accepting both is the only fix that does not require every
+ // sender to change in the same release.
+ const auth = req.headers['x-internal-token'] ?? req.headers['x-aisoc-internal-token'];
if (typeof auth !== 'string' || auth.length !== INTERNAL_TOKEN.length) {
res.status(401).json({ error: 'unauthorized' });
return false;
diff --git a/services/realtime/src/push.ts b/services/realtime/src/push.ts
index b4f799b6d..c8b33d0de 100644
--- a/services/realtime/src/push.ts
+++ b/services/realtime/src/push.ts
@@ -89,23 +89,42 @@ function key(...parts: string[]): string {
return ['aisoc', 'push', ...parts].join(':');
}
+class MissingTenant extends Error {
+ constructor() {
+ super('X-Tenant-Id is required');
+ this.name = 'MissingTenant';
+ }
+}
+
+/**
+ * The tenant this request acts for, from the header the API proxy stamps.
+ *
+ * Two fallbacks were removed rather than kept as defence in depth, because
+ * both were ways to get the wrong answer quietly. A `tenant_id` query
+ * parameter is caller-supplied, and the routes now require the internal token
+ * so the only caller is the proxy, which sends a header. And the literal
+ * `'default'` is not a tenant at all: migration 001 seeds the canonical tenant
+ * with that *slug* and the demo seed renames it, so every subscription that
+ * reached the fallback was filed under a key belonging to nobody, silently.
+ */
function tenantOf(req: Request): string {
const hdr = req.headers['x-tenant-id'];
if (typeof hdr === 'string' && hdr.length > 0) return hdr;
if (Array.isArray(hdr) && hdr[0]) return hdr[0];
- const q = req.query.tenant_id;
- if (typeof q === 'string' && q.length > 0) return q;
- return 'default';
+ throw new MissingTenant();
}
function userOf(req: Request, body: SubscribeRequestBody): string | null {
- // Prefer the body field for now; the API gateway is expected to validate
- // it against the bearer token before traffic reaches the realtime
- // service in production.
- if (body.user_id) return body.user_id;
+ // The header first, and the body only as a fallback. This was the other way
+ // round, under a comment saying the API gateway "is expected to" validate
+ // the body field against the bearer token. It does not: it stamps
+ // `X-User-Id` from the authenticated principal and forwards the body
+ // untouched, so preferring the body let a caller enrol a push endpoint
+ // against another user in their own tenant.
const hdr = req.headers['x-user-id'];
if (typeof hdr === 'string' && hdr.length > 0) return hdr;
if (Array.isArray(hdr) && hdr[0]) return hdr[0];
+ if (body.user_id) return body.user_id;
return null;
}
@@ -445,7 +464,13 @@ export class PushManager {
};
unsubscribeHandler = async (req: Request, res: Response): Promise<void> => {
- const tenantId = tenantOf(req);
+ let tenantId: string;
+ try {
+ tenantId = tenantOf(req);
+ } catch (err) {
+ res.status(400).json({ error: (err as Error).message });
+ return;
+ }
const { endpoint } = (req.body ?? {}) as { endpoint?: string };
if (!endpoint) {
res.status(400).json({ error: 'endpoint is required' });
@@ -460,7 +485,13 @@ export class PushManager {
res.status(503).json({ error: 'push not configured' });
return;
}
- const tenantId = tenantOf(req);
+ let tenantId: string;
+ try {
+ tenantId = tenantOf(req);
+ } catch (err) {
+ res.status(400).json({ error: (err as Error).message });
+ return;
+ }
const userIdHdr = req.headers['x-user-id'];
const userId =
typeof userIdHdr === 'string'
@@ -505,9 +536,21 @@ export class PushManager {
res.status(400).json({ error: 'notification.title and body are required' });
return;
}
+ // This route is already behind the internal token, so a service naming the
+ // tenant in the body is the normal case. The header is the fallback, and
+ // `tenantOf` now throws rather than inventing 'default', so a call that
+ // names no tenant at all has to answer 400 instead of filing the
+ // notification under a key belonging to nobody.
+ let tenantId: string;
+ try {
+ tenantId = body.tenant_id ?? tenantOf(req);
+ } catch (err) {
+ res.status(400).json({ error: (err as Error).message });
+ return;
+ }
const stats = await this.sendToTarget(
{
- tenant_id: body.tenant_id ?? tenantOf(req),
+ tenant_id: tenantId,
user_ids: body.user_ids,
topic: body.topic,
},
@@ -516,3 +559,10 @@ export class PushManager {
res.json(stats);
};
}
+
+/**
+ * The two request resolvers, for the unit test that pins where the tenant and
+ * the user come from. Grouped under one name so the module's public surface
+ * still reads as "the PushManager class" at a glance.
+ */
+export const __testing = { tenantOf, userOf };
diff --git a/services/realtime/test/push-tenant.test.ts b/services/realtime/test/push-tenant.test.ts
new file mode 100644
index 000000000..b414383b5
--- /dev/null
+++ b/services/realtime/test/push-tenant.test.ts
@@ -0,0 +1,72 @@
+/**
+ * Web Push must not take the tenant from whoever asks.
+ *
+ * `tenantOf` read the `x-tenant-id` header, then a `tenant_id` query
+ * parameter, then fell back to the literal string `'default'`. Both fallbacks
+ * were ways to get the wrong answer quietly:
+ *
+ * - the query parameter is caller-supplied, and the three `POST /v1/push/*`
+ * routes carried only a rate limiter, so any caller who could reach the
+ * port could enrol a push endpoint against any tenant, unsubscribe another
+ * tenant's devices, or make this service send a notification;
+ * - `'default'` is not a tenant. Migration 001 seeds the canonical tenant
+ * with that *slug* and the demo seed renames it, so every subscription that
+ * reached the fallback was filed under a Redis key belonging to nobody.
+ *
+ * The routes now require the same internal token `/internal/*` does, which
+ * makes the API's `/api/v1/push/*` proxy the only way in — the arrangement
+ * the module's own comments already assumed. These tests exercise the
+ * resolver directly, because standing up Express, Redis and a VAPID keypair
+ * to assert a 400 would test the harness rather than the rule.
+ */
+import { test } from 'node:test';
+import assert from 'node:assert/strict';
+
+import { __testing } from '../src/push';
+
+const { tenantOf, userOf } = __testing;
+
+function req(headers: Record<string, string | string[]> = {}, query: Record<string, string> = {}) {
+ return { headers, query } as never;
+}
+
+test('the tenant comes from the header the proxy stamps', () => {
+ assert.equal(tenantOf(req({ 'x-tenant-id': 'tenant-a' })), 'tenant-a');
+});
+
+test('a repeated header is read as its first value, not joined', () => {
+ assert.equal(tenantOf(req({ 'x-tenant-id': ['tenant-a', 'tenant-b'] })), 'tenant-a');
+});
+
+test('a caller-supplied query parameter is not a tenant claim', () => {
+ assert.throws(() => tenantOf(req({}, { tenant_id: 'tenant-victim' })), /X-Tenant-Id is required/);
+});
+
+test('no tenant at all throws rather than inventing one', () => {
+ // The assertion that matters. This used to return 'default'.
+ assert.throws(() => tenantOf(req()), /X-Tenant-Id is required/);
+});
+
+test('an empty header is not a tenant either', () => {
+ assert.throws(() => tenantOf(req({ 'x-tenant-id': '' })), /X-Tenant-Id is required/);
+});
+
+test('the user comes from the stamped header in preference to the body', () => {
+ // This was the other way round, under a comment saying the API gateway
+ // "is expected to" validate the body field against the bearer token. It
+ // does not: it stamps X-User-Id from the authenticated principal and
+ // forwards the body untouched, so preferring the body let a caller enrol a
+ // push endpoint against another user in their own tenant.
+ assert.equal(
+ userOf(req({ 'x-user-id': 'real-user' }), { user_id: 'someone-else' } as never),
+ 'real-user',
+ );
+});
+
+test('the body is still read when no header was stamped', () => {
+ assert.equal(userOf(req(), { user_id: 'body-user' } as never), 'body-user');
+});
+
+test('an anonymous subscription is still allowed to have no user', () => {
+ assert.equal(userOf(req(), {} as never), null);
+});
diff --git a/services/slack-bot/app/notify.py b/services/slack-bot/app/notify.py
index df53d65f7..56029dd31 100644
--- a/services/slack-bot/app/notify.py
+++ b/services/slack-bot/app/notify.py
@@ -22,7 +22,6 @@ signature and is authenticated with a shared token instead.
from __future__ import annotations
import hmac
-import os
from typing import Any
import structlog
@@ -64,15 +63,18 @@ class ApprovalCardRequest(BaseModel):
def _authorized(supplied: str | None) -> bool:
"""Compare the internal token in constant time, failing closed.
- An unset token refuses every internal call unless dev mode is on. The
- alternative — treating "no token configured" as "no auth needed" — makes
- a route that can post into a workspace channel open to anything that can
- reach the pod.
+ An unset token refuses every internal call, with no dev-mode exemption.
+ That exemption used to exist and it was the only state a stock install ran
+ in: `docker-compose.yml` defaults `AISOC_DEV_MODE` to 1 on this service,
+ and `AISOC_INTERNAL_TOKEN` appeared only commented out in `.env.example`
+ with nothing generating it. So a route that posts into a workspace channel
+ was open to anything that could reach the pod. `scripts/ensure_env.py` now
+ generates the token, which is what makes requiring it viable.
"""
settings = get_settings()
expected = settings.AISOC_INTERNAL_TOKEN.strip()
if not expected:
- return os.environ.get("AISOC_DEV_MODE", "").strip().lower() in {"1", "true", "yes", "on"}
+ return False
return bool(supplied) and hmac.compare_digest(supplied.strip(), expected)
diff --git a/services/threatintel/app/security/tenant_scope.py b/services/threatintel/app/security/tenant_scope.py
index 6d862aeba..7a5aa15ac 100644
--- a/services/threatintel/app/security/tenant_scope.py
+++ b/services/threatintel/app/security/tenant_scope.py
@@ -62,6 +62,7 @@ import base64
import binascii
import hashlib
import hmac
+import ipaddress
import json
import logging
import os
@@ -92,8 +93,29 @@ INSECURE_SECRET_DEFAULTS = frozenset(
)
#: Deterministic demo tenant, matching ``DEMO_TENANT_ID`` in
-#: services/api/app/api/v1/dev_auth.py. Only ever used under AISOC_DEV_MODE.
-DEV_TENANT_ID = uuid.UUID("00000000-0000-0000-0000-000000000001")
+#: services/api/app/api/v1/dev_auth.py. Only ever reached when
+#: :func:`auth_bypass_refusal` returns ``None``.
+#:
+#: This used to be ``…0001``, the canonical tenant that migration 001 seeds
+#: and that ``bootstrap_admin`` puts the real administrator into, so an
+#: unauthenticated caller was scoped to the operator's own data. The canonical
+#: id stays where it is because ten modules pin it; the demo identity moved.
+DEV_TENANT_ID = uuid.UUID("00000000-0000-0000-0000-0000000000de")
+
+#: The environment variable whose only job is to enable the anonymous shim.
+#:
+#: Deliberately not ``AISOC_DEV_MODE``. That flag already selects table
+#: autocreate, docs URLs, the metrics gate, log formatting, the GraphiQL UI
+#: and an ephemeral vault key, and ``docker-compose.yml`` defaults it to ``1``
+#: on ten services. A flag that means nine things cannot be refused for one
+#: of them, so the bypass gets a variable that means exactly one thing and
+#: that no compose file or template sets.
+DEV_AUTH_BYPASS_VAR = "AISOC_DEV_AUTH_BYPASS"
+
+#: Where the deployment publishes this service, comma-separated. Supplied by
+#: compose, because a container binds ``0.0.0.0`` and cannot otherwise know
+#: whether anyone but the operator's own machine can reach it.
+PUBLISHED_BIND_VAR = "AISOC_PUBLISHED_BIND_ADDRS"
#: Header a trusted service uses to declare which tenant it is acting for.
TENANT_HEADER = "X-AiSOC-Tenant-ID"
@@ -200,6 +222,86 @@ def _dev_mode() -> bool:
return os.getenv("AISOC_DEV_MODE", "").strip().lower() in _TRUTHY
+def _is_loopback(address: str) -> bool:
+ """Whether ``address`` reaches only the machine this service runs on."""
+ host = address.strip().lower()
+ if not host:
+ return True
+ if host.startswith("["):
+ host = host.partition("]")[0].lstrip("[")
+ elif host.count(":") == 1:
+ host = host.partition(":")[0]
+ if host in {"localhost", "::1", "ip6-localhost"}:
+ return True
+ try:
+ return ipaddress.ip_address(host).is_loopback
+ except ValueError:
+ # A hostname that is not obviously loopback. When the question cannot
+ # be decided, the safe answer is the one that refuses the bypass.
+ return False
+
+
+def auth_bypass_refusal() -> str | None:
+ """Why an uncredentialed caller must not be admitted, or ``None``.
+
+ Three conditions, where there used to be one. ``AISOC_DEV_MODE`` alone
+ admitted a caller with no credential at all whenever neither a console
+ secret nor a service token was configured — and ``docker-compose.yml``
+ defaults that flag to ``1`` on ten services while generating no service
+ token, so a hand-copied ``.env`` and a plain ``docker compose up`` left
+ this open.
+
+ Returns a sentence for the log, so an operator who expected the bypass
+ learns which condition withheld it instead of seeing an unexplained 503.
+ """
+ if not _dev_mode():
+ return "AISOC_DEV_MODE is not set"
+ if os.getenv(DEV_AUTH_BYPASS_VAR, "").strip().lower() not in _TRUTHY:
+ return (
+ f"{DEV_AUTH_BYPASS_VAR} is not set. AISOC_DEV_MODE selects several "
+ "unrelated development behaviours and defaults to 1 in compose, so "
+ "it no longer enables the anonymous path on its own. Set "
+ f"{DEV_AUTH_BYPASS_VAR}=1 deliberately, or use "
+ "infra/compose/docker-compose.dev.yml, which sets it"
+ )
+ published = [a for a in os.getenv(PUBLISHED_BIND_VAR, "").split(",") if a.strip()]
+ reachable = [a.strip() for a in published if not _is_loopback(a)]
+ if reachable:
+ return (
+ f"{DEV_AUTH_BYPASS_VAR} is set, but this deployment publishes "
+ f"{', '.join(reachable)}, which is not loopback. Bind to 127.0.0.1, "
+ "or configure SECRET_KEY and a service token"
+ )
+ return None
+
+
+#: Refusals and activations already logged, so a hot path does not emit one
+#: line per request. Keyed on the reason rather than counted, so a deployment
+#: whose refusal *changes* still says so once.
+_BYPASS_LOGGED: set[str] = set()
+
+
+def _bypass_permitted() -> bool:
+ """Whether to admit an uncredentialed caller, logging the decision once."""
+ refusal = auth_bypass_refusal()
+ if refusal is None:
+ if "__active__" not in _BYPASS_LOGGED:
+ _BYPASS_LOGGED.add("__active__")
+ logger.warning(
+ "ANONYMOUS ACCESS IS ENABLED on %s. Requests with no credential "
+ "resolve to the demo tenant %s. This is a development "
+ "convenience and must not be used where anyone else can reach "
+ "this host.",
+ SERVICE_NAME,
+ DEV_TENANT_ID,
+ )
+ return True
+ if refusal not in _BYPASS_LOGGED:
+ _BYPASS_LOGGED.add(refusal)
+ logger.info("anonymous access refused on %s: %s", SERVICE_NAME, refusal)
+ return False
+
+
def resolve_console_secret() -> str:
"""The HS256 secret console tokens are signed with, or "" when unusable."""
secret = (os.getenv("SECRET_KEY") or "").strip()
@@ -302,7 +404,7 @@ async def require_console_or_service_auth(
service_token = resolve_service_token()
if not console_secret and not service_token:
- if _dev_mode():
+ if _bypass_permitted():
return TenantPrincipal(
tenant_ids=frozenset({DEV_TENANT_ID}),
subject="dev",
diff --git a/services/threatintel/tests/test_tenant_scope.py b/services/threatintel/tests/test_tenant_scope.py
index adc488b19..3ba17f28d 100644
--- a/services/threatintel/tests/test_tenant_scope.py
+++ b/services/threatintel/tests/test_tenant_scope.py
@@ -269,6 +269,8 @@ async def test_dev_mode_only_applies_with_nothing_configured(monkeypatch: pytest
every tenant-scoped route.
"""
monkeypatch.setenv("AISOC_DEV_MODE", "1")
+ monkeypatch.setenv("AISOC_DEV_AUTH_BYPASS", "1")
+ monkeypatch.delenv("AISOC_PUBLISHED_BIND_ADDRS", raising=False)
monkeypatch.setenv("SECRET_KEY", SECRET)
monkeypatch.setenv("AISOC_SERVICE_TOKEN", SERVICE_TOKEN)
with pytest.raises(HTTPException) as exc:
@@ -277,10 +279,79 @@ async def test_dev_mode_only_applies_with_nothing_configured(monkeypatch: pytest
monkeypatch.setenv("SECRET_KEY", "")
monkeypatch.setenv("AISOC_SERVICE_TOKEN", "")
+ ts._BYPASS_LOGGED.clear()
principal = await ts.require_console_or_service_auth(authorization=None, x_aisoc_tenant_id=None)
assert principal.tenant_ids == frozenset({ts.DEV_TENANT_ID})
+async def test_dev_mode_alone_no_longer_admits_an_uncredentialed_caller(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ """The defect, stated as a test.
+
+ `AISOC_DEV_MODE` selects table autocreate, docs URLs, the metrics gate,
+ log formatting, the GraphiQL UI and an ephemeral vault key. It also
+ defaults to `1` on ten services in `docker-compose.yml`, while nothing
+ generates a service token. So a hand-copied `.env` and a plain
+ `docker compose up` admitted a caller with no credential at all, and the
+ only thing that had to be true for it was a flag nobody set for this
+ purpose.
+ """
+ monkeypatch.setenv("AISOC_DEV_MODE", "1")
+ monkeypatch.delenv("AISOC_DEV_AUTH_BYPASS", raising=False)
+ monkeypatch.delenv("AISOC_PUBLISHED_BIND_ADDRS", raising=False)
+ monkeypatch.setenv("SECRET_KEY", "")
+ monkeypatch.setenv("AISOC_SERVICE_TOKEN", "")
+ ts._BYPASS_LOGGED.clear()
+ with pytest.raises(HTTPException) as exc:
+ await ts.require_console_or_service_auth(authorization=None, x_aisoc_tenant_id=None)
+ assert exc.value.status_code == 503
+
+
+async def test_the_bypass_is_refused_on_a_reachable_published_address(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ """An anonymous principal is a convenience only on loopback.
+
+ The service binds `0.0.0.0` inside its container and cannot work out for
+ itself whether anyone else can reach it, so compose passes the published
+ addresses in. Both directions, because a check that only ever refuses is
+ indistinguishable from one that removed the shim.
+ """
+ monkeypatch.setenv("AISOC_DEV_MODE", "1")
+ monkeypatch.setenv("AISOC_DEV_AUTH_BYPASS", "1")
+ monkeypatch.setenv("SECRET_KEY", "")
+ monkeypatch.setenv("AISOC_SERVICE_TOKEN", "")
+
+ for published in ("0.0.0.0", "192.168.1.10", "127.0.0.1,0.0.0.0:8080", "aisoc.example.com"):
+ monkeypatch.setenv("AISOC_PUBLISHED_BIND_ADDRS", published)
+ ts._BYPASS_LOGGED.clear()
+ with pytest.raises(HTTPException) as exc:
+ await ts.require_console_or_service_auth(authorization=None, x_aisoc_tenant_id=None)
+ assert exc.value.status_code == 503, published
+
+ for published in ("127.0.0.1", "localhost:8080", "::1", "[::1]:9000"):
+ monkeypatch.setenv("AISOC_PUBLISHED_BIND_ADDRS", published)
+ ts._BYPASS_LOGGED.clear()
+ principal = await ts.require_console_or_service_auth(authorization=None, x_aisoc_tenant_id=None)
+ assert principal.tenant_ids == frozenset({ts.DEV_TENANT_ID}), published
+
+
+def test_the_dev_tenant_is_not_the_canonical_tenant() -> None:
+ """The crux, and it was byte-identical.
+
+ `DEV_TENANT_ID` was `…0001`, the tenant migration 001 seeds and that
+ `bootstrap_admin` puts the real administrator into. So an unauthenticated
+ caller admitted by the shim was scoped to the operator's own data rather
+ than to a demo tenant.
+ """
+ import uuid as _uuid
+
+ assert ts.DEV_TENANT_ID != _uuid.UUID("00000000-0000-0000-0000-000000000001")
+
+
@pytest.mark.asyncio
async def test_a_per_service_token_override_wins(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv(f"AISOC_{ts.SERVICE_NAME}_SERVICE_TOKEN", "service-specific")
diff --git a/services/ueba/app/security/service_auth.py b/services/ueba/app/security/service_auth.py
index a2c7df12e..cff0a7184 100644
--- a/services/ueba/app/security/service_auth.py
+++ b/services/ueba/app/security/service_auth.py
@@ -44,12 +44,6 @@ from fastapi import Header, HTTPException, status
#: Set by each vendored copy so the per-service override resolves.
SERVICE_NAME = "UEBA"
-_TRUTHY = {"1", "true", "yes", "on"}
-
-
-def _dev_mode() -> bool:
- return os.getenv("AISOC_DEV_MODE", "").strip().lower() in _TRUTHY
-
def resolve_token() -> str:
"""Per-service override wins, then the shared platform token."""
@@ -63,8 +57,13 @@ async def require_service_auth(authorization: str | None = Header(default=None))
"""FastAPI dependency. Fails closed unless explicitly in dev mode."""
token = resolve_token()
if not token:
- if _dev_mode():
- return
+ # No dev-mode exemption, matching services/actions/app/security/authz.py.
+ # This branch used to return early when AISOC_DEV_MODE was set, and
+ # docker-compose.yml defaults that flag to 1 on ten services while
+ # nothing generates a service token — so the exemption was not a
+ # developer convenience, it was the state every stock install ran in.
+ # A service-to-service dependency has no browser to keep usable, so
+ # 503 with an actionable message is the whole of what is needed.
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail=(
diff --git a/services/ueba/app/security/tenant_scope.py b/services/ueba/app/security/tenant_scope.py
index 4a04c6c40..28265ddf6 100644
--- a/services/ueba/app/security/tenant_scope.py
+++ b/services/ueba/app/security/tenant_scope.py
@@ -62,6 +62,7 @@ import base64
import binascii
import hashlib
import hmac
+import ipaddress
import json
import logging
import os
@@ -92,8 +93,29 @@ INSECURE_SECRET_DEFAULTS = frozenset(
)
#: Deterministic demo tenant, matching ``DEMO_TENANT_ID`` in
-#: services/api/app/api/v1/dev_auth.py. Only ever used under AISOC_DEV_MODE.
-DEV_TENANT_ID = uuid.UUID("00000000-0000-0000-0000-000000000001")
+#: services/api/app/api/v1/dev_auth.py. Only ever reached when
+#: :func:`auth_bypass_refusal` returns ``None``.
+#:
+#: This used to be ``…0001``, the canonical tenant that migration 001 seeds
+#: and that ``bootstrap_admin`` puts the real administrator into, so an
+#: unauthenticated caller was scoped to the operator's own data. The canonical
+#: id stays where it is because ten modules pin it; the demo identity moved.
+DEV_TENANT_ID = uuid.UUID("00000000-0000-0000-0000-0000000000de")
+
+#: The environment variable whose only job is to enable the anonymous shim.
+#:
+#: Deliberately not ``AISOC_DEV_MODE``. That flag already selects table
+#: autocreate, docs URLs, the metrics gate, log formatting, the GraphiQL UI
+#: and an ephemeral vault key, and ``docker-compose.yml`` defaults it to ``1``
+#: on ten services. A flag that means nine things cannot be refused for one
+#: of them, so the bypass gets a variable that means exactly one thing and
+#: that no compose file or template sets.
+DEV_AUTH_BYPASS_VAR = "AISOC_DEV_AUTH_BYPASS"
+
+#: Where the deployment publishes this service, comma-separated. Supplied by
+#: compose, because a container binds ``0.0.0.0`` and cannot otherwise know
+#: whether anyone but the operator's own machine can reach it.
+PUBLISHED_BIND_VAR = "AISOC_PUBLISHED_BIND_ADDRS"
#: Header a trusted service uses to declare which tenant it is acting for.
TENANT_HEADER = "X-AiSOC-Tenant-ID"
@@ -200,6 +222,86 @@ def _dev_mode() -> bool:
return os.getenv("AISOC_DEV_MODE", "").strip().lower() in _TRUTHY
+def _is_loopback(address: str) -> bool:
+ """Whether ``address`` reaches only the machine this service runs on."""
+ host = address.strip().lower()
+ if not host:
+ return True
+ if host.startswith("["):
+ host = host.partition("]")[0].lstrip("[")
+ elif host.count(":") == 1:
+ host = host.partition(":")[0]
+ if host in {"localhost", "::1", "ip6-localhost"}:
+ return True
+ try:
+ return ipaddress.ip_address(host).is_loopback
+ except ValueError:
+ # A hostname that is not obviously loopback. When the question cannot
+ # be decided, the safe answer is the one that refuses the bypass.
+ return False
+
+
+def auth_bypass_refusal() -> str | None:
+ """Why an uncredentialed caller must not be admitted, or ``None``.
+
+ Three conditions, where there used to be one. ``AISOC_DEV_MODE`` alone
+ admitted a caller with no credential at all whenever neither a console
+ secret nor a service token was configured — and ``docker-compose.yml``
+ defaults that flag to ``1`` on ten services while generating no service
+ token, so a hand-copied ``.env`` and a plain ``docker compose up`` left
+ this open.
+
+ Returns a sentence for the log, so an operator who expected the bypass
+ learns which condition withheld it instead of seeing an unexplained 503.
+ """
+ if not _dev_mode():
+ return "AISOC_DEV_MODE is not set"
+ if os.getenv(DEV_AUTH_BYPASS_VAR, "").strip().lower() not in _TRUTHY:
+ return (
+ f"{DEV_AUTH_BYPASS_VAR} is not set. AISOC_DEV_MODE selects several "
+ "unrelated development behaviours and defaults to 1 in compose, so "
+ "it no longer enables the anonymous path on its own. Set "
+ f"{DEV_AUTH_BYPASS_VAR}=1 deliberately, or use "
+ "infra/compose/docker-compose.dev.yml, which sets it"
+ )
+ published = [a for a in os.getenv(PUBLISHED_BIND_VAR, "").split(",") if a.strip()]
+ reachable = [a.strip() for a in published if not _is_loopback(a)]
+ if reachable:
+ return (
+ f"{DEV_AUTH_BYPASS_VAR} is set, but this deployment publishes "
+ f"{', '.join(reachable)}, which is not loopback. Bind to 127.0.0.1, "
+ "or configure SECRET_KEY and a service token"
+ )
+ return None
+
+
+#: Refusals and activations already logged, so a hot path does not emit one
+#: line per request. Keyed on the reason rather than counted, so a deployment
+#: whose refusal *changes* still says so once.
+_BYPASS_LOGGED: set[str] = set()
+
+
+def _bypass_permitted() -> bool:
+ """Whether to admit an uncredentialed caller, logging the decision once."""
+ refusal = auth_bypass_refusal()
+ if refusal is None:
+ if "__active__" not in _BYPASS_LOGGED:
+ _BYPASS_LOGGED.add("__active__")
+ logger.warning(
+ "ANONYMOUS ACCESS IS ENABLED on %s. Requests with no credential "
+ "resolve to the demo tenant %s. This is a development "
+ "convenience and must not be used where anyone else can reach "
+ "this host.",
+ SERVICE_NAME,
+ DEV_TENANT_ID,
+ )
+ return True
+ if refusal not in _BYPASS_LOGGED:
+ _BYPASS_LOGGED.add(refusal)
+ logger.info("anonymous access refused on %s: %s", SERVICE_NAME, refusal)
+ return False
+
+
def resolve_console_secret() -> str:
"""The HS256 secret console tokens are signed with, or "" when unusable."""
secret = (os.getenv("SECRET_KEY") or "").strip()
@@ -302,7 +404,7 @@ async def require_console_or_service_auth(
service_token = resolve_service_token()
if not console_secret and not service_token:
- if _dev_mode():
+ if _bypass_permitted():
return TenantPrincipal(
tenant_ids=frozenset({DEV_TENANT_ID}),
subject="dev",
diff --git a/services/ueba/tests/test_tenant_scope.py b/services/ueba/tests/test_tenant_scope.py
index adc488b19..3ba17f28d 100644
--- a/services/ueba/tests/test_tenant_scope.py
+++ b/services/ueba/tests/test_tenant_scope.py
@@ -269,6 +269,8 @@ async def test_dev_mode_only_applies_with_nothing_configured(monkeypatch: pytest
every tenant-scoped route.
"""
monkeypatch.setenv("AISOC_DEV_MODE", "1")
+ monkeypatch.setenv("AISOC_DEV_AUTH_BYPASS", "1")
+ monkeypatch.delenv("AISOC_PUBLISHED_BIND_ADDRS", raising=False)
monkeypatch.setenv("SECRET_KEY", SECRET)
monkeypatch.setenv("AISOC_SERVICE_TOKEN", SERVICE_TOKEN)
with pytest.raises(HTTPException) as exc:
@@ -277,10 +279,79 @@ async def test_dev_mode_only_applies_with_nothing_configured(monkeypatch: pytest
monkeypatch.setenv("SECRET_KEY", "")
monkeypatch.setenv("AISOC_SERVICE_TOKEN", "")
+ ts._BYPASS_LOGGED.clear()
principal = await ts.require_console_or_service_auth(authorization=None, x_aisoc_tenant_id=None)
assert principal.tenant_ids == frozenset({ts.DEV_TENANT_ID})
+async def test_dev_mode_alone_no_longer_admits_an_uncredentialed_caller(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ """The defect, stated as a test.
+
+ `AISOC_DEV_MODE` selects table autocreate, docs URLs, the metrics gate,
+ log formatting, the GraphiQL UI and an ephemeral vault key. It also
+ defaults to `1` on ten services in `docker-compose.yml`, while nothing
+ generates a service token. So a hand-copied `.env` and a plain
+ `docker compose up` admitted a caller with no credential at all, and the
+ only thing that had to be true for it was a flag nobody set for this
+ purpose.
+ """
+ monkeypatch.setenv("AISOC_DEV_MODE", "1")
+ monkeypatch.delenv("AISOC_DEV_AUTH_BYPASS", raising=False)
+ monkeypatch.delenv("AISOC_PUBLISHED_BIND_ADDRS", raising=False)
+ monkeypatch.setenv("SECRET_KEY", "")
+ monkeypatch.setenv("AISOC_SERVICE_TOKEN", "")
+ ts._BYPASS_LOGGED.clear()
+ with pytest.raises(HTTPException) as exc:
+ await ts.require_console_or_service_auth(authorization=None, x_aisoc_tenant_id=None)
+ assert exc.value.status_code == 503
+
+
+async def test_the_bypass_is_refused_on_a_reachable_published_address(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ """An anonymous principal is a convenience only on loopback.
+
+ The service binds `0.0.0.0` inside its container and cannot work out for
+ itself whether anyone else can reach it, so compose passes the published
+ addresses in. Both directions, because a check that only ever refuses is
+ indistinguishable from one that removed the shim.
+ """
+ monkeypatch.setenv("AISOC_DEV_MODE", "1")
+ monkeypatch.setenv("AISOC_DEV_AUTH_BYPASS", "1")
+ monkeypatch.setenv("SECRET_KEY", "")
+ monkeypatch.setenv("AISOC_SERVICE_TOKEN", "")
+
+ for published in ("0.0.0.0", "192.168.1.10", "127.0.0.1,0.0.0.0:8080", "aisoc.example.com"):
+ monkeypatch.setenv("AISOC_PUBLISHED_BIND_ADDRS", published)
+ ts._BYPASS_LOGGED.clear()
+ with pytest.raises(HTTPException) as exc:
+ await ts.require_console_or_service_auth(authorization=None, x_aisoc_tenant_id=None)
+ assert exc.value.status_code == 503, published
+
+ for published in ("127.0.0.1", "localhost:8080", "::1", "[::1]:9000"):
+ monkeypatch.setenv("AISOC_PUBLISHED_BIND_ADDRS", published)
+ ts._BYPASS_LOGGED.clear()
+ principal = await ts.require_console_or_service_auth(authorization=None, x_aisoc_tenant_id=None)
+ assert principal.tenant_ids == frozenset({ts.DEV_TENANT_ID}), published
+
+
+def test_the_dev_tenant_is_not_the_canonical_tenant() -> None:
+ """The crux, and it was byte-identical.
+
+ `DEV_TENANT_ID` was `…0001`, the tenant migration 001 seeds and that
+ `bootstrap_admin` puts the real administrator into. So an unauthenticated
+ caller admitted by the shim was scoped to the operator's own data rather
+ than to a demo tenant.
+ """
+ import uuid as _uuid
+
+ assert ts.DEV_TENANT_ID != _uuid.UUID("00000000-0000-0000-0000-000000000001")
+
+
@pytest.mark.asyncio
async def test_a_per_service_token_override_wins(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv(f"AISOC_{ts.SERVICE_NAME}_SERVICE_TOKEN", "service-specific")
diff --git a/tests/e2e/golden_pipeline/run_golden_pipeline.py b/tests/e2e/golden_pipeline/run_golden_pipeline.py
index 427054a9e..9065f2dd5 100755
--- a/tests/e2e/golden_pipeline/run_golden_pipeline.py
+++ b/tests/e2e/golden_pipeline/run_golden_pipeline.py
@@ -30,6 +30,8 @@ Environment:
python -m app.scripts.mint_ingest_token --quiet`
AISOC_INGEST_URL default http://localhost:8081
AISOC_API_URL default http://localhost:8000
+ AISOC_API_TOKEN required — the bearer the alert reads need. Mint
+ one with `make api-token`; `make smoke` does it.
AISOC_TENANT_ID default the seeded canonical tenant
AISOC_GOLDEN_TIMEOUT seconds to wait for the alert (default 90)
@@ -58,6 +60,13 @@ API_URL = os.environ.get("AISOC_API_URL", "http://localhost:8000").rstrip("/")
TENANT_ID = os.environ.get("AISOC_TENANT_ID", "00000000-0000-0000-0000-000000000001")
TIMEOUT_SECONDS = int(os.environ.get("AISOC_GOLDEN_TIMEOUT", "90"))
INGEST_TOKEN = os.environ.get("AISOC_INGEST_TOKEN", "").strip()
+# The API refuses an uncredentialed read. It used to serve one, because a
+# request with no bearer token resolved to a demo administrator in a
+# development-class environment and every documented path produced one —
+# so this harness was reading the API as an anonymous admin without
+# anybody having decided that.
+API_TOKEN = os.environ.get("AISOC_API_TOKEN", "").strip()
+API_AUTH = {"Authorization": f"Bearer {API_TOKEN}"} if API_TOKEN else {}
#: Unique per run so a re-run cannot pass by finding the previous run's alert
#: — the failure mode that makes an end-to-end test permanently green.
@@ -208,7 +217,7 @@ def main() -> int:
alert: dict | None = None
last_status = 0
while time.time() < deadline:
- last_status, body = _request("GET", f"{API_URL}/api/v1/alerts?limit=50")
+ last_status, body = _request("GET", f"{API_URL}/api/v1/alerts?limit=50", headers=API_AUTH)
if last_status == 200:
try:
items = json.loads(body).get("items", [])
@@ -222,22 +231,39 @@ def main() -> int:
break
time.sleep(3)
+ # A read that was refused is not a pipeline failure, and saying so saves
+ # an operator from reading Kafka logs about a working consumer. The first
+ # version of this spent 90 seconds on a 401 and then suggested three
+ # things to check, none of which was the credential.
+ refused = last_status in (401, 403)
if not r.record(
"event traversed the spine and became an alert",
alert is not None,
- "" if alert else (f"no alert carrying run id {RUN_ID} after {TIMEOUT_SECONDS}s (last API status {last_status})"),
+ ""
+ if alert
+ else (
+ f"the API refused the read (HTTP {last_status}). This harness needs a bearer "
+ "token: set AISOC_API_TOKEN, or run `make smoke`, which mints one."
+ if refused
+ else f"no alert carrying run id {RUN_ID} after {TIMEOUT_SECONDS}s (last API status {last_status})"
+ ),
):
- print("\n The event was accepted but never became an alert. Check, in order:")
- print(" docker compose logs fusion | tail -60 # consumer + promotion")
- print(" docker compose exec kafka kafka-topics --bootstrap-server localhost:29092 --list")
- print(" docker compose exec postgres psql -U aisoc -d aisoc -c 'select count(*) from alerts;'")
+ if refused:
+ print("\n The event reached ingest; the API would not let this harness read it.")
+ print(' AISOC_API_TOKEN="$(docker compose run --rm -T api \\')
+ print(' python -m app.scripts.mint_api_token --quiet)"')
+ else:
+ print("\n The event was accepted but never became an alert. Check, in order:")
+ print(" docker compose logs fusion | tail -60 # consumer + promotion")
+ print(" docker compose exec kafka kafka-topics --bootstrap-server localhost:29092 --list")
+ print(" docker compose exec postgres psql -U aisoc -d aisoc -c 'select count(*) from alerts;'")
return _finish(r)
assert alert is not None # for type checkers; guarded above
# ── 7. The alert is retrievable individually, not just in a list ─────
alert_id = alert.get("id")
- status, body = _request("GET", f"{API_URL}/api/v1/alerts/{alert_id}")
+ status, body = _request("GET", f"{API_URL}/api/v1/alerts/{alert_id}", headers=API_AUTH)
r.record("alert is retrievable by id from the API", status == 200, f"HTTP {status}" if status != 200 else str(alert_id))
# ── 8. Provenance survived the journey ───────────────────────────────