From b4db6dbf077135ce30c99106b9214cf643b65732 Mon Sep 17 00:00:00 2001 From: Jay Wedgeworth <12656028+jaywedgeworth22@users.noreply.github.com> Date: Wed, 25 Nov 2026 04:26:29 -0600 Subject: [PATCH 1/4] collectors: resolve the scoped grant before the environment (OPS-1120) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `resolveCollectorToken` read the environment first and returned immediately, so a value sitting in the environment beat the reviewed Infisical grant and nothing proved where it came from.  It now asks the consumer's scoped Infisical binding first, per name, and consults the environment only for a name the store did not answer. An environment value shaped like an Infisical service token (`st..`) is now refused outside `COLLECTOR_STRICT_INJECTED_SECRETS=1`, because this synchronous resolver cannot read that token's own metadata to prove whose narrow grant it is.  Strict-injected mode is a delivery channel (the injector already read the store), so it is exempt.  The documented static producer tokens (`_INGEST_TOKEN`, `USAGE_INGEST_TOKEN`) are app bearers rather than Infisical grants and keep resolving from the host bootstrap store; a production smoke test over the server collector env resolved every producer name unchanged. Also wires `scripts/__tests__/resolve-collector-token.test.mjs` into the collectors vitest config.  That suite existed but no runner included it (the main config excludes `scripts/__tests__/**` and the collectors config listed three other files), so the token precedence contract it pins had been gating nothing.  Its service-token fixture is built at runtime so no token-shaped literal enters the tree. --- AGENTS.md | 2 +- .../resolve-collector-token.test.mjs | 74 +++++++++++++++++-- scripts/lib/run-session-token-collector.mjs | 47 +++++++++--- scripts/vitest.collectors.config.mjs | 1 + 4 files changed, 106 insertions(+), 18 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index fd8b36aa..c3a875d2 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -516,7 +516,7 @@ and is synced to tmpfs at `/run/usage-monitor/usage-monitor.env` (mode 0600). T receipt importer, alongside the stable 32+ character `BILLING_RECEIPT_IDENTITY_KEY`. The identity key must not rotate with the signing key because it derives durable receipt IDs. Receipt credentials are manually provisioned and are not used by ordinary -telemetry. `USAGE_INGEST_PRODUCER_TOKENS` is an optional comma-separated list of `producerId:token` pairs that provides per-producer token scoping and isolated rate-limit buckets. When `USAGE_INGEST_REQUIRE_SCOPED_TOKENS=true` is set, unscoped `USAGE_INGEST_TOKEN` ingest is denied. A scoped token authorizes exactly one producer: its v2 `producerId` (the persisted `sourceApp`), OTLP metrics only for `claude-code`, and `/api/ingest/mac-heartbeat` only for `mac-host`. Mac collectors read their own `_INGEST_TOKEN` name first (`CODEX_`, `GROK_`, `COPILOT_`, `MINIMAX_`, `DEEPSEEK_`, `ANTIGRAVITY_`, `CLAUDE_CODE_`, `MAC_HEARTBEAT_`), then the unscoped fallback. The producer inventory and rotation runbook are in `docs/rollouts/2026-09-24-ingest-token-scoping.md`. `USAGE_READ_TOKEN` is a separate read-only token for +telemetry. `USAGE_INGEST_PRODUCER_TOKENS` is an optional comma-separated list of `producerId:token` pairs that provides per-producer token scoping and isolated rate-limit buckets. When `USAGE_INGEST_REQUIRE_SCOPED_TOKENS=true` is set, unscoped `USAGE_INGEST_TOKEN` ingest is denied. A scoped token authorizes exactly one producer: its v2 `producerId` (the persisted `sourceApp`), OTLP metrics only for `claude-code`, and `/api/ingest/mac-heartbeat` only for `mac-host`. Mac collectors read their own `_INGEST_TOKEN` name first (`CODEX_`, `GROK_`, `COPILOT_`, `MINIMAX_`, `DEEPSEEK_`, `ANTIGRAVITY_`, `CLAUDE_CODE_`, `MAC_HEARTBEAT_`), then the unscoped fallback.  **Provenance order (OPS-1120):** `resolveCollectorToken` asks this consumer's scoped Infisical binding before the environment, so a reviewed grant never loses to a value that merely sits in the environment, and an environment value shaped like an Infisical service token (`st..`) is refused outside `COLLECTOR_STRICT_INJECTED_SECRETS=1`, because that synchronous resolver cannot read the token's own metadata to prove which grant it is.  The documented static producer tokens are app bearers rather than Infisical grants and keep working from the host bootstrap store when the scoped store has no answer. The producer inventory and rotation runbook are in `docs/rollouts/2026-09-24-ingest-token-scoping.md`. `USAGE_READ_TOKEN` is a separate read-only token for `/api/budget-status` and `GET /api/subscriptions`. It is **required in production** (the deploy preflight hard-fails without it): the `USAGE_INGEST_TOKEN` fallback only applies outside production or when diff --git a/scripts/__tests__/resolve-collector-token.test.mjs b/scripts/__tests__/resolve-collector-token.test.mjs index 8b6b1ea0..ceea41c9 100644 --- a/scripts/__tests__/resolve-collector-token.test.mjs +++ b/scripts/__tests__/resolve-collector-token.test.mjs @@ -6,7 +6,19 @@ function fakeLookup(values) { return vi.fn((name) => values[name] ?? null); } -describe("resolveCollectorToken injected lookup compatibility", () => { +// A service-token-shaped fixture assembled at runtime: the resolver's shape +// check needs the form, and a literal here trips the repo's secret scanner. +const FAKE_SERVICE_TOKEN = ["st", "abc123", "secret456"].join("."); + +describe("resolveCollectorToken store-first precedence (OPS-1120)", () => { + it("prefers the scoped store for the same name over an environment value", () => { + const lookup = fakeLookup({ CODEX_INGEST_TOKEN: "scoped-lookup" }); + expect( + resolveCollectorToken(["CODEX_INGEST_TOKEN"], { env: { CODEX_INGEST_TOKEN: "env-value" }, fleetSecretsGet: lookup }), + ).toBe("scoped-lookup"); + expect(lookup).toHaveBeenCalledWith("CODEX_INGEST_TOKEN"); + }); + it("prefers a scoped name from the supplied lookup over an unscoped environment value", () => { const token = resolveCollectorToken(["CODEX_INGEST_TOKEN", "USAGE_INGEST_TOKEN"], { env: { USAGE_INGEST_TOKEN: "unscoped-env" }, @@ -15,11 +27,43 @@ describe("resolveCollectorToken injected lookup compatibility", () => { expect(token).toBe("scoped-lookup"); }); - it("prefers the environment for the same name without invoking the supplied lookup", () => { + it("falls back to the environment only for a name the store did not answer", () => { + const lookup = fakeLookup({}); + expect( + resolveCollectorToken(["CODEX_INGEST_TOKEN", "USAGE_INGEST_TOKEN"], { + env: { CODEX_INGEST_TOKEN: "env-codex", USAGE_INGEST_TOKEN: "env-unscoped" }, + fleetSecretsGet: lookup, + }), + ).toBe("env-codex"); + expect(lookup).toHaveBeenCalledWith("CODEX_INGEST_TOKEN"); + }); + + it("refuses a service-token-shaped environment value the store did not provide", () => { + expect(() => + resolveCollectorToken(["CODEX_INGEST_TOKEN"], { + env: { CODEX_INGEST_TOKEN: FAKE_SERVICE_TOKEN }, + fleetSecretsGet: fakeLookup({}), + }), + ).toThrow(/own Infisical binding/); + }); + + it("accepts a service-token-shaped value in strict injected-secrets mode", () => { + expect( + resolveCollectorToken(["CODEX_INGEST_TOKEN"], { + env: { CODEX_INGEST_TOKEN: FAKE_SERVICE_TOKEN, COLLECTOR_STRICT_INJECTED_SECRETS: "1" }, + fleetSecretsGet: fakeLookup({ CODEX_INGEST_TOKEN: "ignored" }), + }), + ).toBe(FAKE_SERVICE_TOKEN); + }); + + it("skips the store entirely in strict injected-secrets mode", () => { const lookup = fakeLookup({ CODEX_INGEST_TOKEN: "scoped-lookup" }); expect( - resolveCollectorToken(["CODEX_INGEST_TOKEN"], { env: { CODEX_INGEST_TOKEN: "scoped-env" }, fleetSecretsGet: lookup }), - ).toBe("scoped-env"); + resolveCollectorToken(["CODEX_INGEST_TOKEN"], { + env: { CODEX_INGEST_TOKEN: "delivered-value", COLLECTOR_STRICT_INJECTED_SECRETS: "1" }, + fleetSecretsGet: lookup, + }), + ).toBe("delivered-value"); expect(lookup).not.toHaveBeenCalled(); }); @@ -31,4 +75,24 @@ describe("resolveCollectorToken injected lookup compatibility", () => { expect(resolveCollectorToken(["CODEX_INGEST_TOKEN"], { env: {}, fleetSecretsGet: lookup })).toBeNull(); expect(resolveCollectorToken(["CODEX_INGEST_TOKEN"], { env: {}, fleetSecretsGet: () => null })).toBeNull(); }); -}); + + it("rethrows a deferred store failure only when no name yields a value", () => { + const boom = new Error("scoped read unavailable"); + expect(() => + resolveCollectorToken(["CODEX_INGEST_TOKEN"], { + env: {}, + fleetSecretsGet: () => { + throw boom; + }, + }), + ).toThrow(boom); + expect( + resolveCollectorToken(["CODEX_INGEST_TOKEN"], { + env: { CODEX_INGEST_TOKEN: "env-value" }, + fleetSecretsGet: () => { + throw boom; + }, + }), + ).toBe("env-value"); + }); +}); \ No newline at end of file diff --git a/scripts/lib/run-session-token-collector.mjs b/scripts/lib/run-session-token-collector.mjs index a2bb4716..dc9891db 100644 --- a/scripts/lib/run-session-token-collector.mjs +++ b/scripts/lib/run-session-token-collector.mjs @@ -90,29 +90,41 @@ function scopedInfisicalRead(name, env) { } /** - * Resolves an ingest token from the environment, then from Infisical through - * this consumer's scoped service-token binding. + * Resolves an ingest token through this consumer's scoped Infisical binding + * first, then from the environment (OPS-1120). * - * Names are tried strictly in order, each in the environment and then through - * the scoped lookup, so a producer-scoped name kept in Infisical still beats - * an unscoped USAGE_INGEST_TOKEN exported in the environment. Pass - * `fleetSecretsGet: () => null` for an environment-only lookup; the option - * key keeps its historical name for caller/test compatibility, but the + * Infisical is the source; the environment only transports values synced to + * the host, so a reviewed grant in the store must never lose to a value that + * merely sits in the environment. Names are still tried strictly in order, + * and for each name the scoped lookup answers before the environment is + * consulted. + * + * An environment value shaped like an Infisical service token + * (`st..`) is refused outside strict-injected mode: this + * synchronous resolver cannot read that token's own metadata to prove whose + * grant it is, so a hand-set service token never stands in for the reviewed + * grant. The documented static producer tokens (`_INGEST_TOKEN` / + * `USAGE_INGEST_TOKEN`, which are app bearers and not Infisical grants) keep + * working from the host bootstrap store when the scoped store has no answer. + * + * Pass `fleetSecretsGet: () => null` for an environment-only lookup; the + * option key keeps its historical name for caller/test compatibility, but the * default no longer touches the retired fleet-secrets CLI. */ +const SERVICE_TOKEN_VALUE_SHAPE = /^st\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+(?:\.|$)/; + export function resolveCollectorToken( tokenEnvVarNames = ["USAGE_INGEST_TOKEN"], { env = process.env, fleetSecretsGet: lookup } = {}, ) { const effectiveLookup = lookup ?? ((name) => scopedInfisicalRead(name, env)); + // Strict injected-secrets mode skips the runtime lookup entirely: the + // injector already read the store, so only the delivered values count. + const strictInjected = env.COLLECTOR_STRICT_INJECTED_SECRETS === "1"; let deferredFailure = null; for (const name of tokenEnvVarNames) { - const val = env[name]?.trim(); - if (val) return val; - // Strict injected-secrets mode skips the runtime lookup entirely: - // only values already injected into the environment count. let fromLookup = null; - if (env.COLLECTOR_STRICT_INJECTED_SECRETS !== "1") { + if (!strictInjected) { try { fromLookup = effectiveLookup(name)?.trim(); } catch (err) { @@ -126,6 +138,17 @@ export function resolveCollectorToken( } } if (fromLookup) return fromLookup; + const val = env[name]?.trim(); + if (!val) continue; + // Strict injected-secrets mode is a delivery channel: the injector read + // the store, so a delivered service-token value is already the reviewed + // grant and the shape check cannot apply there. + if (!strictInjected && SERVICE_TOKEN_VALUE_SHAPE.test(val)) { + throw new Error( + `${name} holds an Infisical service token in the environment; store it in this consumer's own Infisical binding instead (the environment cannot prove which grant it is)`, + ); + } + return val; } if (deferredFailure) throw deferredFailure; return null; diff --git a/scripts/vitest.collectors.config.mjs b/scripts/vitest.collectors.config.mjs index a40e4fb4..30e79067 100644 --- a/scripts/vitest.collectors.config.mjs +++ b/scripts/vitest.collectors.config.mjs @@ -4,6 +4,7 @@ const config = { include: [ "scripts/__tests__/kimi-zcode-telemetry.test.mjs", "scripts/__tests__/muse-code-usage-collector.test.mjs", + "scripts/__tests__/resolve-collector-token.test.mjs", "scripts/__tests__/transfer-muse-code-access-token-to-infisical.test.mjs", ], }, From 41b99157bfc324ba6fb4456f5d970777d6707fd0 Mon Sep 17 00:00:00 2001 From: Jay Wedgeworth <12656028+jaywedgeworth22@users.noreply.github.com> Date: Wed, 25 Nov 2026 04:35:47 -0600 Subject: [PATCH 2/4] collectors: honour store-first in the runtime fixture; defer a refused name MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Review findings on the store-first change.  Two fixture cases in `scripts/__tests__/run-session-token-collector.fixture.mjs` still asserted the old environment-first contract and failed under `test:collector-runtime`; they now assert the store value wins and that the environment is trimmed and returned only after the store answers empty. A refused service-token-shaped environment value is now deferred instead of thrown at once, so one poisoned candidate name cannot block a valid `USAGE_INGEST_TOKEN` under a later name; the refusal is raised only when no name yields a usable value. --- .../__tests__/resolve-collector-token.test.mjs | 9 +++++++++ .../run-session-token-collector.fixture.mjs | 16 +++++++++++++--- scripts/lib/run-session-token-collector.mjs | 6 +++++- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/scripts/__tests__/resolve-collector-token.test.mjs b/scripts/__tests__/resolve-collector-token.test.mjs index ceea41c9..1e077a8d 100644 --- a/scripts/__tests__/resolve-collector-token.test.mjs +++ b/scripts/__tests__/resolve-collector-token.test.mjs @@ -47,6 +47,15 @@ describe("resolveCollectorToken store-first precedence (OPS-1120)", () => { ).toThrow(/own Infisical binding/); }); + it("does not let a refused name block a valid later candidate", () => { + expect( + resolveCollectorToken(["CODEX_INGEST_TOKEN", "USAGE_INGEST_TOKEN"], { + env: { CODEX_INGEST_TOKEN: FAKE_SERVICE_TOKEN, USAGE_INGEST_TOKEN: "usage-env" }, + fleetSecretsGet: fakeLookup({}), + }), + ).toBe("usage-env"); + }); + it("accepts a service-token-shaped value in strict injected-secrets mode", () => { expect( resolveCollectorToken(["CODEX_INGEST_TOKEN"], { diff --git a/scripts/__tests__/run-session-token-collector.fixture.mjs b/scripts/__tests__/run-session-token-collector.fixture.mjs index 9a79f796..e1f2a986 100644 --- a/scripts/__tests__/run-session-token-collector.fixture.mjs +++ b/scripts/__tests__/run-session-token-collector.fixture.mjs @@ -77,15 +77,25 @@ function stubEnv(mode, extra = {}) { function resetRecord() { writeFileSync(recordFile, ""); } function record() { return existsSync(recordFile) ? readFileSync(recordFile, "utf8") : ""; } -// 1. env value wins; lookup must not run +// 1. store value wins (OPS-1120); env is consulted only after the store { let called = 0; const v = resolveCollectorToken(["USAGE_INGEST_TOKEN"], { env: { USAGE_INGEST_TOKEN: " env-token " }, fleetSecretsGet: () => { called++; return "lookup-token"; }, }); - check("env value wins and is trimmed", v === "env-token"); - check("lookup not called when env has the value", called === 0); + check("store value wins over env for the same name", v === "lookup-token"); + check("store is consulted before the env value", called === 1); +} +{ + // env is still trimmed and returned when the store has no answer + let called = 0; + const v = resolveCollectorToken(["USAGE_INGEST_TOKEN"], { + env: { USAGE_INGEST_TOKEN: " env-token " }, + fleetSecretsGet: () => { called++; return null; }, + }); + check("env fallback value is trimmed", v === "env-token"); + check("store consulted once before the env fallback", called === 1); } // 2. per-name order: name1 env-then-lookup before name2 diff --git a/scripts/lib/run-session-token-collector.mjs b/scripts/lib/run-session-token-collector.mjs index dc9891db..a1d88f83 100644 --- a/scripts/lib/run-session-token-collector.mjs +++ b/scripts/lib/run-session-token-collector.mjs @@ -144,9 +144,13 @@ export function resolveCollectorToken( // the store, so a delivered service-token value is already the reviewed // grant and the shape check cannot apply there. if (!strictInjected && SERVICE_TOKEN_VALUE_SHAPE.test(val)) { - throw new Error( + // A refused value is never used, but it must not poison a later + // candidate either: defer, so a valid USAGE_INGEST_TOKEN still resolves + // and the refusal is raised only when no name yields a usable value. + deferredFailure ??= new Error( `${name} holds an Infisical service token in the environment; store it in this consumer's own Infisical binding instead (the environment cannot prove which grant it is)`, ); + continue; } return val; } From 443aca23232190903195f8a31056d146beaa1ba5 Mon Sep 17 00:00:00 2001 From: Jay Wedgeworth <12656028+jaywedgeworth22@users.noreply.github.com> Date: Wed, 25 Nov 2026 04:36:50 -0600 Subject: [PATCH 3/4] collectors: trim the strict-injected flag like token values MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `COLLECTOR_STRICT_INJECTED_SECRETS` was compared with exact `=== "1"`, so a padded value silently dropped the process into the non-strict path: extra scoped reads and a spurious shape refusal.  It is trimmed like the token values now, with a test. --- scripts/__tests__/resolve-collector-token.test.mjs | 9 +++++++++ scripts/lib/run-session-token-collector.mjs | 4 +++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/scripts/__tests__/resolve-collector-token.test.mjs b/scripts/__tests__/resolve-collector-token.test.mjs index 1e077a8d..b5d69998 100644 --- a/scripts/__tests__/resolve-collector-token.test.mjs +++ b/scripts/__tests__/resolve-collector-token.test.mjs @@ -76,6 +76,15 @@ describe("resolveCollectorToken store-first precedence (OPS-1120)", () => { expect(lookup).not.toHaveBeenCalled(); }); + it("treats a whitespace-padded strict flag as strict", () => { + expect( + resolveCollectorToken(["CODEX_INGEST_TOKEN"], { + env: { CODEX_INGEST_TOKEN: "delivered-value", COLLECTOR_STRICT_INJECTED_SECRETS: " 1 " }, + fleetSecretsGet: fakeLookup({ CODEX_INGEST_TOKEN: "ignored" }), + }), + ).toBe("delivered-value"); + }); + it("falls back in order and returns null when nothing matches", () => { const lookup = fakeLookup({ USAGE_INGEST_TOKEN: "unscoped-lookup" }); expect(resolveCollectorToken(["CODEX_INGEST_TOKEN", "USAGE_INGEST_TOKEN"], { env: {}, fleetSecretsGet: lookup })).toBe( diff --git a/scripts/lib/run-session-token-collector.mjs b/scripts/lib/run-session-token-collector.mjs index a1d88f83..76bae7c1 100644 --- a/scripts/lib/run-session-token-collector.mjs +++ b/scripts/lib/run-session-token-collector.mjs @@ -120,7 +120,9 @@ export function resolveCollectorToken( const effectiveLookup = lookup ?? ((name) => scopedInfisicalRead(name, env)); // Strict injected-secrets mode skips the runtime lookup entirely: the // injector already read the store, so only the delivered values count. - const strictInjected = env.COLLECTOR_STRICT_INJECTED_SECRETS === "1"; + // Trimmed like the token values themselves: a padded flag must not silently + // drop the process into the non-strict path. + const strictInjected = String(env.COLLECTOR_STRICT_INJECTED_SECRETS ?? "").trim() === "1"; let deferredFailure = null; for (const name of tokenEnvVarNames) { let fromLookup = null; From 939f69ca11e3f7960f76fc414f09924e61223e4d Mon Sep 17 00:00:00 2001 From: Jay Wedgeworth <12656028+jaywedgeworth22@users.noreply.github.com> Date: Wed, 25 Nov 2026 08:27:38 -0600 Subject: [PATCH 4/4] collectors: keep every deferred token failure visible MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `deferredFailure ??=` kept only the first deferred error, so a second refused name or a scoped-read failure behind a shape refusal was silently dropped and diagnosing took one run per problem.  The resolver now keeps the two kinds apart: a failed scoped read is thrown in preference to a shape refusal because it is the more actionable error, and when no value resolves the refusal names every refused candidate at once. --- .../resolve-collector-token.test.mjs | 21 +++++++++++++++ scripts/lib/run-session-token-collector.mjs | 26 +++++++++++++------ 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/scripts/__tests__/resolve-collector-token.test.mjs b/scripts/__tests__/resolve-collector-token.test.mjs index b5d69998..7eb7c90e 100644 --- a/scripts/__tests__/resolve-collector-token.test.mjs +++ b/scripts/__tests__/resolve-collector-token.test.mjs @@ -76,6 +76,27 @@ describe("resolveCollectorToken store-first precedence (OPS-1120)", () => { expect(lookup).not.toHaveBeenCalled(); }); + it("names every refused candidate when no value resolves", () => { + expect(() => + resolveCollectorToken(["CODEX_INGEST_TOKEN", "OTHER_INGEST_TOKEN"], { + env: { CODEX_INGEST_TOKEN: FAKE_SERVICE_TOKEN, OTHER_INGEST_TOKEN: FAKE_SERVICE_TOKEN }, + fleetSecretsGet: fakeLookup({}), + }), + ).toThrow(/CODEX_INGEST_TOKEN, OTHER_INGEST_TOKEN/); + }); + + it("surfaces a scoped-read failure ahead of a shape refusal", () => { + const boom = new Error("scoped read unavailable"); + expect(() => + resolveCollectorToken(["CODEX_INGEST_TOKEN"], { + env: { CODEX_INGEST_TOKEN: FAKE_SERVICE_TOKEN }, + fleetSecretsGet: () => { + throw boom; + }, + }), + ).toThrow(boom); + }); + it("treats a whitespace-padded strict flag as strict", () => { expect( resolveCollectorToken(["CODEX_INGEST_TOKEN"], { diff --git a/scripts/lib/run-session-token-collector.mjs b/scripts/lib/run-session-token-collector.mjs index 76bae7c1..51cfa123 100644 --- a/scripts/lib/run-session-token-collector.mjs +++ b/scripts/lib/run-session-token-collector.mjs @@ -123,7 +123,11 @@ export function resolveCollectorToken( // Trimmed like the token values themselves: a padded flag must not silently // drop the process into the non-strict path. const strictInjected = String(env.COLLECTOR_STRICT_INJECTED_SECRETS ?? "").trim() === "1"; - let deferredFailure = null; + // Two separate slots, so one kind of deferred failure cannot hide the other: + // a scoped-read failure is an infrastructure problem and outranks a shape + // refusal, and every refused name is reported rather than only the first. + let deferredLookupFailure = null; + const refusedNames = []; for (const name of tokenEnvVarNames) { let fromLookup = null; if (!strictInjected) { @@ -136,7 +140,7 @@ export function resolveCollectorToken( // rethrown only when no name yields a value at all: a failure with no // usable token anywhere is not a silent miss (deliberate contract // change from the retired helper's null-on-failure). - deferredFailure ??= err; + deferredLookupFailure ??= err; } } if (fromLookup) return fromLookup; @@ -147,16 +151,22 @@ export function resolveCollectorToken( // grant and the shape check cannot apply there. if (!strictInjected && SERVICE_TOKEN_VALUE_SHAPE.test(val)) { // A refused value is never used, but it must not poison a later - // candidate either: defer, so a valid USAGE_INGEST_TOKEN still resolves - // and the refusal is raised only when no name yields a usable value. - deferredFailure ??= new Error( - `${name} holds an Infisical service token in the environment; store it in this consumer's own Infisical binding instead (the environment cannot prove which grant it is)`, - ); + // candidate either: collect, so a valid USAGE_INGEST_TOKEN still + // resolves and the refusal is raised only when no name yields a usable + // value. + refusedNames.push(name); continue; } return val; } - if (deferredFailure) throw deferredFailure; + // A failed scoped read is the more actionable error, so it wins; otherwise + // every refused name is named at once instead of one per run. + if (deferredLookupFailure) throw deferredLookupFailure; + if (refusedNames.length > 0) { + throw new Error( + `${refusedNames.join(", ")} hold${refusedNames.length === 1 ? "s" : ""} an Infisical service token in the environment; store it in this consumer's own Infisical binding instead (the environment cannot prove which grant it is)`, + ); + } return null; }