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