@nextblock-cms/db 0.10.5 → 0.10.7
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/lib/secret-crypto.cjs.js +1 -1
- package/lib/secret-crypto.es.js +85 -79
- package/package.json +1 -1
package/lib/secret-crypto.cjs.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const o=require("node:crypto"),c="aes-256-gcm",a=1;function E(t){const e=t.trim();if(!e)throw new Error("A non-empty encryption secret is required to manage stored secrets.");return o.createHash("sha256").update(e).digest()}function i(t){if(!t||typeof t!="object")return!1;const e=t;return e.algorithm===c&&e.version===a&&typeof e.authTag=="string"&&typeof e.ciphertext=="string"&&typeof e.iv=="string"}function g(t){if(!i(t))throw new Error("Invalid encrypted secret payload.");const e=t;return{algorithm:e.algorithm,authTag:e.authTag,ciphertext:e.ciphertext,iv:e.iv,last4:typeof e.last4=="string"?e.last4:"",updatedAt:typeof e.updatedAt=="string"?e.updatedAt:"",version:e.version}}function d(t){const e=t.value.trim();if(!e)throw new Error("A non-empty value is required to encrypt a secret.");const n=E(t.encryptionSecret),r=o.randomBytes(12),s=o.createCipheriv(c,n,r),S=Buffer.concat([s.update(e,"utf8"),s.final()]),h=s.getAuthTag();return{algorithm:c,authTag:h.toString("base64"),ciphertext:S.toString("base64"),iv:r.toString("base64"),last4:e.slice(-4),updatedAt:(t.now??new Date).toISOString(),version:a}}function u(t){const e=g(t.envelope),n=E(t.encryptionSecret);try{const r=o.createDecipheriv(c,n,Buffer.from(e.iv,"base64"));return r.setAuthTag(Buffer.from(e.authTag,"base64")),Buffer.concat([r.update(Buffer.from(e.ciphertext,"base64")),r.final()]).toString("utf8")}catch{throw new Error("Failed to decrypt stored secret.")}}function l(t){const e=(t??"").trim();return e?`•••• ${e}`:"Stored secret"}function v(t){if(!i(t))return{hasStoredValue:!1,last4:null,maskedValue:null,updatedAt:null};const e=t,n=typeof e.last4=="string"&&e.last4?e.last4:null;return{hasStoredValue:!0,last4:n,maskedValue:l(n),updatedAt:typeof e.updatedAt=="string"&&e.updatedAt?e.updatedAt:null}}const _="nextblock-secret-encryption-key-v1";function m(){const t=process.env.SUPABASE_SERVICE_ROLE_KEY??process.env.SUPABASE_SECRET_KEY;return typeof t!="string"||!t.trim()?null:o.createHmac("sha256",t.trim()).update(_).digest("hex")}function f(){const t=[],e=n=>{const r=typeof n=="string"?n.trim():"";r&&!t.includes(r)&&t.push(r)};return e(process.env.NEXTBLOCK_ENCRYPTION_KEY),e(process.env.CORTEX_AI_ENCRYPTION_KEY),e(m()),t}function p(){return f()[0]??null}function K(){return p()!==null}function y(){const t=p();if(!t)throw new Error("An encryption key (NEXTBLOCK_ENCRYPTION_KEY or CORTEX_AI_ENCRYPTION_KEY) is required to manage stored secrets.");return t}function T(t,e){return d({value:t,encryptionSecret:y(),now:e})}function A(t){return u({envelope:t,encryptionSecret:y()})}function O(t){if(!i(t))return null;for(const e of f())try{return u({envelope:t,encryptionSecret:e})}catch{}return null}exports.SECRET_ENVELOPE_ALGORITHM=c;exports.SECRET_ENVELOPE_VERSION=a;exports.decryptSecret=u;exports.decryptWithEnvKey=A;exports.encryptSecret=d;exports.encryptWithEnvKey=T;exports.getMaskedSecret=l;exports.getSecretEnvelopeStatus=v;exports.hasSecretEncryptionKey=K;exports.isEncryptedSecretEnvelope=i;exports.requireSecretEncryptionKey=y;exports.resolveSecretEncryptionKey=p;exports.tryDecryptWithEnvKey=O;
|
package/lib/secret-crypto.es.js
CHANGED
|
@@ -1,125 +1,131 @@
|
|
|
1
|
-
import { createDecipheriv as
|
|
2
|
-
const o = "aes-256-gcm",
|
|
3
|
-
function a(
|
|
4
|
-
const
|
|
5
|
-
if (!
|
|
1
|
+
import { createDecipheriv as E, randomBytes as h, createCipheriv as g, createHash as S, createHmac as v } from "node:crypto";
|
|
2
|
+
const o = "aes-256-gcm", s = 1;
|
|
3
|
+
function a(t) {
|
|
4
|
+
const e = t.trim();
|
|
5
|
+
if (!e)
|
|
6
6
|
throw new Error("A non-empty encryption secret is required to manage stored secrets.");
|
|
7
|
-
return
|
|
7
|
+
return S("sha256").update(e).digest();
|
|
8
8
|
}
|
|
9
|
-
function
|
|
10
|
-
if (!
|
|
9
|
+
function i(t) {
|
|
10
|
+
if (!t || typeof t != "object")
|
|
11
11
|
return !1;
|
|
12
|
-
const
|
|
13
|
-
return
|
|
12
|
+
const e = t;
|
|
13
|
+
return e.algorithm === o && e.version === s && typeof e.authTag == "string" && typeof e.ciphertext == "string" && typeof e.iv == "string";
|
|
14
14
|
}
|
|
15
|
-
function
|
|
16
|
-
if (!
|
|
15
|
+
function m(t) {
|
|
16
|
+
if (!i(t))
|
|
17
17
|
throw new Error("Invalid encrypted secret payload.");
|
|
18
|
-
const
|
|
18
|
+
const e = t;
|
|
19
19
|
return {
|
|
20
|
-
algorithm:
|
|
21
|
-
authTag:
|
|
22
|
-
ciphertext:
|
|
23
|
-
iv:
|
|
24
|
-
last4: typeof
|
|
25
|
-
updatedAt: typeof
|
|
26
|
-
version:
|
|
20
|
+
algorithm: e.algorithm,
|
|
21
|
+
authTag: e.authTag,
|
|
22
|
+
ciphertext: e.ciphertext,
|
|
23
|
+
iv: e.iv,
|
|
24
|
+
last4: typeof e.last4 == "string" ? e.last4 : "",
|
|
25
|
+
updatedAt: typeof e.updatedAt == "string" ? e.updatedAt : "",
|
|
26
|
+
version: e.version
|
|
27
27
|
};
|
|
28
28
|
}
|
|
29
|
-
function
|
|
30
|
-
const
|
|
31
|
-
if (!
|
|
29
|
+
function A(t) {
|
|
30
|
+
const e = t.value.trim();
|
|
31
|
+
if (!e)
|
|
32
32
|
throw new Error("A non-empty value is required to encrypt a secret.");
|
|
33
|
-
const
|
|
33
|
+
const n = a(t.encryptionSecret), r = h(12), c = g(o, n, r), l = Buffer.concat([c.update(e, "utf8"), c.final()]), y = c.getAuthTag();
|
|
34
34
|
return {
|
|
35
35
|
algorithm: o,
|
|
36
36
|
authTag: y.toString("base64"),
|
|
37
37
|
ciphertext: l.toString("base64"),
|
|
38
|
-
iv:
|
|
39
|
-
last4:
|
|
40
|
-
updatedAt: (
|
|
41
|
-
version:
|
|
38
|
+
iv: r.toString("base64"),
|
|
39
|
+
last4: e.slice(-4),
|
|
40
|
+
updatedAt: (t.now ?? /* @__PURE__ */ new Date()).toISOString(),
|
|
41
|
+
version: s
|
|
42
42
|
};
|
|
43
43
|
}
|
|
44
|
-
function
|
|
45
|
-
const
|
|
44
|
+
function u(t) {
|
|
45
|
+
const e = m(t.envelope), n = a(t.encryptionSecret);
|
|
46
46
|
try {
|
|
47
|
-
const
|
|
47
|
+
const r = E(
|
|
48
48
|
o,
|
|
49
|
-
|
|
50
|
-
Buffer.from(
|
|
49
|
+
n,
|
|
50
|
+
Buffer.from(e.iv, "base64")
|
|
51
51
|
);
|
|
52
|
-
return
|
|
53
|
-
|
|
54
|
-
|
|
52
|
+
return r.setAuthTag(Buffer.from(e.authTag, "base64")), Buffer.concat([
|
|
53
|
+
r.update(Buffer.from(e.ciphertext, "base64")),
|
|
54
|
+
r.final()
|
|
55
55
|
]).toString("utf8");
|
|
56
56
|
} catch {
|
|
57
57
|
throw new Error("Failed to decrypt stored secret.");
|
|
58
58
|
}
|
|
59
59
|
}
|
|
60
|
-
function
|
|
61
|
-
const
|
|
62
|
-
return
|
|
60
|
+
function _(t) {
|
|
61
|
+
const e = (t ?? "").trim();
|
|
62
|
+
return e ? `•••• ${e}` : "Stored secret";
|
|
63
63
|
}
|
|
64
|
-
function
|
|
65
|
-
if (!
|
|
64
|
+
function R(t) {
|
|
65
|
+
if (!i(t))
|
|
66
66
|
return { hasStoredValue: !1, last4: null, maskedValue: null, updatedAt: null };
|
|
67
|
-
const
|
|
67
|
+
const e = t, n = typeof e.last4 == "string" && e.last4 ? e.last4 : null;
|
|
68
68
|
return {
|
|
69
69
|
hasStoredValue: !0,
|
|
70
|
-
last4:
|
|
71
|
-
maskedValue:
|
|
72
|
-
updatedAt: typeof
|
|
70
|
+
last4: n,
|
|
71
|
+
maskedValue: _(n),
|
|
72
|
+
updatedAt: typeof e.updatedAt == "string" && e.updatedAt ? e.updatedAt : null
|
|
73
73
|
};
|
|
74
74
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
75
|
+
const T = "nextblock-secret-encryption-key-v1";
|
|
76
|
+
function K() {
|
|
77
|
+
const t = process.env.SUPABASE_SERVICE_ROLE_KEY ?? process.env.SUPABASE_SECRET_KEY;
|
|
78
|
+
return typeof t != "string" || !t.trim() ? null : v("sha256", t.trim()).update(T).digest("hex");
|
|
79
|
+
}
|
|
80
|
+
function p() {
|
|
81
|
+
const t = [], e = (n) => {
|
|
82
|
+
const r = typeof n == "string" ? n.trim() : "";
|
|
83
|
+
r && !t.includes(r) && t.push(r);
|
|
84
|
+
};
|
|
85
|
+
return e(process.env.NEXTBLOCK_ENCRYPTION_KEY), e(process.env.CORTEX_AI_ENCRYPTION_KEY), e(K()), t;
|
|
86
|
+
}
|
|
87
|
+
function d() {
|
|
88
|
+
return p()[0] ?? null;
|
|
81
89
|
}
|
|
82
90
|
function N() {
|
|
83
|
-
return
|
|
91
|
+
return d() !== null;
|
|
84
92
|
}
|
|
85
93
|
function f() {
|
|
86
|
-
const
|
|
87
|
-
if (!
|
|
94
|
+
const t = d();
|
|
95
|
+
if (!t)
|
|
88
96
|
throw new Error(
|
|
89
97
|
"An encryption key (NEXTBLOCK_ENCRYPTION_KEY or CORTEX_AI_ENCRYPTION_KEY) is required to manage stored secrets."
|
|
90
98
|
);
|
|
91
|
-
return
|
|
99
|
+
return t;
|
|
92
100
|
}
|
|
93
|
-
function
|
|
94
|
-
return
|
|
101
|
+
function C(t, e) {
|
|
102
|
+
return A({ value: t, encryptionSecret: f(), now: e });
|
|
95
103
|
}
|
|
96
|
-
function
|
|
97
|
-
return
|
|
104
|
+
function I(t) {
|
|
105
|
+
return u({ envelope: t, encryptionSecret: f() });
|
|
98
106
|
}
|
|
99
|
-
function
|
|
100
|
-
if (!
|
|
101
|
-
return null;
|
|
102
|
-
const t = s();
|
|
103
|
-
if (!t)
|
|
107
|
+
function w(t) {
|
|
108
|
+
if (!i(t))
|
|
104
109
|
return null;
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
+
for (const e of p())
|
|
111
|
+
try {
|
|
112
|
+
return u({ envelope: t, encryptionSecret: e });
|
|
113
|
+
} catch {
|
|
114
|
+
}
|
|
115
|
+
return null;
|
|
110
116
|
}
|
|
111
117
|
export {
|
|
112
118
|
o as SECRET_ENVELOPE_ALGORITHM,
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
119
|
+
s as SECRET_ENVELOPE_VERSION,
|
|
120
|
+
u as decryptSecret,
|
|
121
|
+
I as decryptWithEnvKey,
|
|
122
|
+
A as encryptSecret,
|
|
123
|
+
C as encryptWithEnvKey,
|
|
124
|
+
_ as getMaskedSecret,
|
|
125
|
+
R as getSecretEnvelopeStatus,
|
|
120
126
|
N as hasSecretEncryptionKey,
|
|
121
|
-
|
|
127
|
+
i as isEncryptedSecretEnvelope,
|
|
122
128
|
f as requireSecretEncryptionKey,
|
|
123
|
-
|
|
124
|
-
|
|
129
|
+
d as resolveSecretEncryptionKey,
|
|
130
|
+
w as tryDecryptWithEnvKey
|
|
125
131
|
};
|