@tpsdev-ai/flair 0.62.0 → 0.63.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 (232) hide show
  1. package/README.md +6 -6
  2. package/SECURITY.md +18 -10
  3. package/config.yaml +11 -11
  4. package/dist/bridges/runtime/execute.js +1 -0
  5. package/dist/bridges/types.js +1 -0
  6. package/dist/build-info.json +3 -3
  7. package/dist/cli.js +66 -122
  8. package/dist/commands/agent.js +27 -65
  9. package/dist/commands/backup-approval.js +218 -0
  10. package/dist/commands/backup.js +37 -33
  11. package/dist/commands/bootstrap.js +3 -1
  12. package/dist/commands/deploy.js +37 -7
  13. package/dist/commands/doctor.js +119 -0
  14. package/dist/commands/export.js +3 -4
  15. package/dist/commands/federation.js +653 -68
  16. package/dist/commands/grant.js +5 -6
  17. package/dist/commands/help.js +25 -0
  18. package/dist/commands/idp.js +14 -19
  19. package/dist/commands/import.js +131 -19
  20. package/dist/commands/init.js +7 -7
  21. package/dist/commands/integrity.js +5 -6
  22. package/dist/commands/mcp.js +11 -17
  23. package/dist/commands/memory.js +33 -15
  24. package/dist/commands/orgevent.js +5 -10
  25. package/dist/commands/principal.js +13 -25
  26. package/dist/commands/quality.js +3 -0
  27. package/dist/commands/rem.js +484 -65
  28. package/dist/commands/restore.js +352 -44
  29. package/dist/commands/service.js +84 -2
  30. package/dist/commands/soul.js +0 -6
  31. package/dist/commands/status.js +51 -33
  32. package/dist/commands/test.js +4 -1
  33. package/dist/commands/upgrade.js +53 -22
  34. package/dist/commands/workspace.js +7 -8
  35. package/dist/deploy.js +59 -3
  36. package/dist/doctor-client.js +7 -2
  37. package/dist/fabric-upgrade.js +12 -2
  38. package/dist/federation/presence-send.js +139 -0
  39. package/dist/federation-verify.js +269 -177
  40. package/dist/install/clients.js +171 -35
  41. package/dist/lib/agent-id-rule.js +44 -2
  42. package/dist/lib/atomic-write.js +1 -1
  43. package/dist/lib/auth-resolve.js +1028 -52
  44. package/dist/lib/backup-approval-store.js +612 -0
  45. package/dist/lib/backup-artifact.js +343 -0
  46. package/dist/lib/config-critical-section.js +212 -25
  47. package/dist/lib/connect-phase.js +33 -0
  48. package/dist/lib/daemon-liveness.js +2 -1
  49. package/dist/lib/federation-pair-access.js +4 -0
  50. package/dist/lib/federation-retry-queue.js +283 -0
  51. package/dist/lib/federation-retry-row.js +101 -0
  52. package/dist/lib/instruction-event-contract.js +17 -0
  53. package/dist/lib/instruction-event-doctor.js +102 -0
  54. package/dist/lib/launchd-management.js +14 -3
  55. package/dist/lib/mcp-enable.js +14 -15
  56. package/dist/lib/memory-attributes.js +2 -0
  57. package/dist/lib/memory-provider-timeout.js +33 -0
  58. package/dist/lib/mqtt-config.js +132 -0
  59. package/dist/lib/mqtt-listener-doctor.js +104 -0
  60. package/dist/lib/open-live-row.js +20 -0
  61. package/dist/lib/ops-url.js +52 -0
  62. package/dist/lib/promotion-content-hash.js +20 -0
  63. package/dist/lib/restore-archive.js +760 -0
  64. package/dist/lib/restore-verify.js +1 -1
  65. package/dist/lib/skill-assignment-preflight.js +159 -0
  66. package/dist/lib/skill-seed.js +91 -8
  67. package/dist/lib/skill-tombstone-doctor.js +133 -0
  68. package/dist/lib/soul-scope-fields.js +26 -0
  69. package/dist/lib/uninstall-purge.js +266 -11
  70. package/dist/lib/upgrade-prior-liveness.js +2 -1
  71. package/dist/mcp-client-assertion.js +2 -10
  72. package/dist/memory-id-checkpoint.js +28 -7
  73. package/dist/mqtt-off-deploy.js +550 -0
  74. package/dist/rem/promote-policy.js +3 -3
  75. package/dist/rem/restore.js +302 -26
  76. package/dist/rem/runner.js +220 -58
  77. package/dist/rem/scheduler.js +21 -4
  78. package/dist/resources/A2AAdapter.js +131 -178
  79. package/dist/resources/AdminMemory.js +2 -0
  80. package/dist/resources/AdminMemoryCandidates.js +116 -0
  81. package/dist/resources/Agent.js +155 -45
  82. package/dist/resources/AgentCard.js +4 -0
  83. package/dist/resources/AgentKeyRegister.js +56 -22
  84. package/dist/resources/AgentReadPosition.js +7 -0
  85. package/dist/resources/AgentSeed.js +232 -107
  86. package/dist/resources/AutoPromoteCandidates.js +413 -84
  87. package/dist/resources/Credential.js +6 -8
  88. package/dist/resources/Federation.js +219 -57
  89. package/dist/resources/FederationRetry.js +4 -4
  90. package/dist/resources/InstructionVersion.js +1 -1
  91. package/dist/resources/MCPClientMetadata.js +6 -3
  92. package/dist/resources/Memory.js +1303 -270
  93. package/dist/resources/MemoryArchive.js +149 -64
  94. package/dist/resources/MemoryBootstrap.js +12 -22
  95. package/dist/resources/MemoryCandidate.js +16 -2
  96. package/dist/resources/MemoryConsolidate.js +2 -2
  97. package/dist/resources/MemoryFeed.js +360 -147
  98. package/dist/resources/MemoryMaintenance.js +290 -72
  99. package/dist/resources/MemoryPurge.js +138 -22
  100. package/dist/resources/MemoryReflect.js +68 -14
  101. package/dist/resources/MemoryReindex.js +91 -13
  102. package/dist/resources/Message.js +11 -2
  103. package/dist/resources/OAuth.js +156 -39
  104. package/dist/resources/OAuthClient.js +1 -1
  105. package/dist/resources/OrgEvent.js +292 -29
  106. package/dist/resources/OrgEventCatchup.js +7 -0
  107. package/dist/resources/OrgEventMaintenance.js +48 -10
  108. package/dist/resources/PairingToken.js +1 -1
  109. package/dist/resources/Presence.js +31 -11
  110. package/dist/resources/PromoteMemoryCandidate.js +8 -2
  111. package/dist/resources/RecordUsage.js +2 -4
  112. package/dist/resources/Soul.js +2 -0
  113. package/dist/resources/SoulFeed.js +2 -1
  114. package/dist/resources/WorkspaceLatest.js +4 -0
  115. package/dist/resources/XAA.js +36 -10
  116. package/dist/resources/a2a-stream.js +313 -0
  117. package/dist/resources/a2a-url.js +4 -5
  118. package/dist/resources/agent-admin.js +10 -1
  119. package/dist/resources/agent-auth.js +98 -68
  120. package/dist/resources/agent-read-position-lib.js +4 -1
  121. package/dist/resources/agent-status-guard.js +9 -7
  122. package/dist/resources/auth-middleware.js +166 -42
  123. package/dist/resources/authority-field-guard.js +9 -11
  124. package/dist/resources/auto-promote-lib.js +8 -5
  125. package/dist/resources/basic-auth-header.js +21 -0
  126. package/dist/resources/bm25-index-service.js +4 -5
  127. package/dist/resources/dedup-cluster.js +6 -1
  128. package/dist/resources/embeddings-provider.js +25 -0
  129. package/dist/resources/federation-classify.js +89 -0
  130. package/dist/resources/federation-retry-lib.js +6 -81
  131. package/dist/resources/health.js +43 -4
  132. package/dist/resources/host-pointer/registry.js +29 -0
  133. package/dist/resources/host-pointer-adapter.js +1 -0
  134. package/dist/resources/in-process.js +24 -22
  135. package/dist/resources/instruction-version-record.js +251 -7
  136. package/dist/resources/internal-authority.js +13 -0
  137. package/dist/resources/mcp-handler.js +106 -12
  138. package/dist/resources/mcp-oauth-flag.js +12 -14
  139. package/dist/resources/mcp-oauth.js +430 -53
  140. package/dist/resources/mcp-tools.js +60 -22
  141. package/dist/resources/memory-declared-attributes.js +1 -0
  142. package/dist/resources/memory-embed-hash.js +56 -0
  143. package/dist/resources/memory-host-source.js +5 -2
  144. package/dist/resources/memory-reflect-lib.js +1 -1
  145. package/dist/resources/memory-write-lock.js +202 -23
  146. package/dist/resources/memory-write-response.js +1 -1
  147. package/dist/resources/memory-write-transaction.js +243 -0
  148. package/dist/resources/migration-boot.js +17 -12
  149. package/dist/resources/migrations/boot-progress.js +25 -0
  150. package/dist/resources/migrations/data-dir.js +16 -3
  151. package/dist/resources/migrations/dir-safety.js +60 -2
  152. package/dist/resources/migrations/embedding-stamp.js +315 -49
  153. package/dist/resources/migrations/graph-heal.js +9 -2
  154. package/dist/resources/migrations/ledger.js +5 -2
  155. package/dist/resources/migrations/lock.js +867 -80
  156. package/dist/resources/migrations/progress.js +71 -10
  157. package/dist/resources/migrations/runner.js +212 -18
  158. package/dist/resources/migrations/state.js +36 -1
  159. package/dist/resources/migrations/status.js +2 -13
  160. package/dist/resources/migrations/synthetic-test-migration.js +78 -12
  161. package/dist/resources/migrations/types.js +26 -1
  162. package/dist/resources/migrations/visibility-backfill.js +111 -70
  163. package/dist/resources/oauth-discovery.js +0 -5
  164. package/dist/resources/object-body-guard.js +12 -5
  165. package/dist/resources/org-event-reserved.js +92 -0
  166. package/dist/resources/org-event-write-fault.js +19 -0
  167. package/dist/resources/owner-field-guard.js +1 -1
  168. package/dist/resources/owner-scoped-write-policy.js +65 -21
  169. package/dist/resources/owner-scoped-writes.js +37 -6
  170. package/dist/resources/path-id-rewrite.js +76 -0
  171. package/dist/resources/presence-internal.js +5 -5
  172. package/dist/resources/promotion-binding.js +43 -0
  173. package/dist/resources/promotion-stamp.js +124 -33
  174. package/dist/resources/protocol-context.js +189 -0
  175. package/dist/resources/record-type-kit.js +2 -4
  176. package/dist/resources/request-target.js +28 -1
  177. package/dist/resources/request-transaction.js +11 -1
  178. package/dist/resources/seed-reservation.js +1 -2
  179. package/dist/resources/semantic-retrieval-core.js +7 -6
  180. package/dist/resources/server-row-validation.js +71 -0
  181. package/dist/resources/skill-manifest.js +4 -3
  182. package/dist/resources/skill-version-write.js +384 -27
  183. package/dist/resources/skill-write-fault.js +6 -3
  184. package/dist/resources/skill-write-policy.js +2 -1
  185. package/dist/resources/soul-write-policy.js +26 -7
  186. package/dist/resources/table-helpers.js +1 -1
  187. package/dist/resources/table-subscription-policy.js +518 -85
  188. package/dist/resources/table-subscriptions.js +20 -7
  189. package/dist/resources/txn-pause-point.js +122 -5
  190. package/dist/resources/usage-recording.js +54 -46
  191. package/dist/resources/write-back.js +176 -11
  192. package/dist/resources/write-journal-context.js +5 -0
  193. package/dist/resources/write-journal.js +289 -0
  194. package/dist/src/lib/agent-id-rule.js +44 -2
  195. package/dist/src/lib/atomic-write.js +1 -1
  196. package/dist/src/lib/federation-retry-row.js +101 -0
  197. package/dist/src/lib/instruction-event-contract.js +17 -0
  198. package/dist/src/lib/memory-attributes.js +2 -0
  199. package/dist/src/lib/open-live-row.js +20 -0
  200. package/dist/src/lib/soul-scope-fields.js +26 -0
  201. package/dist/src/rem/promote-policy.js +3 -3
  202. package/dist/src/rem/scheduler.js +21 -4
  203. package/docs/access-control.md +18 -9
  204. package/docs/api-reference.md +126 -28
  205. package/docs/auth.md +40 -18
  206. package/docs/claude-code.md +3 -3
  207. package/docs/deploying-on-fabric.md +108 -0
  208. package/docs/deployment.md +17 -0
  209. package/docs/embedding-in-a-harper-app.md +16 -14
  210. package/docs/entity-vocabulary.md +3 -6
  211. package/docs/federation.md +29 -2
  212. package/docs/integrations.md +11 -11
  213. package/docs/mcp-clients.md +7 -5
  214. package/docs/notes/mcp-agent-auth-consumer.md +4 -9
  215. package/docs/notes/mcp-oauth-model2.md +6 -6
  216. package/docs/openclaw.md +1 -1
  217. package/docs/quickstart-fabric.md +2 -2
  218. package/docs/quickstart.md +3 -3
  219. package/docs/releasing.md +13 -13
  220. package/docs/rem.md +24 -3
  221. package/docs/secrets-and-keys.md +2 -2
  222. package/docs/standalone-local.md +29 -2
  223. package/docs/supply-chain-policy.md +28 -20
  224. package/docs/the-team.md +2 -2
  225. package/docs/upgrade.md +14 -0
  226. package/package.json +18 -11
  227. package/schemas/agent.graphql +3 -3
  228. package/schemas/federation.graphql +10 -7
  229. package/schemas/memory.graphql +7 -2
  230. package/templates/bin/flair-rem-nightly.sh.tmpl +7 -1
  231. package/templates/launchd/start-flair-with-admin-pass.sh +188 -10
  232. package/dist/resources/soul-adk-guard.js +0 -68
