@decocms/blocks 8.7.0 → 8.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (154) hide show
  1. package/dist/index.d.ts +1 -1
  2. package/dist/index.d.ts.map +1 -1
  3. package/dist/index.js +1 -1
  4. package/dist/index.js.map +1 -1
  5. package/dist/protocol/conformance/cases/changes.d.ts.map +1 -1
  6. package/dist/protocol/conformance/cases/changes.js +14 -3
  7. package/dist/protocol/conformance/cases/changes.js.map +1 -1
  8. package/dist/protocol/conformance/cases/guards.d.ts.map +1 -1
  9. package/dist/protocol/conformance/cases/guards.js +19 -42
  10. package/dist/protocol/conformance/cases/guards.js.map +1 -1
  11. package/dist/protocol/conformance/cases/overlay.d.ts +3 -0
  12. package/dist/protocol/conformance/cases/overlay.d.ts.map +1 -0
  13. package/dist/protocol/conformance/cases/overlay.js +164 -0
  14. package/dist/protocol/conformance/cases/overlay.js.map +1 -0
  15. package/dist/protocol/conformance/cases/reads.js +5 -5
  16. package/dist/protocol/conformance/cases/reads.js.map +1 -1
  17. package/dist/protocol/conformance/cases/writes.d.ts.map +1 -1
  18. package/dist/protocol/conformance/cases/writes.js +15 -3
  19. package/dist/protocol/conformance/cases/writes.js.map +1 -1
  20. package/dist/protocol/conformance/context.d.ts +0 -10
  21. package/dist/protocol/conformance/context.d.ts.map +1 -1
  22. package/dist/protocol/conformance/context.js.map +1 -1
  23. package/dist/protocol/conformance/index.d.ts.map +1 -1
  24. package/dist/protocol/conformance/index.js +2 -0
  25. package/dist/protocol/conformance/index.js.map +1 -1
  26. package/dist/protocol/server/content.d.ts +8 -1
  27. package/dist/protocol/server/content.d.ts.map +1 -1
  28. package/dist/protocol/server/content.js +18 -3
  29. package/dist/protocol/server/content.js.map +1 -1
  30. package/dist/protocol/server/core.d.ts +0 -6
  31. package/dist/protocol/server/core.d.ts.map +1 -1
  32. package/dist/protocol/server/core.js +0 -2
  33. package/dist/protocol/server/core.js.map +1 -1
  34. package/dist/protocol/server/methods/apply.d.ts +16 -5
  35. package/dist/protocol/server/methods/apply.d.ts.map +1 -1
  36. package/dist/protocol/server/methods/apply.js +42 -38
  37. package/dist/protocol/server/methods/apply.js.map +1 -1
  38. package/dist/protocol/server/methods/changes.d.ts.map +1 -1
  39. package/dist/protocol/server/methods/changes.js +31 -3
  40. package/dist/protocol/server/methods/changes.js.map +1 -1
  41. package/dist/protocol/server/methods/read.d.ts.map +1 -1
  42. package/dist/protocol/server/methods/read.js +2 -23
  43. package/dist/protocol/server/methods/read.js.map +1 -1
  44. package/dist/protocol/storage/fs/index.d.ts.map +1 -1
  45. package/dist/protocol/storage/fs/index.js +0 -18
  46. package/dist/protocol/storage/fs/index.js.map +1 -1
  47. package/dist/protocol/storage/memory.d.ts +11 -3
  48. package/dist/protocol/storage/memory.d.ts.map +1 -1
  49. package/dist/protocol/storage/memory.js +114 -45
  50. package/dist/protocol/storage/memory.js.map +1 -1
  51. package/dist/protocol/storage.d.ts +68 -18
  52. package/dist/protocol/storage.d.ts.map +1 -1
  53. package/dist/protocol/storage.js +7 -0
  54. package/dist/protocol/storage.js.map +1 -1
  55. package/dist/protocol/types.d.ts +51 -8
  56. package/dist/protocol/types.d.ts.map +1 -1
  57. package/dist/protocol/types.js +5 -3
  58. package/dist/protocol/types.js.map +1 -1
  59. package/dist/v8/builtins/index.d.ts +1 -4
  60. package/dist/v8/builtins/index.d.ts.map +1 -1
  61. package/dist/v8/builtins/index.js +1 -6
  62. package/dist/v8/builtins/index.js.map +1 -1
  63. package/dist/v8/builtins/legacy.d.ts +4 -4
  64. package/dist/v8/builtins/legacy.d.ts.map +1 -1
  65. package/dist/v8/builtins/legacy.js +4 -5
  66. package/dist/v8/builtins/legacy.js.map +1 -1
  67. package/dist/v8/cli/builtins.d.ts.map +1 -1
  68. package/dist/v8/cli/builtins.js +0 -1
  69. package/dist/v8/cli/builtins.js.map +1 -1
  70. package/dist/v8/cli/check/index.d.ts +1 -1
  71. package/dist/v8/cli/check/index.d.ts.map +1 -1
  72. package/dist/v8/cli/check/index.js +13 -33
  73. package/dist/v8/cli/check/index.js.map +1 -1
  74. package/dist/v8/cli/schema/builtinSchemas.d.ts.map +1 -1
  75. package/dist/v8/cli/schema/builtinSchemas.js +0 -8
  76. package/dist/v8/cli/schema/builtinSchemas.js.map +1 -1
  77. package/dist/v8/cli/schema/generate.d.ts.map +1 -1
  78. package/dist/v8/cli/schema/generate.js +1 -8
  79. package/dist/v8/cli/schema/generate.js.map +1 -1
  80. package/dist/v8/cli/schema/typeToSchema.d.ts +0 -1
  81. package/dist/v8/cli/schema/typeToSchema.d.ts.map +1 -1
  82. package/dist/v8/cli/schema/typeToSchema.js +5 -30
  83. package/dist/v8/cli/schema/typeToSchema.js.map +1 -1
  84. package/dist/v8/client.d.ts +1 -1
  85. package/dist/v8/client.d.ts.map +1 -1
  86. package/dist/v8/cms.d.ts.map +1 -1
  87. package/dist/v8/cms.js +5 -13
  88. package/dist/v8/cms.js.map +1 -1
  89. package/dist/v8/index.d.ts +1 -1
  90. package/dist/v8/index.d.ts.map +1 -1
  91. package/dist/v8/resolver.d.ts +1 -1
  92. package/dist/v8/resolver.d.ts.map +1 -1
  93. package/dist/v8/telemetry.d.ts +0 -2
  94. package/dist/v8/telemetry.d.ts.map +1 -1
  95. package/dist/v8/telemetry.js +5 -20
  96. package/dist/v8/telemetry.js.map +1 -1
  97. package/dist/v8/types.d.ts +0 -8
  98. package/dist/v8/types.d.ts.map +1 -1
  99. package/package.json +1 -6
  100. package/src/index.ts +1 -2
  101. package/src/protocol/conformance/cases/changes.ts +16 -4
  102. package/src/protocol/conformance/cases/guards.ts +19 -55
  103. package/src/protocol/conformance/cases/overlay.ts +195 -0
  104. package/src/protocol/conformance/cases/reads.ts +4 -4
  105. package/src/protocol/conformance/cases/writes.ts +15 -3
  106. package/src/protocol/conformance/context.ts +0 -7
  107. package/src/protocol/conformance/index.ts +2 -0
  108. package/src/protocol/server/content.ts +30 -4
  109. package/src/protocol/server/core.ts +0 -3
  110. package/src/protocol/server/methods/apply.ts +43 -38
  111. package/src/protocol/server/methods/changes.ts +42 -4
  112. package/src/protocol/server/methods/read.ts +2 -24
  113. package/src/protocol/storage/fs/index.ts +0 -18
  114. package/src/protocol/storage/memory.ts +124 -46
  115. package/src/protocol/storage.ts +69 -18
  116. package/src/protocol/types.ts +51 -8
  117. package/src/v8/builtins/index.ts +1 -6
  118. package/src/v8/builtins/legacy.ts +4 -5
  119. package/src/v8/cli/builtins.ts +0 -1
  120. package/src/v8/cli/check/index.ts +15 -34
  121. package/src/v8/cli/schema/builtinSchemas.ts +0 -8
  122. package/src/v8/cli/schema/generate.ts +1 -8
  123. package/src/v8/cli/schema/typeToSchema.ts +5 -31
  124. package/src/v8/client.ts +1 -1
  125. package/src/v8/cms.ts +5 -14
  126. package/src/v8/index.ts +0 -1
  127. package/src/v8/resolver.ts +1 -1
  128. package/src/v8/telemetry.ts +5 -20
  129. package/src/v8/types.ts +0 -5
  130. package/dist/protocol/ciphertext.d.ts +0 -7
  131. package/dist/protocol/ciphertext.d.ts.map +0 -1
  132. package/dist/protocol/ciphertext.js +0 -7
  133. package/dist/protocol/ciphertext.js.map +0 -1
  134. package/dist/protocol/secrets.d.ts +0 -22
  135. package/dist/protocol/secrets.d.ts.map +0 -1
  136. package/dist/protocol/secrets.js +0 -207
  137. package/dist/protocol/secrets.js.map +0 -1
  138. package/dist/v8/builtins/secret.d.ts +0 -7
  139. package/dist/v8/builtins/secret.d.ts.map +0 -1
  140. package/dist/v8/builtins/secret.js +0 -68
  141. package/dist/v8/builtins/secret.js.map +0 -1
  142. package/dist/v8/ciphertext.d.ts +0 -58
  143. package/dist/v8/ciphertext.d.ts.map +0 -1
  144. package/dist/v8/ciphertext.js +0 -134
  145. package/dist/v8/ciphertext.js.map +0 -1
  146. package/dist/v8/secrets.d.ts +0 -9
  147. package/dist/v8/secrets.d.ts.map +0 -1
  148. package/dist/v8/secrets.js +0 -14
  149. package/dist/v8/secrets.js.map +0 -1
  150. package/src/protocol/ciphertext.ts +0 -6
  151. package/src/protocol/secrets.ts +0 -239
  152. package/src/v8/builtins/secret.ts +0 -82
  153. package/src/v8/ciphertext.ts +0 -158
  154. package/src/v8/secrets.ts +0 -17
