Multiplayer agent harness for work.
Show security flags and quarantines outside the newest 200 audit events (#2597)
Joshua Francef0896f988d45
diff --git a/src/admin/postgres-audit-log.ts b/src/admin/postgres-audit-log.tsindex 38fe745ff..a6b09e918 100644--- a/src/admin/postgres-audit-log.ts+++ b/src/admin/postgres-audit-log.ts@@ -99,7 +99,7 @@ export function createPostgresAuditLog(connectionString: string): AuditLog {}if (action !== undefined) {params.push(action);- conds.push(`action = $${params.length}`);+ conds.push(typeof action === "string" ? `action = $${params.length}` : `action = ANY($${params.length})`);}if (since !== undefined) {params.push(since);diff --git a/src/api/routes/admin/security.ts b/src/api/routes/admin/security.tsindex f175beb79..efc89ddcb 100644--- a/src/api/routes/admin/security.ts+++ b/src/api/routes/admin/security.ts@@ -1,8 +1,10 @@import { sendJson } from "../../http.ts";import { audit, authorizeAdmin, orgScope } from "../shared.ts";import type { ApiCtx } from "../route.ts";+import { SECURITY_FLAGGED, SECURITY_TOOL_RESULT_QUARANTINE } from "../../../audit/audit-log.ts";const MAX_FLAGS = 200;+const FLAG_ACTIONS = [SECURITY_FLAGGED, SECURITY_TOOL_RESULT_QUARANTINE];export async function listSecurityFlags(ctx: ApiCtx): Promise<void> {const scope = orgScope();@@ -10,17 +12,14 @@ export async function listSecurityFlags(ctx: ApiCtx): Promise<void> {if (!actor) return;const requested = Number(ctx.url.searchParams.get("limit") ?? 50);const limit = Number.isInteger(requested) && requested > 0 ? Math.min(requested, MAX_FLAGS) : 50;- const events = (await ctx.deps.auditLog?.tail({ limit: MAX_FLAGS })) ?? [];- const flags = events- .filter((event) => event.action === "security_posture.flagged" || event.action === "security_posture.quarantine")- .slice(0, limit)- .map((event) => ({- at: event.at,- principal: event.principalId,- scope: event.scopeLabel,- surface: event.resource,- detail: event.detail,- }));+ const events = (await ctx.deps.auditLog?.tail({ limit, action: FLAG_ACTIONS })) ?? [];+ const flags = events.map((event) => ({+ at: event.at,+ principal: event.principalId,+ scope: event.scopeLabel,+ surface: event.resource,+ detail: event.detail,+ }));audit(ctx.deps, {principalId: actor.id,action: "security_posture.flags.read",diff --git a/src/audit/audit-log.ts b/src/audit/audit-log.tsindex b48aa6e45..39d103d34 100644--- a/src/audit/audit-log.ts+++ b/src/audit/audit-log.ts@@ -18,12 +18,15 @@ export interface AuditLog {tail(opts: {limit: number;scopeLabel?: ScopeId;- action?: string;+ action?: string | readonly string[];since?: number;resourceContains?: string;}): Promise<readonly AuditEvent[]>;}+export const SECURITY_FLAGGED = "security_posture.flagged";+export const SECURITY_TOOL_RESULT_QUARANTINE = "security_posture.tool_result_quarantine";+const MAX = 50000;export function createAuditLog(): AuditLog {@@ -46,7 +49,7 @@ export function createAuditLog(): AuditLog {.list({ limit: MAX, ...(scopeLabel ? { scopeId: scopeLabel } : {}) }).filter((e) =>- (action === undefined || e.action === action) &&+ (action === undefined || [action].flat().includes(e.action)) &&(since === undefined || e.at >= since) &&(resourceContains === undefined || e.resource.includes(resourceContains)),)diff --git a/src/core/orchestrator.ts b/src/core/orchestrator.tsindex d463f6f9f..c80ab1321 100644--- a/src/core/orchestrator.ts+++ b/src/core/orchestrator.ts@@ -1,4 +1,5 @@import { DEFAULT_CRON_TIMEZONE } from "../cron/schedule.ts";+import { SECURITY_FLAGGED, SECURITY_TOOL_RESULT_QUARANTINE } from "../audit/audit-log.ts";import { createAdminCall } from "../tools/admin.ts";import { scopeTeamsToTurn } from "../teams/teams.ts";import { externalSlackRequestAllowed, currentExternalSlackRun } from "../resolution/external-slack.ts";@@ -899,7 +900,7 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {deps.auditLog.record({at: Date.now(),principalId: actor.id,- action: "security_posture.flagged",+ action: SECURITY_FLAGGED,resource: input.surface ?? "unknown",scopeLabel: scopeId,status: "pending_approval",@@ -3568,7 +3569,7 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {deps.auditLog.record({at: Date.now(),principalId: actor.id,- action: "security_posture.tool_result_quarantine",+ action: SECURITY_TOOL_RESULT_QUARANTINE,resource: input.surface ?? "unknown",scopeLabel: scopeId,status: "refused",diff --git a/test/admin-resources.test.ts b/test/admin-resources.test.tsindex 66303e790..a1770244c 100644--- a/test/admin-resources.test.ts+++ b/test/admin-resources.test.ts@@ -84,6 +84,25 @@ test("security flags are visible to an org admin", async () => {}});+test("security flags include quarantines and survive a busy audit log (#868)", async () => {+ const srv = start();+ try {+ const rec = (at: number, action: string) =>+ srv.built.auditLog.record({ at, principalId: "U1", action, resource: "slack", scopeLabel: "channel:C1" });+ rec(1, "security_posture.flagged");+ rec(2, "security_posture.tool_result_quarantine");+ for (let i = 0; i < 201; i++) rec(10 + i, "tool.call");+ const r = await fetch(`${srv.base}/v1/admin/security/flags`, { headers: ADMIN });+ const body = (await r.json()) as { flags: { at: number }[] };+ assert.deepEqual(+ body.flags.map((f) => f.at),+ [2, 1],+ );+ } finally {+ await srv.close();+ }+});+test("GET /v1/admin/resources returns a manifest entry for every registered resource", async () => {const srv = start();try {diff --git a/test/postgres-audit-log.test.ts b/test/postgres-audit-log.test.tsindex 7f722346e..b9a0b9794 100644--- a/test/postgres-audit-log.test.ts+++ b/test/postgres-audit-log.test.ts@@ -44,6 +44,13 @@ test("pg audit log: tail returns newest `limit`, scoped, and reads-its-writes","scope filter",);+ const byActions = await log.tail({ limit: 10, action: ["a1", "a4"] });+ assert.deepEqual(+ byActions.map((e) => e.action),+ ["a4", "a1"],+ "action list filter",+ );+const all = await log.events();assert.deepEqual(all.map((e) => e.action),