@@ -21,7 +21,11 @@ import { esc } from "./admin-layout.js";
21
21
  *
22
22
  * 1.0 constraints (per FLAIR-PRINCIPALS § 2):
23
23
  * - Only https://claude.com/api/mcp/auth_callback permitted as redirect URI
24
- * - PKCE required (S256 only)
24
+ * - PKCE required (S256 only): GET and POST /OAuthAuthorize refuse a request
25
+ * without an S256 code_challenge (checkConsentPkce), and the
26
+ * authorization-code exchange at /OAuthToken requires the stored challenge
27
+ * and a matching code_verifier (the refresh_token and jwt-bearer grants
28
+ * take other branches)
25
29
  * - Access tokens: max 1 hour
26
30
  * - Refresh token rotation on each use
27
31
  * - No "remember this decision" — each authorize is explicit
@@ -103,6 +107,93 @@ async function withTokenLifecycleLock(tokenHash, fn) {
103
107
  * the token endpoint would later refuse.
104
108
  */
105
109
  const SUPPORTED_SCOPES = new Set(buildAuthorizationServerMetadata().scopes_supported);
110
+ /** The scope a consent request names when it names none, or only whitespace. */
111
+ const DEFAULT_CONSENT_SCOPE = "memory:read";
112
+ /**
113
+ * The scope a consent request names: `memory:read` when the field is absent
114
+ * (`undefined`), else the string trimmed, `memory:read` when that is blank.
115
+ * Any other value — `null`, a number, an array, an object — gives `null`,
116
+ * which checkConsentScope refuses with invalid_scope. get() passes an absent
117
+ * query parameter as `undefined`.
118
+ */
119
+ function consentScope(raw) {
120
+ if (raw === undefined)
121
+ return DEFAULT_CONSENT_SCOPE;
122
+ if (typeof raw !== "string")
123
+ return null;
124
+ return raw.trim() || DEFAULT_CONSENT_SCOPE;
125
+ }
126
+ /**
127
+ * Every requested scope must be one the server advertises (RFC 6749
128
+ * §4.1.2.1: invalid_scope). The consent page (GET) and the consent form's POST
129
+ * both run this, before checkConsentClient.
130
+ */
131
+ function checkConsentScope(scope) {
132
+ if (scope !== null) {
133
+ const scopeTokens = scope.split(/\s+/).filter(Boolean);
134
+ if (scopeTokens.every((s) => SUPPORTED_SCOPES.has(s)))
135
+ return { ok: true, scope, scopeTokens };
136
+ }
137
+ return {
138
+ ok: false,
139
+ response: new Response(JSON.stringify({
140
+ error: "invalid_scope",
141
+ error_description: "one or more requested scopes are not supported by this server",
142
+ }), { status: 400, headers: { "content-type": "application/json" } }),
143
+ };
144
+ }
145
+ /**
146
+ * The OAuthClient record for `clientId` must exist (invalid_client). The
147
+ * consent page (GET) and the consent form's POST both run this, after
148
+ * checkConsentScope; the POST runs it before any authorization code is issued.
149
+ * post() passes the body's value as received; get() passes the query
150
+ * parameter, an absent one as the empty string. A value that is not a string
151
+ * (`undefined`, `null`, a number, an array, an object) is refused without a
152
+ * lookup; every string is looked up, the empty string included.
153
+ */
154
+ async function checkConsentClient(clientId) {
155
+ const client = typeof clientId === "string"
156
+ ? await databases.flair.OAuthClient.get(clientId)
157
+ : null;
158
+ if (!client) {
159
+ return {
160
+ ok: false,
161
+ response: new Response(JSON.stringify({ error: "invalid_client" }), {
162
+ status: 400, headers: { "content-type": "application/json" },
163
+ }),
164
+ };
165
+ }
166
+ return { ok: true, client };
167
+ }
168
+ /**
169
+ * An S256 `code_challenge` is BASE64URL(SHA256(verifier)) (RFC 7636 §4.2): exactly 43 characters of the
170
+ * base64url alphabet, without padding.
171
+ */
172
+ const S256_CODE_CHALLENGE = /^[A-Za-z0-9_-]{43}$/;
173
+ /**
174
+ * The handlers require PKCE, S256 only; the metadata lists S256 as the
175
+ * supported method (`code_challenge_methods_supported: ["S256"]`). `code_challenge` must be a
176
+ * string matching S256_CODE_CHALLENGE and `code_challenge_method` exactly
177
+ * "S256"; anything else (absent, empty, not a string, "plain") is refused with
178
+ * 400 invalid_request. The consent page (GET) and the consent form's POST both
179
+ * run this, after checkConsentClient: GET before rendering the form, POST
180
+ * before authentication and before any authorization code is issued or stored.
181
+ */
182
+ function checkConsentPkce(codeChallenge, codeChallengeMethod) {
183
+ const refuse = (description) => ({
184
+ ok: false,
185
+ response: new Response(JSON.stringify({ error: "invalid_request", error_description: description }), {
186
+ status: 400, headers: { "content-type": "application/json" },
187
+ }),
188
+ });
189
+ if (typeof codeChallenge !== "string" || !S256_CODE_CHALLENGE.test(codeChallenge)) {
190
+ return refuse("PKCE is required: code_challenge must be an S256 challenge of 43 base64url characters");
191
+ }
192
+ if (codeChallengeMethod !== "S256") {
193
+ return refuse("PKCE is required: code_challenge_method must be S256");
194
+ }
195
+ return { ok: true, codeChallenge };
196
+ }
106
197
  /** `state` is an opaque passthrough: printable ASCII, bounded (RFC 6749 §4.1.2). */
107
198
  const STATE_MAX_LENGTH = 512;
108
199
  function isOpaqueState(value) {
@@ -259,7 +350,7 @@ export class OAuthAuthorize extends Resource {
259
350
  const clientId = url.searchParams.get("client_id") ?? "";
260
351
  const redirectUri = url.searchParams.get("redirect_uri") ?? "";
261
352
  const responseType = url.searchParams.get("response_type") ?? "";
262
- const scope = (url.searchParams.get("scope") ?? "").trim() || "memory:read";
353
+ const requestedScope = consentScope(url.searchParams.get("scope") ?? undefined);
263
354
  const state = url.searchParams.get("state") ?? "";
264
355
  const codeChallenge = url.searchParams.get("code_challenge") ?? "";
265
356
  const codeChallengeMethod = url.searchParams.get("code_challenge_method") ?? "";
@@ -281,22 +372,20 @@ export class OAuthAuthorize extends Resource {
281
372
  error_description: `state must be printable ASCII of at most ${STATE_MAX_LENGTH} characters`,
282
373
  }), { status: 400, headers: { "content-type": "application/json" } });
283
374
  }