@@ -33,7 +33,7 @@ type Path = (string | number)[];
33
33
  interface ResolverOptions {
34
34
  snapshot: Snapshot;
35
35
  blocks: Blocks;
36
- /** The built-ins, with this CMS's `secret`. */
36
+ /** The built-in blocks. */
37
37
  builtIns: Readonly<Blocks>;
38
38
  /** The client this resolver belongs to: `currentClient()` while a block function runs. */
39
39
  client?: Client;
@@ -15,7 +15,7 @@
15
15
  * Workers, on an unref'd timer elsewhere); a send that fails is retried
16
16
  * once on 429/502/503/504 and otherwise dropped.
17
17
  * - Everything is scrubbed before encoding: no query strings, tokens,
18
- * cookies or authorization values, and no value a secret block decrypted.
18
+ * cookies or authorization values.
19
19
  */
20
20
  import { hasBackgroundHook, later, runInBackground } from "./background.ts";
21
21
  import { rate, TELEMETRY_DEFAULTS } from "./builtins/data.ts";
@@ -29,7 +29,6 @@ const SINK = Symbol.for("decocms.blocks.telemetry");
29
29
  const FLUSH_MS = 10_000;
30
30
  const MAX_LOGS = 200;
31
31
  const MAX_SPANS = 1_000;
32
- const MAX_SECRETS = 1_000;
33
32
  const RETRY_STATUSES = new Set([429, 502, 503, 504]);
34
33
  /** OpenTelemetry's recommended `http.*.request.duration` buckets, in seconds. */
35
34
  const BOUNDS = [0.005, 0.01, 0.025, 0.05, 0.075, 0.1, 0.25, 0.5, 0.75, 1, 2.5, 5, 7.5, 10];
@@ -131,19 +130,12 @@ export class TelemetryPipeline {
131
130
  #spans: (SpanInput & { spanId: string })[] = [];
132
131
  #windowStart = Date.now();
133
132
  #scheduled = false;
134
- /** Values secret blocks decrypted, replaced with `[redacted]` wherever they'd be sent. */
135
- readonly #secrets = new Set<string>();
136
133
 
137
134
  constructor(destination: Destination) {
138
135
  this.#destination = destination;
139
136
  this.apply(TELEMETRY_DEFAULTS);
140
137
  }
141
138
 
142
- /** Never send `value`: it's replaced with `[redacted]` in every log, span and label. */
143
- redact(value: string): void {
144
- if (value.length > 0 && this.#secrets.size < MAX_SECRETS) this.#secrets.add(value);
145
- }
146
-
147
139
  /** The release being served: its revision labels batches. */
148
140
  useRelease(snapshot: Snapshot): void {
149
141
  this.#release = snapshot.revision;
@@ -272,8 +264,7 @@ export class TelemetryPipeline {
272
264
  this.#histograms = new Map();
273
265
  this.#logs = [];
274
266
  this.#spans = [];
275
- const scrub = (text: string) => this.#scrub(text);
276
- const encode = (attributes: Attributes) => encodeAttributes(attributes, scrub);
267
+ const encode = encodeAttributes;
277
268
  const resource = { attributes: encode(this.#resource()) };
278
269
  const scope = { name: "@decocms/blocks" };
279
270
  const sends: Promise<void>[] = [];
@@ -326,7 +317,7 @@ export class TelemetryPipeline {
326
317
  timeUnixNano: nanos(log.time),
327
318
  severityNumber: 17,
328
319
  severityText: "ERROR",
329
- body: { stringValue: scrub(log.message) },
320
+ body: { stringValue: scrubText(log.message) },
330
321
  attributes: encode(log.attributes),
331
322
  })),
332
323
  },
@@ -367,12 +358,6 @@ export class TelemetryPipeline {
367
358
  await Promise.all(sends);
368
359
  }
369
360
 
370
- #scrub(text: string): string {
371
- let out = text;
372
- for (const secret of this.#secrets) out = out.split(secret).join("[redacted]");
373
- return scrubText(out);
374
- }
375
-
376
361
  #schedule(): void {
377
362
  if (this.#scheduled) return;
378
363
  this.#scheduled = true;
@@ -480,14 +465,14 @@ function randomHex(bytes: number): string {
480
465
  return Array.from(values, (b) => b.toString(16).padStart(2, "0")).join("");
481
466
  }
482
467
 
483
- function encodeAttributes(attributes: Attributes, scrub: (text: string) => string) {
468
+ function encodeAttributes(attributes: Attributes) {
484
469
  return Object.entries(attributes)
485
470
  .filter(([, value]) => value !== undefined)
486
471
  .map(([key, value]) => ({
487
472
  key,
488
473
  value:
489
474
  typeof value === "string"
490
- ? { stringValue: scrub(value) }
475
+ ? { stringValue: scrubText(value) }
491
476
  : typeof value === "boolean"
492
477
  ? { boolValue: value }
493
478
  : Number.isInteger(value)
package/src/v8/types.ts CHANGED
@@ -151,9 +151,6 @@ export interface LegacyRedirect {
151
151
  };
152
152
  }
153
153
 
154
- /** A field the site editor saves encrypted, as a `secret` block. */
155
- export type Secret = string & { readonly __secret: true };
156
-
157
154
  /** A prop the built-in `lazy` block fills; resolves its value when called, at most once. */
158
155
  export type Lazy<T> = () => Promise<T>;
159
156
 
@@ -298,8 +295,6 @@ export interface CMSConfig {
298
295
  */
299
296
  draftHosts?: string[];
300
297
  };
301
- /** The private key that decrypts `secret` blocks. */
302
- secrets?: { key?: string };
303
298
  /**
304
299
  * Your site ID (Studio's site slug): turns on hosted releases from
305
300
  * `delivery.decocms.com`. Drafts don't need it.
@@ -1,7 +0,0 @@
1
- /**
2
- * The secret ciphertext format. The implementation is a dependency-free leaf
3
- * module of the SDK (`src/v8/ciphertext.ts`), where the `secret` built-in
4
- * decrypts it; the protocol re-exports it for its secret guard.
5
- */
6
- export * from "../v8/ciphertext.ts";
7
- //# sourceMappingURL=ciphertext.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"ciphertext.d.ts","sourceRoot":"","sources":["../../src/protocol/ciphertext.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,cAAc,qBAAqB,CAAC"}
@@ -1,7 +0,0 @@
1
- /**
2
- * The secret ciphertext format. The implementation is a dependency-free leaf
3
- * module of the SDK (`src/v8/ciphertext.ts`), where the `secret` built-in
4
- * decrypts it; the protocol re-exports it for its secret guard.
5
- */
6
- export * from "../v8/ciphertext.js";
7
- //# sourceMappingURL=ciphertext.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"ciphertext.js","sourceRoot":"","sources":["../../src/protocol/ciphertext.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,cAAc,qBAAqB,CAAC"}
@@ -1,22 +0,0 @@
1
- import type { BlockViolation } from "./errors.ts";
2
- import type { DecoMeta } from "./types.ts";
3
- /** The built-in block type that holds an encrypted value. */
4
- export declare const SECRET_BLOCK_TYPE = "secret";
5
- /** The JSON Schema `format` that marks a `Secret` field. */
6
- export declare const SECRET_FORMAT = "secret";
7
- /**
8
- * True when `ciphertext` is a well-formed secret ciphertext:
9
- * `v1.<wrappedKey>.<iv>.<ciphertext>` with segment lengths an RSA-OAEP and
10
- * AES-256-GCM encryption produces. Checking it needs no key.
11
- */
12
- export declare function isWellFormedCiphertext(ciphertext: unknown): ciphertext is string;
13
- /** True when a JSON Schema node describes a `Secret` field. */
14
- export declare function isSecretFieldSchema(schema: unknown): boolean;
15
- /** True when `value` is a `secret` block with a well-formed `ciphertext`. */
16
- export declare function isSecretBlock(value: unknown): boolean;
17
- /**
18
- * Checks one entry against the secret guard. `meta` is the parsed schema;
19
- * without one, only the `secret` blocks' ciphertexts can be checked.
20
- */
21
- export declare function checkSecrets(name: string, entry: unknown, meta: DecoMeta | null): BlockViolation[];
22
- //# sourceMappingURL=secrets.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"secrets.d.ts","sourceRoot":"","sources":["../../src/protocol/secrets.ts"],"names":[],"mappings":"AAeA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAClD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAE3C,6DAA6D;AAC7D,eAAO,MAAM,iBAAiB,WAAW,CAAC;AAE1C,4DAA4D;AAC5D,eAAO,MAAM,aAAa,WAAW,CAAC;AAsBtC;;;;GAIG;AACH,wBAAgB,sBAAsB,CAAC,UAAU,EAAE,OAAO,GAAG,UAAU,IAAI,MAAM,CAEhF;AAED,+DAA+D;AAC/D,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,OAAO,GAAG,OAAO,CAE5D;AAED,6EAA6E;AAC7E,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAIrD;AAmKD;;;GAGG;AACH,wBAAgB,YAAY,CAC1B,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,OAAO,EACd,IAAI,EAAE,QAAQ,GAAG,IAAI,GACpB,cAAc,EAAE,CAIlB"}
@@ -1,207 +0,0 @@
1
- /**
2
- * The secret guard: the content protocol refuses plain text in a `Secret`
3
- * field, so the site editor and agents can't save a credential by mistake.
4
- *
5
- * A `Secret` field must hold a `secret` block with a well-formed
6
- * `ciphertext` (`{ "__resolveType": "secret", "ciphertext": "v1.…" }`, in the
7
- * format `./ciphertext` pins: three base64url segments of checked lengths), or a
8
- * variant block whose every value does. Every `secret` block anywhere in an
9
- * entry must carry a well-formed `ciphertext`, whatever its field.
10
- *
11
- * The schema marks a `Secret` field with `"format": "secret"` — the contract
12
- * `deco schema` emits for the `Secret` type. A legacy secret loader block
13
- * (`website/loaders/secret.ts`) is left alone. Browser-safe.
14
- */
15
- import { parseCiphertext } from "./ciphertext.js";
16
- /** The built-in block type that holds an encrypted value. */
17
- export const SECRET_BLOCK_TYPE = "secret";
18
- /** The JSON Schema `format` that marks a `Secret` field. */
19
- export const SECRET_FORMAT = "secret";
20
- const MULTIVARIATE_TYPES = new Set(["multivariate", "website/flags/multivariate.ts"]);
21
- const LAZY_TYPE = "lazy";
22
- /**
23
- * The legacy (v7) secret loader. Its `encrypted` prop is a string marked
24
- * `"format": "secret"` that already holds the site's own ciphertext, so it
25
- * isn't a `Secret` field; its blocks are migrated, not guarded.
26
- */
27
- const LEGACY_SECRET_LOADER = /(^|\/)loaders\/secret\.ts$/;
28
- const MAX_DEPTH = 512;
29
- const isObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
30
- const resolveTypeOf = (value) => isObject(value) && typeof value.__resolveType === "string" ? value.__resolveType : undefined;
31
- const escapePointer = (key) => key.replace(/~/g, "~0").replace(/\//g, "~1");
32
- /**
33
- * True when `ciphertext` is a well-formed secret ciphertext:
34
- * `v1.<wrappedKey>.<iv>.<ciphertext>` with segment lengths an RSA-OAEP and
35
- * AES-256-GCM encryption produces. Checking it needs no key.
36
- */
37
- export function isWellFormedCiphertext(ciphertext) {
38
- return parseCiphertext(ciphertext) !== null;
39
- }
40
- /** True when a JSON Schema node describes a `Secret` field. */
41
- export function isSecretFieldSchema(schema) {
42
- return isObject(schema) && schema.format === SECRET_FORMAT;
43
- }
44
- /** True when `value` is a `secret` block with a well-formed `ciphertext`. */
45
- export function isSecretBlock(value) {
46
- return (resolveTypeOf(value) === SECRET_BLOCK_TYPE && isWellFormedCiphertext(value.ciphertext));
47
- }
48
- const blockIndexCache = new WeakMap();
49
- /** Every block type in the schema's manifest, by key, to the schema of its arguments. */
50
- function blockIndex(meta) {
51
- const cached = blockIndexCache.get(meta);
52
- if (cached)
53
- return cached;
54
- const index = new Map();
55
- for (const group of Object.values(meta.manifest?.blocks ?? {})) {
56
- if (!isObject(group))
57
- continue;
58
- for (const [key, schema] of Object.entries(group)) {
59
- if (!index.has(key))
60
- index.set(key, schema);
61
- }
62
- }
63
- blockIndexCache.set(meta, index);
64
- return index;
65
- }
66
- class SecretWalker {
67
- name;
68
- meta;
69
- violations = [];
70
- constructor(name, meta) {
71
- this.name = name;
72
- this.meta = meta;
73
- }
74
- report(pointer, rule, message) {
75
- this.violations.push({ name: this.name, pointer, rule, message });
76
- }
77
- /** Follows local `$ref`s into `schema.definitions`. */
78
- deref(schema) {
79
- let current = schema;
80
- for (let hops = 0; hops < 32 && isObject(current) && typeof current.$ref === "string"; hops++) {
81
- const match = /^#\/definitions\/(.+)$/.exec(current.$ref);
82
- if (!match)
83
- return current;
84
- const key = decodeURIComponent(match[1]).replace(/~1/g, "/").replace(/~0/g, "~");
85
- current = this.meta?.schema?.definitions?.[key];
86
- }
87
- return current;
88
- }
89
- /** The schemas of a node, flattened through `allOf`/`anyOf`/`oneOf`. */
90
- branches(schema, depth = 0) {
91
- const node = this.deref(schema);
92
- if (!isObject(node) || depth > 16)
93
- return [];
94
- const out = [node];
95
- for (const combinator of ["allOf", "anyOf", "oneOf"]) {
96
- const list = node[combinator];
97
- if (Array.isArray(list))
98
- for (const item of list)
99
- out.push(...this.branches(item, depth + 1));
100
- }
101
- return out;
102
- }
103
- isSecretField(schema) {
104
- return this.branches(schema).some(isSecretFieldSchema);
105
- }
106
- propertySchema(schema, key) {
107
- for (const branch of this.branches(schema)) {
108
- const properties = branch.properties;
109
- if (isObject(properties) && key in properties)
110
- return properties[key];
111
- }
112
- for (const branch of this.branches(schema)) {
113
- if (isObject(branch.additionalProperties))
114
- return branch.additionalProperties;
115
- }
116
- return undefined;
117
- }
118
- itemSchema(schema) {
119
- for (const branch of this.branches(schema)) {
120
- if (isObject(branch.items))
121
- return branch.items;
122
- }
123
- return undefined;
124
- }
125
- /** A value stored in a `Secret` field. */
126
- checkSecretValue(value, pointer, depth) {
127
- if (depth > MAX_DEPTH)
128
- return this.report(pointer, "too-deep", "the value is nested too deeply");
129
- const type = resolveTypeOf(value);
130
- if (type === SECRET_BLOCK_TYPE)
131
- return this.walkBlock(value, pointer, depth);
132
- if (type !== undefined && MULTIVARIATE_TYPES.has(type)) {
133
- const variants = value.variants;
134
- if (!Array.isArray(variants)) {
135
- return this.report(pointer, "secret-field", "a Secret field holds a variant block without variants");
136
- }
137
- for (const [index, variant] of variants.entries()) {
138
- const at = `${pointer}/variants/${index}`;
139
- if (!isObject(variant)) {
140
- this.report(at, "secret-field", "a variant must be an object");
141
- continue;
142
- }
143
- if (variant.rule !== undefined)
144
- this.walkAny(variant.rule, `${at}/rule`, depth + 1);
145
- let inner = variant.value;
146
- let innerAt = `${at}/value`;
147
- if (resolveTypeOf(inner) === LAZY_TYPE) {
148
- inner = inner.value;
149
- innerAt = `${innerAt}/value`;
150
- }
151
- this.checkSecretValue(inner, innerAt, depth + 1);
152
- }
153
- return;
154
- }
155
- this.report(pointer, "secret-field", 'a Secret field must hold a "secret" block with a well-formed ciphertext, never plain text');
156
- }
157
- /** A block value: its own definition (from the manifest) describes its arguments. */
158
- walkBlock(block, pointer, depth) {
159
- const type = block.__resolveType;
160
- if (type === SECRET_BLOCK_TYPE && !isWellFormedCiphertext(block.ciphertext)) {
161
- this.report(pointer, "secret-ciphertext", 'a "secret" block must carry a well-formed "ciphertext" (v1.<wrappedKey>.<iv>.<ciphertext>)');
162
- }
163
- const definition = this.meta && !LEGACY_SECRET_LOADER.test(type) ? blockIndex(this.meta).get(type) : undefined;
164
- this.walkObject(block, definition, pointer, depth);
165
- }
166
- walkObject(object, schema, pointer, depth) {
167
- for (const [key, child] of Object.entries(object)) {
168
- if (key === "__resolveType")
169
- continue;
170
- const childSchema = schema === undefined ? undefined : this.propertySchema(schema, key);
171
- this.walk(child, childSchema, `${pointer}/${escapePointer(key)}`, depth + 1);
172
- }
173
- }
174
- walk(value, schema, pointer, depth) {
175
- if (depth > MAX_DEPTH)
176
- return this.report(pointer, "too-deep", "the value is nested too deeply");
177
- if (schema !== undefined && this.isSecretField(schema)) {
178
- return this.checkSecretValue(value, pointer, depth);
179
- }
180
- if (Array.isArray(value)) {
181
- const items = schema === undefined ? undefined : this.itemSchema(schema);
182
- for (const [index, item] of value.entries()) {
183
- this.walk(item, items, `${pointer}/${index}`, depth + 1);
184
- }
185
- return;
186
- }
187
- if (!isObject(value))
188
- return;
189
- // A nested block is described by its own definition, not the field's union.
190
- if (resolveTypeOf(value) !== undefined)
191
- return this.walkBlock(value, pointer, depth);
192
- this.walkObject(value, schema, pointer, depth);
193
- }
194
- walkAny(value, pointer, depth) {
195
- this.walk(value, undefined, pointer, depth);
196
- }
197
- }
198
- /**
199
- * Checks one entry against the secret guard. `meta` is the parsed schema;
200
- * without one, only the `secret` blocks' ciphertexts can be checked.
201
- */
202
- export function checkSecrets(name, entry, meta) {
203
- const walker = new SecretWalker(name, meta);
204
- walker.walk(entry, undefined, "", 0);
205
- return walker.violations;
206
- }
207
- //# sourceMappingURL=secrets.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"secrets.js","sourceRoot":"","sources":["../../src/protocol/secrets.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,OAAO,EAAE,eAAe,EAAE,MAAM,i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EAAE,IAAI,CAAC,CAAC;IAC5C,MAAM,CAAC,IAAI,CAAC,KAAK,EAAE,SAAS,EAAE,EAAE,EAAE,CAAC,CAAC,CAAC;IACrC,OAAO,MAAM,CAAC,UAAU,CAAC;AAC3B,CAAC"}
@@ -1,7 +0,0 @@
1
- import type { BlockFunction } from "../types.ts";
2
- /**
3
- * The `secret` block for one private key. The CMS creates one per instance;
4
- * the built-in list holds the keyless one, which always fails.
5
- */
6
- export declare function secretBlock(key?: string, onDecrypt?: (value: string) => void): BlockFunction;
7
- //# sourceMappingURL=secret.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"secret.d.ts","sourceRoot":"","sources":["../../../src/v8/builtins/secret.ts"],"names":[],"mappings":"AAoBA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAcjD;;;GAGG;AACH,wBAAgB,WAAW,CAAC,GAAG,CAAC,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,GAAG,aAAa,CA2C5F"}
@@ -1,68 +0,0 @@
1
- /**
2
- * The built-in `secret` block (see /next/built-in-blocks#secrets):
3
- * `{ "__resolveType": "secret", "ciphertext": "v1.…" }` resolves to the
4
- * decrypted string, on the server only.
5
- *
6
- * The ciphertext format lives in `../ciphertext` (a leaf module with no
7
- * imports, which the protocol re-exports): a fresh AES-256-GCM key per value, wrapped with the
8
- * site's RSA public key using RSA-OAEP and SHA-256. Decrypting takes the
9
- * matching PKCS#8 private key, the PEM `createCMS({ secrets: { key } })` gets.
10
- *
11
- * Every failure (no key, the wrong key, a malformed ciphertext, a browser)
12
- * throws, so the resolver reports `BLOCK_FAILED` for that block alone. No
13
- * message ever includes the key or a decrypted value. `{ run: false }` and
14
- * `client.list` never run functions, so they return the ciphertext as saved.
15
- *
16
- * Every decrypted value is handed to `onDecrypt` (the CMS redacts it from
17
- * telemetry). A decrypted value stays on the server because it's resolved
18
- * there; tainting it (React's taint API) is up to the site's own code.
19
- */
20
- import { parseCiphertext } from "../ciphertext.js";
21
- /** True in a browser, where a secret must never be decrypted. */
22
- function inBrowser() {
23
- const scope = globalThis;
24
- return typeof scope.window === "object" && typeof scope.document === "object";
25
- }
26
- /** The DER bytes of a single `PRIVATE KEY` (PKCS#8) PEM block. */
27
- function privateKeyDer(pem) {
28
- const match = /-----BEGIN PRIVATE KEY-----([\s\S]*?)-----END PRIVATE KEY-----/.exec(pem);
29
- return Uint8Array.from(atob(match?.[1]?.replace(/\s+/g, "") ?? ""), (c) => c.charCodeAt(0));
30
- }
31
- /**
32
- * The `secret` block for one private key. The CMS creates one per instance;
33
- * the built-in list holds the keyless one, which always fails.
34
- */
35
- export function secretBlock(key, onDecrypt) {
36
- let imported;
37
- return async ({ ciphertext } = {}) => {
38
- if (inBrowser())
39
- throw new Error("a secret block resolves on the server only");
40
- if (!key)
41
- throw new Error("no key to decrypt secrets: pass createCMS({ secrets: { key } })");
42
- const parts = parseCiphertext(ciphertext);
43
- if (!parts)
44
- throw new Error("the ciphertext is not well formed");
45
- imported ??= Promise.resolve().then(() => crypto.subtle.importKey("pkcs8", privateKeyDer(key), { name: "RSA-OAEP", hash: "SHA-256" }, false, ["decrypt"]));
46
- let rsa;
47
- try {
48
- rsa = await imported;
49
- }
50
- catch {
51
- throw new Error("the secrets key is not an RSA private key in PKCS#8 PEM");
52
- }
53
- let value;
54
- try {
55
- const raw = await crypto.subtle.decrypt({ name: "RSA-OAEP" }, rsa, parts.wrappedKey);
56
- const aes = await crypto.subtle.importKey("raw", raw, "AES-GCM", false, ["decrypt"]);
57
- const plain = await crypto.subtle.decrypt({ name: "AES-GCM", iv: parts.iv }, aes, parts.ciphertext);
58
- value = new TextDecoder().decode(plain);
59
- }
60
- catch {
61
- // Web Crypto's errors say nothing useful, and this one never names the value.
62
- throw new Error("the secret could not be decrypted with this key");
63
- }
64
- onDecrypt?.(value);
65
- return value;
66
- };
67
- }
68
- //# sourceMappingURL=secret.js.map
@@ -1 +0,0 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C,SAAS,CACrB,OAAO,EACP,aAAa,CAAC,GAAG,CAAiB,EAClC,EAAE,IAAI,EAAE,UAAU,EAAE,IAAI,EAAE,SAAS,EAAE,EACrC,KAAK,EACL,CAAC,SAAS,CAAC,CACZ,CACF,CAAC;QACF,IAAI,GAAc,CAAC;QACnB,IAAI,CAAC;YACH,GAAG,GAAG,MAAM,QAAQ,CAAC;QACvB,CAAC;QAAC,MAAM,CAAC;YACP,MAAM,IAAI,KAAK,CAAC,yDAAyD,CAAC,CAAC;QAC7E,CAAC;QACD,IAAI,KAAa,CAAC;QAClB,IAAI,CAAC;YACH,MAAM,GAAG,GAAG,MAAM,MAAM,CAAC,MAAM,CAAC,OAAO,CACrC,EAAE,IAAI,EAAE,UAAU,EAAE,EACpB,GAAG,EACH,KAAK,CAAC,UAA0B,CACjC,CAAC;YACF,MAAM,GAAG,GAAG,MAAM,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,KAAK,EAAE,GAAG,EAAE,SAAS,EAAE,KAAK,EAAE,CAAC,SAAS,CAAC,CAAC,CAAC;YACrF,MAAM,KAAK,GAAG,MAAM,MAAM,CAAC,MAAM,CAAC,OAAO,CACvC,EAAE,IAAI,EAAE,SAAS,EAAE,EAAE,EAAE,KAAK,CAAC,EAAkB,EAAE,EACjD,GAAG,EACH,KAAK,CAAC,UAA0B,CACjC,CAAC;YACF,KAAK,GAAG,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QAC1C,CAAC;QAAC,MAAM,CAAC;YACP,8EAA8E;YAC9E,MAAM,IAAI,KAAK,CAAC,iDAAiD,CAAC,CAAC;QACrE,CAAC;QACD,SAAS,EAAE,CAAC,KAAK,CAAC,CAAC;QACnB,OAAO,KAAK,CAAC;IACf,CAAC,CAAC;AACJ,CAAC"}
@@ -1,58 +0,0 @@
1
- /**
2
- * The `secret` block's ciphertext format, in one place: the secret guard
3
- * checks it, and `encryptSecret` (`@decocms/blocks/secrets`) produces it.
4
- *
5
- * ```
6
- * v1.<wrappedKey>.<iv>.<ciphertext>
7
- * ```
8
- *
9
- * Each segment is unpadded base64url. A fresh AES-256-GCM key encrypts the
10
- * UTF-8 value; the key is wrapped with the site's RSA public key using
11
- * RSA-OAEP with SHA-256.
12
- *
13
- * - `wrappedKey` — the RSA-OAEP output: as long as the modulus, so 256, 384
14
- * or 512 bytes (2048-, 3072- or 4096-bit keys; `deco` creates 3072-bit ones).
15
- * - `iv` — the AES-GCM nonce: exactly 12 bytes.
16
- * - `ciphertext` — the AES-GCM output, its 16-byte tag included: at least 16 bytes.
17
- *
18
- * Checking the structure needs no key, so the content protocol and
19
- * `deco check` refuse text that merely looks like a ciphertext (`v1.hunter2`).
20
- * Web Crypto only: runs in browsers, on Workers and on Node.
21
- */
22
- /** The format version every ciphertext starts with. */
23
- export declare const CIPHERTEXT_VERSION = "v1";
24
- /** The wrapped-key lengths a ciphertext may carry: 2048-, 3072- and 4096-bit RSA. */
25
- export declare const WRAPPED_KEY_BYTES: readonly number[];
26
- /** The AES-GCM nonce length. */
27
- export declare const IV_BYTES = 12;
28
- /** The AES-GCM authentication tag length, the smallest ciphertext segment. */
29
- export declare const GCM_TAG_BYTES = 16;
30
- /**
31
- * The shape of a ciphertext: `v1.` and three base64url segments. A match is
32
- * necessary but not sufficient; `parseCiphertext` also checks segment lengths.
33
- */
34
- export declare const CIPHERTEXT_PATTERN: RegExp;
35
- /** The decoded segments of a ciphertext. */
36
- export interface CiphertextParts {
37
- wrappedKey: Uint8Array;
38
- iv: Uint8Array;
39
- ciphertext: Uint8Array;
40
- }
41
- /** Unpadded base64url. */
42
- export declare function encodeBase64Url(bytes: Uint8Array): string;
43
- /** Decodes unpadded base64url; `null` when `text` isn't canonical base64url. */
44
- export declare function decodeBase64Url(text: string): Uint8Array | null;
45
- /** Splits and decodes a ciphertext; `null` when it isn't well formed. */
46
- export declare function parseCiphertext(text: unknown): CiphertextParts | null;
47
- /** Joins decoded segments into a ciphertext. Throws when a segment has the wrong length. */
48
- export declare function formatCiphertext(parts: CiphertextParts): string;
49
- /** The DER bytes of a single `PUBLIC KEY` PEM block; `null` for anything else. */
50
- export declare function publicKeyDerFromPem(pem: string): Uint8Array | null;
51
- /** A `PUBLIC KEY` PEM block for SPKI `der` bytes, with 64-character lines. */
52
- export declare function publicKeyPemFromDer(der: Uint8Array): string;
53
- /**
54
- * Encrypts `value` for the holder of the private key matching `publicKeyPem`
55
- * (an SPKI `PUBLIC KEY` PEM, such as `.deco/secrets.pub`).
56
- */
57
- export declare function encryptToCiphertext(publicKeyPem: string, value: string): Promise<string>;
58
- //# sourceMappingURL=ciphertext.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"ciphertext.d.ts","sourceRoot":"","sources":["../../src/v8/ciphertext.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,uDAAuD;AACvD,eAAO,MAAM,kBAAkB,OAAO,CAAC;AAEvC,qFAAqF;AACrF,eAAO,MAAM,iBAAiB,EAAE,SAAS,MAAM,EAAoB,CAAC;AAEpE,gCAAgC;AAChC,eAAO,MAAM,QAAQ,KAAK,CAAC;AAE3B,8EAA8E;AAC9E,eAAO,MAAM,aAAa,KAAK,CAAC;AAEhC;;;GAGG;AACH,eAAO,MAAM,kBAAkB,QAAyD,CAAC;AAEzF,4CAA4C;AAC5C,MAAM,WAAW,eAAe;IAC9B,UAAU,EAAE,UAAU,CAAC;IACvB,EAAE,EAAE,UAAU,CAAC;IACf,UAAU,EAAE,UAAU,CAAC;CACxB;AAID,0BAA0B;AAC1B,wBAAgB,eAAe,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAMzD;AAED,gFAAgF;AAChF,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,UAAU,GAAG,IAAI,CAa/D;AAOD,yEAAyE;AACzE,wBAAgB,eAAe,CAAC,IAAI,EAAE,OAAO,GAAG,eAAe,GAAG,IAAI,CASrE;AAED,4FAA4F;AAC5F,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,eAAe,GAAG,MAAM,CAY/D;AAID,kFAAkF;AAClF,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU,GAAG,IAAI,CAYlE;AAED,8EAA8E;AAC9E,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,UAAU,GAAG,MAAM,CAQ3D;AAED;;;GAGG;AACH,wBAAsB,mBAAmB,CAAC,YAAY,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAmB9F"}