@app-vitals/agent-admin 1.120.0 → 1.121.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. package/package.json +2 -2
  2. package/src/account-repo-lookup-api.integration.test.ts +1 -1
  3. package/src/admin-boot-config.ts +2 -2
  4. package/src/admin-boot-config.unit.test.ts +1 -1
  5. package/src/admin-boot.ts +1 -1
  6. package/src/admin-ui-account.ts +2 -2
  7. package/src/admin-ui-agent-template.smoke.test.ts +1 -1
  8. package/src/admin-ui-github-identity-pages.ts +2 -2
  9. package/src/admin-ui-push-test.smoke.test.ts +7 -5
  10. package/src/admin-ui-styles.unit.test.ts +3 -1
  11. package/src/admin-ui-templates-pages.ts +5 -5
  12. package/src/agent-cron-run-stats.integration.test.ts +11 -2
  13. package/src/agent-deletion-checklist.unit.test.ts +1 -1
  14. package/src/agent-manifest.unit.test.ts +3 -3
  15. package/src/agent-marketplaces-api.integration.test.ts +22 -1
  16. package/src/agent-members.integration.test.ts +1 -1
  17. package/src/agent-pod-labels.unit.test.ts +2 -2
  18. package/src/agent-provisioner.unit.test.ts +4 -4
  19. package/src/agent-setup.unit.test.ts +14 -3
  20. package/src/agent-template-items.ts +1 -1
  21. package/src/agent-templates.ts +1 -1
  22. package/src/agent-tools-token.integration.test.ts +3 -3
  23. package/src/api-auth.ts +11 -4
  24. package/src/brand-tokens.json +23 -4
  25. package/src/brand-tokens.unit.test.ts +11 -4
  26. package/src/chat-credential-config.unit.test.ts +6 -2
  27. package/src/chat-markers.unit.test.ts +5 -2
  28. package/src/cron-run-now.smoke.test.ts +1 -1
  29. package/src/cron-run-now.ts +1 -1
  30. package/src/errors.unit.test.ts +7 -4
  31. package/src/fixtures/github-agent-installation-cassette.json +35 -5
  32. package/src/github-access-backfill.ts +1 -1
  33. package/src/github-access-backfill.unit.test.ts +1 -1
  34. package/src/github-agent-installation-crawler.integration.test.ts +29 -12
  35. package/src/github-agent-installation-crawler.ts +2 -2
  36. package/src/github-agent-installation-crawler.unit.test.ts +28 -7
  37. package/src/github-app-api.ts +1 -1
  38. package/src/github-app-jwt.unit.test.ts +2 -1
  39. package/src/github-cache-roundtrip.integration.test.ts +4 -4
  40. package/src/github-enterprise-api.integration.test.ts +4 -4
  41. package/src/github-enterprise-api.ts +6 -6
  42. package/src/github-enterprise-client-page.unit.test.ts +18 -6
  43. package/src/github-enterprise-client.integration.test.ts +97 -32
  44. package/src/github-enterprise-client.ts +81 -22
  45. package/src/github-enterprise-client.unit.test.ts +1 -1
  46. package/src/github-enterprise-warnings.unit.test.ts +19 -6
  47. package/src/github-installation-selection.integration.test.ts +4 -1
  48. package/src/github-installation-wiring.unit.test.ts +1 -1
  49. package/src/github-repo-refresh-discovery.unit.test.ts +1 -1
  50. package/src/github-repo-refresher.unit.test.ts +11 -5
  51. package/src/github-sync-cache.integration.test.ts +1 -1
  52. package/src/github-sync-cached-access.ts +1 -1
  53. package/src/github-sync-reverify.unit.test.ts +1 -1
  54. package/src/github-sync-snapshot-refresh.integration.test.ts +1 -1
  55. package/src/github-sync.unit.test.ts +6 -6
  56. package/src/installation-ports-fixture.integration.test.ts +2 -2
  57. package/src/installation-repos-service.ts +5 -5
  58. package/src/kubernetes-client.integration.test.ts +1 -2
  59. package/src/platform-routes.ts +1 -1
  60. package/src/push-content.unit.test.ts +2 -2
  61. package/src/push-sender.unit.test.ts +1 -1
  62. package/src/push-service.integration.test.ts +1 -1
  63. package/src/pwa-icons.unit.test.ts +1 -1
  64. package/src/slack-config-token-resolve.unit.test.ts +25 -7
  65. package/src/slack-provisioning-client.integration.test.ts +10 -8
  66. package/src/slack-provisioning-client.ts +2 -6
  67. package/src/tools-token-policy.integration.test.ts +68 -6
  68. package/src/tools-token-policy.smoke.test.ts +22 -5
  69. package/src/tools-token-policy.ts +100 -8
  70. package/src/tools-token-policy.unit.test.ts +137 -4
  71. package/src/trial-expiry-sweeper.unit.test.ts +1 -1
  72. package/src/user-prefs.integration.test.ts +1 -1
  73. package/src/user-repo-access-resolver.unit.test.ts +1 -1
