diff --git a/src/common/known-secret-keys.ts b/src/common/known-secret-keys.ts index 48d6f4a..0bf26f3 100644 --- a/src/common/known-secret-keys.ts +++ b/src/common/known-secret-keys.ts @@ -9,6 +9,11 @@ export const KNOWN_SECRET_KEYS = [ "GARNET_PASSWORD", "MONGODB_CONNECTION_STRING", "MONGODB_PROJECT_ID", + "OPENROUTER_API_KEY", + "AGENT_LLM_API_KEY", + "OPENAI_API_KEY", + "EMBEDDING_API_KEY", + "PHOENIX_API_KEY", "SLACK_ALERT_WEBHOOK_URL", ] as const; diff --git a/src/common/nats-skill-acl.test.ts b/src/common/nats-skill-acl.test.ts new file mode 100644 index 0000000..4348b9a --- /dev/null +++ b/src/common/nats-skill-acl.test.ts @@ -0,0 +1,41 @@ +import { afterEach, describe, expect, it, vi } from "vitest"; + +import { runWithContext } from "../observability/context.js"; +import { NatsClient } from "./nats.js"; +import { AGENT_ALERT_SUBJECT, DISCORD_MEMBER_LOOKUP_SUBJECT } from "./nats-subjects.js"; +import { checkPublishAllowed } from "./nats-skill-acl.js"; + +describe("NATS skill ACL", () => { + afterEach(() => { + vi.unstubAllEnvs(); + }); + + it("allows declared publish subjects", () => { + expect(checkPublishAllowed("onboarding", DISCORD_MEMBER_LOOKUP_SUBJECT)).toBe( + true, + ); + expect(checkPublishAllowed("supervisor", AGENT_ALERT_SUBJECT)).toBe(true); + expect(checkPublishAllowed("onboarding", AGENT_ALERT_SUBJECT)).toBe(false); + }); + + it("enforces denied publish subjects when enabled", async () => { + vi.stubEnv("AGENT_NATS_ACL_MODE", "enforce"); + const client = new NatsClient(); + + await expect( + runWithContext( + { + chatroomId: "room-1", + requestId: "req-1", + skill: "onboarding", + startedAt: "2026-05-18T00:00:00.000Z", + trigger: "user-message", + userId: "user-1", + }, + () => client.publish(AGENT_ALERT_SUBJECT, { ok: true }), + ), + ).rejects.toThrow( + `NATS publish denied for skill onboarding on subject ${AGENT_ALERT_SUBJECT}`, + ); + }); +}); diff --git a/src/common/nats-skill-acl.ts b/src/common/nats-skill-acl.ts new file mode 100644 index 0000000..f78bced --- /dev/null +++ b/src/common/nats-skill-acl.ts @@ -0,0 +1,54 @@ +import { + AGENT_ALERT_SUBJECT, + DISCORD_MEMBER_LOOKUP_SUBJECT, +} from "./nats-subjects.js"; + +export interface SkillNatsAcl { + skill: string; + allowedPublishSubjects: string[]; + allowedSubscribeSubjects: string[]; +} + +export const DEFAULT_NATS_ACL: SkillNatsAcl[] = [ + { + skill: "onboarding", + allowedPublishSubjects: [DISCORD_MEMBER_LOOKUP_SUBJECT], + allowedSubscribeSubjects: [], + }, + { + skill: "moderation", + allowedPublishSubjects: [], + allowedSubscribeSubjects: [], + }, + { + skill: "supervisor", + allowedPublishSubjects: [AGENT_ALERT_SUBJECT], + allowedSubscribeSubjects: [], + }, +]; + +function wildcardToRegExp(pattern: string): RegExp { + const escaped = pattern + .split("*") + .map((part) => part.replace(/[.*+?^${}()|[\]\\]/gu, "\\$&")) + .join(".*"); + return new RegExp(`^${escaped}$`, "u"); +} + +function matchesAny(subject: string, patterns: readonly string[]): boolean { + return patterns.some((pattern) => wildcardToRegExp(pattern).test(subject)); +} + +function getAcl(skill: string): SkillNatsAcl | undefined { + return DEFAULT_NATS_ACL.find((entry) => entry.skill === skill); +} + +export function checkPublishAllowed(skill: string, subject: string): boolean { + const acl = getAcl(skill); + return Boolean(acl && matchesAny(subject, acl.allowedPublishSubjects)); +} + +export function checkSubscribeAllowed(skill: string, subject: string): boolean { + const acl = getAcl(skill); + return Boolean(acl && matchesAny(subject, acl.allowedSubscribeSubjects)); +} diff --git a/src/common/nats.ts b/src/common/nats.ts index f4c9e03..cc9c72c 100644 --- a/src/common/nats.ts +++ b/src/common/nats.ts @@ -6,7 +6,13 @@ import { type Subscription, } from "nats"; import { getSecret } from "@common/secret-manager"; +import { getNatsAclMode } from "../config.js"; +import { getContext } from "../observability/context.js"; import { getLogger } from "@obs/logger"; +import { + checkPublishAllowed, + checkSubscribeAllowed, +} from "./nats-skill-acl.js"; export interface NatsMessageContext { data: T; @@ -81,6 +87,7 @@ export class NatsClient { subject: string, handler: (msg: Msg) => void | Promise, ): Promise { + this.checkSubjectAcl("subscribe", subject); const subscription = this.getConnection().subscribe(subject); void (async () => { @@ -101,6 +108,7 @@ export class NatsClient { } async publish(subject: string, data: T): Promise { + this.checkSubjectAcl("publish", subject); const client = this.getConnection(); client.publish(subject, this.stringCodec.encode(JSON.stringify(data))); await client.flush(); @@ -111,6 +119,7 @@ export class NatsClient { data: TRequest, options?: { timeoutMs?: number | undefined }, ): Promise { + this.checkSubjectAcl("publish", subject); const requestOptions = options?.timeoutMs !== undefined ? { @@ -126,6 +135,36 @@ export class NatsClient { return this.decodeMessage(response); } + private checkSubjectAcl( + operation: "publish" | "subscribe", + subject: string, + ): void { + const mode = getNatsAclMode(); + if (mode === "off") { + return; + } + const skill = getContext()?.skill; + if (!skill) { + return; + } + const allowed = + operation === "publish" + ? checkPublishAllowed(skill, subject) + : checkSubscribeAllowed(skill, subject); + if (allowed) { + return; + } + + const payload = { mode, operation, skill, subject }; + if (mode === "enforce") { + this.logger.error(payload, "[NATS ACL] Subject denied"); + throw new Error( + `NATS ${operation} denied for skill ${skill} on subject ${subject}`, + ); + } + this.logger.warn(payload, "[NATS ACL] Subject would be denied"); + } + respond(msg: Msg, data: unknown): Promise { if (!msg.reply) { this.logger.warn( diff --git a/src/observability/logger.ts b/src/observability/logger.ts index 7014c31..b088bff 100644 --- a/src/observability/logger.ts +++ b/src/observability/logger.ts @@ -15,9 +15,21 @@ type RequestBindings = { function buildRedactionPaths(): string[] { const commonPaths = [ 'authorization', + 'apiKey', + 'api_key', 'headers.authorization', + 'headers.x-api-key', + 'headers["x-api-key"]', 'req.headers.authorization', + 'req.headers.x-api-key', + 'req.headers["x-api-key"]', 'request.headers.authorization', + 'request.headers.x-api-key', + 'request.headers["x-api-key"]', + 'request.apiKey', + 'request.api_key', + 'response.apiKey', + 'response.api_key', ]; const secretPaths = KNOWN_SECRET_KEYS.flatMap((key) => [