@halonicjs/testing 0.1.0 → 0.1.1

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 (153) hide show
  1. package/NOTICE +4 -0
  2. package/README.md +1 -1
  3. package/dist/adapters/http-kit/src/address-checking.d.ts +19 -0
  4. package/dist/adapters/http-kit/src/address-checking.js +1 -0
  5. package/dist/adapters/http-kit/src/index.d.ts +1 -0
  6. package/dist/adapters/integration-kit/src/auth-placement.d.ts +40 -0
  7. package/dist/adapters/integration-kit/src/auth-placement.js +102 -0
  8. package/dist/adapters/integration-kit/src/current-secret-forms.d.ts +28 -0
  9. package/dist/adapters/integration-kit/src/current-secret-forms.js +60 -0
  10. package/dist/adapters/integration-kit/src/declaration.d.ts +2 -1
  11. package/dist/adapters/integration-kit/src/declaration.js +4 -3
  12. package/dist/adapters/integration-kit/src/host.d.ts +7 -1
  13. package/dist/adapters/integration-kit/src/host.js +1 -0
  14. package/dist/adapters/integration-kit/src/index.d.ts +3 -1
  15. package/dist/adapters/integration-kit/src/index.js +1 -0
  16. package/dist/adapters/integration-kit/src/leak-attempt.d.ts +4 -2
  17. package/dist/adapters/integration-kit/src/leak-attempt.js +5 -2
  18. package/dist/adapters/integration-kit/src/limited-http.d.ts +7 -1
  19. package/dist/adapters/integration-kit/src/limited-http.js +53 -10
  20. package/dist/adapters/integration-kit/src/loader.d.ts +29 -2
  21. package/dist/adapters/integration-kit/src/loader.js +44 -12
  22. package/dist/adapters/integration-kit/src/own-request.d.ts +2 -2
  23. package/dist/adapters/integration-kit/src/own-request.js +1 -1
  24. package/dist/adapters/integration-kit/src/plugin-load.d.ts +3 -3
  25. package/dist/adapters/integration-kit/src/plugin-load.js +5 -5
  26. package/dist/adapters/integration-kit/src/policy.d.ts +28 -0
  27. package/dist/adapters/integration-kit/src/response-body.d.ts +6 -2
  28. package/dist/adapters/integration-kit/src/secret-filter.d.ts +3 -3
  29. package/dist/adapters/integration-kit/src/secret-filter.js +19 -4
  30. package/dist/adapters/integration-kit/src/secret-in-request.d.ts +3 -0
  31. package/dist/adapters/integration-kit/src/secret-in-request.js +8 -2
  32. package/dist/adapters/integration-kit/src/secret-placement.d.ts +35 -25
  33. package/dist/adapters/integration-kit/src/secret-placement.js +65 -95
  34. package/dist/adapters/integration-kit/src/sign-in-placement.d.ts +29 -0
  35. package/dist/adapters/integration-kit/src/sign-in-placement.js +92 -0
  36. package/dist/core/events/src/blocked-sites.d.ts +57 -0
  37. package/dist/core/events/src/blocked-sites.js +89 -0
  38. package/dist/core/events/src/index.d.ts +5 -1
  39. package/dist/core/events/src/index.js +3 -0
  40. package/dist/core/events/src/run-basics.d.ts +30 -7
  41. package/dist/core/events/src/run-event.d.ts +49 -5
  42. package/dist/core/events/src/run-reads.d.ts +25 -0
  43. package/dist/core/events/src/run-reads.js +44 -0
  44. package/dist/core/events/src/run-sends.d.ts +47 -0
  45. package/dist/core/events/src/run-sends.js +68 -0
  46. package/dist/core/events/src/summarize.js +5 -1
  47. package/dist/core/events/src/timeline-entry.js +1 -0
  48. package/dist/core/format/src/index.d.ts +7 -5
  49. package/dist/core/format/src/index.js +5 -3
  50. package/dist/core/format/src/integration/index.d.ts +3 -2
  51. package/dist/core/format/src/integration/index.js +2 -1
  52. package/dist/core/format/src/kinds/agent/sandbox-fields.d.ts +15 -5
  53. package/dist/core/format/src/kinds/agent/sandbox-fields.js +29 -10
  54. package/dist/core/format/src/kinds/agent/schema.js +2 -2
  55. package/dist/core/format/src/kinds/connector/is-local.js +1 -1
  56. package/dist/core/format/src/kinds/connector/kind.js +99 -0
  57. package/dist/core/format/src/kinds/connector/schema.d.ts +24 -0
  58. package/dist/core/format/src/kinds/connector/schema.js +53 -2
  59. package/dist/core/format/src/kinds/deployments/kind.js +25 -1
  60. package/dist/core/format/src/kinds/deployments/schema.d.ts +40 -0
  61. package/dist/core/format/src/kinds/deployments/schema.js +52 -0
  62. package/dist/core/format/src/kinds/integration/approved-sign-ins.d.ts +62 -0
  63. package/dist/core/format/src/kinds/integration/approved-sign-ins.js +125 -0
  64. package/dist/core/format/src/kinds/integration/host-key.d.ts +7 -0
  65. package/dist/core/format/src/kinds/integration/host-key.js +8 -0
  66. package/dist/core/format/src/kinds/integration/kind.d.ts +4 -2
  67. package/dist/core/format/src/kinds/integration/kind.js +49 -12
  68. package/dist/core/format/src/kinds/integration/policy.d.ts +47 -2
  69. package/dist/core/format/src/kinds/integration/policy.js +61 -2
  70. package/dist/core/format/src/kinds/integration/schema.d.ts +180 -3
  71. package/dist/core/format/src/kinds/integration/schema.js +132 -6
  72. package/dist/core/format/src/kinds/integration/sign-in-checks.d.ts +13 -0
  73. package/dist/core/format/src/kinds/integration/sign-in-checks.js +120 -0
  74. package/dist/core/format/src/kinds/integration/sign-in-urls.d.ts +19 -0
  75. package/dist/core/format/src/kinds/integration/sign-in-urls.js +47 -0
  76. package/dist/core/format/src/kinds/plugin/checks.js +7 -1
  77. package/dist/core/format/src/kinds/plugin/schema.d.ts +130 -0
  78. package/dist/core/format/src/kinds/plugin/schema.js +2 -2
  79. package/dist/core/format/src/kinds/plugin/ui-checks.d.ts +1 -1
  80. package/dist/core/format/src/kinds/plugin/ui-checks.js +1 -10
  81. package/dist/core/format/src/kinds/routine/kind.js +38 -1
  82. package/dist/core/format/src/kinds/routine/schema.d.ts +10 -0
  83. package/dist/core/format/src/kinds/routine/schema.js +23 -1
  84. package/dist/core/format/src/kinds/routine/webhook-headers.d.ts +9 -0
  85. package/dist/core/format/src/kinds/routine/webhook-headers.js +26 -0
  86. package/dist/core/format/src/kinds/workflow/schema.js +1 -1
  87. package/dist/core/format/src/kinds/workspace/kind.js +1 -1
  88. package/dist/core/format/src/kinds/workspace/schema.d.ts +2 -2
  89. package/dist/core/format/src/kinds/workspace/schema.js +1 -1
  90. package/dist/core/format/src/shared/fields.d.ts +3 -6
  91. package/dist/core/format/src/shared/fields.js +33 -13
  92. package/dist/core/ports/src/agent-runs-not-available.d.ts +21 -0
  93. package/dist/core/ports/src/agent-runs-not-available.js +25 -0
  94. package/dist/core/ports/src/blob-store.d.ts +12 -0
  95. package/dist/core/ports/src/blob-store.js +22 -0
  96. package/dist/core/ports/src/connector-client.d.ts +48 -4
  97. package/dist/core/ports/src/connector-client.js +17 -0
  98. package/dist/core/ports/src/definition-store.d.ts +23 -2
  99. package/dist/core/ports/src/harness-bridge-server.d.ts +12 -3
  100. package/dist/core/ports/src/harness-sign-in-command.d.ts +27 -0
  101. package/dist/core/ports/src/harness-sign-in-command.js +31 -0
  102. package/dist/core/ports/src/harness.d.ts +46 -13
  103. package/dist/core/ports/src/harness.js +1 -1
  104. package/dist/core/ports/src/host-settings-store.d.ts +116 -0
  105. package/dist/core/ports/src/host-settings-store.js +106 -0
  106. package/dist/core/ports/src/index.d.ts +18 -10
  107. package/dist/core/ports/src/index.js +15 -7
  108. package/dist/core/ports/src/instruction-trust-store.d.ts +81 -0
  109. package/dist/core/ports/src/instruction-trust-store.js +43 -0
  110. package/dist/core/ports/src/integration.d.ts +71 -7
  111. package/dist/core/ports/src/integration.js +22 -4
  112. package/dist/core/ports/src/llm-provider.d.ts +3 -1
  113. package/dist/core/ports/src/llm-provider.js +3 -0
  114. package/dist/core/ports/src/password-hasher.d.ts +36 -20
  115. package/dist/core/ports/src/port-versions.d.ts +7 -4
  116. package/dist/core/ports/src/port-versions.js +6 -0
  117. package/dist/core/ports/src/program-probe.d.ts +25 -2
  118. package/dist/core/ports/src/program-probe.js +1 -1
  119. package/dist/core/ports/src/provider-default-base-url.d.ts +10 -0
  120. package/dist/core/ports/src/provider-default-base-url.js +18 -0
  121. package/dist/core/ports/src/record-store.d.ts +17 -0
  122. package/dist/core/ports/src/record-store.js +70 -0
  123. package/dist/core/ports/src/run-executor.d.ts +5 -4
  124. package/dist/core/ports/src/run-target-not-found.d.ts +34 -0
  125. package/dist/core/ports/src/run-target-not-found.js +41 -0
  126. package/dist/core/ports/src/sandbox-setting-store.d.ts +68 -0
  127. package/dist/core/ports/src/sandbox-setting-store.js +33 -0
  128. package/dist/core/ports/src/sandbox.d.ts +130 -13
  129. package/dist/core/ports/src/sandbox.js +38 -2
  130. package/dist/core/ports/src/secrets.d.ts +73 -0
  131. package/dist/core/ports/src/secrets.js +56 -0
  132. package/dist/core/ports/src/tool-approval-store.d.ts +50 -8
  133. package/dist/core/ports/src/tool-approval-store.js +59 -0
  134. package/dist/core/ports/src/vcs-status.d.ts +8 -0
  135. package/dist/core/ports/src/vcs-status.js +90 -0
  136. package/dist/core/std/src/amount.d.ts +13 -0
  137. package/dist/core/std/src/amount.js +22 -0
  138. package/dist/core/std/src/hidden-characters.d.ts +19 -0
  139. package/dist/core/std/src/hidden-characters.js +54 -0
  140. package/dist/core/std/src/index.d.ts +5 -0
  141. package/dist/core/std/src/index.js +5 -0
  142. package/dist/core/std/src/public-suffixes.generated.d.ts +9 -0
  143. package/dist/core/std/src/public-suffixes.generated.js +10429 -0
  144. package/dist/core/std/src/registrable-domain.d.ts +10 -0
  145. package/dist/core/std/src/registrable-domain.js +62 -0
  146. package/dist/core/std/src/secret-variable-name.d.ts +12 -0
  147. package/dist/core/std/src/secret-variable-name.js +34 -0
  148. package/dist/core/std/src/site-name.d.ts +35 -0
  149. package/dist/core/std/src/site-name.js +111 -0
  150. package/dist/core/std/src/url-host.d.ts +35 -0
  151. package/dist/core/std/src/url-host.js +170 -0
  152. package/dist/testing/integration/src/load.js +3 -1
  153. package/package.json +2 -2