@@ -15,7 +15,7 @@ import {
15
15
  DEPLOYMENT_WIDE_ACCOUNT_KEY,
16
16
  } from "./github-agent-installation-crawler.ts";
17
17
  import { GitHubRepoCacheService } from "./github-repo-cache.ts";
18
- import { GithubSyncService, type GithubSyncDeps } from "./github-sync.ts";
18
+ import { type GithubSyncDeps, GithubSyncService } from "./github-sync.ts";
19
19
  import type { InstallationReposPort } from "./installation-repos-service.ts";
20
20
  import { createAdminPrismaClient } from "./prisma-client.ts";
21
21
 
@@ -1,24 +1,24 @@
1
1
  import { describe, expect, it, test } from "bun:test";
2
- import type {
3
- CachedAccessRow,
4
- InstallationSnapshotRow,
5
- } from "./installation-repos-service.ts";
6
2
  import type { GithubAccessPreviewFn } from "./github-access-preview.ts";
7
3
  import {
8
4
  ARCHIVED_REASON,
9
- GITHUB_SYNC_AUDIT_PLAN_HASH,
10
5
  type CachedLookupOrg,
6
+ GITHUB_SYNC_AUDIT_PLAN_HASH,
7
+ type GithubSyncPlanInput,
11
8
  GithubSyncService,
12
9
  NOT_ADDABLE_REASON,
13
10
  NOT_PRESENT_REASON,
14
11
  planGithubSync,
15
12
  STILL_PUSHABLE_REASON,
16
- type GithubSyncPlanInput,
17
13
  } from "./github-sync.ts";
18
14
  import type {
19
15
  InstallationAgentRecord,
20
16
  InstallationAuditEntry,
21
17
  } from "./installation-ports.ts";