284
- // Every requested scope must be one the server advertises (RFC 6749
285
- // §4.1.2.1: invalid_scope).
286
- const scopeTokens = scope.split(/\s+/).filter(Boolean);
287
- if (scopeTokens.some((s) => !SUPPORTED_SCOPES.has(s))) {
288
- return new Response(JSON.stringify({
289
- error: "invalid_scope",
290
- error_description: "one or more requested scopes are not supported by this server",
291
- }), { status: 400, headers: { "content-type": "application/json" } });
292
- }
293
- // Verify client exists
294
- const client = await databases.flair.OAuthClient.get(clientId);
295
- if (!client) {
296
- return new Response(JSON.stringify({ error: "invalid_client" }), {
297
- status: 400, headers: { "content-type": "application/json" },
298
- });
299
- }
375
+ // Scope, then client, then PKCE — the same three checks, in the same order,
376
+ // that the consent form's POST runs before it issues a code. The form is
377
+ // rendered only for a request that passes all three.
378
+ const scopeCheck = checkConsentScope(requestedScope);
379
+ if (!scopeCheck.ok)
380
+ return scopeCheck.response;
381
+ const { scope, scopeTokens } = scopeCheck;
382
+ const clientCheck = await checkConsentClient(clientId);
383
+ if (!clientCheck.ok)
384
+ return clientCheck.response;
385
+ const { client } = clientCheck;
386
+ const pkceCheck = checkConsentPkce(codeChallenge, codeChallengeMethod);
387
+ if (!pkceCheck.ok)
388
+ return pkceCheck.response;
300
389
  // Server-rendered consent page (minimal HTML, no JS frameworks). EVERY
