@runeditrun/rer 0.2.76 → 0.2.78

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 (191) hide show
  1. package/README.md +32 -0
  2. package/cli/README.md +55 -0
  3. package/cli/contract.mjs +1 -1
  4. package/cli/deploy-command.mjs +4 -2
  5. package/cli/deploy-inputs.mjs +15 -3
  6. package/cli/deploy-installation-key.mjs +71 -0
  7. package/cli/deploy-secrets.mjs +2 -0
  8. package/cli/deploy.mjs +13 -2
  9. package/cli/deployment-record.mjs +2 -0
  10. package/cli/export.mjs +1 -1
  11. package/cli/main.mjs +1 -1
  12. package/cli/parse.mjs +3 -2
  13. package/cli/portability-migrations.mjs +35 -14
  14. package/cli/portability-worker.mjs +2 -2
  15. package/cli/portability.mjs +2 -2
  16. package/cli/runtime-front-door.mjs +1 -1
  17. package/global/CHANGELOG.md +14 -0
  18. package/global/README.md +32 -0
  19. package/modules/dependencies/README.md +71 -1
  20. package/modules/dependencies/auth.mjs +4 -4
  21. package/modules/dependencies/crypto.mjs +6 -1
  22. package/modules/dependencies/custody.mjs +16 -0
  23. package/modules/dependencies/index.d.mts +1 -1
  24. package/modules/dependencies/index.mjs +6 -5
  25. package/modules/dependencies/schema/keys-sealed.sql +4 -0
  26. package/modules/dependencies/sealing.mjs +55 -0
  27. package/modules/dependencies/store.mjs +62 -9
  28. package/modules/deploy-kit/README.md +1 -0
  29. package/modules/deploy-kit/owner-cli.mjs +19 -7
  30. package/modules/deploy-kit/owner-rekey.mjs +69 -0
  31. package/modules/deploy-kit/provider.mjs +9 -4
  32. package/modules/import-kit/src/d1/README.md +1322 -0
  33. package/modules/import-kit/src/d1/apply.ts +115 -0
  34. package/modules/import-kit/src/d1/budget.ts +89 -0
  35. package/modules/import-kit/src/d1/compressed-plan.ts +63 -0
  36. package/modules/import-kit/src/d1/creation.ts +95 -0
  37. package/modules/import-kit/src/d1/custody-fence.ts +27 -0
  38. package/modules/import-kit/src/d1/custody-state-schema.ts +67 -0
  39. package/modules/import-kit/src/d1/database.ts +163 -0
  40. package/modules/import-kit/src/d1/entry-locator.ts +61 -0
  41. package/modules/import-kit/src/d1/erased-artifacts.ts +38 -0
  42. package/modules/import-kit/src/d1/erased-custody.ts +36 -0
  43. package/modules/import-kit/src/d1/erased-plan.ts +47 -0
  44. package/modules/import-kit/src/d1/erased-reversal.ts +19 -0
  45. package/modules/import-kit/src/d1/erasure-bounded.ts +22 -0
  46. package/modules/import-kit/src/d1/erasure-claims.ts +27 -0
  47. package/modules/import-kit/src/d1/erasure-compiler.ts +67 -0
  48. package/modules/import-kit/src/d1/erasure-disposition.ts +16 -0
  49. package/modules/import-kit/src/d1/erasure-fragments.ts +86 -0
  50. package/modules/import-kit/src/d1/erasure-index.ts +53 -0
  51. package/modules/import-kit/src/d1/erasure-pages.ts +62 -0
  52. package/modules/import-kit/src/d1/erasure-progress-schema.ts +8 -0
  53. package/modules/import-kit/src/d1/erasure-progress.ts +61 -0
  54. package/modules/import-kit/src/d1/erasure-related.ts +124 -0
  55. package/modules/import-kit/src/d1/erasure-report.ts +31 -0
  56. package/modules/import-kit/src/d1/erasure-schema.ts +24 -0
  57. package/modules/import-kit/src/d1/erasure-selection.ts +77 -0
  58. package/modules/import-kit/src/d1/erasure-snapshot.ts +15 -0
  59. package/modules/import-kit/src/d1/erasure-stage.ts +146 -0
  60. package/modules/import-kit/src/d1/erasure-step.ts +12 -0
  61. package/modules/import-kit/src/d1/erasure-superseded.ts +98 -0
  62. package/modules/import-kit/src/d1/erasure-values.ts +83 -0
  63. package/modules/import-kit/src/d1/erasure-writes.ts +46 -0
  64. package/modules/import-kit/src/d1/erasure.ts +170 -0
  65. package/modules/import-kit/src/d1/evolutions/0001-preserved-undo.ts +13 -0
  66. package/modules/import-kit/src/d1/expected-state.ts +91 -0
  67. package/modules/import-kit/src/d1/field-redaction.ts +86 -0
  68. package/modules/import-kit/src/d1/guards.ts +37 -0
  69. package/modules/import-kit/src/d1/history.ts +71 -0
  70. package/modules/import-kit/src/d1/index.ts +88 -0
  71. package/modules/import-kit/src/d1/integrity.ts +127 -0
  72. package/modules/import-kit/src/d1/inventory-snapshot.ts +84 -0
  73. package/modules/import-kit/src/d1/jobs.ts +97 -0
  74. package/modules/import-kit/src/d1/native-set.ts +48 -0
  75. package/modules/import-kit/src/d1/native-staging-admission.ts +19 -0
  76. package/modules/import-kit/src/d1/native-staging-context.ts +44 -0
  77. package/modules/import-kit/src/d1/native-staging-lifecycle.ts +33 -0
  78. package/modules/import-kit/src/d1/native-staging-offset.ts +74 -0
  79. package/modules/import-kit/src/d1/native-staging-publication.ts +18 -0
  80. package/modules/import-kit/src/d1/native-staging-rows.ts +72 -0
  81. package/modules/import-kit/src/d1/native-staging-schema.ts +28 -0
  82. package/modules/import-kit/src/d1/native-staging-state.ts +22 -0
  83. package/modules/import-kit/src/d1/native-staging.ts +64 -0
  84. package/modules/import-kit/src/d1/original-copies.ts +53 -0
  85. package/modules/import-kit/src/d1/original-storage.ts +56 -0
  86. package/modules/import-kit/src/d1/originals.ts +74 -0
  87. package/modules/import-kit/src/d1/plan-dictionary.ts +80 -0
  88. package/modules/import-kit/src/d1/plans.ts +121 -0
  89. package/modules/import-kit/src/d1/preparation-fingerprint.ts +57 -0
  90. package/modules/import-kit/src/d1/preparation-header.ts +33 -0
  91. package/modules/import-kit/src/d1/preparation-reader.ts +98 -0
  92. package/modules/import-kit/src/d1/preparation-schema.ts +16 -0
  93. package/modules/import-kit/src/d1/preparation-state.ts +56 -0
  94. package/modules/import-kit/src/d1/preparation-step-combine.ts +80 -0
  95. package/modules/import-kit/src/d1/preparation-step-control.ts +48 -0
  96. package/modules/import-kit/src/d1/preparation-step-hash.ts +131 -0
  97. package/modules/import-kit/src/d1/preparation-step-pages.ts +77 -0
  98. package/modules/import-kit/src/d1/preparation-steps-schema.ts +11 -0
  99. package/modules/import-kit/src/d1/preparation-steps.ts +102 -0
  100. package/modules/import-kit/src/d1/preparations.ts +87 -0
  101. package/modules/import-kit/src/d1/preserved-preview.ts +89 -0
  102. package/modules/import-kit/src/d1/preserved-reverse.ts +91 -0
  103. package/modules/import-kit/src/d1/provenance-set.ts +87 -0
  104. package/modules/import-kit/src/d1/publication-fence.ts +22 -0
  105. package/modules/import-kit/src/d1/publication-receipt-schema.ts +15 -0
  106. package/modules/import-kit/src/d1/published-stream.ts +11 -0
  107. package/modules/import-kit/src/d1/read-fence.ts +64 -0
  108. package/modules/import-kit/src/d1/read-results.ts +16 -0
  109. package/modules/import-kit/src/d1/redacted-custody.ts +76 -0
  110. package/modules/import-kit/src/d1/redaction-step.ts +125 -0
  111. package/modules/import-kit/src/d1/referenced-plans.ts +136 -0
  112. package/modules/import-kit/src/d1/retained-values.ts +35 -0
  113. package/modules/import-kit/src/d1/retirement-bounded.ts +71 -0
  114. package/modules/import-kit/src/d1/retirement-door.ts +105 -0
  115. package/modules/import-kit/src/d1/retirement-schema.ts +39 -0
  116. package/modules/import-kit/src/d1/retirement-scope.ts +94 -0
  117. package/modules/import-kit/src/d1/retirement-steps.ts +76 -0
  118. package/modules/import-kit/src/d1/retirement-tally.ts +74 -0
  119. package/modules/import-kit/src/d1/retirement.ts +136 -0
  120. package/modules/import-kit/src/d1/reversal-conversion-record.ts +19 -0
  121. package/modules/import-kit/src/d1/reversal-conversion-schema.ts +11 -0
  122. package/modules/import-kit/src/d1/reversal-conversion.ts +40 -0
  123. package/modules/import-kit/src/d1/reversal-fence-schema.ts +14 -0
  124. package/modules/import-kit/src/d1/reversal-fence.ts +106 -0
  125. package/modules/import-kit/src/d1/reversal-journal.ts +24 -0
  126. package/modules/import-kit/src/d1/reverse.ts +103 -0
  127. package/modules/import-kit/src/d1/reviewed-source.ts +23 -0
  128. package/modules/import-kit/src/d1/sets.ts +78 -0
  129. package/modules/import-kit/src/d1/staged-creation.ts +201 -0
  130. package/modules/import-kit/src/d1/staging-admission.ts +45 -0
  131. package/modules/import-kit/src/d1/staging-control.ts +43 -0
  132. package/modules/import-kit/src/d1/staging-erasure.ts +28 -0
  133. package/modules/import-kit/src/d1/staging-lifecycle.ts +49 -0
  134. package/modules/import-kit/src/d1/staging-plan.ts +15 -0
  135. package/modules/import-kit/src/d1/staging-privacy.ts +22 -0
  136. package/modules/import-kit/src/d1/staging-publication.ts +79 -0
  137. package/modules/import-kit/src/d1/staging-schema.ts +37 -0
  138. package/modules/import-kit/src/d1/staging-statements.ts +8 -0
  139. package/modules/import-kit/src/d1/staging-transport.ts +70 -0
  140. package/modules/import-kit/src/d1/stream-boundary.ts +11 -0
  141. package/modules/import-kit/src/d1/stream-create.ts +55 -0
  142. package/modules/import-kit/src/d1/stream-identity-admission.ts +37 -0
  143. package/modules/import-kit/src/d1/stream-input.ts +18 -0
  144. package/modules/import-kit/src/d1/stream-layout.ts +43 -0
  145. package/modules/import-kit/src/d1/stream-origin.ts +44 -0
  146. package/modules/import-kit/src/d1/stream-page-integrity.ts +18 -0
  147. package/modules/import-kit/src/d1/stream-partition.ts +50 -0
  148. package/modules/import-kit/src/d1/stream-plan.ts +133 -0
  149. package/modules/import-kit/src/d1/stream-receipt.ts +45 -0
  150. package/modules/import-kit/src/d1/stream-revision-schema.ts +35 -0
  151. package/modules/import-kit/src/d1/stream-snapshot.ts +26 -0
  152. package/modules/import-kit/src/d1/stream-stage.ts +79 -0
  153. package/modules/import-kit/src/d1/stream-state.ts +25 -0
  154. package/modules/import-kit/src/d1/stream-working-set.ts +30 -0
  155. package/modules/import-kit/src/d1/streams.ts +39 -0
  156. package/modules/import-kit/src/d1/target-guards.ts +9 -0
  157. package/modules/import-kit/src/d1/target-identities.ts +17 -0
  158. package/modules/import-kit/src/d1/transport.ts +10 -0
  159. package/modules/import-kit/src/d1/types.ts +162 -0
  160. package/modules/import-kit/src/d1/undo-observations.ts +39 -0
  161. package/modules/import-kit/src/d1/undo-report.ts +22 -0
  162. package/modules/import-kit/src/d1/undo-snapshot.ts +87 -0
  163. package/modules/import-kit/src/d1/undo-state.ts +84 -0
  164. package/modules/import-kit/src/d1/undo-types.ts +110 -0
  165. package/modules/import-kit/src/journal/authority.ts +12 -0
  166. package/modules/import-kit/src/plan/budget.ts +60 -0
  167. package/modules/import-kit/src/plan/index.ts +123 -0
  168. package/modules/import-kit/src/plan/size.ts +122 -0
  169. package/modules/import-kit/src/provenance/canonical-hash.ts +130 -0
  170. package/modules/import-kit/src/provenance/canonical-stream.ts +85 -0
  171. package/modules/import-kit/src/provenance/credential-names.ts +131 -0
  172. package/modules/import-kit/src/provenance/credentials.ts +317 -0
  173. package/modules/import-kit/src/provenance/identity.ts +51 -0
  174. package/modules/import-kit/src/provenance/json-snapshot.ts +21 -0
  175. package/modules/import-kit/src/provenance/resumable-sha256.ts +75 -0
  176. package/modules/import-kit/src/provenance/validate-json.ts +19 -0
  177. package/modules/import-kit/src/storage/originals.ts +87 -0
  178. package/modules/import-kit/src/storage/provenance.ts +64 -0
  179. package/modules/import-kit/src/storage/schema.ts +42 -0
  180. package/modules/import-kit/src/types.ts +47 -0
  181. package/modules/operator-auth/docs/native-accounts.md +4 -1
  182. package/modules/operator-auth/identity/oidc/aes-gcm.mjs +21 -0
  183. package/modules/operator-auth/identity/oidc/crypto.mjs +5 -12
  184. package/modules/operator-auth/index.d.mts +1 -1
  185. package/modules/portability/README.md +98 -2
  186. package/modules/portability/backfills.mjs +42 -15
  187. package/modules/portability/ledgers.mjs +7 -1
  188. package/modules/portability/transient-tables.mjs +40 -0
  189. package/onboarding/local/environment.mjs +2 -2
  190. package/package.json +3 -1
  191. package/scripts/publish-check/reviewed.py +3 -1
