@decocms/start 0.35.1 → 0.36.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -1
- package/src/apps/autoconfig.ts +9 -6
- package/src/sdk/crypto.ts +150 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@decocms/start",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.36.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Deco framework for TanStack Start - CMS bridge, admin protocol, hooks, schema generation",
|
|
6
6
|
"main": "./src/index.ts",
|
|
@@ -21,6 +21,7 @@
|
|
|
21
21
|
"./sdk/cachedLoader": "./src/sdk/cachedLoader.ts",
|
|
22
22
|
"./sdk/serverTimings": "./src/sdk/serverTimings.ts",
|
|
23
23
|
"./sdk/cacheHeaders": "./src/sdk/cacheHeaders.ts",
|
|
24
|
+
"./sdk/crypto": "./src/sdk/crypto.ts",
|
|
24
25
|
"./sdk/invoke": "./src/sdk/invoke.ts",
|
|
25
26
|
"./sdk/instrumentedFetch": "./src/sdk/instrumentedFetch.ts",
|
|
26
27
|
"./sdk/workerEntry": "./src/sdk/workerEntry.ts",
|
package/src/apps/autoconfig.ts
CHANGED
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
|
|
14
14
|
import { setInvokeActions, type InvokeAction } from "../admin/invoke";
|
|
15
15
|
import { onChange } from "../cms/loader";
|
|
16
|
+
import { resolveSecret } from "../sdk/crypto";
|
|
16
17
|
|
|
17
18
|
// ---------------------------------------------------------------------------
|
|
18
19
|
// Known app block keys → dynamic import + configure
|
|
@@ -33,12 +34,14 @@ const KNOWN_APPS: Record<string, AppAutoconfigurator> = {
|
|
|
33
34
|
const { configureResend } = resendClient as { configureResend: (cfg: any) => void };
|
|
34
35
|
const { sendEmail } = resendActions as { sendEmail: (props: any) => Promise<any> };
|
|
35
36
|
|
|
36
|
-
const apiKey =
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
:
|
|
40
|
-
|
|
41
|
-
|
|
37
|
+
const apiKey = await resolveSecret(block.apiKey, "RESEND_API_KEY");
|
|
38
|
+
if (!apiKey) {
|
|
39
|
+
console.warn(
|
|
40
|
+
"[autoconfig] deco-resend: no API key found." +
|
|
41
|
+
" Set DECO_CRYPTO_KEY to decrypt CMS secrets, or set RESEND_API_KEY as fallback.",
|
|
42
|
+
);
|
|
43
|
+
return {};
|
|
44
|
+
}
|
|
42
45
|
|
|
43
46
|
configureResend({
|
|
44
47
|
apiKey,
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Secret decryption for CMS encrypted values.
|
|
3
|
+
*
|
|
4
|
+
* CMS blocks store sensitive values (API keys, tokens) encrypted with AES-CBC.
|
|
5
|
+
* The encryption key is stored in the DECO_CRYPTO_KEY environment variable
|
|
6
|
+
* as a base64-encoded JSON: { key: number[], iv: number[] }
|
|
7
|
+
*
|
|
8
|
+
* Usage:
|
|
9
|
+
* import { decryptSecret, resolveSecret } from "@decocms/start/sdk/crypto";
|
|
10
|
+
*
|
|
11
|
+
* // Decrypt a hex-encoded encrypted string
|
|
12
|
+
* const apiKey = await decryptSecret("888fafd937dd...");
|
|
13
|
+
*
|
|
14
|
+
* // Or resolve a CMS secret block (handles all formats)
|
|
15
|
+
* const apiKey = await resolveSecret(block.apiKey, "RESEND_API_KEY");
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
const textDecoder = new TextDecoder();
|
|
19
|
+
const textEncoder = new TextEncoder();
|
|
20
|
+
|
|
21
|
+
// Cache the imported key
|
|
22
|
+
let cachedKey: Promise<{ key: CryptoKey; iv: Uint8Array }> | null = null;
|
|
23
|
+
|
|
24
|
+
function hexToBytes(hex: string): Uint8Array {
|
|
25
|
+
const bytes = new Uint8Array(hex.length / 2);
|
|
26
|
+
for (let i = 0; i < hex.length; i += 2) {
|
|
27
|
+
bytes[i / 2] = parseInt(hex.substring(i, i + 2), 16);
|
|
28
|
+
}
|
|
29
|
+
return bytes;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Get the AES key from DECO_CRYPTO_KEY environment variable.
|
|
34
|
+
* Returns null if not set.
|
|
35
|
+
*/
|
|
36
|
+
function getKeyFromEnv(): Promise<{ key: CryptoKey; iv: Uint8Array }> | null {
|
|
37
|
+
const envKey = process.env.DECO_CRYPTO_KEY;
|
|
38
|
+
if (!envKey) return null;
|
|
39
|
+
|
|
40
|
+
return cachedKey ??= (async () => {
|
|
41
|
+
const parsed = JSON.parse(atob(envKey));
|
|
42
|
+
const keyBytes = new Uint8Array(
|
|
43
|
+
Array.isArray(parsed.key) ? parsed.key : Object.values(parsed.key),
|
|
44
|
+
);
|
|
45
|
+
const iv = new Uint8Array(
|
|
46
|
+
Array.isArray(parsed.iv) ? parsed.iv : Object.values(parsed.iv),
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
const importedKey = await crypto.subtle.importKey(
|
|
50
|
+
"raw",
|
|
51
|
+
keyBytes.buffer,
|
|
52
|
+
"AES-CBC",
|
|
53
|
+
false,
|
|
54
|
+
["decrypt"],
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
return { key: importedKey, iv };
|
|
58
|
+
})();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Check if the crypto key is available.
|
|
63
|
+
*/
|
|
64
|
+
export function hasCryptoKey(): boolean {
|
|
65
|
+
return !!process.env.DECO_CRYPTO_KEY;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Decrypt a hex-encoded AES-CBC encrypted string.
|
|
70
|
+
* Requires DECO_CRYPTO_KEY environment variable.
|
|
71
|
+
*
|
|
72
|
+
* @returns The decrypted string, or null if decryption fails.
|
|
73
|
+
*/
|
|
74
|
+
export async function decryptSecret(encryptedHex: string): Promise<string | null> {
|
|
75
|
+
const keyPromise = getKeyFromEnv();
|
|
76
|
+
if (!keyPromise) {
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
try {
|
|
81
|
+
const { key, iv } = await keyPromise;
|
|
82
|
+
const encryptedBytes = hexToBytes(encryptedHex);
|
|
83
|
+
const decrypted = await crypto.subtle.decrypt(
|
|
84
|
+
{ name: "AES-CBC", iv } as AesCbcParams,
|
|
85
|
+
key,
|
|
86
|
+
encryptedBytes as unknown as BufferSource,
|
|
87
|
+
);
|
|
88
|
+
return textDecoder.decode(new Uint8Array(decrypted));
|
|
89
|
+
} catch (e) {
|
|
90
|
+
console.warn("[crypto] Failed to decrypt secret:", (e as Error).message);
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// In-memory cache for resolved secrets
|
|
96
|
+
const secretCache = new Map<string, string | null>();
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Resolve a CMS secret value from multiple sources.
|
|
100
|
+
*
|
|
101
|
+
* Resolution order:
|
|
102
|
+
* 1. Plain string → use directly
|
|
103
|
+
* 2. Object with .get() → call .get() (old Secret loader pattern)
|
|
104
|
+
* 3. Object with .encrypted → decrypt using DECO_CRYPTO_KEY
|
|
105
|
+
* 4. Environment variable (envVarName) → fallback
|
|
106
|
+
*
|
|
107
|
+
* @param value - The secret value from CMS block (string | { encrypted } | { get })
|
|
108
|
+
* @param envVarName - Optional env var name to use as fallback (e.g. "RESEND_API_KEY")
|
|
109
|
+
* @returns The resolved secret string, or null if not available
|
|
110
|
+
*/
|
|
111
|
+
export async function resolveSecret(
|
|
112
|
+
value: unknown,
|
|
113
|
+
envVarName?: string,
|
|
114
|
+
): Promise<string | null> {
|
|
115
|
+
// 1. Plain string
|
|
116
|
+
if (typeof value === "string" && value.length > 0) {
|
|
117
|
+
return value;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (value && typeof value === "object") {
|
|
121
|
+
const obj = value as Record<string, any>;
|
|
122
|
+
|
|
123
|
+
// 2. Secret object with .get()
|
|
124
|
+
if (typeof obj.get === "function") {
|
|
125
|
+
const result = obj.get();
|
|
126
|
+
if (typeof result === "string" && result.length > 0) return result;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// 3. Encrypted secret
|
|
130
|
+
if (typeof obj.encrypted === "string" && obj.encrypted.length > 0) {
|
|
131
|
+
const cacheKey = obj.encrypted;
|
|
132
|
+
if (secretCache.has(cacheKey)) return secretCache.get(cacheKey)!;
|
|
133
|
+
|
|
134
|
+
const decrypted = await decryptSecret(obj.encrypted);
|
|
135
|
+
// Only cache successful decryptions — null would block env var fallback
|
|
136
|
+
if (decrypted) {
|
|
137
|
+
secretCache.set(cacheKey, decrypted);
|
|
138
|
+
return decrypted;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// 4. Environment variable fallback
|
|
144
|
+
if (envVarName) {
|
|
145
|
+
const envValue = process.env[envVarName];
|
|
146
|
+
if (envValue) return envValue;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return null;
|
|
150
|
+
}
|