@heybejs/core 1.2.1 → 1.4.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 +1 -1
- package/src/app.ts +5 -1
- package/src/commands/args.ts +1 -0
- package/src/commands/help.ts +6 -0
- package/src/commands/serve.ts +1 -0
- package/src/http/routes/health.ts +3 -1
- package/src/http/routes/installer.ts +1 -1
- package/src/import/decrypt.ts +12 -1
- package/src/settings.ts +60 -3
- package/ui/dist/assets/index-DYrAitH0.js +23 -0
- package/ui/dist/index.html +1 -1
- package/ui/dist/assets/index-CEiZE18p.js +0 -23
package/package.json
CHANGED
package/src/app.ts
CHANGED
|
@@ -175,6 +175,10 @@ function globalFlags(argv: string[]) {
|
|
|
175
175
|
? values.migrationsDir
|
|
176
176
|
: undefined,
|
|
177
177
|
hooksDir: typeof values.hooksDir === "string" ? values.hooksDir : undefined,
|
|
178
|
+
encryptionEnv:
|
|
179
|
+
typeof values.encryptionEnv === "string" && values.encryptionEnv
|
|
180
|
+
? values.encryptionEnv
|
|
181
|
+
: undefined,
|
|
178
182
|
automigrate: !automigrate
|
|
179
183
|
? undefined
|
|
180
184
|
: raw === undefined || TRUE_VALUES.includes(raw)
|
|
@@ -289,7 +293,7 @@ export function createApp(opts: AppOptions = {}): App {
|
|
|
289
293
|
? openMemoryDb(join(dataDir, "data.db"), now)
|
|
290
294
|
: openDb(join(dataDir, "data.db"), undefined, realTimers);
|
|
291
295
|
const auxDb = openAuxDb(join(dataDir, "auxiliary.db"));
|
|
292
|
-
const settings = new Settings(db);
|
|
296
|
+
const settings = new Settings(db, flags.encryptionEnv);
|
|
293
297
|
const files = appFilesystem(
|
|
294
298
|
s3Switch(
|
|
295
299
|
localFilesystem(join(dataDir, "storage")),
|
package/src/commands/args.ts
CHANGED
package/src/commands/help.ts
CHANGED
|
@@ -32,6 +32,12 @@ export const GLOBAL_FLAG_HELP: FlagHelp[] = [
|
|
|
32
32
|
usage: "the Heybe data directory",
|
|
33
33
|
default: '"./hb_data"',
|
|
34
34
|
},
|
|
35
|
+
{
|
|
36
|
+
name: "encryptionEnv",
|
|
37
|
+
type: "string",
|
|
38
|
+
usage:
|
|
39
|
+
"the env variable whose value of 32 characters will be used\nto encrypt the app settings stored in the database",
|
|
40
|
+
},
|
|
35
41
|
{
|
|
36
42
|
name: "migrationsDir",
|
|
37
43
|
type: "string",
|
package/src/commands/serve.ts
CHANGED
|
@@ -82,6 +82,7 @@ export async function serveCommand(args: string[], app: App): Promise<void> {
|
|
|
82
82
|
const origins = (
|
|
83
83
|
Array.isArray(values.origins) ? values.origins.map(String) : ["*"]
|
|
84
84
|
).flatMap((o) => o.split(","));
|
|
85
|
+
await app.settings.encryptStored();
|
|
85
86
|
await new MigrationsRunner(app, await migrationsList(app)).up();
|
|
86
87
|
if (app.dev) {
|
|
87
88
|
app.collections.onChange = () => writeTypesSafely(app, DEFAULT_TYPES_FILE);
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import type { Router } from "../router";
|
|
2
|
+
import { needsInstaller } from "./installer";
|
|
2
3
|
|
|
3
4
|
const PROXY_HEADERS = ["CF-Connecting-IP", "Fly-Client-IP", "X-Forwarded-For"];
|
|
4
5
|
|
|
5
6
|
// PB apis/health.go
|
|
6
7
|
export function registerHealthRoutes(router: Router): void {
|
|
7
8
|
router.route("GET", "/api/health", (e) => {
|
|
8
|
-
let data = {};
|
|
9
|
+
let data: Record<string, unknown> = {};
|
|
9
10
|
if (e.hasSuperuserAuth()) {
|
|
10
11
|
const headers = [
|
|
11
12
|
...e.app.settings.current().trustedProxy.headers,
|
|
@@ -18,6 +19,7 @@ export function registerHealthRoutes(router: Router): void {
|
|
|
18
19
|
headers.find((h) => (e.req.headers.get(h) ?? "") !== "") ?? "",
|
|
19
20
|
};
|
|
20
21
|
}
|
|
22
|
+
if (needsInstaller(e.app)) data.needsSetup = true;
|
|
21
23
|
return e.json(200, { message: "API is healthy.", code: 200, data });
|
|
22
24
|
});
|
|
23
25
|
}
|
|
@@ -5,7 +5,7 @@ import { internalRecords } from "../../records/internal";
|
|
|
5
5
|
|
|
6
6
|
const TOKEN_DURATION_SECONDS = 30 * 60;
|
|
7
7
|
|
|
8
|
-
function needsInstaller(app: App): boolean {
|
|
8
|
+
export function needsInstaller(app: App): boolean {
|
|
9
9
|
const row = app.db.get<{ total: number }>(
|
|
10
10
|
"SELECT COUNT(*) AS total FROM _superusers WHERE email != ?",
|
|
11
11
|
[INSTALLER_EMAIL],
|
package/src/import/decrypt.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { createDecipheriv } from "node:crypto";
|
|
1
|
+
import { createCipheriv, createDecipheriv, randomBytes } from "node:crypto";
|
|
2
2
|
|
|
3
3
|
const NONCE_SIZE = 12;
|
|
4
4
|
const TAG_SIZE = 16;
|
|
@@ -19,3 +19,14 @@ export function decryptPbValue(cipherText: string, key: string): string {
|
|
|
19
19
|
decipher.final(),
|
|
20
20
|
]).toString("utf8");
|
|
21
21
|
}
|
|
22
|
+
|
|
23
|
+
// PB security.Encrypt, the inverse of decryptPbValue
|
|
24
|
+
export function encryptPbValue(plainText: string, key: string): string {
|
|
25
|
+
const nonce = randomBytes(NONCE_SIZE);
|
|
26
|
+
const cipher = createCipheriv("aes-256-gcm", Buffer.from(key, "utf8"), nonce);
|
|
27
|
+
const body = Buffer.concat([
|
|
28
|
+
cipher.update(plainText, "utf8"),
|
|
29
|
+
cipher.final(),
|
|
30
|
+
]);
|
|
31
|
+
return Buffer.concat([nonce, body, cipher.getAuthTag()]).toString("base64");
|
|
32
|
+
}
|
package/src/settings.ts
CHANGED
|
@@ -15,6 +15,7 @@ import { SettingsReloadEvent } from "./hooks/events";
|
|
|
15
15
|
import { isEmail } from "./fields/email";
|
|
16
16
|
import { isUrl } from "./fields/url";
|
|
17
17
|
import { isIpOrSubnet } from "./ip";
|
|
18
|
+
import { decryptPbValue, encryptPbValue } from "./import/decrypt";
|
|
18
19
|
import { ValidationError } from "./util";
|
|
19
20
|
|
|
20
21
|
export interface RateLimitRule {
|
|
@@ -557,7 +558,63 @@ export class Settings {
|
|
|
557
558
|
private cached: SettingsData | null = null;
|
|
558
559
|
app?: App;
|
|
559
560
|
|
|
560
|
-
|
|
561
|
+
// `encryptionEnv` is the --encryptionEnv flag: the env variable that holds the settings key
|
|
562
|
+
constructor(
|
|
563
|
+
private readonly db: Db,
|
|
564
|
+
private readonly encryptionEnv?: string,
|
|
565
|
+
) {}
|
|
566
|
+
|
|
567
|
+
private key(): string | undefined {
|
|
568
|
+
if (!this.encryptionEnv) return undefined;
|
|
569
|
+
const key = process.env[this.encryptionEnv];
|
|
570
|
+
if (!key)
|
|
571
|
+
throw new Error(
|
|
572
|
+
`the --encryptionEnv variable "${this.encryptionEnv}" is not set or empty`,
|
|
573
|
+
);
|
|
574
|
+
if (key.length !== 32)
|
|
575
|
+
throw new Error(
|
|
576
|
+
`the --encryptionEnv variable "${this.encryptionEnv}" must hold exactly 32 characters`,
|
|
577
|
+
);
|
|
578
|
+
return key;
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
// plain JSON always starts with "{", an encrypted value is base64
|
|
582
|
+
private decode(stored: string): unknown {
|
|
583
|
+
if (stored.startsWith("{")) return JSON.parse(stored);
|
|
584
|
+
const key = this.key();
|
|
585
|
+
if (!key)
|
|
586
|
+
throw new Error(
|
|
587
|
+
"the app settings are encrypted, start with --encryptionEnv and the name of the variable that holds the key",
|
|
588
|
+
);
|
|
589
|
+
try {
|
|
590
|
+
return JSON.parse(decryptPbValue(stored, key));
|
|
591
|
+
} catch {
|
|
592
|
+
throw new Error(
|
|
593
|
+
`failed to decrypt the app settings: the key in "${this.encryptionEnv}" does not match the one they were encrypted with`,
|
|
594
|
+
);
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
private encode(data: SettingsData): string {
|
|
599
|
+
const json = JSON.stringify(data);
|
|
600
|
+
const key = this.key();
|
|
601
|
+
return key ? encryptPbValue(json, key) : json;
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
// Encrypts settings that were stored as plain JSON, so turning the flag on takes effect at start
|
|
605
|
+
async encryptStored(): Promise<void> {
|
|
606
|
+
const key = this.key();
|
|
607
|
+
if (!key) return;
|
|
608
|
+
await this.db.write(() => {
|
|
609
|
+
const row = this.db.get<{ value: string }>(
|
|
610
|
+
"SELECT value FROM _params WHERE key = 'settings'",
|
|
611
|
+
);
|
|
612
|
+
if (!row?.value.startsWith("{")) return;
|
|
613
|
+
this.db.run("UPDATE _params SET value = ? WHERE key = 'settings'", [
|
|
614
|
+
encryptPbValue(row.value, key),
|
|
615
|
+
]);
|
|
616
|
+
});
|
|
617
|
+
}
|
|
561
618
|
|
|
562
619
|
// The cached settings without a copy, for hot paths: callers must not modify them.
|
|
563
620
|
current(): Readonly<SettingsData> {
|
|
@@ -590,7 +647,7 @@ export class Settings {
|
|
|
590
647
|
);
|
|
591
648
|
return merge(
|
|
592
649
|
SCHEMA,
|
|
593
|
-
row ?
|
|
650
|
+
row ? this.decode(row.value) : {},
|
|
594
651
|
DEFAULTS,
|
|
595
652
|
) as SettingsData;
|
|
596
653
|
}
|
|
@@ -625,7 +682,7 @@ export class Settings {
|
|
|
625
682
|
);
|
|
626
683
|
this.db.run(
|
|
627
684
|
"INSERT INTO _params (key, value) VALUES ('settings', ?) ON CONFLICT(key) DO UPDATE SET value = excluded.value",
|
|
628
|
-
[
|
|
685
|
+
[this.encode(next)],
|
|
629
686
|
);
|
|
630
687
|
this.db.onComplete((err) => {
|
|
631
688
|
if (err !== null) this.cached = null;
|