@@ -0,0 +1,85 @@
1
+ import { Buffer } from 'node:buffer';
2
+ import { jsonStringParts } from './identity.ts';
3
+ export { canonicalHash } from './canonical-hash.ts';
4
+
5
+ function* segments(value: unknown): Generator<string> {
6
+ if (typeof value === 'string') { yield* jsonStringParts(value); return; }
7
+ if (value === null || typeof value === 'boolean' || (typeof value === 'number' && Number.isFinite(value))) { yield JSON.stringify(value); return; }
8
+ if (Array.isArray(value)) {
9
+ yield '[';
10
+ const length = value.length;
11
+ for (let i = 0; i < length; i++) { if (i) yield ','; yield* segments(value[i]); }
12
+ yield ']'; return;
13
+ }
14
+ if (typeof value === 'object' && value && (Object.getPrototypeOf(value) === Object.prototype || Object.getPrototypeOf(value) === null)) {
15
+ yield '{'; let first = true;
16
+ for (const key of Object.keys(value).sort()) { if (!first) yield ','; first = false; yield* jsonStringParts(key); yield ':'; yield* segments((value as Record<string, unknown>)[key]); }
17
+ yield '}'; return;
18
+ }
19
+ throw new Error('INVALID_JSON: durable values must be finite JSON');
20
+ }
21
+
22
+ /** Exact canonical JSON without constructing a second whole-plan string.
23
+ * Storage chunks are bounded by UTF-8 bytes and never split a code point. */
24
+ export function* canonicalChunks(value: unknown, limit = 128 * 1024): Generator<string> {
25
+ let parts: string[] = [], bytes = 0;
26
+ for (const segment of segments(value)) {
27
+ const size = Buffer.byteLength(segment);
28
+ if (size <= limit) {
29
+ if (bytes + size > limit) { yield parts.join(''); parts = []; bytes = 0; }
30
+ parts.push(segment); bytes += size;
31
+ } else {
32
+ const encoded = Buffer.from(segment, 'utf8');
33
+ for (let start = 0; start < encoded.length;) {
34
+ if (bytes) { yield parts.join(''); parts = []; bytes = 0; }
35
+ let end = Math.min(start + limit, encoded.length);
36
+ while (end < encoded.length && (encoded[end] & 0xc0) === 0x80) end--;
37
+ const part = encoded.toString('utf8', start, end); start = end;
38
+ if (end < encoded.length) yield part; else { parts.push(part); bytes = Buffer.byteLength(part); }
39
+ }
40
+ }
41
+ }
42
+ if (bytes) yield parts.join('');
43
+ }
44
+
45
+ /** Copy validated JSON without materializing a second whole-document string.
46
+ * Immutable string values stay shared; mutable containers remain independent. */
47
+ export function cloneJson<T>(value: T): T {
48
+ if (value === null || typeof value === 'string' || typeof value === 'boolean' || (typeof value === 'number' && Number.isFinite(value))) return (Object.is(value, -0) ? 0 : value) as T;
49
+ if (Array.isArray(value)) return Array.from({ length: value.length }, (_, index) => cloneJson(value[index])) as T;
50
+ if (typeof value === 'object' && value && (Object.getPrototypeOf(value) === Object.prototype || Object.getPrototypeOf(value) === null)) {
51
+ const result: Record<string, unknown> = {};
52
+ for (const key of Object.keys(value).sort()) Object.defineProperty(result, key, {value:cloneJson((value as any)[key]),writable:true,enumerable:true,configurable:true});
53
+ return result as T;
54
+ }
55
+ throw new Error('INVALID_JSON: durable values must be finite JSON');
56
+ }
57
+
58
+ /** Exact encoded size without constructing the encoded document. */
59
+ export function canonicalSize(value: unknown): number {
60
+ const strings = new Map<string, number>();
61
+ const textSize = (text: string) => {
62
+ const previous = strings.get(text); if (previous !== undefined) return previous;
63
+ let size = 0;
64
+ for (const part of jsonStringParts(text)) size += Buffer.byteLength(part);
65
+ if (strings.size < 4096) strings.set(text, size);
66
+ return size;
67
+ };
68
+ const sizeOf = (item: unknown): number => {
69
+ if (item === null) return 4;
70
+ if (typeof item === 'string') return textSize(item);
71
+ if (typeof item === 'boolean') return item ? 4 : 5;
72
+ if (typeof item === 'number' && Number.isFinite(item)) return JSON.stringify(item).length;
73
+ if (Array.isArray(item)) { const length = item.length; let size = 2 + Math.max(0, length - 1); for (let index = 0; index < length; index++) size += sizeOf(item[index]); return size; }
74
+ if (item && typeof item === 'object') {
75
+ const prototype = Object.getPrototypeOf(item);
76
+ if (prototype === Object.prototype || prototype === null) {
77
+ const keys = Object.keys(item); let size = 2 + Math.max(0, keys.length - 1);
78
+ for (const key of keys) size += textSize(key) + 1 + sizeOf((item as any)[key]);
79
+ return size;
80
+ }
81
+ }
82
+ throw new Error('INVALID_JSON: durable values must be finite JSON');
83
+ };
84
+ return sizeOf(value);
85
+ }
@@ -0,0 +1,131 @@
1
+ /** Whether a name (an object key, a header name, the name in a `[name, value]`
2
+ * pair, a URL query or fragment key) names a credential, by its whole parts.
3
+ *
4
+ * A name is split into lower-case parts at camelCase and PascalCase boundaries
5
+ * (`apiKey`, `XAPIKey`), at `_`, `-`, `.`, spaces and any other non-letter, and
6
+ * at digits (`token2` is `token`). A name is a credential when its parts are a
7
+ * credential word or phrase, never because a part only contains one: `keyword`,
8
+ * `monkey`, `author`, `signal`, `design` and `tokenizer` hold the letters of a
9
+ * credential word and are not credentials. */
10
+ export interface CredentialName {
11
+ /** The name's letters, lower-cased: `apiKey` and `api_key` are both `apikey`. */
12
+ name: string;
13
+ /** The word's plural (`tokens`, `apiKeys`): it holds credentials only as text, so a count stays. */
14
+ plural: boolean;
15
+ /** `auth`, `X-Auth` or `credential(s)`: its value is a credential when it is
16
+ * text that names no scheme or mode (`include`, `oauth2`), or a list with such
17
+ * a member; an object under it is searched field by field instead. */
18
+ container: boolean;
19
+ }
20
+ /** `refuse`: the whole name is a credential phrase, so a pagination cursor such as
21
+ * `next_page_token` or a count such as `input_tokens` is not one. `remove`: the
22
+ * name ends in a credential word (`pushToken`, `webhook_url`), the wider net of
23
+ * the import door, which drops the value and keeps the record. `query`: a URL
24
+ * query or fragment key, as `remove` plus a bare `key` and `X-Amz-Signature`. */
25
+ export type CredentialNameMode = 'refuse' | 'remove' | 'query';
26
+ /** Where a name sits in `refuse` or `remove` mode: a record `field`, or an HTTP
27
+ * `header` name (a `[name, value]` tuple, a key of a `headers` object, a
28
+ * `{name|key, value}` pair in a header list). `sig`, `signature` and `sessionId` are credentials only as
29
+ * header names and URL query or fragment keys, never as record fields, so an
30
+ * agent's `signature` or a chat's `session_id` imports. */
31
+ export type CredentialNamePlace = 'field' | 'header';
32
+
33
+ const WORDS = new Set(['token', 'secret', 'password', 'passwd', 'pwd', 'cookie', 'authorization', 'bearer', 'credential']);
34
+ /** Words that make a trailing `key` a credential: `api key`, `secret key`, `Ocp-Apim-Subscription-Key`. A bare `key` or `primaryKey` is not. */
35
+ const KEY_QUALIFIERS = new Set(['api', 'access', 'private', 'secret', 'auth', 'client', 'integration', 'routing', 'service', 'signing', 'session',
36
+ 'refresh', 'subscription', 'functions', 'consumer', 'app', 'license', 'master', 'encryption', 'admin']);
37
+ /** URL query and fragment keys only: `?application_key=` (Datadog), `?user_key=` (3scale), `?account_key=`, `?deploy_key=`, `?shared_key=`, and `?token_auth=` (Matomo), `?basic_auth=`. */
38
+ const QUERY_KEY_QUALIFIERS = new Set(['application', 'user', 'account', 'deploy', 'shared']);
39
+ const QUERY_AUTH_QUALIFIERS = new Set(['token', 'basic', 'http', 'proxy', 'api']);
40
+ const TOKEN_QUALIFIERS = ['access', 'refresh', 'auth', 'api', 'bearer', 'id', 'session', 'csrf', 'xsrf', 'bot', 'push', 'oauth', 'jwt'];
41
+ const SECRET_QUALIFIERS = ['client', 'app', 'api', 'oauth', 'webhook', 'signing', 'shared'];
42
+ const PASSWORD_QUALIFIERS = ['http', 'db', 'ftp', 'smtp', 'admin', 'user', 'basic', 'proxy', 'root', 'mail', 'login'];
43
+ /** One-word credentials however a name splits them: a `passphrase` (`passPhrase`), a `jwt`, and Java's and PHP's session
44
+ * cookies (`JSESSIONID`, `jsessionId`, `PHPSESSID`). They name nothing else, so they count as fields, header names and URL keys. */
45
+ const SPELLED = /^(?:passphrase|jwt|jsessionid|phpsessid)(s?)$/;
46
+ /** What may stand before one in `refuse` mode (`ssh_passphrase`, `id_jwt`). */
47
+ const SPELLED_QUALIFIERS = new Set([...PASSWORD_QUALIFIERS, ...TOKEN_QUALIFIERS, 'ssh', 'key', 'gpg', 'pgp', 'private']);
48
+ /** What may stand before a credential word in `refuse` mode, which names the whole phrase (`access token`, `http password`). */
49
+ const QUALIFIERS: Record<string, ReadonlySet<string>> = {
50
+ token: new Set(TOKEN_QUALIFIERS), secret: new Set(SECRET_QUALIFIERS),
51
+ password: new Set(PASSWORD_QUALIFIERS), passwd: new Set(PASSWORD_QUALIFIERS), pwd: new Set(PASSWORD_QUALIFIERS),
52
+ key: KEY_QUALIFIERS,
53
+ };
54
+ /** The same phrases written as one lower-case part (`apikey`, `accesstoken`, `clientsecret`, `httppassword`, `webhookurl`). */
55
+ const COMPOUND = new RegExp('^x?(?:'
56
+ + `(?:${[...KEY_QUALIFIERS].join('|')})(?:key)|(?:${TOKEN_QUALIFIERS.join('|')})(?:token)|(?:${SECRET_QUALIFIERS.join('|')})(?:secret)`
57
+ + `|(?:${PASSWORD_QUALIFIERS.join('|')})(?:password|passwd|pwd)|webhook(?:url)|cal(?:url))(s?)$`);
58
+ /** A scheme or mode an `auth` or `credentials` field names, never a credential:
59
+ * fetch's `include`, `same-origin` and `omit`, HTTP and API authentication schemes, and boolean
60
+ * words (Keycloak's `smtpServer.auth: "true"`). */
61
+ const SCHEME = /^(?:omit|sameorigin|include|none|noauth|inherit|anonymous|basic|bearer|digest|negotiate|ntlm|kerberos|oauth(?:1|2|10|20|1a)?|apikey|jwt|hawk|hmac|aws|awsv4|sigv4|awssigv4|edgegrid|mutual|vapid|token|password|cookie|session|header|query|true|false|yes|no|on|off|enabled|disabled)$/;
62
+
63
+ /** Whether text under an `auth` or `credentials` name names a scheme or mode (`include`, `oauth2`, `Bearer`, `same-origin`). */
64
+ export function credentialScheme(text: string): boolean {
65
+ return SCHEME.test(text.trim().toLowerCase().replace(/[\s_.-]/g, ''));
66
+ }
67
+ /** Whether an object under this key holds HTTP headers by name (`headers`, `customHttpHeaders`, `header`). */
68
+ export function headerContainer(key: string): boolean {
69
+ const last = nameParts(key).at(-1);
70
+ return last === 'headers' || last === 'header';
71
+ }
72
+ /** Under `auth`, `X-Auth`, `credential` or `credentials`, a member holding the credential under a generic name: `value`
73
+ * (Keycloak's `{type: 'password', value}`, `credential: {type: 'apiKey', value}`), `pass` and `pw` (nodemailer's
74
+ * `{user, pass}`), or a name whose first part is `secret` (Keycloak's `secretData`). Its name's letters, or undefined. */
75
+ export function credentialMember(name: string): string | undefined {
76
+ const parts = nameParts(name);
77
+ return (parts.length === 1 && /^(?:value|pass|pw)$/.test(parts[0])) || parts[0] === 'secret' ? parts.join('') : undefined;
78
+ }
79
+
80
+ /** A name's lower-case parts, without digits. Format characters and accents are folded away first. */
81
+ export function nameParts(name: string): string[] {
82
+ const text = name.normalize('NFKC').replace(/[\p{Cf}\p{M}]/gu, '');
83
+ return (text.match(/\p{Lu}+(?!\p{Ll})|\p{Lu}?\p{Ll}+|\p{Lo}+/gu) ?? []).map(part => part.toLowerCase());
84
+ }
85
+
86
+ export function credentialName(name: string, mode: CredentialNameMode, place: CredentialNamePlace = 'field'): CredentialName | undefined {
87
+ const parts = nameParts(name), last = parts.at(-1), before = parts.at(-2);
88
+ if (last === undefined) return undefined;
89
+ const letters = name.normalize('NFKC').toLowerCase().replace(/[^a-z]/g, '').slice(-64);
90
+ const compound = COMPOUND.exec(last);
91
+ if (compound) return { name: letters, plural: compound[1] === 's', container: false };
92
+ // A header's leading `x-` (`x-api-key`, `X-Auth-Token`) is not part of the phrase; `refuse` mode takes the rest whole.
93
+ const phrase = parts.length > 1 && parts[0] === 'x' ? parts.slice(1) : parts;
94
+ const found = (plural: boolean, container = false) => ({ name: letters, plural, container });
95
+ const qualifier = (word: string | undefined) => word !== undefined && (KEY_QUALIFIERS.has(word) || (word.startsWith('x') && KEY_QUALIFIERS.has(word.slice(1))));
96
+ const strict = mode === 'refuse';
97
+ // A URL key or a header name: where `sig`, `signature` and `sessionId` are credentials.
98
+ const header = mode === 'query' || place === 'header';
99
+ for (let size = 1; size <= Math.min(3, phrase.length); size++) {
100
+ const spelled = SPELLED.exec(phrase.slice(-size).join(''));
101
+ if (spelled && (!strict || phrase.length === size || (phrase.length === size + 1 && SPELLED_QUALIFIERS.has(phrase[0])))) return found(spelled[1] === 's');
102
+ }
103
+ // `credentials` holds a credential object as one value; the other plurals hold a list or map of text.
104
+ const word = last === 'credentials' ? 'credential' : WORDS.has(last) ? last : last.endsWith('s') && WORDS.has(last.slice(0, -1)) ? last.slice(0, -1) : undefined;
105
+ if (word !== undefined) {
106
+ const plural = word !== last && last !== 'credentials';
107
+ // `refuse` mode: the word alone or after one qualifier (`token`, `access_token`), and `pwd` after anything.
108
+ const named = phrase.length === 1 || (phrase.length === 2 && QUALIFIERS[word]?.has(phrase[0]) === true) || word === 'pwd';
109
+ if (!strict || named) return found(plural, word === 'credential' && mode !== 'query');
110
+ }
111
+ if (last === 'key' || last === 'keys') {
112
+ // `refuse` mode: `api_key`, `x-functions-key`, and `secret`/`subscription`/`functions` before the key under any name.
113
+ const named = phrase.length === 2 || (before !== undefined && ['secret', 'subscription', 'functions'].includes(before));
114
+ if (qualifier(before) && (!strict || named)) return found(last === 'keys');
115
+ // A bare `key` is a credential only as a URL query or fragment key (the `?key=` convention).
116
+ if (mode === 'query' && parts.length === 1 && last === 'key') return found(false);
117
+ if (mode === 'query' && before !== undefined && QUERY_KEY_QUALIFIERS.has(before)) return found(last === 'keys');
118
+ }
119
+ // A session id is a credential as a header or URL key (`Session-Id`, `?sessionid=`), never as a record's field (a chat's `session_id`).
120
+ const session = /^x?sessionid(s?)$/.exec(last);
121
+ if (header && session) return found(session[1] === 's');
122
+ if (header && last === 'id' && before === 'session' && (!strict || phrase.length === 2)) return found(false);
123
+ if (!strict && last === 'url' && (before === 'webhook' || before === 'cal')) return found(false);
124
+ // A signature is a credential as a whole header name (`Signature`) and, as a query key, as its last part (an Azure SAS `sig`, `X-Amz-Signature`); an agent's `signature` field is not.
125
+ if ((last === 'sig' || last === 'signature') && (mode === 'query' || (place === 'header' && parts.length === 1))) return found(false);
126
+ if (last === 'auth' && (parts.length === 1 || (parts.length === 2 && before === 'x'))) return found(false, mode !== 'query');
127
+ if (mode === 'query' && last === 'auth' && before !== undefined && QUERY_AUTH_QUALIFIERS.has(before)) return found(false);
128
+ // A query key written as one part that ends in an unambiguous phrase: HubSpot's `?hapikey=`.
129
+ if (mode === 'query' && parts.length === 1 && /(?:apikey|accesstoken|authtoken)$/.test(last)) return found(false);
130
+ return undefined;
131
+ }
@@ -0,0 +1,317 @@
1
+ import type { ImportPlan, Json, PaginationCursorPolicy } from '../types.ts';
2
+ import { credentialMember, credentialName, credentialScheme, headerContainer, type CredentialNamePlace } from './credential-names.ts';
3
+
4
+ const MAX_ENCODING_DEPTH = 32;
5
+
6
+ /** Trusted adapter selection, never inferred from response metadata. */
7
+ export function paginationPolicy(value: unknown): PaginationCursorPolicy | undefined {
8
+ if (value !== undefined && value !== 'page-token-v1') throw new Error('INVALID_PAGINATION_CURSOR_POLICY');
9
+ return value;
10
+ }
11
+
12
+ /** Inspect every encoding layer without losing malformed or Latin-1 bytes. */
13
+ function* credentialTextLayers(value: string): Generator<string> {
14
+ let decoded = value, depth = 0;
15
+ yield decoded;
16
+ while (/%[\da-f]{2}/i.test(decoded)) {
17
+ if (++depth > MAX_ENCODING_DEPTH) throw new Error('CREDENTIAL_ENCODING_DEPTH_LIMIT');
18
+ // Decode individual bytes so an unrelated malformed escape cannot hide a key
19
+ // or credential. UTF-8 interpretations are matched against known secrets below.
20
+ const next = decoded.replace(/%([\da-f]{2})/gi, (_, hex: string) => String.fromCharCode(parseInt(hex, 16)));
21
+ if (next === decoded) break;
22
+ decoded = next;
23
+ yield decoded;
24
+ }
25
+ }
26
+
27
+ /** Decode nested percent escapes for inspection only; never rewrite retained bytes or requests. */
28
+ export function decodedCredentialText(value: string): string {
29
+ let decoded = value;
30
+ for (const layer of credentialTextLayers(value)) decoded = layer;
31
+ return decoded;
32
+ }
33
+
34
+ /** Match only provided credentials, in literal or encoded byte form at each layer. */
35
+ export function reflectedCredentialMatcher(secrets: readonly string[]): (value: string) => boolean {
36
+ if (!secrets.length) return () => false;
37
+ const escape = (text: string) => text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
38
+ const bytePattern = (byte: number) => {
39
+ const hex = byte.toString(16).padStart(2, '0').replace(/[a-f]/g, digit => `[${digit}${digit.toUpperCase()}]`);
40
+ return `(?:${escape(String.fromCharCode(byte))}|%(?:25){0,${MAX_ENCODING_DEPTH - 1}}${hex})`;
41
+ };
42
+ const encoder = new TextEncoder();
43
+ const patterns = secrets.map(secret => new RegExp([...secret].map(character => {
44
+ const codePoint = character.codePointAt(0)!;
45
+ const literal = codePoint <= 255 ? bytePattern(codePoint) : escape(character);
46
+ return codePoint < 128 ? literal : `(?:${literal}|${Array.from(encoder.encode(character), bytePattern).join('')})`;
47
+ }).join('')));
48
+ return value => {
49
+ // Per-byte alternatives preserve literal '%' in credentials while another part
50
+ // is escaped, and allow mixed literal/Latin-1/UTF-8 representations.
51
+ for (const layer of credentialTextLayers(value)) if (patterns.some(pattern => pattern.test(layer))) return true;
52
+ return false;
53
+ };
54
+ }
55
+
56
+ export function credentialQueryKey(rawKey: string, policy?: PaginationCursorPolicy): boolean {
57
+ // The sole exception is the exact wire spelling, not encoded/case aliases.
58
+ if (policy === 'page-token-v1' && rawKey === 'page_token') return false;
59
+ // A credential by its whole name parts (`api_key`, `access_token`, `sig`, `X-Amz-Signature`), never `keyword` or `author`.
60
+ return credentialName(decodedCredentialText(rawKey), 'query') !== undefined;
61
+ }
62
+
63
+ /** Inspection-only structure; opaque values are checked separately, never reparsed as outer query syntax. */
64
+ function credentialUrlStructure(value: string, policy?: PaginationCursorPolicy): string {
65
+ if (policy !== 'page-token-v1') return value;
66
+ const queryStart = value.indexOf('?'), fragmentStart = value.indexOf('#');
67
+ if (queryStart < 0 || (fragmentStart >= 0 && fragmentStart < queryStart)) return value;
68
+ const queryEnd = fragmentStart < 0 ? value.length : fragmentStart;
69
+ const pairs = value.slice(queryStart + 1, queryEnd).split('&');
70
+ // Split before decoding so only an exact wire key owns an opaque value.
71
+ const structure = pairs.filter(pair => pair.split('=')[0] !== 'page_token').join('&');
72
+ return value.slice(0, queryStart + 1) + structure + value.slice(queryEnd);
73
+ }
74
+
75
+ /** Where a credential sat: a named field (its normalised key), a URL's user
76
+ * name and password, a URL credential query parameter, or a value repeating one
77
+ * removed elsewhere. Never the value. */
78
+ export type CredentialField = { kind: 'field'; name: string } | { kind: 'url-userinfo' } | { kind: 'url-query' } | { kind: 'reflected' };
79
+ export class CredentialError extends Error {
80
+ readonly field: CredentialField;
81
+ constructor(code: 'CREDENTIAL_IN_DURABLE_DATA' | 'CREDENTIAL_IN_DURABLE_URL', field: CredentialField) {
82
+ super(code);
83
+ this.name = 'CredentialError';
84
+ this.field = field;
85
+ }
86
+ }
87
+ export function credentialFieldText(field: CredentialField): string {
88
+ return field.kind === 'field' ? `credential field "${field.name}"` : field.kind === 'url-userinfo' ? 'URL user name or password'
89
+ : field.kind === 'url-query' ? 'URL credential query parameter' : 'repeated credential value';
90
+ }
91
+ /** A credential-named field (`apiKey`, `client_secret`, `X-Api-Key`,
92
+ * `Ocp-Apim-Subscription-Key`, `X-Auth`, `pwd`, `passphrase`, `jwt`, `JSESSIONID`; in remove mode also `httpPassword`
93
+ * and `pushToken`; as a header name also `Signature` and `Session-Id`), by whole
94
+ * name parts, and whether a value under it counts: a plural name holds
95
+ * credentials only as text, so `tokens: 1500` stays a count; `auth` and
96
+ * `credentials` hold one only as text naming no scheme or mode (`include`,
97
+ * `oauth2`), their objects being searched field by field; and null, a boolean or
98
+ * a mask is no credential. Refuse mode takes the whole name as the phrase, so a
99
+ * pagination cursor such as `next_page_token` is accepted; remove mode, which
100
+ * keeps the record, takes any name ending in one. */
101
+ function credentialKey(key: string, mode: 'refuse' | 'remove', place: CredentialNamePlace = 'field'): { name: string; holds: (value: unknown) => boolean } | undefined {
102
+ const credential = credentialName(decodedCredentialText(key), mode, place);
103
+ return credential && { name: credential.name, holds: credential.container ? containerText : credential.plural ? credentialText : value => !emptyCredential(value) };
104
+ }
105
+ /** Under `auth` or `credentials`: text that is no placeholder and names no
106
+ * scheme or mode, or a list with such a member. `auth: {type: 'oauth2'}`,
107
+ * `credentials: []` and fetch's `credentials: 'include'` hold none; an object's
108
+ * own credential fields (`auth: {token: '…'}`) are found field by field. */
109
+ function containerText(value: unknown): boolean {
110
+ if (typeof value === 'string') return !credentialPlaceholder(value) && !credentialScheme(value);
111
+ return Array.isArray(value) && value.some(member => typeof member === 'string' && containerText(member));
112
+ }
113
+ /** A value holding credentials as text: a string that is no placeholder, or a
114
+ * list or map with one as a member (`['…']`, `{github: '…'}`). A list of objects
115
+ * is not: its members' own credential fields are found field by field. */
116
+ function credentialText(value: unknown): boolean {
117
+ if (typeof value === 'string') return !credentialPlaceholder(value);
118
+ return !!value && typeof value === 'object' && Object.values(value).some(child => typeof child === 'string' || Array.isArray(child) ? credentialText(child) : false);
119
+ }
120
+ /** The `[name, value]` pairs of a list holding only such pairs (Sentry's header
121
+ * form, `[['Authorization', 'Bearer …'], ['Accept', '*\/*']]`), by index. A lone
122
+ * two-item list such as `tags: ['password', 'reset']` is not one. */
123
+ function namedTuples(list: readonly unknown[]): Map<number, string> {
124
+ const tuples = new Map<number, string>();
125
+ if (!list.length || !list.every(item => Array.isArray(item) && item.length === 2 && typeof item[0] === 'string')) return tuples;
126
+ list.forEach((item, index) => tuples.set(index, (item as [string, unknown])[0]));
127
+ return tuples;
128
+ }
129
+ /** Null, an empty string or a mask/placeholder where a credential would go is not a credential. */
130
+ export function credentialPlaceholder(value: unknown): boolean {
131
+ return value === null || (typeof value === 'string'
132
+ && /^(?:\s*|[*\u2022\u25CF\u00B7x#_.-]{1,64}|[<[({]?\s*(?:redacted|hidden|masked|removed|omitted|none|null|n\/a|placeholder)\s*[>\])}]?)$/i.test(value));
133
+ }
134
+ /** What an exporter writes in place of a credential value it removed before upload. */
135
+ export const REMOVED_CREDENTIAL = '[removed]';
136
+ /** A value nothing can authenticate with: a placeholder, or a boolean flag such as `secret: true`. */
137
+ const emptyCredential = (value: unknown) => typeof value === 'boolean' || credentialPlaceholder(value);
138
+ /** Whether this key is `auth`, `X-Auth`, `credential` or `credentials`, whose members are searched for a credential under a generic name. */
139
+ const credentialContainer = (key: string) => credentialName(decodedCredentialText(key), 'remove')?.container === true;
140
+ /** A member of such a container holding the credential under a generic name (`value`, `pass`, `pw`, `secretData`). */
141
+ function memberKey(key: string): ReturnType<typeof credentialKey> {
142
+ const name = credentialMember(decodedCredentialText(key));
143
+ return name === undefined ? undefined : { name, holds: value => !emptyCredential(value) };
144
+ }
145
+ const looksLikeUrl = (value: string) => /^(?:https?:\/\/|\/|\?)/i.test(decodedCredentialText(value));
146
+
147
+ export function assertNoCredentials(value: unknown, policy?: PaginationCursorPolicy): void {
148
+ paginationPolicy(policy);
149
+ // `headers`: the keys of this object, or of objects in this list, are HTTP header names.
150
+ // `inside`: this object, or the objects in this list, sit under `auth` or `credential(s)`.
151
+ const visit = (item: unknown, headers = false, inside = false): void => {
152
+ if (Array.isArray(item)) {
153
+ for (const [index, name] of namedTuples(item)) {
154
+ const credential = credentialKey(name, 'refuse', 'header'), value = (item[index] as unknown[])[1];
155
+ if (credential && !emptyCredential(value) && credential.holds(value)) throw new CredentialError('CREDENTIAL_IN_DURABLE_DATA', { kind: 'field', name: credential.name });
156
+ }
157
+ for (const child of item) visit(child, headers, inside);
158
+ return;
159
+ }
160
+ if (item && typeof item === 'object') {
161
+ for (const [key, child] of Object.entries(item)) {
162
+ const credential = credentialKey(key, 'refuse', headers ? 'header' : 'field') ?? (inside ? memberKey(key) : undefined);
163
+ // A plural name holds a credential as text; a container under it is still searched.
164
+ if (credential?.holds(child)) throw new CredentialError('CREDENTIAL_IN_DURABLE_DATA', { kind: 'field', name: credential.name });
165
+ visit(child, headerContainer(key), credentialContainer(key));
166
+ }
167
+ } else if (typeof item === 'string') {
168
+ // Relative pagination links receive the same check as absolute links.
169
+ if (!looksLikeUrl(item)) return;
170
+ const url = new URL(item, 'https://credential-check.invalid');
171
+ const structure = decodedCredentialText(credentialUrlStructure(item, policy).split('#')[0]);
172
+ const decodedUrl = new URL(structure, 'https://credential-check.invalid');
173
+ if (url.username || url.password || decodedUrl.username || decodedUrl.password) throw new CredentialError('CREDENTIAL_IN_DURABLE_URL', { kind: 'url-userinfo' });
174
+ for (const pair of url.search.slice(1).split('&')) {
175
+ if (credentialQueryKey(pair.split('=')[0], policy)) throw new CredentialError('CREDENTIAL_IN_DURABLE_URL', { kind: 'url-query' });
176
+ }
177
+ for (const child of url.searchParams.values()) visit(child);
178
+ // Encoded delimiters outside opaque values must not hide credential keys.
179
+ // Any key introduced by decoding has no exact-wire cursor exemption.
180
+ // A decoded fragment delimiter must not truncate these query checks either.
181
+ const queryStart = structure.indexOf('?');
182
+ for (const pair of (queryStart < 0 ? '' : structure.slice(queryStart + 1)).split('&')) {
183
+ const key = pair.split('=')[0];
184
+ if (credentialQueryKey(key)) throw new CredentialError('CREDENTIAL_IN_DURABLE_URL', { kind: 'url-query' });
185
+ }
186
+ }
187
+ };
188
+ visit(value);
189
+ }
190
+
191
+ export interface RemovedCredential {
192
+ /** JSON pointer of the field within the value. */
193
+ path: string;
194
+ field: CredentialField;
195
+ /** No live value went: an exporter had already removed it. A field holds
196
+ * REMOVED_CREDENTIAL; a URL holds a placeholder where the value was, such as
197
+ * the `removed@` user name and `=removed` parameter rer's vendor exports leave. */
198
+ beforeUpload?: true;
199
+ }
200
+ /** Whether a removal took out a live credential value, not an exporter's marker. */
201
+ export const liveCredentialRemoved = (item: RemovedCredential): boolean => !item.beforeUpload;
202
+ /** Whether a removal withholds the original bytes: a live value, or any URL
203
+ * credential part, marker included, since refuse mode, which scans retained
204
+ * originals, refuses a URL's user name and credential parameters whatever they hold. */
205
+ export const withholdsOriginal = (item: RemovedCredential): boolean => !item.beforeUpload || item.field.kind !== 'field';
206
+ /** Any `scheme://` (or `scheme:\\`, `//`) URL after leading whitespace, and the relative forms refuse mode checks. */
207
+ const AUTHORITY = /^(\s*(?:[a-z][a-z\d+.-]*:)?[\\/]{2})([^\\/?#]*)@/i;
208
+ const removableUrl = (text: string) => /^\s*(?:[a-z][a-z\d+.-]*:)?[\\/]{2}/i.test(text) || looksLikeUrl(text);
209
+ /** A copy of `value` without plain credential values: a field whose name's parts
210
+ * are a credential word or phrase (`apiKey`, `httpPassword`, `client_secret`,
211
+ * `X-Api-Key`, `secret_key`, `X-Auth`, `pwd`, plural `apiKeys`, and as a header
212
+ * name `Signature` and `Session-Id`; never `keyword`, `author`, or an agent's
213
+ * `signature` field) holding a value is dropped, as
214
+ * is the `value` of a `{name|key, value}` pair naming one (an `Authorization`
215
+ * header pair; its name is a header name only in a header list) and of a
216
+ * `[name, value]` pair in a list of them (Sentry's header tuples), which keeps
217
+ * its name, and directly under `auth` or `credential(s)` a `value`, `pass`, `pw`
218
+ * or `secret…` member (Keycloak's `credentials: [{type, value}]`). A URL of any scheme loses its
219
+ * user name and password, and its credential query and fragment parameters.
220
+ * Placeholders and booleans stay. `removed` reports each by JSON pointer and
221
+ * kind, never value. `secrets` holds the removed values (six characters or
222
+ * longer) so a reader can refuse any record that still repeats one elsewhere.
223
+ * Encoded forms are left for assertNoCredentials to refuse. */
224
+ export function removeCredentials(value: Json, policy?: PaginationCursorPolicy): { value: Json; removed: RemovedCredential[]; secrets: string[] } {
225
+ paginationPolicy(policy);
226
+ const removed: RemovedCredential[] = [], secrets: string[] = [];
227
+ const secret = (text: string) => { if (text.trim().length >= 6 && !credentialPlaceholder(text)) secrets.push(text); };
228
+ const leaves = (item: Json): void => {
229
+ if (typeof item === 'string' || typeof item === 'number') secret(String(item));
230
+ else if (item && typeof item === 'object') for (const child of Object.values(item)) leaves(child);
231
+ };
232
+ const pointer = (path: string, key: string | number) => `${path}/${String(key).replace(/~/g, '~0').replace(/\//g, '~1')}`;
233
+ /** Drops `key=value` pairs naming a credential from `&`-separated text. */
234
+ const pairs = (text: string, path: string, requireValue: boolean): string => {
235
+ const all = text.split('&'), kept = all.filter(pair => (requireValue && !pair.includes('=')) || !credentialQueryKey(pair.split('=')[0], policy));
236
+ if (kept.length === all.length) return text;
237
+ const values = all.filter(pair => !kept.includes(pair)).map(pair => pair.slice(pair.indexOf('=') + 1));
238
+ values.forEach(secret);
239
+ // rer's `=removed` marker (or another placeholder) in every dropped parameter: no live value went.
240
+ removed.push({ path, field: { kind: 'url-query' }, ...(values.every(credentialPlaceholder) ? { beforeUpload: true as const } : {}) });
241
+ return kept.join('&');
242
+ };
243
+ const url = (text: string, path: string): string => {
244
+ if (!removableUrl(text)) return text;
245
+ let result = text;
246
+ const userinfo = AUTHORITY.exec(result);
247
+ if (userinfo) {
248
+ secret(userinfo[2]);
249
+ result = userinfo[1] + result.slice(userinfo[0].length);
250
+ // Even rer's `removed@` marker goes: refuse mode, which scans retained
251
+ // originals, refuses any user name, so the original is withheld either way.
252
+ removed.push({ path, field: { kind: 'url-userinfo' }, ...(credentialPlaceholder(userinfo[2]) ? { beforeUpload: true as const } : {}) });
253
+ }
254
+ const fragmentStart = result.indexOf('#'), queryStart = result.indexOf('?');
255
+ const end = fragmentStart < 0 ? result.length : fragmentStart;
256
+ let fragment = fragmentStart < 0 ? '' : result.slice(fragmentStart);
257
+ // Fragment parameters such as an OAuth `#access_token=` go; a plain anchor stays.
258
+ if (fragment.includes('=')) { const kept = pairs(fragment.slice(1), path, true); fragment = kept ? '#' + kept : ''; }
259
+ if (queryStart < 0 || queryStart > end) return result.slice(0, end) + fragment;
260
+ const query = pairs(result.slice(queryStart + 1, end), path, false);
261
+ return result.slice(0, query ? queryStart + 1 : queryStart) + query + fragment;
262
+ };
263
+ /** Whether a value under a credential name goes; an exporter's marker is noted as removed before upload. */
264
+ const strip = (credential: ReturnType<typeof credentialKey>, child: Json, at: string, fallback: string): boolean => {
265
+ if (credential !== undefined && !emptyCredential(child) && credential.holds(child)) {
266
+ leaves(child);
267
+ removed.push({ path: at, field: { kind: 'field', name: credential.name } });
268
+ return true;
269
+ }
270
+ // An exporter's removal marker is noted under any field name it chose to strip.
271
+ if (child === REMOVED_CREDENTIAL) removed.push({ path: at, field: { kind: 'field', name: credential?.name ?? fallback.toLowerCase().slice(0, 64) }, beforeUpload: true });
272
+ return false;
273
+ };
274
+ // `headers`: the keys of this object, or of objects in this list, are HTTP header names.
275
+ // `inside`: this object, or the objects in this list, sit under `auth` or `credential(s)`.
276
+ const visit = (item: Json, path: string, headers = false, inside = false): Json => {
277
+ if (Array.isArray(item)) {
278
+ // A credential-named `[name, value]` pair keeps its name: `['Authorization']`.
279
+ const tuples = namedTuples(item);
280
+ return item.map((child, index) => {
281
+ const name = tuples.get(index);
282
+ if (name !== undefined && strip(credentialKey(name, 'remove', 'header'), (child as Json[])[1], pointer(pointer(path, index), 1), name)) return [name];
283
+ return visit(child, pointer(path, index), headers, inside);
284
+ });
285
+ }
286
+ if (item && typeof item === 'object') {
287
+ const entries: [string, Json][] = [];
288
+ // A named pair: `{name: 'Authorization', value}` or `{key: 'X-Api-Key', value}`. Its name is a header name only
289
+ // under a header container (`customHttpHeaders`, HAR `headers`, Postman `header`); elsewhere it labels a field
290
+ // (Help Scout's `customFields: [{name: 'Session ID', value}]`), so `Signature` and `Session ID` stay data there.
291
+ const place = headers ? 'header' : 'field';
292
+ const label = Object.entries(item).find(([key, child]) => /^(?:name|key)$/i.test(key) && typeof child === 'string');
293
+ const pairName = label && Object.hasOwn(item, 'value') ? credentialKey(label[1] as string, 'remove', place) : undefined;
294
+ for (const [key, child] of Object.entries(item)) {
295
+ const credential = pairName !== undefined && key === 'value' ? pairName : credentialKey(key, 'remove', place) ?? (inside ? memberKey(key) : undefined);
296
+ if (strip(credential, child, pointer(path, key), key)) continue;
297
+ entries.push([key, visit(child, pointer(path, key), headerContainer(key), credentialContainer(key))]);
298
+ }
299
+ // fromEntries defines own properties, so a `__proto__` key stays data.
300
+ return Object.fromEntries(entries);
301
+ }
302
+ return typeof item === 'string' ? url(item, path) : item;
303
+ };
304
+ return { value: visit(value, ''), removed, secrets: [...new Set(secrets)] };
305
+ }
306
+
307
+ /** Only source custody carries the opt-in; mapped target effects stay strict. */
308
+ export function assertPlanCredentials(plan: ImportPlan): void {
309
+ const policy = paginationPolicy(plan.source.paginationCursorPolicy);
310
+ for (const [key, value] of Object.entries(plan)) {
311
+ if (key === 'source') assertNoCredentials({ [key]: value }, policy);
312
+ else if (key === 'entries') for (const entry of plan.entries) {
313
+ for (const [field, item] of Object.entries(entry)) assertNoCredentials({ [field]: item }, field === 'source' ? policy : undefined);
314
+ }
315
+ else assertNoCredentials({ [key]: value });
316
+ }
317
+ }
@@ -0,0 +1,51 @@
1
+ import { Buffer } from 'node:buffer';
2
+ import { createHash } from 'node:crypto';
3
+ import type { Json } from '../types.ts';
4
+
5
+ const STRING_SLICE = 1 << 20;
6
+ /** JSON.stringify of one string, escaped in bounded slices. A single
7
+ * JSON.stringify call buffers its whole output, and V8 aborts the process
8
+ * (uncatchable) once that buffer passes 256 MiB. Slices never split a
9
+ * surrogate pair, so the output is identical to JSON.stringify(text). */
10
+ export function jsonString(text: string): string {
11
+ if (text.length <= STRING_SLICE) return JSON.stringify(text);
12
+ return [...jsonStringParts(text)].join('');
13
+ }
14
+ /** The same exact escaping for consumers which must not join large literals. */
15
+ export function* jsonStringParts(text: string): Generator<string> {
16
+ if (text.length <= STRING_SLICE) { yield JSON.stringify(text); return; }
17
+ yield '"';
18
+ for (let start = 0; start < text.length;) {
19
+ let end = Math.min(start + STRING_SLICE, text.length);
20
+ const last = text.charCodeAt(end - 1);
21
+ if (end < text.length && last >= 0xd800 && last <= 0xdbff) end--;
22
+ yield JSON.stringify(text.slice(start, end)).slice(1, -1);
23
+ start = end;
24
+ }
25
+ yield '"';
26
+ }
27
+ /** Reject lossy JS values so a fingerprint always names exactly what will persist. */
28
+ export function canonicalJson(value: unknown): string {
29
+ if (typeof value === 'string') return jsonString(value);
30
+ if (value === null || typeof value === 'boolean') return JSON.stringify(value);
31
+ if (typeof value === 'number' && Number.isFinite(value)) return JSON.stringify(value);
32
+ if (Array.isArray(value)) return '[' + Array.from({ length: value.length }, (_, index) => canonicalJson(value[index])).join(',') + ']';
33
+ if (typeof value === 'object' && value && (Object.getPrototypeOf(value) === Object.prototype || Object.getPrototypeOf(value) === null)) {
34
+ return '{' + Object.keys(value).sort().map(key => jsonString(key) + ':' + canonicalJson((value as Record<string, Json>)[key])).join(',') + '}';
35
+ }
36
+ throw new Error('INVALID_JSON: durable values must be finite JSON');
37
+ }
38
+ export const sha256 = (value: string | Uint8Array): string => createHash('sha256').update(value).digest('hex');
39
+ export const sourceIdentity = (destination: string, namespace: string, kind: string, sourceId: string): string => sha256(canonicalJson([destination, namespace, kind, sourceId]));
40
+ export const targetIdentity = (destination: string, kind: string, id: string): string => canonicalJson([destination, kind, id]);
41
+ export function requireText(value: unknown, label: string): asserts value is string {
42
+ if (typeof value !== 'string' || !value.trim() || value.length > 4096) throw new Error(`INVALID_ID: ${label}`);
43
+ }
44
+ export { assertNoCredentials } from './credentials.ts';
45
+ export function attachmentHash(attachment: { base64: string; sha256: string; mediaType: string } | undefined): string | null {
46
+ if (!attachment) return null;
47
+ const bytes = Buffer.from(attachment.base64, 'base64');
48
+ if (bytes.toString('base64') !== attachment.base64 || sha256(bytes) !== attachment.sha256) throw new Error('ATTACHMENT_HASH_MISMATCH');
49
+ requireText(attachment.mediaType, 'mediaType');
50
+ return attachment.sha256;
51
+ }
@@ -0,0 +1,21 @@
1
+ /** Capture each mapper property once in canonical order before admission.
2
+ * Shared containers stay shared in this private snapshot, so a small DAG with
3
+ * an enormous logical encoding is measured before any expanded clone. */
4
+ export function snapshotJson<T>(value: T): T {
5
+ const copies = new WeakMap<object, object>(), active = new Set<object>();
6
+ const visit = (item: unknown, depth: number): any => {
7
+ if (item === null || typeof item === 'string' || typeof item === 'boolean' || typeof item === 'number' && Number.isFinite(item)) return Object.is(item, -0) ? 0 : item;
8
+ if (!item || typeof item !== 'object' || !Array.isArray(item) && Object.getPrototypeOf(item) !== Object.prototype && Object.getPrototypeOf(item) !== null) throw new Error('INVALID_JSON: durable values must be finite JSON');
9
+ if (depth >= 256 || active.has(item)) throw new Error('INVALID_JSON: cyclic or excessively nested durable value');
10
+ const previous = copies.get(item); if (previous) return previous;
11
+ active.add(item);
12
+ const copy: any = Array.isArray(item) ? [] : {};
13
+ copies.set(item, copy);
14
+ if (Array.isArray(item)) {
15
+ const length = item.length;
16
+ for (let index = 0; index < length; index++) copy.push(visit(item[index], depth + 1));
17
+ } else for (const key of Object.keys(item).sort()) Object.defineProperty(copy, key, { value: visit((item as Record<string, unknown>)[key], depth + 1), enumerable: true, writable: true, configurable: true });
18
+ active.delete(item); return copy;
19
+ };
20
+ return visit(value, 0);
21
+ }