@decocms/blocks 7.34.3 → 7.34.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@decocms/blocks",
3
- "version": "7.34.3",
3
+ "version": "7.34.4",
4
4
  "type": "module",
5
5
  "engines": {
6
6
  "node": ">=24"
@@ -0,0 +1,65 @@
1
+ /**
2
+ * Regression test for the CF Workers secret-decryption bug: in production/preview
3
+ * DECO_CRYPTO_KEY is neither in process.env nor in .dev.vars — it only reaches
4
+ * getEnvVar() via the `env` binding stashed in RequestContext (setRuntimeEnv).
5
+ * If getEnvVar() stops consulting getRuntimeEnv(), encrypted secrets silently
6
+ * decrypt to null in production and this test fails.
7
+ */
8
+
9
+ import { webcrypto } from "node:crypto";
10
+ import { beforeAll, describe, expect, it } from "vitest";
11
+ import { resolveSecret } from "./crypto";
12
+ import { setRuntimeEnv } from "./otelAdapters";
13
+ import { RequestContext } from "./requestContext";
14
+
15
+ // Ensure crypto.subtle exists in the Node test runtime.
16
+ beforeAll(() => {
17
+ if (!globalThis.crypto?.subtle) {
18
+ (globalThis as { crypto: Crypto }).crypto = webcrypto as unknown as Crypto;
19
+ }
20
+ });
21
+
22
+ /** Build a DECO_CRYPTO_KEY value + an encrypted-hex ciphertext for `plaintext`. */
23
+ async function makeKeyAndCiphertext(plaintext: string) {
24
+ const rawKey = webcrypto.getRandomValues(new Uint8Array(16));
25
+ const iv = webcrypto.getRandomValues(new Uint8Array(16));
26
+ const key = await webcrypto.subtle.importKey("raw", rawKey, "AES-CBC", false, [
27
+ "encrypt",
28
+ ]);
29
+ const ct = new Uint8Array(
30
+ await webcrypto.subtle.encrypt(
31
+ { name: "AES-CBC", iv },
32
+ key,
33
+ new TextEncoder().encode(plaintext),
34
+ ),
35
+ );
36
+ const encryptedHex = Array.from(ct)
37
+ .map((b) => b.toString(16).padStart(2, "0"))
38
+ .join("");
39
+ const decoCryptoKey = btoa(
40
+ JSON.stringify({ key: Array.from(rawKey), iv: Array.from(iv) }),
41
+ );
42
+ return { decoCryptoKey, encryptedHex };
43
+ }
44
+
45
+ describe("resolveSecret in CF Workers (no process.env / no .dev.vars)", () => {
46
+ it("decrypts using DECO_CRYPTO_KEY from the RequestContext env binding", async () => {
47
+ // Use a unique plaintext so the module-level secretCache can't mask a
48
+ // regression with a value cached by another test/run.
49
+ const plaintext = `appToken-${Math.random().toString(36).slice(2)}`;
50
+ const { decoCryptoKey, encryptedHex } = await makeKeyAndCiphertext(plaintext);
51
+
52
+ // Guard: process.env must NOT carry the key, so the only source is the ALS.
53
+ expect(process.env.DECO_CRYPTO_KEY).toBeUndefined();
54
+
55
+ const resolved = await RequestContext.run(
56
+ new Request("https://site.example/"),
57
+ async () => {
58
+ setRuntimeEnv({ DECO_CRYPTO_KEY: decoCryptoKey });
59
+ return resolveSecret({ encrypted: encryptedHex });
60
+ },
61
+ );
62
+
63
+ expect(resolved).toBe(plaintext);
64
+ });
65
+ });
package/src/sdk/crypto.ts CHANGED
@@ -15,6 +15,8 @@
15
15
  * const apiKey = await resolveSecret(block.apiKey, "RESEND_API_KEY");
16
16
  */
17
17
 
18
+ import { getRuntimeEnv } from "./otelAdapters";
19
+
18
20
  const textDecoder = new TextDecoder();
19
21
  const textEncoder = new TextEncoder();
20
22
 
@@ -150,10 +152,13 @@ export async function resolveSecret(
150
152
  }
151
153
 
152
154
  /**
153
- * Get an environment variable, checking process.env first, then .dev.vars file.
155
+ * Get an environment variable, checking process.env first, then .dev.vars file,
156
+ * then the Cloudflare Workers `env` binding stashed in RequestContext.
154
157
  * Cloudflare Workers dev mode stores env vars in .dev.vars but they're only
155
158
  * accessible via the `env` binding inside request handlers. During setup.ts
156
- * (module-level init), we need to read the file directly.
159
+ * (module-level init), we need to read the file directly. In production/preview
160
+ * there is no .dev.vars and no process.env — the only source is the `env` object
161
+ * passed to the fetch handler, which workerEntry.ts stashes via setRuntimeEnv().
157
162
  */
158
163
  function getEnvVar(name: string): string | undefined {
159
164
  // 1. process.env (works in Node, may work in Workers with nodejs_compat)
@@ -181,5 +186,14 @@ function getEnvVar(name: string): string | undefined {
181
186
  // fs not available (e.g., pure Worker runtime) — ignore
182
187
  }
183
188
 
189
+ // 3. CF Workers `env` binding, stashed per-request in RequestContext by
190
+ // workerEntry.ts's setRuntimeEnv(env). This is the ONLY source in a real
191
+ // Workers production/preview deploy (no process.env, no .dev.vars). Returns
192
+ // undefined at module-init time (no active request) — that's why apps are
193
+ // re-configured on the first request via reconfigureAppsOnce().
194
+ const runtimeEnv = getRuntimeEnv();
195
+ const v = runtimeEnv?.[name];
196
+ if (typeof v === "string" && v) return v;
197
+
184
198
  return undefined;
185
199
  }