@sjawhar/claude-envoy 6.3.1 → 6.5.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.
package/dist/dispatch.js CHANGED
@@ -15546,7 +15546,7 @@ var dispatchToolSpecs = [
15546
15546
  {
15547
15547
  name: "dispatch_review",
15548
15548
  example: { issue: "DSP-1", version: 2, verdict: "approved" },
15549
- description: "Approve one version of an issue's document, or request changes on it, as the person your " + "token names: what the document header's Approve and Request changes record. A review " + "records an approval or a changes request in the person's name at that version, so it " + "works only with a token that names a person who holds approve; Dispatch refuses it to " + "any other token, with 403 OAUTH_SCOPE_REQUIRED or 403 SIGN_IN_REQUIRED. --version is the " + "version you read, sent in the review route's path, and it must be the version the review " + "would record: the document's latest settled version, or, while an approval request is " + "open on it, the version that request names for it. Any other is refused 409 " + "REVIEW_VERSION_MOVED, which names the version to read, and nothing is recorded. A " + "Dispatch build without the versioned review route answers no route and records nothing; " + "the command then says so and names /healthz, which names the commit of the build that " + "answers it when that build was stamped with one. With a request open the review answers " + "it, and records the same verdict on every document the " + "request names, each at the version the request names. A later version makes an approval " + "stale. A review is the one browser action this command takes for a person; every other " + "browser action is dispatch://INFRA-910's. " + ISSUE_REFERENCE,
15549
+ description: "Approve one version of an issue's document, or request changes on it, as the person your " + "token names: what the document header's Approve and Request changes record. A review " + "records an approval or a changes request in the person's name at that version, so it " + "works only with a token that names a person who holds approve; Dispatch refuses it to " + "any other token, with 403 OAUTH_SCOPE_REQUIRED or 403 SIGN_IN_REQUIRED. On a box where " + "the person signed in with agent-secrets login, the review goes through Cipher's proxy " + "instead, with the approve placeholder the session trades that sign-in for; a secrets " + "broker that serves no approve capability refuses that trade 400 invalid_request, with a " + "reason that says so, and nothing is sent to Dispatch. --version is the " + "version you read, sent in the review route's path, and it must be the version the review " + "would record: the document's latest settled version, or, while an approval request is " + "open on it, the version that request names for it. Any other is refused 409 " + "REVIEW_VERSION_MOVED, which names the version to read, and nothing is recorded. A " + "Dispatch build without the versioned review route answers no route and records nothing; " + "the command then says so and names /healthz, which names the commit of the build that " + "answers it when that build was stamped with one. With a request open the review answers " + "it, and records the same verdict on every document the " + "request names, each at the version the request names. A later version makes an approval " + "stale. A review is the one browser action this command takes for a person; every other " + "browser action is dispatch://INFRA-910's. " + ISSUE_REFERENCE,
15550
15550
  arguments: (z) => ({
15551
15551
  issue: z.string().describe(ISSUE_REFERENCE),
15552
15552
  artifact: z.string().describe("Document artifact id, slug, or filename; the issue's spec by default.").optional(),
@@ -23156,6 +23156,11 @@ var SUBJECT_TOKEN_TYPE_GITHUB = "urn:envoy:params:oauth:token-type:github-workfl
23156
23156
  var SUBJECT_TOKEN_TYPE_PERSON = "urn:envoy:params:oauth:token-type:person-google-identity";
23157
23157
  var ACTOR_TOKEN_TYPE_MACHINE = "urn:envoy:params:oauth:token-type:machine-google-identity";
23158
23158
  var TOKEN_TYPE_DPOP = "DPoP";
23159
+ var FORM_REQUESTED_TOKEN_TYPE = "requested_token_type";
23160
+ var TOKEN_TYPE_PROXY_PLACEHOLDER = "urn:envoy:params:oauth:token-type:proxy-placeholder";
23161
+ var TOKEN_TYPE_NOT_APPLICABLE = "N_A";
23162
+ var PLACEHOLDER_PREFIX = "agent-secrets-placeholder-";
23163
+ var PROXY_APPROVE_NAME = "DISPATCH_APPROVE_TOKEN";
23159
23164
  var SUBJECT_TYPE_ACCESS = "urn:ietf:params:oauth:token-type:access_token";
23160
23165
  var DEVICE_AUTHORIZATION_PATH = "/v1/oauth/device_authorization";
23161
23166
  var GRANT_TYPE_DEVICE_CODE = "urn:ietf:params:oauth:grant-type:device_code";
@@ -23277,6 +23282,8 @@ var KNOWN_BROKER_REFUSAL_PATTERNS = [
23277
23282
  `Refused: this pod's registration for session '${SESSION_SLOT}' was released; a pod trades only under a registration its registrar has not released\\.`,
23278
23283
  `The session name '${SESSION_SLOT}' belongs to another daemon: a pod trades only under a session name the daemon that registered it holds\\.`,
23279
23284
  `The session name '${SESSION_SLOT}' is bound to no daemon: a pod trades only under a session name the daemon that registered it holds\\.`,
23285
+ "Only a person's sign-in is traded for the approve capability's proxy placeholder, on a broker that serves it\\.",
23286
+ "A trade for the approve capability's proxy placeholder asks for the scope read, and a trade narrowed to reading approves nothing: trade for the placeholder without the scope, a cut or a DPoP-Holder proof\\.",
23280
23287
  `${PERSON_SLOT}'s credentials are held by Dispatch; they work again when \\k<person> signs in to Dispatch, or when Dispatch can confirm \\k<person> with Google again`,
23281
23288
  "The 'as_of' parameter names a cut, and only a trade that asks for the scope read may name one\\.",
23282
23289
  "The 'as_of' parameter is not an RFC 3339 time with at most six fractional digits\\.",
@@ -24321,7 +24328,10 @@ var WRITE_TRANSITIONS = {
24321
24328
  deadline: { "nothing-sent": nothing("end"), "may-have-landed": landed("end") },
24322
24329
  "cancelled-in-flight": { "nothing-sent": landed("end"), "may-have-landed": landed("end") },
24323
24330
  cancelled: { "nothing-sent": nothing("end"), "may-have-landed": landed("end") },
24324
- exhausted: { "nothing-sent": nothing("end"), "may-have-landed": landed("end") }
24331
+ exhausted: { "nothing-sent": nothing("end"), "may-have-landed": landed("end") },
24332
+ "proxy-refused": { "nothing-sent": nothing("end"), "may-have-landed": landed("end") },
24333
+ "proxy-failed": { "nothing-sent": nothing("retry"), "may-have-landed": landed("retry") },
24334
+ "proxy-unsettled": { "nothing-sent": landed("retry"), "may-have-landed": landed("retry") }
24325
24335
  };
24326
24336
  function writeStep(state, event, writes) {
24327
24337
  const step = WRITE_TRANSITIONS[event][state];
@@ -24371,6 +24381,32 @@ class DispatchGatewayError extends DispatchServiceError {
24371
24381
  this.mayHaveReachedDispatch = reachesDispatch(status);
24372
24382
  }
24373
24383
  }
24384
+ var PROXY_CODE_HEADER = "Cipher-Proxy-Code";
24385
+ var PROXY_FORWARDED_HEADER = "Cipher-Proxy-Forwarded";
24386
+
24387
+ class DispatchProxyRefusalError extends DispatchServiceError {
24388
+ fromDispatch = false;
24389
+ }
24390
+ var PROXY_CODE_FORM = /^[A-Z][A-Z0-9_]{0,63}$/;
24391
+ function proxyTunnelAnswer(error) {
24392
+ if (typeof error !== "object" || error === null)
24393
+ return;
24394
+ const { code, status, headers } = error;
24395
+ if (code !== "ERR_PROXY_TUNNEL" || typeof status !== "number" || !(headers instanceof Headers)) {
24396
+ return;
24397
+ }
24398
+ const proxyCode = headers.get(PROXY_CODE_HEADER) ?? "";
24399
+ if (status < 400 || status > 599 || !PROXY_CODE_FORM.test(proxyCode))
24400
+ return;
24401
+ const forwarded = headers.get(PROXY_FORWARDED_HEADER);
24402
+ return new Response(null, {
24403
+ status,
24404
+ headers: {
24405
+ [PROXY_CODE_HEADER]: proxyCode,
24406
+ ...forwarded === null ? {} : { [PROXY_FORWARDED_HEADER]: forwarded }
24407
+ }
24408
+ });
24409
+ }
24374
24410
  var CREDENTIAL_ECHOED = "CREDENTIAL_ECHOED";
24375
24411
 
24376
24412
  class DispatchCredentialEchoError extends DispatchServiceError {
@@ -24387,7 +24423,7 @@ class DispatchWriteUnsettledError extends Error {
24387
24423
  name = "DispatchWriteUnsettledError";
24388
24424
  constructor(last, attempts, key, unanswered) {
24389
24425
  let answer;
24390
- let why = last instanceof DispatchServiceError && last.status >= 500 ? "an attempt of the write may have reached Dispatch" : "an earlier attempt of the write may have reached Dispatch";
24426
+ let why = last instanceof DispatchServiceError && last.status >= 500 || last instanceof DispatchProxyRefusalError ? "an attempt of the write may have reached Dispatch" : "an earlier attempt of the write may have reached Dispatch";
24391
24427
  if (last instanceof DispatchGatewayError) {
24392
24428
  answer = last.answer;
24393
24429
  } else if (last instanceof DispatchServiceError && last.code === IDEMPOTENCY_KEY_IN_FLIGHT) {
@@ -24477,9 +24513,11 @@ class DispatchNoRouteError extends DispatchServiceError {
24477
24513
 
24478
24514
  class DispatchUnreachableError extends Error {
24479
24515
  reachedNothing;
24480
- constructor(message, reachedNothing) {
24516
+ failure;
24517
+ constructor(message, reachedNothing, failure) {
24481
24518
  super(message);
24482
24519
  this.reachedNothing = reachedNothing;
24520
+ this.failure = failure;
24483
24521
  }
24484
24522
  }
24485
24523
  function reviewNoRouteRefusal(baseUrl, route, version) {
@@ -24558,6 +24596,10 @@ class WriteAttemptSilent extends Error {
24558
24596
  super(`made no progress for ${silenceMs / 1000} s, so it was cut off`);
24559
24597
  }
24560
24598
  }
24599
+ function originOf(url) {
24600
+ const parsed = new URL(url);
24601
+ return `${parsed.protocol}//${parsed.host}`;
24602
+ }
24561
24603
  function parsedJson(response, text) {
24562
24604
  if (text === "" || !isJson(response))
24563
24605
  return;
@@ -24616,8 +24658,13 @@ async function writeAnswer(response) {
24616
24658
  function isStoredKeyStatus(status) {
24617
24659
  return status >= 400 && status < 500 && status !== 401 && status !== 408 && status !== 429;
24618
24660
  }
24619
- function answerEvent(answer, requestKey, keyEchoed, canTellDeadToken) {
24661
+ function answerEvent(answer, requestKey, keyEchoed, canTellDeadToken, proxied) {
24620
24662
  const { status } = answer.response;
24663
+ if (proxied && answer.response.headers.get(PROXY_CODE_HEADER) !== null) {
24664
+ if (answer.response.headers.get(PROXY_FORWARDED_HEADER) !== "false")
24665
+ return "proxy-unsettled";
24666
+ return status >= 500 ? "proxy-failed" : "proxy-refused";
24667
+ }
24621
24668
  if (status >= 200 && status < 300) {
24622
24669
  if (answer.notJson)
24623
24670
  return "foreign-2xx";
@@ -24738,7 +24785,7 @@ class DispatchClient {
24738
24785
  return await fetchImpl(input, { ...init, redirect: "manual" });
24739
24786
  } catch (error) {
24740
24787
  if (error instanceof TypeError) {
24741
- throw new DispatchUnreachableError(`Dispatch at ${shown} is unreachable: ${errorClass(error)}. If the Dispatch URL changed, restart this agent process so it picks up the new configuration.`, reachedNothing(error));
24788
+ throw new DispatchUnreachableError(`Dispatch at ${shown} is unreachable: ${errorClass(error)}. If the Dispatch URL changed, restart this agent process so it picks up the new configuration.`, reachedNothing(error), errorClass(error));
24742
24789
  }
24743
24790
  throw error;
24744
24791
  }
@@ -25005,11 +25052,31 @@ class DispatchClient {
25005
25052
  const path = ["api", "v1", "artifacts", artifactID, "versions", String(version), "reviews"];
25006
25053
  const body = { state: input.state, reason: input.reason };
25007
25054
  try {
25055
+ const route = await this.#credential.approveRoute?.(this.#credentialSignal(this.#signal));
25056
+ if (route !== undefined) {
25057
+ const via = this.#throughProxy(route, this.#url(path));
25058
+ return (await this.#jsonAnswer("POST", path, body, { via })).payload;
25059
+ }
25008
25060
  return await this.#json("POST", path, body);
25009
25061
  } catch (error) {
25010
25062
  throw this.#refusalOfNoRoute(error, path, (route) => reviewNoRouteRefusal(this.#baseUrl, route, version));
25011
25063
  }
25012
25064
  }
25065
+ #throughProxy(route, url) {
25066
+ const target = new URL(url);
25067
+ if (target.protocol !== "https:" || target.port !== "" || !route.hosts.includes(target.hostname)) {
25068
+ throw new LoggableError(`a person's review goes through Cipher's proxy, which reaches https://${route.hosts.join(" and https://")} alone, and the Dispatch URL is ${urlForText(this.#baseUrl, DISPATCH_URL_SETTING)}, so no review was sent`);
25069
+ }
25070
+ const proxy = new URL(route.proxyUrl);
25071
+ proxy.username = "agent";
25072
+ proxy.password = route.placeholder;
25073
+ const credential = {
25074
+ scheme: "DPoP",
25075
+ headers: () => ({ Authorization: `DPoP ${route.placeholder}` }),
25076
+ secret: () => route.placeholder
25077
+ };
25078
+ return { credential, init: { proxy: proxy.toString(), tls: { ca: route.caCertificate } } };
25079
+ }
25013
25080
  async recordResult(issue, input) {
25014
25081
  return this.#json("POST", ["api", "v1", "issues", await this.#resolveIssue(issue), "results"], input);
25015
25082
  }
@@ -25241,12 +25308,12 @@ class DispatchClient {
25241
25308
  return (await this.#jsonAnswer(method, path, body, { query, signal, key })).payload;
25242
25309
  }
25243
25310
  async#jsonAnswer(method, path, body, options = {}) {
25244
- const { query, signal, key, writes = true } = options;
25311
+ const { query, signal, key, writes = true, via } = options;
25245
25312
  const url = this.#url(path, query);
25246
25313
  const encoded = body === undefined ? undefined : { content: JSON.stringify(body), type: "application/json" };
25247
25314
  const write = this.#writeCall(method, url, key, writes);
25248
25315
  if (write !== undefined) {
25249
- const answer = await this.#write(method, url, encoded, "application/json", signal, write);
25316
+ const answer = await this.#write(method, url, encoded, "application/json", signal, write, via);
25250
25317
  return { status: answer.response.status, payload: this.#payloadOf(method, url, answer) };
25251
25318
  }
25252
25319
  return this.#noRoute(async () => {
@@ -25387,9 +25454,10 @@ class DispatchClient {
25387
25454
  });
25388
25455
  return { answer, presented };
25389
25456
  }
25390
- async#write(method, url, body, accept, signal, write) {
25457
+ async#write(method, url, body, accept, signal, write, via) {
25391
25458
  const limit = signal === undefined ? this.#signal : AbortSignal.any([this.#signal, signal]);
25392
25459
  const { key, progress } = write;
25460
+ const credential = via?.credential ?? this.#credential;
25393
25461
  const headers = { Accept: accept, [IDEMPOTENCY_KEY_HEADER]: key };
25394
25462
  if (body !== undefined)
25395
25463
  headers["Content-Type"] = body.type;
@@ -25409,7 +25477,7 @@ class DispatchClient {
25409
25477
  let presented;
25410
25478
  try {
25411
25479
  limit.throwIfAborted();
25412
- presented = await this.#credential.headers(method, url, undefined, this.#credentialSignal(limit));
25480
+ presented = await credential.headers(method, url, undefined, this.#credentialSignal(limit));
25413
25481
  this.#sent.note(presented);
25414
25482
  limit.throwIfAborted();
25415
25483
  } catch (error) {
@@ -25420,7 +25488,7 @@ class DispatchClient {
25420
25488
  ...error === limit.reason ? {} : { credentialFailure: error }
25421
25489
  };
25422
25490
  }
25423
- const identity = this.#credential.identity?.(presented);
25491
+ const identity = credential.identity?.(presented);
25424
25492
  if (state === "may-have-landed" && identity !== login) {
25425
25493
  return {
25426
25494
  event: "no-credential-final",
@@ -25432,7 +25500,7 @@ class DispatchClient {
25432
25500
  progress.sent += 1;
25433
25501
  let response;
25434
25502
  try {
25435
- response = await this.#attempt(url, method, { ...headers, ...presented }, body, silenceMs, limit);
25503
+ response = await this.#attempt(url, method, { ...headers, ...presented }, body, silenceMs, limit, via?.init);
25436
25504
  } catch (error) {
25437
25505
  return {
25438
25506
  event: stoppedEvent(limit, true, error) ?? (error instanceof WriteAttemptSilent ? "silence" : error instanceof DispatchUnreachableError && error.reachedNothing ? "unreached" : "reset"),
@@ -25458,7 +25526,7 @@ class DispatchClient {
25458
25526
  };
25459
25527
  }
25460
25528
  return {
25461
- event: answerEvent(answer, write.key, progress.keyEchoed, this.#credential.stale !== undefined && !deadTokenTold),
25529
+ event: answerEvent(answer, write.key, progress.keyEchoed, credential.stale !== undefined && !deadTokenTold, via !== undefined),
25462
25530
  answer,
25463
25531
  presented,
25464
25532
  sent,
@@ -25493,10 +25561,10 @@ class DispatchClient {
25493
25561
  }
25494
25562
  return last.answer;
25495
25563
  case "end":
25496
- throw await this.#endWrite(method, url, state, event === "exhausted" ? last.event : event, last.answer, state === "nothing-sent" && credentialFailure !== undefined && (event === "deadline" || event === "cancelled") ? credentialFailure : last.failure, write, event === "exhausted" && last.event === "no-route" && this.#retryNoRoute);
25564
+ throw await this.#endWrite(method, url, state, event === "exhausted" ? last.event : event, last.answer, state === "nothing-sent" && credentialFailure !== undefined && (event === "deadline" || event === "cancelled") ? credentialFailure : last.failure, write, event === "exhausted" && last.event === "no-route" && this.#retryNoRoute, via);
25497
25565
  case "retry-at-once":
25498
25566
  deadTokenTold = true;
25499
- this.#credential.stale?.(undefined, last.sent?.with);
25567
+ credential.stale?.(undefined, last.sent?.with);
25500
25568
  last = await next();
25501
25569
  event = last.event;
25502
25570
  continue;
@@ -25553,7 +25621,7 @@ class DispatchClient {
25553
25621
  #now() {
25554
25622
  return this.writeRetry.now?.() ?? performance.now();
25555
25623
  }
25556
- async#attempt(url, method, headers, body, silenceMs, limit) {
25624
+ async#attempt(url, method, headers, body, silenceMs, limit, transport) {
25557
25625
  const cut = new AbortController;
25558
25626
  const silent = () => cut.abort(new WriteAttemptSilent(silenceMs));
25559
25627
  let timer = setTimeout(silent, silenceMs);
@@ -25568,21 +25636,30 @@ class DispatchClient {
25568
25636
  return await this.#fetch(url, {
25569
25637
  method,
25570
25638
  signal: AbortSignal.any([limit, cut.signal]),
25639
+ ...transport,
25571
25640
  ...body === undefined ? { headers } : attemptBody(body, headers, progress)
25572
25641
  });
25573
25642
  } catch (error) {
25574
25643
  if (cut.signal.aborted && !limit.aborted)
25575
25644
  throw cut.signal.reason;
25645
+ const refused = transport === undefined ? undefined : proxyTunnelAnswer(error);
25646
+ if (refused !== undefined)
25647
+ return refused;
25576
25648
  throw error;
25577
25649
  } finally {
25578
25650
  answered = true;
25579
25651
  clearTimeout(timer);
25580
25652
  }
25581
25653
  }
25582
- async#endWrite(method, url, state, event, answer, failure, write, routeSpent) {
25654
+ async#endWrite(method, url, state, event, answer, failure, write, routeSpent, via) {
25583
25655
  const { request, progress } = write;
25584
25656
  let own = failure;
25585
- if (answer?.response.ok === true) {
25657
+ const proxyCode = via === undefined ? null : answer?.response.headers.get(PROXY_CODE_HEADER);
25658
+ if (via !== undefined && answer !== undefined && typeof proxyCode === "string") {
25659
+ own = this.#proxyRefusal(request, via, answer, proxyCode, state, event);
25660
+ } else if (via !== undefined && failure instanceof DispatchUnreachableError) {
25661
+ own = this.#proxyUnreachable(via, failure, state);
25662
+ } else if (answer?.response.ok === true) {
25586
25663
  const { status } = answer.response;
25587
25664
  own = new Error(answer.notJson ? `${request} answered ${status} with ${bodyForText(answer.text, this.#sent.with(this.token), "JSON")}, though every Dispatch write answers JSON, so something in Dispatch's place answered` : event === "foreign-2xx" ? `${request} answered ${status} without carrying back its ${IDEMPOTENCY_KEY_HEADER}, though an earlier answer of this write carried it and Dispatch answers every keyed write with it, so something in Dispatch's place answered` : `${request} answered ${status} with a body that is not the JSON its Content-Type says`);
25588
25665
  } else if (answer !== undefined) {
@@ -25618,6 +25695,17 @@ class DispatchClient {
25618
25695
  }
25619
25696
  return new DispatchWriteUnsettledError(last, progress.sent, write.key, unanswered);
25620
25697
  }
25698
+ #proxyUnreachable(via, failure, state) {
25699
+ const again = state === "nothing-sent" ? " The review can be sent again once the broker answers." : "";
25700
+ return new DispatchUnreachableError(`Dispatch at ${urlForText(this.#baseUrl, DISPATCH_URL_SETTING)}, through Cipher's proxy at ${originOf(via.init.proxy)}, is unreachable: ${failure.failure}. The proxy is the secrets broker's.${again}`, failure.reachedNothing, failure.failure);
25701
+ }
25702
+ #proxyRefusal(request, via, answer, code, state, event) {
25703
+ const { status } = answer.response;
25704
+ const said = excerpt(answer.text, this.#sent.with(this.token));
25705
+ const by = `${request} was answered ${status} ${code} by Cipher's proxy at ${originOf(via.init.proxy)}${said === "" ? "" : ` (${JSON.stringify(said)})`}`;
25706
+ const message = state !== "nothing-sent" ? event === "proxy-unsettled" ? `${by}, after this attempt may have reached Dispatch` : `${by} before this attempt left the proxy, after an earlier attempt of the review may have reached Dispatch` : event === "proxy-failed" ? `${by} before any of it left the proxy, so nothing reached Dispatch. The proxy is the secrets broker's, and a new \`dispatch review\` trades a new grant once the broker answers.` : `${by} before any of it left the proxy, so nothing reached Dispatch, and a new \`dispatch review\` trades a new grant.`;
25707
+ return new DispatchProxyRefusalError(code, status, message);
25708
+ }
25621
25709
  #url(path, query) {
25622
25710
  const url = new URL(`${path.map((segment) => encodeURIComponent(segment)).join("/")}`, `${this.#baseUrl}/`);
25623
25711
  if (query) {
@@ -26850,8 +26938,25 @@ class BrokerTokenClient {
26850
26938
  return await this.#exchange(await source.read(signal), signal);
26851
26939
  }
26852
26940
  async#exchange(subject, signal) {
26941
+ const { endpoint, answered, raw } = await this.#grant(subject, signal, {});
26942
+ return this.#read(endpoint, answered, raw);
26943
+ }
26944
+ async proxyPlaceholder(signal) {
26945
+ const trade = async (subject) => {
26946
+ const { endpoint, answered, raw } = await this.#grant(subject, signal, {
26947
+ [FORM_REQUESTED_TOKEN_TYPE]: TOKEN_TYPE_PROXY_PLACEHOLDER
26948
+ });
26949
+ return this.#readPlaceholder(endpoint, answered, raw);
26950
+ };
26951
+ const source = this.#subject;
26952
+ if (source.spend !== undefined)
26953
+ return await source.spend(trade, signal);
26954
+ return await trade(await source.read(signal));
26955
+ }
26956
+ async#grant(subject, signal, asked) {
26853
26957
  holdSecret(subject.token, heldUntil(subject.token));
26854
26958
  const form = {
26959
+ ...asked,
26855
26960
  subject_token: subject.token,
26856
26961
  subject_token_type: subject.tokenType
26857
26962
  };
@@ -26892,7 +26997,7 @@ class BrokerTokenClient {
26892
26997
  ...this.#session === "" ? {} : { session: this.#session }
26893
26998
  });
26894
26999
  }
26895
- return this.#read(urlForText(endpoint, BROKER_URL_SETTING), answered, raw.body);
27000
+ return { endpoint: urlForText(endpoint, BROKER_URL_SETTING), answered, raw: raw.body };
26896
27001
  }
26897
27002
  async#send(url, options, signal, raw) {
26898
27003
  const answer = await sendWithProof({ key: this.#key, holderKey: this.#holderKey, fetch: this.#fetch, nonce: this.#nonce }, url, options, signal);
@@ -26930,6 +27035,56 @@ class BrokerTokenClient {
26930
27035
  credentialExpiresAt
26931
27036
  };
26932
27037
  }
27038
+ #readPlaceholder(endpoint, answered, raw) {
27039
+ const refused = (what) => new BrokerTradeFailedError(`the broker at ${endpoint} answered a proxy placeholder trade with ${what}`);
27040
+ const body = typeof raw === "object" && raw !== null ? raw : {};
27041
+ if (answered["issued_token_type"] !== TOKEN_TYPE_PROXY_PLACEHOLDER) {
27042
+ throw refused("an issued_token_type that is not the proxy placeholder's");
27043
+ }
27044
+ if (body["token_type"] !== TOKEN_TYPE_NOT_APPLICABLE) {
27045
+ throw refused(`a token_type other than ${TOKEN_TYPE_NOT_APPLICABLE}`);
27046
+ }
27047
+ if (answered.refresh_token !== undefined && answered.refresh_token !== "") {
27048
+ throw refused("a refresh token, which this grant never issues");
27049
+ }
27050
+ const seconds = answered.expires_in ?? 0;
27051
+ if (!(seconds > 0))
27052
+ throw refused("no usable expires_in (a positive number of seconds)");
27053
+ const placeholder = answered.access_token;
27054
+ if (!placeholder.startsWith(PLACEHOLDER_PREFIX) || placeholder.length <= PLACEHOLDER_PREFIX.length) {
27055
+ throw refused("an access_token that is no placeholder");
27056
+ }
27057
+ holdSecret(placeholder, Date.now() + seconds * 1000);
27058
+ if (typeof body["grant_id"] !== "string" || body["grant_id"] === "")
27059
+ throw refused("no grant_id");
27060
+ const release = body["proxy"];
27061
+ if (typeof release !== "object" || release === null)
27062
+ throw refused("no proxy release");
27063
+ const {
27064
+ proxy_url: proxyUrl,
27065
+ ca_certificate: caCertificate,
27066
+ proxy_password: password,
27067
+ names
27068
+ } = release;
27069
+ if (typeof proxyUrl !== "string" || !isHttpUrl(proxyUrl))
27070
+ throw refused("a proxy_url that is no http URL");
27071
+ if (typeof caCertificate !== "string" || caCertificate.trim() === "")
27072
+ throw refused("no ca_certificate");
27073
+ if (password !== placeholder)
27074
+ throw refused("a proxy_password other than the placeholder");
27075
+ if (typeof names !== "object" || names === null || Object.keys(names).length === 0) {
27076
+ throw refused("no name behind the proxy");
27077
+ }
27078
+ const read = {};
27079
+ for (const [name, entry] of Object.entries(names)) {
27080
+ const { value, hosts } = typeof entry === "object" && entry !== null ? entry : {};
27081
+ if (value !== placeholder || !Array.isArray(hosts) || hosts.length === 0 || !hosts.every((host) => typeof host === "string" && host !== "")) {
27082
+ throw refused("a name holding no host, or a value other than the placeholder");
27083
+ }
27084
+ read[name] = { hosts };
27085
+ }
27086
+ return { placeholder, proxyUrl, caCertificate, names: read };
27087
+ }
26933
27088
  }
26934
27089
  function heldUntil(token) {
26935
27090
  const now = Date.now();
@@ -28747,6 +28902,22 @@ function brokerCredential(env, session, read) {
28747
28902
  const told = headers === undefined ? undefined : presentedAs.get(headers);
28748
28903
  dropHeld(told?.key ?? keyOf(env, own(acting), read), told?.token);
28749
28904
  },
28905
+ async approveRoute(signal) {
28906
+ const { made } = await untilAborted(clientFor(env, own(undefined), read), signal);
28907
+ if (made.client === undefined || made.signIn === "")
28908
+ return;
28909
+ const held = await made.client.proxyPlaceholder(signal);
28910
+ const approve = held.names[PROXY_APPROVE_NAME];
28911
+ if (approve === undefined) {
28912
+ throw new LoggableError(`the secrets broker answered a placeholder of Cipher's proxy that holds no ${PROXY_APPROVE_NAME}, so no review was sent`);
28913
+ }
28914
+ return {
28915
+ placeholder: held.placeholder,
28916
+ proxyUrl: held.proxyUrl,
28917
+ caCertificate: held.caCertificate,
28918
+ hosts: approve.hosts
28919
+ };
28920
+ },
28750
28921
  secret: () => presented
28751
28922
  };
28752
28923
  }