18
+ import type {
19
+ CachedAccessRow,
20
+ InstallationSnapshotRow,
21
+ } from "./installation-repos-service.ts";
22
22
  import {
23
23
  type InstallationReposPort,
24
24
  NO_INSTALLATION_REASON,
@@ -5,13 +5,13 @@
5
5
  */
6
6
 
7
7
  import { describe, expect, it } from "bun:test";
8
- import { toInstallation } from "./github-enterprise-client.ts";
9
8
  import enterpriseOrgInstallations from "./fixtures/enterprise-org-installations.json";
10
- import { createInstallationReposPort } from "./installation-repos-service.ts";
9
+ import { toInstallation } from "./github-enterprise-client.ts";
11
10
  import {
12
11
  AppNotInstalledError,
13
12
  createInstallationPort,
14
13
  } from "./installation-ports.ts";
14
+ import { createInstallationReposPort } from "./installation-repos-service.ts";
15
15
 
16
16
  const ORG = "example-org";
17
17
  const CLIENT_ID = "Iv1.0000000000000000";
@@ -7,16 +7,16 @@
7
7
  * through the installation port.
8
8
  */
9
9
 
10
- import {
11
- GITHUB_APPS_STATE_REASONS,
12
- type GithubAppsState,
13
- type GithubAppsStateFn,
14
- } from "./github-apps-state.ts";
15
10
  import type { GithubAccessPreviewFn } from "./github-access-preview.ts";
16
11
  import {
17
12
  DEPLOYMENT_WIDE_ACCOUNT_KEY,
18
13
  getAccessRowsAcrossKeys,
19
14
  } from "./github-agent-installation-crawler.ts";
15
+ import {
16
+ GITHUB_APPS_STATE_REASONS,
17
+ type GithubAppsState,
18
+ type GithubAppsStateFn,
19
+ } from "./github-apps-state.ts";
20
20
  import {
21
21
  type AppIdentity,
22
22
  type AppInstallationPort,
@@ -9,8 +9,7 @@
9
9
  */
10
10
 
11
11
  import { afterEach, describe, expect, it } from "bun:test";
12
- import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
13
- import { readFileSync } from "node:fs";
12
+ import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
14
13
  import { tmpdir } from "node:os";
15
14
  import { join } from "node:path";
16
15
  import {
@@ -14,13 +14,13 @@
14
14
 
15
15
  import type { OpenAPIHono } from "@hono/zod-openapi";
16
16
  import type { Hono, MiddlewareHandler } from "hono";
17
+ import { createAccountRepoLookupApp } from "./account-repo-lookup-api.ts";
17
18
  import { createAccountsApp } from "./accounts-api.ts";
18
19
  import { createAgentTemplatesApp } from "./agent-templates-api.ts";
19
20
  import { type AdminDeps, createAdminApp } from "./agents-api.ts";
20
21
  import { createAgentRuntimeApp } from "./api.ts";
21
22
  import { createGithubEnterpriseApp } from "./github-enterprise-api.ts";
22
23
  import { mountPlatformDoc } from "./platform-doc-route.ts";
23
- import { createAccountRepoLookupApp } from "./account-repo-lookup-api.ts";
24
24
  import { createPrincipalsApp } from "./principals-api.ts";
25
25
 
26
26
  type AgentRuntimeDeps = Parameters<typeof createAgentRuntimeApp>[0];
@@ -9,11 +9,11 @@
9
9
 
10
10
  import { describe, expect, it } from "bun:test";
11
11
  import {
12
- type PushDetailLevel,
13
- type SessionNotificationKind,
14
12
  buildNotificationPayload,
15
13
  buildSessionNotificationPayload,
14
+ type PushDetailLevel,
16
15
  resolveDetailLevel,
16
+ type SessionNotificationKind,
17
17
  } from "./push-content.ts";
18
18
 
19
19
  describe("resolveDetailLevel", () => {
@@ -5,7 +5,7 @@
5
5
  */
6
6
 
7
7
  import { describe, expect, it } from "bun:test";
8
- import { PushSender, isPushEnabled } from "./push-sender.ts";
8
+ import { isPushEnabled, PushSender } from "./push-sender.ts";
9
9
 
10
10
  // A minimal valid-shaped subscription. Keys are the RFC 8291 vector's UA keys
11
11
  // so the encryption path exercises real values.
@@ -8,9 +8,9 @@
8
8
 
9
9
  import { afterEach, beforeEach, describe, expect, it } from "bun:test";
10
10
  import type { PrismaClient } from "../prisma/client/client.ts";
11
- import { resetMemberIdFixture } from "./test-support/member-id-test-support.ts";
12
11
  import { createAdminPrismaClient } from "./prisma-client.ts";
13
12
  import { PushService } from "./push-service.ts";
13
+ import { resetMemberIdFixture } from "./test-support/member-id-test-support.ts";
14
14
 
15
15
  const TEST_DB = process.env.DATABASE_URL_ADMIN_TEST;
16
16
  const describeOrSkip = TEST_DB ? describe : describe.skip;
@@ -7,7 +7,7 @@
7
7
 
8
8
  import { describe, expect, it } from "bun:test";
9
9
  import { join } from "node:path";
10
- import { PWA_ICONS, PWA_ASSETS_DIR } from "./pwa.ts";
10
+ import { PWA_ASSETS_DIR, PWA_ICONS } from "./pwa.ts";
11
11
 
12
12
  const PNG_MAGIC = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
13
13
  const MAX_ICON_BYTES = 60 * 1024;
@@ -131,17 +131,35 @@ describe("resolveSlackConfigTokenRequirement", () => {
131
131
  { accountId: null, slackTeamId: "T-default" },
132
132
  { accountId: "a1", slackTeamId: "T-acct" },
133
133
  ]);
134
- expect(await resolveSlackConfigTokenRequirement(resolver, "a1", "T-acct")).toBe("optional");
135
- expect(await resolveSlackConfigTokenRequirement(resolver, "a1", "T-default")).toBe("required");
134
+ expect(
135
+ await resolveSlackConfigTokenRequirement(resolver, "a1", "T-acct"),
136
+ ).toBe("optional");
137
+ expect(
138
+ await resolveSlackConfigTokenRequirement(resolver, "a1", "T-default"),
139
+ ).toBe("required");
136
140
  });
137
141
  test("falls back to the default row", async () => {
138
142
  const { resolver } = fake([{ accountId: null, slackTeamId: "T-default" }]);
139
- expect(await resolveSlackConfigTokenRequirement(resolver, "a1", "T-other")).toBe("required");
140
- expect(await resolveSlackConfigTokenRequirement(resolver, "a1", "T-default")).toBe("optional");
143
+ expect(
144
+ await resolveSlackConfigTokenRequirement(resolver, "a1", "T-other"),
145
+ ).toBe("required");
146
+ expect(
147
+ await resolveSlackConfigTokenRequirement(resolver, "a1", "T-default"),
148
+ ).toBe("optional");
141
149
  });
142
150
  test("no resolver, no row, or null team ids -> optional", async () => {
143
- expect(await resolveSlackConfigTokenRequirement(undefined, "a1", "T1")).toBe("optional");
144
- expect(await resolveSlackConfigTokenRequirement(fake([]).resolver, "a1", "T1")).toBe("optional");
145
- expect(await resolveSlackConfigTokenRequirement(fake([{ accountId: null }]).resolver, "a1", "T1")).toBe("optional");
151
+ expect(
152
+ await resolveSlackConfigTokenRequirement(undefined, "a1", "T1"),
153
+ ).toBe("optional");
154
+ expect(
155
+ await resolveSlackConfigTokenRequirement(fake([]).resolver, "a1", "T1"),
156
+ ).toBe("optional");
157
+ expect(
158
+ await resolveSlackConfigTokenRequirement(
159
+ fake([{ accountId: null }]).resolver,
160
+ "a1",
161
+ "T1",
162
+ ),
163
+ ).toBe("optional");
146
164
  });
147
165
  });
@@ -15,8 +15,8 @@
15
15
 
16
16
  import { describe, expect, it } from "bun:test";
17
17
  import { readFileSync } from "node:fs";
18
- import { HttpSlackProvisioningClient } from "./slack-provisioning-client.ts";
19
18
  import type { AppManifest } from "./slack-provisioning-client.ts";
19
+ import { HttpSlackProvisioningClient } from "./slack-provisioning-client.ts";
20
20
 
21
21
  // ─── Cassette ───────────────────────────────────────────────────────────────
22
22
 
@@ -193,16 +193,16 @@ describe("HttpSlackProvisioningClient — deleteApp", () => {
193
193
 
194
194
  it("throws on an HTTP error response", async () => {
195
195
  const { client } = makeClient("deleteApp_http_error");
196
- await expect(
197
- client.deleteApp("xoxp-token", "A0123456789"),
198
- ).rejects.toThrow(/Slack apps\.manifest\.delete HTTP error: 500/);
196
+ await expect(client.deleteApp("xoxp-token", "A0123456789")).rejects.toThrow(
197
+ /Slack apps\.manifest\.delete HTTP error: 500/,
198
+ );
199
199
  });
200
200
 
201
201
  it("throws when Slack returns ok: false", async () => {
202
202
  const { client } = makeClient("deleteApp_slack_error");
203
- await expect(
204
- client.deleteApp("xoxp-token", "A0123456789"),
205
- ).rejects.toThrow(/Slack apps\.manifest\.delete failed: app_not_found/);
203
+ await expect(client.deleteApp("xoxp-token", "A0123456789")).rejects.toThrow(
204
+ /Slack apps\.manifest\.delete failed: app_not_found/,
205
+ );
206
206
  });
207
207
  });
208
208
 
@@ -369,7 +369,9 @@ describe("HttpSlackProvisioningClient — rotateConfigToken", () => {
369
369
  } catch (err: unknown) {
370
370
  expect(err instanceof Error).toBe(true);
371
371
  const error = err as Error;
372
- expect(error.message).toMatch(/Slack tooling\.tokens\.rotate HTTP error: 500/);
372
+ expect(error.message).toMatch(
373
+ /Slack tooling\.tokens\.rotate HTTP error: 500/,
374
+ );
373
375
  if (err && typeof err === "object" && "code" in err) {
374
376
  expect((err as any).code).toBe("http_error");
375
377
  }
@@ -142,9 +142,7 @@ export interface SlackConfigTokenClient {
142
142
  * Throws SlackConfigTokenError on Slack API errors (invalid_refresh_token, token_revoked, etc.)
143
143
  * or on HTTP errors.
144
144
  */
145
- rotateConfigToken(
146
- refreshToken: string,
147
- ): Promise<{
145
+ rotateConfigToken(refreshToken: string): Promise<{
148
146
  accessToken: string;
149
147
  refreshToken: string;
150
148
  teamId: string;
@@ -460,9 +458,7 @@ export class HttpSlackProvisioningClient
460
458
  };
461
459
  }
462
460
 
463
- async rotateConfigToken(
464
- refreshToken: string,
465
- ): Promise<{
461
+ async rotateConfigToken(refreshToken: string): Promise<{
466
462
  accessToken: string;
467
463
  refreshToken: string;
468
464
  teamId: string;
@@ -37,6 +37,7 @@ describeOrSkip("tools-kind token policy (integration)", () => {
37
37
  let agentId: string;
38
38
  let toolsToken: string;
39
39
  let runtimeToken: string;
40
+ let seedCrypto: ReturnType<typeof makeTokenCrypto>;
40
41
  let root: {
41
42
  request: (path: string, init?: RequestInit) => Response | Promise<Response>;
42
43
  };
@@ -62,6 +63,7 @@ describeOrSkip("tools-kind token policy (integration)", () => {
62
63
  const saved = process.env.SHIPWRIGHT_ENCRYPTION_KEY;
63
64
  process.env.SHIPWRIGHT_ENCRYPTION_KEY = REAL_KEY;
64
65
  const crypto = makeTokenCrypto();
66
+ seedCrypto = crypto;
65
67
  if (saved === undefined) delete process.env.SHIPWRIGHT_ENCRYPTION_KEY;
66
68
  else process.env.SHIPWRIGHT_ENCRYPTION_KEY = saved;
67
69
 
@@ -189,10 +191,10 @@ describeOrSkip("tools-kind token policy (integration)", () => {
189
191
  });
190
192
 
191
193
  it("config gives a tools token env {} and a runtime token its env", async () => {
192
- const write = await call(runtimeToken, "POST", `/agents/${agentId}/envs`, {
194
+ // Runtime tokens can no longer write env; seed it directly.
195
+ await new AgentEnvService(prisma, seedCrypto).upsert(agentId, {
193
196
  SECRET_KEY: "s3cr3t",
194
197
  });
195
- expect([200, 201]).toContain(write.status);
196
198
 
197
199
  const asTools = await call(toolsToken, "GET", `/agents/${agentId}/config`);
198
200
  expect(asTools.status).toBe(200);
@@ -211,10 +213,70 @@ describeOrSkip("tools-kind token policy (integration)", () => {
211
213
  });
212
214
  });
213
215
 
214
- it("runtime token can still mint a token", async () => {
215
- const res = await call(runtimeToken, "POST", `/agents/${agentId}/tokens`, {
216
- label: "x",
216
+ describe("runtime-kind denylist", () => {
217
+ const saved = process.env.SHIPWRIGHT_RUNTIME_TOKEN_POLICY;
218
+ afterEach(() => {
219
+ if (saved === undefined)
220
+ delete process.env.SHIPWRIGHT_RUNTIME_TOKEN_POLICY;
221
+ else process.env.SHIPWRIGHT_RUNTIME_TOKEN_POLICY = saved;
222
+ });
223
+
224
+ it("runtime token is 403 on token mint and env write", async () => {
225
+ delete process.env.SHIPWRIGHT_RUNTIME_TOKEN_POLICY;
226
+ const mint = await call(
227
+ runtimeToken,
228
+ "POST",
229
+ `/agents/${agentId}/tokens`,
230
+ { label: "x" },
231
+ );
232
+ expect(mint.status).toBe(403);
233
+ const env = await call(runtimeToken, "POST", `/agents/${agentId}/envs`, {
234
+ A: "1",
235
+ });
236
+ expect(env.status).toBe(403);
237
+ });
238
+
239
+ it("runtime token is 200 on config, crons reconcile and cron run reports", async () => {
240
+ delete process.env.SHIPWRIGHT_RUNTIME_TOKEN_POLICY;
241
+ expect(
242
+ (await call(runtimeToken, "GET", `/agents/${agentId}/config`)).status,
243
+ ).toBe(200);
244
+ expect(
245
+ (await call(runtimeToken, "GET", `/agents/${agentId}/crons`)).status,
246
+ ).toBe(200);
247
+ const rec = await call(
248
+ runtimeToken,
249
+ "POST",
250
+ `/agents/${agentId}/crons/reconcile`,
251
+ { crons: [] },
252
+ );
253
+ expect(rec.status).not.toBe(403);
254
+ // A run report must pass the policy (the cron itself need not exist).
255
+ const run = await call(
256
+ runtimeToken,
257
+ "POST",
258
+ `/agents/${agentId}/crons/some-cron/runs`,
259
+ {},
260
+ );
261
+ expect(run.status).not.toBe(403);
262
+ const patch = await call(
263
+ runtimeToken,
264
+ "PATCH",
265
+ `/agents/${agentId}/crons/some-cron/runs/some-run`,
266
+ {},
267
+ );
268
+ expect(patch.status).not.toBe(403);
269
+ });
270
+
271
+ it("SHIPWRIGHT_RUNTIME_TOKEN_POLICY=off restores access", async () => {
272
+ process.env.SHIPWRIGHT_RUNTIME_TOKEN_POLICY = "off";
273
+ const mint = await call(
274
+ runtimeToken,
275
+ "POST",
276
+ `/agents/${agentId}/tokens`,
277
+ { label: "x" },
278
+ );
279
+ expect(mint.status).toBe(201);
217
280
  });
218
- expect(res.status).toBe(201);
219
281
  });
220
282
  });
@@ -154,11 +154,28 @@ describe("tools-kind token through the in-process admin app", () => {
154
154
  expect(((await res.json()) as { env: unknown }).env).toEqual({});
155
155
  });
156
156
 
157
- it("runtime-kind token still mints a token and receives env", async () => {
158
- const mint = await send(root, RUNTIME, "POST", `/agents/${ID}/tokens`, {
159
- label: "x",
160
- });
161
- expect(mint.status).toBe(201);
157
+ it("runtime-kind token is denied credential writes but receives env", async () => {
158
+ const saved = process.env.SHIPWRIGHT_RUNTIME_TOKEN_POLICY;
159
+ delete process.env.SHIPWRIGHT_RUNTIME_TOKEN_POLICY;
160
+ try {
161
+ const mint = await send(root, RUNTIME, "POST", `/agents/${ID}/tokens`, {
162
+ label: "x",
163
+ });
164
+ expect(mint.status).toBe(403);
165
+ expect(
166
+ (await send(root, RUNTIME, "POST", `/agents/${ID}/envs`, { env: {} }))
167
+ .status,
168
+ ).toBe(403);
169
+ process.env.SHIPWRIGHT_RUNTIME_TOKEN_POLICY = "off";
170
+ const off = await send(root, RUNTIME, "POST", `/agents/${ID}/tokens`, {
171
+ label: "x",
172
+ });
173
+ expect(off.status).toBe(201);
174
+ } finally {
175
+ if (saved === undefined)
176
+ delete process.env.SHIPWRIGHT_RUNTIME_TOKEN_POLICY;
177
+ else process.env.SHIPWRIGHT_RUNTIME_TOKEN_POLICY = saved;
178
+ }
162
179
  const cfg = await send(root, RUNTIME, "GET", `/agents/${ID}/config`);
163
180
  expect(((await cfg.json()) as { env: unknown }).env).toEqual({
164
181
  SECRET_KEY: "s3cr3t",
@@ -19,9 +19,11 @@
19
19
  * env is blanked (blankEnvForToolsToken) and a cron preCheck must be a
20
20
  * `plugin:script` reference (assertToolsTokenPreCheck).
21
21
  *
22
- * Runtime-kind tokens, admin keys, platform keys and session callers are never
23
- * touched. Routes with no declaration, including extension routes that do not
24
- * declare a policy, default to allowed.
22
+ * Runtime-kind tokens are restricted by a smaller denylist
23
+ * (RUNTIME_TOKEN_DENYLIST, see below) with an admin kill switch. Admin keys,
24
+ * platform keys and session callers are never touched. Routes with no
25
+ * declaration, including extension routes that do not declare a policy,
26
+ * default to allowed.
25
27
  *
26
28
  * Known limit: an extension route mounted outside /agents/* is not covered
27
29
  * (tools tokens are per-agent and every agent route lives under /agents).
@@ -393,6 +395,91 @@ export const TOOLS_TOKEN_POLICY: readonly ToolsTokenPolicyRule[] = [
393
395
  },
394
396
  ];
395
397
 
398
+ // ─── Runtime-kind denylist ────────────────────────────────────────────────────
399
+
400
+ const RD = (
401
+ method: string,
402
+ path: string,
403
+ note: string,
404
+ subpaths = false,
405
+ ): ToolsTokenPolicyRule => ({
406
+ method,
407
+ path,
408
+ decision: D,
409
+ note,
410
+ ...(subpaths ? { subpaths } : {}),
411
+ });
412
+
413
+ /**
414
+ * Routes a runtime-kind token may not call. Deny-only: every other route,
415
+ * including extension routes, stays allowed (the runtime uses config, crons
416
+ * list, crons/template reconcile, cron run POST/PATCH/GET, github-installations,
417
+ * baked-marketplaces, chat-tokens/daily and POST /work-queue). Exact matching
418
+ * (no `subpaths` on the cron rules) keeps `/crons/reconcile`,
419
+ * `/crons/{id}/runs` and `/crons/{id}/runs/{runId}` allowed.
420
+ */
421
+ export const RUNTIME_TOKEN_DENYLIST: readonly ToolsTokenPolicyRule[] = [
422
+ RD("POST", "/agents/{id}/tokens", "token minting", true),
423
+ RD("GET", "/agents/{id}/tokens", "token listing", true),
424
+ RD("DELETE", "/agents/{id}/tokens/{tokenId}", "token revocation", true),
425
+ RD("POST", "/agents/{id}/envs", "env write", true),
426
+ RD("PATCH", "/agents/{id}/envs", "env write", true),
427
+ RD("DELETE", "/agents/{id}/envs/{key}", "env write", true),
428
+ RD("POST", "/agents/{id}/tools", "allowed-tools write", true),
429
+ RD("PATCH", "/agents/{id}/tools/{toolId}", "allowed-tools write", true),
430
+ RD("DELETE", "/agents/{id}/tools/{toolId}", "allowed-tools write", true),
431
+ RD("POST", "/agents/{id}/plugins", "plugin write", true),
432
+ RD("PATCH", "/agents/{id}/plugins", "plugin write", true),
433
+ RD("DELETE", "/agents/{id}/plugins", "plugin write", true),
434
+ RD("POST", "/agents/{id}/marketplaces", "marketplace write", true),
435
+ RD(
436
+ "DELETE",
437
+ "/agents/{id}/marketplaces/{marketplaceId}",
438
+ "marketplace write",
439
+ true,
440
+ ),
441
+ RD("POST", "/agents/{id}/crons", "cron create"),
442
+ RD("PATCH", "/agents/{id}/crons/{cronId}", "cron update"),
443
+ RD("DELETE", "/agents/{id}/crons/{cronId}", "cron delete"),
444
+ ];
445
+
446
+ /** Env var: `enforce` (default) or `off` (runtime-kind tokens unrestricted). */
447
+ export const RUNTIME_TOKEN_POLICY_ENV = "SHIPWRIGHT_RUNTIME_TOKEN_POLICY";
448
+
449
+ /** Read on every call, so flipping the var needs no restart of anything else. */
450
+ export function isRuntimePolicyEnforced(
451
+ env: Record<string, string | undefined> = process.env,
452
+ ): boolean {
453
+ return env[RUNTIME_TOKEN_POLICY_ENV]?.trim().toLowerCase() !== "off";
454
+ }
455
+
456
+ /** Pure decision for a runtime-kind caller (ignores the kill switch). */
457
+ export function runtimeTokenDecision(
458
+ method: string,
459
+ path: string,
460
+ ): ToolsTokenDecision {
461
+ return RUNTIME_TOKEN_DENYLIST.some((r) => ruleMatches(r, method, path))
462
+ ? D
463
+ : A;
464
+ }
465
+
466
+ /**
467
+ * Enforcement helper: true (and one log line) when a runtime-kind token must
468
+ * be denied on method+path. The log line is read by the observation gate.
469
+ */
470
+ export function runtimeTokenDenied(
471
+ method: string,
472
+ path: string,
473
+ env: Record<string, string | undefined> = process.env,
474
+ ): boolean {
475
+ if (!isRuntimePolicyEnforced(env)) return false;
476
+ if (runtimeTokenDecision(method, path) !== D) return false;
477
+ console.warn(
478
+ `[token-policy] runtime token denied ${method.toUpperCase()} ${JSON.stringify(path)}`,
479
+ );
480
+ return true;
481
+ }
482
+
396
483
  // ─── Matching ─────────────────────────────────────────────────────────────────
397
484
 
398
485
  /** Decode, collapse repeated slashes and drop a trailing slash. */
@@ -485,8 +572,8 @@ export function blankEnvForToolsToken(
485
572
 
486
573
  /**
487
574
  * Guard for routes that authenticate on their own (extension API routes). It
488
- * resolves a bearer's kind itself and applies the policy to tools-kind tokens;
489
- * everything else (no bearer, env keys, runtime tokens, invalid tokens) passes
575
+ * resolves a bearer's kind itself and applies the tools policy to tools-kind tokens and the runtime denylist to runtime-kind tokens;
576
+ * everything else (no bearer, env keys, invalid tokens) passes
490
577
  * through for the route's own auth to decide.
491
578
  */
492
579
  export function createToolsTokenGuard(deps: {
@@ -503,9 +590,14 @@ export function createToolsTokenGuard(deps: {
503
590
  if (!deps.adminApiKeys?.has(raw)) {
504
591
  const result = await deps.agentTokenService.validate(raw);
505
592
  if (
506
- result?.kind === "tools" &&
507
- toolsTokenDecision(c.req.method, c.req.path, deps.extensionRules) ===
508
- D
593
+ (result?.kind === "tools" &&
594
+ toolsTokenDecision(
595
+ c.req.method,
596
+ c.req.path,
597
+ deps.extensionRules,
598
+ ) === D) ||
599
+ (result?.kind === "runtime" &&
600
+ runtimeTokenDenied(c.req.method, c.req.path))
509
601
  ) {
510
602
  return c.json({ error: "Forbidden" }, 403);
511
603
  }