@@ -1,8 +1,10 @@
1
- import { integrationPolicy, parseIntegrationSheet, } from "../../../core/format/src/integration/index.js";
2
- import { workspaceSecrets, } from "../../../core/ports/src/index.js";
1
+ import { integrationPolicy, isPlacedSecret, parseIntegrationSheet, } from "../../../core/format/src/integration/index.js";
2
+ import { pluginSecrets, workspaceSecrets, } from "../../../core/ports/src/index.js";
3
3
  import { deepEqual, err, ok } from "../../../core/std/src/index.js";
4
+ import { currentSecretForms } from "./current-secret-forms.js";
4
5
  import { declarationProblems } from "./declaration.js";
5
6
  import { createIntegrationHost } from "./host.js";
7
+ import { checkSecretLength } from "./secret-placement.js";
6
8
  /**
7
9
  * Checks an integration against its permission sheet and builds its host. The
8
10
  * sheet must pass its schema, the code must be exactly what the sheet
@@ -20,7 +22,7 @@ export function loadIntegration(options) {
20
22
  if (!prepared.ok) {
21
23
  return prepared;
22
24
  }
23
- const { readSecrets: _readSecrets, ...loaded } = prepared.value;
25
+ const { readSecrets: _readSecrets, currentSecretForms: _forms, ...loaded } = prepared.value;
24
26
  return ok({ ...loaded, integration });
25
27
  }
26
28
  /**
@@ -48,6 +50,7 @@ function prepare(options, codeProblems) {
48
50
  const problems = [
49
51
  ...approvalProblems(source, sheet),
50
52
  ...localTokenProblems(source, sheet),
53
+ ...localSignInProblems(source, sheet),
51
54
  ...codeProblems(sheet),
52
55
  ...needProblems(sheet, options.capabilities ?? []),
53
56
  ...secretChoiceProblems(sheet, secretNames),
@@ -59,18 +62,20 @@ function prepare(options, codeProblems) {
59
62
  if (problems.length > 0 || !resolved.ok) {
60
63
  return err(problems);
61
64
  }
62
- const secrets = chosenSecrets(options.secrets, secretNames);
65
+ const secrets = chosenSecrets(options.secrets, secretNames, source.plugin ? sheet.id : undefined);
63
66
  const { host, log } = createIntegrationHost({
64
67
  policy: resolved.value.policy,
65
68
  transport: options.transport,
66
69
  secrets,
67
70
  tokens: options.tokens,
71
+ ...(options.signInTokens ? { signInTokens: options.signInTokens } : {}),
68
72
  settings,
69
73
  ...(resolved.value.limits ? { limits: requestLimits(resolved.value.limits) } : {}),
70
74
  ...(options.now ? { now: options.now } : {}),
71
75
  });
72
76
  const readSecrets = () => secretsOnWire(sheet, secrets);
73
- return ok({ origin: source.origin, sheet, host, log, readSecrets });
77
+ const forms = async () => currentSecretForms(await readSecrets(), options.signInTokens);
78
+ return ok({ origin: source.origin, sheet, host, log, readSecrets, currentSecretForms: forms });
74
79
  }
75
80
  /**
76
81
  * Maps a workspace's provider entry onto the sheet, so the integration can
@@ -152,6 +157,23 @@ function localTokenProblems(source, sheet) {
152
157
  },
153
158
  ];
154
159
  }
160
+ /**
161
+ * A local integration can't declare `signIns` until the trust content shows
162
+ * them: the client secret leaves `secrets`, and the token, revoke and
163
+ * authorize hosts needn't be in `hosts`, so the person approving would never
164
+ * see where the code and the client secret go.
165
+ */
166
+ function localSignInProblems(source, sheet) {
167
+ if (source.origin !== "local") {
168
+ return [];
169
+ }
170
+ return Object.keys(sheet.signIns).map((name) => ({
171
+ code: "sign-ins-not-allowed",
172
+ path: ["signIns", name],
173
+ message: `${sheet.id}: an integration you build or change can't declare the sign-in ${name} yet`,
174
+ fix: "Remove signIns from its sheet and use an API key, or ask for sign-in support.",
175
+ }));
176
+ }
155
177
  function needProblems(sheet, capabilities) {
156
178
  return sheet.needs.flatMap((need, index) => capabilities.includes(need)
157
179
  ? []
@@ -180,24 +202,34 @@ function requestLimits(limits) {
180
202
  }
181
203
  /**
182
204
  * The raw secrets seen through the user's picks: the kit asks by the sheet's
183
- * name and reads the workspace secret picked for it, or nothing. The workspace
184
- * chose those names, so they can't reach Halonic's own secrets or tokens.
205
+ * name and reads the secret picked for it, or nothing. A workspace chose
206
+ * those names, so they can't reach Halonic's own secrets, tokens or a
207
+ * plugin's. A plugin's backend's names come from its manifest and can reach
208
+ * only that plugin's own secrets (`plugin`).
185
209
  */
186
- function chosenSecrets(raw, secretNames) {
210
+ function chosenSecrets(raw, secretNames, plugin) {
187
211
  const picks = new Map(Object.entries(secretNames));
188
- const workspace = workspaceSecrets(raw);
212
+ const readable = plugin === undefined ? workspaceSecrets(raw) : pluginSecrets(raw, plugin);
189
213
  return {
190
214
  get: async (name) => {
191
215
  const picked = picks.get(name);
192
- return picked === undefined ? null : await workspace.get(picked);
216
+ return picked === undefined ? null : await readable.get(picked);
193
217
  },
194
218
  };
195
219
  }
196
- /** Each declared secret with a value, and its one placement, as {@link PreparedIntegration.readSecrets} gives them. */
220
+ /**
221
+ * Each declared secret with a value, and its one placement, as {@link PreparedIntegration.readSecrets} gives them.
222
+ *
223
+ * @throws {IntegrationRefusedError} `secret-too-short` for a value the filter can't safely remove
224
+ */
197
225
  async function secretsOnWire(sheet, secrets) {
198
226
  const read = Object.entries(sheet.secrets).map(async ([name, declared]) => {
199
227
  const value = await secrets.get(name);
200
- return value === null ? [] : [{ value, placements: [placementOf(name, declared)] }];
228
+ if (value === null)
229
+ return [];
230
+ checkSecretLength(sheet.id, name, value);
231
+ // A client secret is never placed by the integration, but a server could still echo it.
232
+ return [{ value, placements: isPlacedSecret(declared) ? [placementOf(name, declared)] : [] }];
201
233
  });
202
234
  return (await Promise.all(read)).flat();
203
235
  }
@@ -1,4 +1,4 @@
1
- import type { IntegrationRequestInit, SecretPlacement } from "../../../core/ports/src/index.js";
1
+ import type { AuthPlacement, IntegrationRequestInit } from "../../../core/ports/src/index.js";
2
2
  import type { IntegrationPolicy } from "./policy.js";
3
3
  import type { Body } from "./request-body.js";
4
4
  /**
@@ -19,7 +19,7 @@ export interface OwnRequest {
19
19
  readonly headers: Headers;
20
20
  /** The body, with its bytes or form copied. A kind the kit doesn't support stays as given. */
21
21
  readonly body: Body;
22
- readonly placements: readonly SecretPlacement[];
22
+ readonly placements: readonly AuthPlacement[];
23
23
  readonly signal: AbortSignal | undefined;
24
24
  }
25
25
  /**
@@ -1,4 +1,4 @@
1
- import { copyPlacements } from "./secret-placement.js";
1
+ import { copyPlacements } from "./auth-placement.js";
2
2
  /**
3
3
  * Copies the request before the kit's first `await`, reading each option
4
4
  * once. The code keeps its own objects and could change them, or give a
@@ -2,9 +2,9 @@ import { type Plugin } from "../../../core/format/src/integration/index.js";
2
2
  import { type Integration } from "../../../core/ports/src/index.js";
3
3
  import type { LoadProblem } from "./load-problem.js";
4
4
  /**
5
- * Why a plugin that passed its own checks can't load: an `sdk` other than
6
- * this Halonic's `integrationVersion`, and, when the backend's code is
7
- * given, every way it differs from the backend, with the same rules as for
5
+ * Why a plugin that passed its own checks can't load: an `sdk` newer than
6
+ * this Halonic's `integrationVersion`, and, when the backend's code is given,
7
+ * every way it differs from the backend, with the same rules as for
8
8
  * an integration and its sheet (`declarationProblems`) and each path
9
9
  * pointing into `plugin.yaml`. Empty when the plugin can load.
10
10
  *
@@ -6,9 +6,9 @@ import { backendSheet } from "../../../core/format/src/integration/index.js";
6
6
  import { integrationVersion } from "../../../core/ports/src/index.js";
7
7
  import { declarationProblems } from "./declaration.js";
8
8
  /**
9
- * Why a plugin that passed its own checks can't load: an `sdk` other than
10
- * this Halonic's `integrationVersion`, and, when the backend's code is
11
- * given, every way it differs from the backend, with the same rules as for
9
+ * Why a plugin that passed its own checks can't load: an `sdk` newer than
10
+ * this Halonic's `integrationVersion`, and, when the backend's code is given,
11
+ * every way it differs from the backend, with the same rules as for
12
12
  * an integration and its sheet (`declarationProblems`) and each path
13
13
  * pointing into `plugin.yaml`. Empty when the plugin can load.
14
14
  *
@@ -17,11 +17,11 @@ import { declarationProblems } from "./declaration.js";
17
17
  */
18
18
  export function pluginLoadProblems(plugin, backendCode) {
19
19
  const problems = [];
20
- if (plugin.sdk !== integrationVersion) {
20
+ if (plugin.sdk > integrationVersion) {
21
21
  problems.push({
22
22
  code: "sdk-version",
23
23
  path: ["sdk"],
24
- message: `${plugin.id}: built against SDK ${plugin.sdk}, but this Halonic offers SDK ${integrationVersion}`,
24
+ message: `${plugin.id}: this plugin needs a newer version of Halonic (it is built against SDK ${plugin.sdk}, and this Halonic offers SDK ${integrationVersion})`,
25
25
  });
26
26
  }
27
27
  if (backendCode !== undefined) {
@@ -23,6 +23,12 @@ export interface IntegrationPolicy {
23
23
  * it up, instead of refusing the request.
24
24
  */
25
25
  readonly secrets: Readonly<Record<string, DeclaredSecret>>;
26
+ /**
27
+ * Each sign-in, the hosts its access token may go to, and where the sheet
28
+ * puts it. Halonic's own code runs the sign-in; the integration can only
29
+ * ask the kit to place the access token.
30
+ */
31
+ readonly signIns: Readonly<Record<string, DeclaredSignIn>>;
26
32
  /** The token names it may read and write. */
27
33
  readonly tokens: readonly string[];
28
34
  }
@@ -33,6 +39,28 @@ export interface DeclaredSecret {
33
39
  /** The only way the kit may place it; any other placement is refused. */
34
40
  readonly placement: DeclaredPlacement;
35
41
  }
42
+ /** One sign-in in the permission sheet. */
43
+ export type DeclaredSignIn = DeclaredSignInPlacement & {
44
+ /** The hosts the access token may go to. */
45
+ readonly hosts: readonly string[];
46
+ /**
47
+ * The sign-in's client secret, when it has one. The integration can't
48
+ * place it: only Halonic's sign-in code sends it, to the token and revoke
49
+ * hosts.
50
+ */
51
+ readonly clientSecret?: {
52
+ readonly name: string;
53
+ readonly hosts: readonly string[];
54
+ };
55
+ };
56
+ /** Where the sheet puts a sign-in's access token. There is no `form`. */
57
+ export type DeclaredSignInPlacement = {
58
+ readonly as: "bearer";
59
+ } | {
60
+ readonly as: "header";
61
+ readonly header: string;
62
+ readonly prefix?: string;
63
+ };
36
64
  /** Where the sheet puts a secret: a `SecretPlacement` without the secret's name. */
37
65
  export type DeclaredPlacement = {
38
66
  readonly as: "bearer";
@@ -10,8 +10,12 @@ export interface BodyGuard {
10
10
  readonly onIdle: () => void;
11
11
  /** Called once when the body is finished, failed or cancelled. */
12
12
  readonly done: () => void;
13
- /** The secrets of this request, removed from the status text, the headers, the URL, the body and its errors. */
14
- readonly secrets: SecretForms;
13
+ /**
14
+ * The secrets and tokens of this request, removed from the status text, the
15
+ * headers, the URL, the body and its errors. The kit replaces it when a hop
16
+ * brings in a token that wasn't known before.
17
+ */
18
+ secrets: SecretForms;
15
19
  }
16
20
  /**
17
21
  * Passes the body through with the request's secrets removed, failing it when it grows over the size limit, when no data comes
@@ -1,12 +1,12 @@
1
- import type { SecretPlacement } from "../../../core/ports/src/index.js";
1
+ import type { AuthPlacement } from "../../../core/ports/src/index.js";
2
2
  import { secretRemoved } from "../../../core/std/src/index.js";
3
3
  /** What a secret value is replaced with wherever the filter finds it. */
4
4
  export { secretRemoved };
5
- /** A secret value, and where the kit placed it, if it did. */
5
+ /** A secret or token value, and where the kit placed it, if it did. */
6
6
  export interface SecretOnWire {
7
7
  readonly value: string;
8
8
  /** How the kit put it in a request, so the placed forms are matched whole. */
9
- readonly placements?: readonly SecretPlacement[];
9
+ readonly placements?: readonly AuthPlacement[];
10
10
  }
11
11
  /**
12
12
  * Every form of a set of secrets the filter looks for, longest first. Build
@@ -48,10 +48,25 @@ function placed(placement, value) {
48
48
  * removeSecrets(String(error), forms)
49
49
  */
50
50
  export function removeSecrets(text, forms) {
51
- let filtered = text;
52
- for (const form of forms.texts)
53
- filtered = filtered.replaceAll(form, secretRemoved);
54
- return filtered;
51
+ if (forms.longest === 0)
52
+ return text;
53
+ // One pass over the text, the longest form first at each place, as
54
+ // SecretRemover does for bodies: a marker already written is never
55
+ // searched again, so a secret found inside it can't break it up.
56
+ let filtered = "";
57
+ let kept = 0;
58
+ let at = 0;
59
+ while (at < text.length) {
60
+ const form = forms.texts.find((candidate) => text.startsWith(candidate, at));
61
+ if (form === undefined) {
62
+ at++;
63
+ continue;
64
+ }
65
+ filtered += text.slice(kept, at) + secretRemoved;
66
+ at += form.length;
67
+ kept = at;
68
+ }
69
+ return filtered + text.slice(kept);
55
70
  }
56
71
  /**
57
72
  * The error with every form of the secrets removed from its message, its
@@ -7,6 +7,9 @@ import { type SecretForms } from "./secret-filter.js";
7
7
  * The values of every secret the integration declares and the workspace
8
8
  * has set up, whether this request places them or not. The kit refuses a
9
9
  * request that carries one of them anywhere it didn't put it.
10
+ *
11
+ * @throws {IntegrationRefusedError} `secret-too-short` when a value is under the minimum length:
12
+ * the filter would remove ordinary text for it and show where the value sits
10
13
  */
11
14
  export declare function readDeclaredSecrets(policy: IntegrationPolicy, secrets: Secrets): Promise<Map<string, string>>;
12
15
  /** The parts of a request the integration wrote, before the kit places any secret. */
@@ -1,16 +1,22 @@
1
1
  import { IntegrationRefusedError } from "@halonicjs/adapters/integration-kit";
2
2
  import { removeSecrets, SecretRemover, secretForms, secretRemoved, } from "./secret-filter.js";
3
+ import { checkSecretLength } from "./secret-placement.js";
3
4
  /**
4
5
  * The values of every secret the integration declares and the workspace
5
6
  * has set up, whether this request places them or not. The kit refuses a
6
7
  * request that carries one of them anywhere it didn't put it.
8
+ *
9
+ * @throws {IntegrationRefusedError} `secret-too-short` when a value is under the minimum length:
10
+ * the filter would remove ordinary text for it and show where the value sits
7
11
  */
8
12
  export async function readDeclaredSecrets(policy, secrets) {
9
13
  const values = new Map();
10
14
  for (const name of Object.keys(policy.secrets)) {
11
15
  const value = await secrets.get(name);
12
- if (value !== null && value !== "")
13
- values.set(name, value);
16
+ if (value === null || value === "")
17
+ continue;
18
+ checkSecretLength(policy.name, name, value);
19
+ values.set(name, value);
14
20
  }
15
21
  return values;
16
22
  }
@@ -1,36 +1,46 @@
1
- import type { SecretPlacement, Secrets } from "../../../core/ports/src/index.js";
2
- import { type IntegrationPolicy } from "./policy.js";
1
+ import type { AuthPlacement, SecretPlacement, Secrets } from "../../../core/ports/src/index.js";
2
+ import { type DeclaredPlacement, type DeclaredSignInPlacement, type IntegrationPolicy } from "./policy.js";
3
+ /** Checks that the secret is declared, isn't a client secret, and is placed as the sheet says. */
4
+ export declare function checkSecretPlacement(policy: IntegrationPolicy, placement: SecretPlacement): void;
5
+ /** Checks that the secret may go to this host, over this scheme. */
6
+ export declare function checkSecretHost(policy: IntegrationPolicy, placement: SecretPlacement, url: URL): void;
3
7
  /**
4
- * Copies the placements the integration's code gave into frozen objects of
5
- * text fields, reading each field once. The kit checks and places only
6
- * these copies: the code keeps its own objects and could change them, or
7
- * give a getter, after the check and before the secret is placed. A field
8
- * that isn't text is refused here, because an object could turn into other
9
- * text each time it is read.
8
+ * Whether the code places the secret the way the sheet says. Otherwise it
9
+ * could put a key in a field the service stores, such as a note title,
10
+ * where the integration could read it back. Header names match ignoring
11
+ * ASCII case, as HTTP does; a missing prefix and `""` send the same bytes;
12
+ * form field names are case-sensitive. The placement comes from the
13
+ * integration's code, so nothing in it is trusted to have the right type.
10
14
  */
11
- export declare function copyPlacements(policy: IntegrationPolicy, placements: readonly unknown[]): readonly SecretPlacement[];
15
+ export declare function matchesSheet(placement: AuthPlacement, sheet: DeclaredPlacement | DeclaredSignInPlacement): boolean;
16
+ /** A placement in words, for a refusal message. It never holds a value. */
17
+ export declare function describe(placement: DeclaredPlacement | DeclaredSignInPlacement | AuthPlacement): string;
12
18
  /**
13
- * Checks every placement against the sheet before a secret value is read.
14
- * Give it the copies from {@link copyPlacements}, never the code's own
15
- * objects. Where the secret may go is for {@link checkPlacementHosts}.
19
+ * Text from the integration's code, cut short and quoted, so it can't break
20
+ * the message. It is cut by characters before quoting, so the cut never
21
+ * falls inside an escape or a surrogate pair.
16
22
  */
17
- export declare function checkPlacements(policy: IntegrationPolicy, placements: readonly SecretPlacement[]): void;
18
- /**
19
- * Checks that every placed secret may go to the request's first host, once
20
- * the URL is parsed. {@link checkPlacements} has already run, so every
21
- * secret is declared.
22
- */
23
- export declare function checkPlacementHosts(policy: IntegrationPolicy, placements: readonly SecretPlacement[], url: URL): void;
23
+ export declare function quoted(text: unknown): string;
24
24
  /**
25
25
  * Reads each secret's value once. The values never leave the kit. An
26
26
  * optional secret without a value gets no entry, so `place` leaves it out.
27
27
  */
28
- export declare function readSecrets(policy: IntegrationPolicy, secrets: Secrets, placements: readonly SecretPlacement[]): Promise<Map<string, string>>;
28
+ export declare function readSecrets(policy: IntegrationPolicy, secrets: Secrets, placements: readonly AuthPlacement[]): Promise<Map<string, string>>;
29
+ /**
30
+ * The fewest UTF-8 bytes a secret value may have. The filter replaces every
31
+ * copy of a value in what the integration sees, so a shorter one would
32
+ * remove ordinary text, and where it was removed would give the value away.
33
+ */
34
+ export declare const minSecretBytes = 8;
35
+ /**
36
+ * @throws {IntegrationRefusedError} `secret-too-short` when `value` has fewer than {@link minSecretBytes} bytes
37
+ */
38
+ export declare function checkSecretLength(integration: string, secret: string, value: string): void;
29
39
  /**
30
- * Puts the secrets allowed for this hop's host into the headers and the form
31
- * body. A placement whose secret may not go to this hop is left out, which
32
- * happens after a redirect to another declared host.
40
+ * Puts one secret into the headers or the form body, when it may go to this
41
+ * hop's host. A secret that may not go to this hop is left out, which happens
42
+ * after a redirect to another declared host.
33
43
  *
34
- * @returns the form body to send, when a placement wrote into it
44
+ * @returns the form body to send, which a `form` placement writes into
35
45
  */
36
- export declare function place(policy: IntegrationPolicy, placements: readonly SecretPlacement[], values: ReadonlyMap<string, string>, url: URL, headers: Headers, form: URLSearchParams | undefined): URLSearchParams | undefined;
46
+ export declare function placeSecret(policy: IntegrationPolicy, placement: SecretPlacement, value: string | undefined, url: URL, headers: Headers, form: URLSearchParams | undefined): URLSearchParams | undefined;
@@ -1,73 +1,31 @@
1
1
  import { IntegrationRefusedError } from "@halonicjs/adapters/integration-kit";
2
2
  import { isLoopback } from "./local-address.js";
3
3
  import { hostIn, } from "./policy.js";
4
- /**
5
- * Copies the placements the integration's code gave into frozen objects of
6
- * text fields, reading each field once. The kit checks and places only
7
- * these copies: the code keeps its own objects and could change them, or
8
- * give a getter, after the check and before the secret is placed. A field
9
- * that isn't text is refused here, because an object could turn into other
10
- * text each time it is read.
11
- */
12
- export function copyPlacements(policy, placements) {
13
- return Object.freeze(placements.map((placement) => copyPlacement(policy, placement)));
14
- }
15
- function copyPlacement(policy, placement) {
16
- if (typeof placement !== "object" || placement === null) {
17
- throw notText(policy, "a secret placement");
4
+ /** Checks that the secret is declared, isn't a client secret, and is placed as the sheet says. */
5
+ export function checkSecretPlacement(policy, placement) {
6
+ const declared = declaredSecret(policy, placement.secret);
7
+ if (declared === undefined) {
8
+ if (isClientSecret(policy, placement.secret)) {
9
+ throw new IntegrationRefusedError("secret-sign-in-only", `${policy.name}: the secret ${quoted(placement.secret)} is the client secret of a sign-in, which only Halonic's sign-in code sends`);
10
+ }
11
+ throw new IntegrationRefusedError("secret-not-declared", `${policy.name}: the secret ${quoted(placement.secret)} is not in its permission sheet`);
18
12
  }
19
- const given = placement;
20
- const secret = given.secret;
21
- const as = given.as;
22
- if (typeof secret !== "string")
23
- throw notText(policy, "the secret's name");
24
- if (typeof as !== "string")
25
- throw notText(policy, `the "as" of the secret ${quoted(secret)}`);
26
- const copy = { secret, as };
27
- for (const key of ["header", "prefix", "field"]) {
28
- const value = given[key];
29
- if (value === undefined)
30
- continue;
31
- if (typeof value !== "string")
32
- throw notText(policy, `the "${key}" of the secret ${quoted(secret)}`);
33
- copy[key] = value;
13
+ if (!matchesSheet(placement, declared.placement)) {
14
+ throw new IntegrationRefusedError("placement-not-in-sheet", `${policy.name}: the secret ${quoted(placement.secret)} goes ${describe(declared.placement)} in its permission sheet, not ${describe(placement)}`);
34
15
  }
35
- // Whether the fields fit together is for `checkPlacements` to say.
36
- return Object.freeze(copy);
37
16
  }
38
- function notText(policy, what) {
39
- return new IntegrationRefusedError("placement-not-in-sheet", `${policy.name}: ${what} in a secret placement is not text`);
17
+ /** Whether a sign-in names the secret as its client secret, by own key only. */
18
+ function isClientSecret(policy, secret) {
19
+ return Object.values(policy.signIns).some((signIn) => signIn.clientSecret?.name === secret);
40
20
  }
41
- /**
42
- * Checks every placement against the sheet before a secret value is read.
43
- * Give it the copies from {@link copyPlacements}, never the code's own
44
- * objects. Where the secret may go is for {@link checkPlacementHosts}.
45
- */
46
- export function checkPlacements(policy, placements) {
47
- for (const placement of placements) {
48
- const declared = declaredSecret(policy, placement.secret);
49
- if (declared === undefined) {
50
- throw new IntegrationRefusedError("secret-not-declared", `${policy.name}: the secret ${quoted(placement.secret)} is not in its permission sheet`);
51
- }
52
- if (!matchesSheet(placement, declared.placement)) {
53
- throw new IntegrationRefusedError("placement-not-in-sheet", `${policy.name}: the secret ${quoted(placement.secret)} goes ${describe(declared.placement)} in its permission sheet, not ${describe(placement)}`);
54
- }
21
+ /** Checks that the secret may go to this host, over this scheme. */
22
+ export function checkSecretHost(policy, placement, url) {
23
+ const declared = declaredSecret(policy, placement.secret);
24
+ if (declared === undefined || !hostIn(declared.hosts, url)) {
25
+ throw new IntegrationRefusedError("secret-not-allowed-for-host", `${policy.name}: the secret ${quoted(placement.secret)} may not go to ${url.host}`);
55
26
  }
56
- }
57
- /**
58
- * Checks that every placed secret may go to the request's first host, once
59
- * the URL is parsed. {@link checkPlacements} has already run, so every
60
- * secret is declared.
61
- */
62
- export function checkPlacementHosts(policy, placements, url) {
63
- for (const placement of placements) {
64
- const declared = declaredSecret(policy, placement.secret);
65
- if (declared === undefined || !hostIn(declared.hosts, url)) {
66
- throw new IntegrationRefusedError("secret-not-allowed-for-host", `${policy.name}: the secret ${quoted(placement.secret)} may not go to ${url.host}`);
67
- }
68
- if (!mayCarrySecret(url)) {
69
- throw new IntegrationRefusedError("secret-over-plain-http", `${policy.name}: the secret ${quoted(placement.secret)} may not go over plain http to another machine (${url.host})`);
70
- }
27
+ if (!mayCarrySecret(url)) {
28
+ throw new IntegrationRefusedError("secret-over-plain-http", `${policy.name}: the secret ${quoted(placement.secret)} may not go over plain http to another machine (${url.host})`);
71
29
  }
72
30
  }
73
31
  /** The sheet's entry for a secret, looked up by own key only, so `toString` is never a secret. */
@@ -82,7 +40,7 @@ function declaredSecret(policy, secret) {
82
40
  * form field names are case-sensitive. The placement comes from the
83
41
  * integration's code, so nothing in it is trusted to have the right type.
84
42
  */
85
- function matchesSheet(placement, sheet) {
43
+ export function matchesSheet(placement, sheet) {
86
44
  switch (sheet.as) {
87
45
  case "bearer":
88
46
  return placement.as === "bearer";
@@ -103,7 +61,7 @@ function asciiLowerCase(text) {
103
61
  return text.replace(/[A-Z]/g, (letter) => letter.toLowerCase());
104
62
  }
105
63
  /** A placement in words, for a refusal message. It never holds a value. */
106
- function describe(placement) {
64
+ export function describe(placement) {
107
65
  switch (placement.as) {
108
66
  case "bearer":
109
67
  return "as a bearer token";
@@ -124,7 +82,7 @@ function describe(placement) {
124
82
  * the message. It is cut by characters before quoting, so the cut never
125
83
  * falls inside an escape or a surrogate pair.
126
84
  */
127
- function quoted(text) {
85
+ export function quoted(text) {
128
86
  const characters = Array.from(String(text));
129
87
  const shown = characters.length <= 50 ? characters.join("") : `${characters.slice(0, 50).join("")}…`;
130
88
  return JSON.stringify(shown);
@@ -139,7 +97,7 @@ function mayCarrySecret(url) {
139
97
  */
140
98
  export async function readSecrets(policy, secrets, placements) {
141
99
  const values = new Map();
142
- for (const { secret } of placements) {
100
+ for (const { secret } of placements.filter((placement) => "secret" in placement)) {
143
101
  if (values.has(secret))
144
102
  continue;
145
103
  const value = await secrets.get(secret);
@@ -149,42 +107,54 @@ export async function readSecrets(policy, secrets, placements) {
149
107
  continue;
150
108
  throw new IntegrationRefusedError("secret-not-configured", `${policy.name}: the secret ${quoted(secret)} is not configured`);
151
109
  }
110
+ checkSecretLength(policy.name, secret, value);
152
111
  values.set(secret, value);
153
112
  }
154
113
  return values;
155
114
  }
156
115
  /**
157
- * Puts the secrets allowed for this hop's host into the headers and the form
158
- * body. A placement whose secret may not go to this hop is left out, which
159
- * happens after a redirect to another declared host.
116
+ * The fewest UTF-8 bytes a secret value may have. The filter replaces every
117
+ * copy of a value in what the integration sees, so a shorter one would
118
+ * remove ordinary text, and where it was removed would give the value away.
119
+ */
120
+ export const minSecretBytes = 8;
121
+ /**
122
+ * @throws {IntegrationRefusedError} `secret-too-short` when `value` has fewer than {@link minSecretBytes} bytes
123
+ */
124
+ export function checkSecretLength(integration, secret, value) {
125
+ if (new TextEncoder().encode(value).length >= minSecretBytes)
126
+ return;
127
+ throw new IntegrationRefusedError("secret-too-short", `${integration}: the secret ${quoted(secret)} is shorter than ${minSecretBytes} bytes, too short to keep out of what the integration sees`);
128
+ }
129
+ /**
130
+ * Puts one secret into the headers or the form body, when it may go to this
131
+ * hop's host. A secret that may not go to this hop is left out, which happens
132
+ * after a redirect to another declared host.
160
133
  *
161
- * @returns the form body to send, when a placement wrote into it
134
+ * @returns the form body to send, which a `form` placement writes into
162
135
  */
163
- export function place(policy, placements, values, url, headers, form) {
164
- let body = form;
165
- for (const placement of placements) {
166
- const declared = policy.secrets[placement.secret];
167
- const value = values.get(placement.secret);
168
- if (declared === undefined ||
169
- value === undefined ||
170
- !hostIn(declared.hosts, url) ||
171
- !mayCarrySecret(url))
172
- continue;
173
- switch (placement.as) {
174
- case "bearer":
175
- headers.set("authorization", `Bearer ${value}`);
176
- break;
177
- case "header":
178
- headers.set(placement.header, `${placement.prefix ?? ""}${value}`);
179
- break;
180
- case "form":
181
- if (body === undefined) {
182
- throw new IntegrationRefusedError("secret-needs-form-body", `${policy.name}: the secret ${quoted(placement.secret)} goes in a form field, so the body must be a form`);
183
- }
184
- body = new URLSearchParams(body);
185
- body.set(placement.field, value);
186
- break;
136
+ export function placeSecret(policy, placement, value, url, headers, form) {
137
+ const declared = policy.secrets[placement.secret];
138
+ if (declared === undefined ||
139
+ value === undefined ||
140
+ !hostIn(declared.hosts, url) ||
141
+ !mayCarrySecret(url)) {
142
+ return form;
143
+ }
144
+ switch (placement.as) {
145
+ case "bearer":
146
+ headers.set("authorization", `Bearer ${value}`);
147
+ return form;
148
+ case "header":
149
+ headers.set(placement.header, `${placement.prefix ?? ""}${value}`);
150
+ return form;
151
+ case "form": {
152
+ if (form === undefined) {
153
+ throw new IntegrationRefusedError("secret-needs-form-body", `${policy.name}: the secret ${quoted(placement.secret)} goes in a form field, so the body must be a form`);
154
+ }
155
+ const written = new URLSearchParams(form);
156
+ written.set(placement.field, value);
157
+ return written;
187
158
  }
188
159
  }
189
- return body;
190
160
  }