301
390
  // interpolated value passes through `esc` — correct for element text and
302
391
  // for the double-quoted attribute values below.
@@ -315,8 +404,8 @@ ${scopeTokens.map((s) => `<div class="scope">${esc(s)}</div>`).join("")}
315
404
  <input type="hidden" name="redirect_uri" value="${esc(redirectUri || ALLOWED_REDIRECT_URI)}">
316
405
  <input type="hidden" name="scope" value="${esc(scope)}">
317
406
  <input type="hidden" name="state" value="${esc(state)}">
318
- <input type="hidden" name="code_challenge" value="${esc(codeChallenge)}">
319
- <input type="hidden" name="code_challenge_method" value="${esc(codeChallengeMethod)}">
407
+ <input type="hidden" name="code_challenge" value="${esc(pkceCheck.codeChallenge)}">
408
+ <input type="hidden" name="code_challenge_method" value="S256">
320
409
  <button type="submit" name="action" value="approve" class="approve">Approve</button>
321
410
  <button type="submit" name="action" value="deny" class="deny">Deny</button>
322
411
  </form></body></html>`;
@@ -335,12 +424,15 @@ ${scopeTokens.map((s) => `<div class="scope">${esc(s)}</div>`).join("")}
335
424
  }
336
425
  async post(data) {
337
426
  const action = data?.action;
338
- const clientId = data?.client_id ?? "";
427
+ // As received: checkConsentClient refuses a non-string without a lookup.
428
+ const clientId = data?.client_id;
339
429
  const redirectUri = data?.redirect_uri || ALLOWED_REDIRECT_URI;
340
- const scope = data?.scope ?? "memory:read";
430
+ const requestedScope = consentScope(data?.scope);
341
431
  const state = data?.state ?? "";
342
- const codeChallenge = data?.code_challenge ?? "";
343
- const codeChallengeMethod = data?.code_challenge_method ?? "S256";
432
+ // As received: checkConsentPkce refuses an absent, empty, non-string or
433
+ // non-S256 value.
434
+ const codeChallenge = data?.code_challenge;
435
+ const codeChallengeMethod = data?.code_challenge_method;
344
436
  // Sherlock 2026-04-11: validate redirect_uri in POST handler too.
345
437
  // Without this, an attacker can CSRF the approval form with an arbitrary
346
438
  // redirect_uri, stealing the authorization code via open redirect.
@@ -360,6 +452,24 @@ ${scopeTokens.map((s) => `<div class="scope">${esc(s)}</div>`).join("")}
360
452
  const params = new URLSearchParams({ error: "access_denied", state });
361
453
  return redirectTo(`${redirectUri}?${params}`);
362
454
  }
455
+ // An approve must name what the consent page accepts: the scope check, then
456
+ // the OAuthClient record check, then the S256 PKCE check — the same checks,
457
+ // in the same order and with the same 400 bodies, that get() runs before
458
+ // rendering the form. All three run before authentication (get() runs them
459
+ // with none) and before any code is issued or stored. A deny is answered
460
+ // above, before these and after redirect-URI validation: it issues nothing,
461
+ // stores nothing, and redirects only to the pinned redirect_uri, so it needs
462
+ // no PKCE fields.
463
+ const scopeCheck = checkConsentScope(requestedScope);
464
+ if (!scopeCheck.ok)
465
+ return scopeCheck.response;
466
+ const { scope } = scopeCheck;
467
+ const clientCheck = await checkConsentClient(clientId);
468
+ if (!clientCheck.ok)
469
+ return clientCheck.response;
470
+ const pkceCheck = checkConsentPkce(codeChallenge, codeChallengeMethod);
471
+ if (!pkceCheck.ok)
472
+ return pkceCheck.response;
363
473
  // Determine authenticated principal. Harper v5 does not populate
364
474
  // this.request on Resource subclasses, so `(this as any).request?.tpsAgent`
365
475
  // was always undefined and this ALWAYS fell back to the hardcoded "admin" —
@@ -428,8 +538,8 @@ ${scopeTokens.map((s) => `<div class="scope">${esc(s)}</div>`).join("")}
428
538
  principalId,
429
539
  redirectUri,
430
540
  scope,
431
- codeChallenge,
432
- codeChallengeMethod,
541
+ codeChallenge: pkceCheck.codeChallenge,
542
+ codeChallengeMethod: "S256",
433
543
  expiresAt: futureISO(AUTH_CODE_TTL_MS),
434
544
  used: false,
435
545
  createdAt: now,
@@ -497,19 +607,26 @@ export class OAuthToken extends Resource {
497
607
  status: 400, headers: { "content-type": "application/json" },
498
608
  });
499
609
  }
500
- // PKCE verification
501
- if (authCode.codeChallenge) {
502
- if (!codeVerifier) {
503
- return new Response(JSON.stringify({ error: "invalid_grant", error_description: "code_verifier required" }), {
504
- status: 400, headers: { "content-type": "application/json" },
505
- });
506
- }
507
- const expectedChallenge = createHash("sha256").update(codeVerifier).digest("base64url");
508
- if (expectedChallenge !== authCode.codeChallenge) {
509
- return new Response(JSON.stringify({ error: "invalid_grant", error_description: "PKCE verification failed" }), {
510
- status: 400, headers: { "content-type": "application/json" },
511
- });
512
- }
610
+ // PKCE (S256) on every exchange: the stored code must carry an S256
611
+ // challenge, the request must carry a code_verifier, and the verifier's
612
+ // S256 hash must equal the stored challenge. Each refusal comes before the
613
+ // single-use claim and the `used` write below, so a refused exchange
614
+ // leaves the code unredeemed and the answer names the PKCE refusal.
615
+ if (typeof authCode.codeChallenge !== "string" || !authCode.codeChallenge || authCode.codeChallengeMethod !== "S256") {
616
+ return new Response(JSON.stringify({ error: "invalid_grant", error_description: "authorization code has no S256 code_challenge" }), {
617
+ status: 400, headers: { "content-type": "application/json" },
618
+ });
619
+ }
620
+ if (typeof codeVerifier !== "string" || !codeVerifier) {
621
+ return new Response(JSON.stringify({ error: "invalid_request", error_description: "code_verifier required" }), {
622
+ status: 400, headers: { "content-type": "application/json" },
623
+ });
624
+ }
625
+ const expectedChallenge = createHash("sha256").update(codeVerifier).digest("base64url");
626
+ if (expectedChallenge !== authCode.codeChallenge) {
627
+ return new Response(JSON.stringify({ error: "invalid_grant", error_description: "PKCE verification failed" }), {
628
+ status: 400, headers: { "content-type": "application/json" },
629
+ });
513
630
  }
514
631
  // Claim to redeem once per instance under a per-key lock before any token
515
632
  // is issued (resources/replay-store.ts).
@@ -3,7 +3,7 @@ import { allowAdmin } from "./agent-auth.js";
3
3
  /**
4
4
  * OAuthClient holds registered OAuth client records (incl. secrets) — admin/system
5
5
  * only, no agent grant. OAuth endpoints (OAuthRegister/Authorize/Token) read+write
6
- * clients via internal calls (allowAdmin permits internal), so the OAuth flows keep
6
+ * clients through the raw table rather than this resource, so the OAuth flows keep
7
7
  * working; direct anonymous table access is denied. Self-authorizes now that the
8
8
  * global gate is non-rejecting.
9
9
  */