Files
inbuxa-webmail/server/src/secretenv.test.ts
jcoffey-dev 5e9605ac54
ci / node (pull_request) Skipped
ci / version (pull_request) Skipped
github/ci (branch) GitHub Actions
ci / github (pull_request) Successful in 1m45s
ci / docker-build (pull_request) Skipped
ci / publish (pull_request) Skipped
ci / announce (pull_request) Skipped
Read APP_SECRET and OAUTH_CLIENT_SECRET from files
APP_SECRET_FILE, APP_SECRET_PREVIOUS_FILE and OAUTH_CLIENT_SECRET_FILE
read the secret from a mounted file, with one trailing newline dropped.
Both forms set, or a file that can't be read or is empty, stops the start
with an error naming the variable and path, never the value.

README: the read-only root filesystem, and what the webmail writes.
2026-11-18 17:06:26 -07:00

46 lines
1.8 KiB
TypeScript

import { test } from "node:test";
import assert from "node:assert/strict";
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { secretEnv } from "./secretenv.js";
const never = () => {
throw new Error("read when it shouldn't be");
};
test("a secret from the environment, as before", () => {
assert.equal(secretEnv("APP_SECRET", { APP_SECRET: "from-env" }, never), "from-env");
assert.equal(secretEnv("APP_SECRET", {}, never), "");
});
test("a secret from a file, one trailing newline dropped", () => {
const dir = mkdtempSync(join(tmpdir(), "webmail-secret-"));
try {
const path = join(dir, "app-secret");
writeFileSync(path, "from-file\n");
assert.equal(secretEnv("APP_SECRET", { APP_SECRET_FILE: path }), "from-file");
writeFileSync(path, "crlf\r\n");
assert.equal(secretEnv("APP_SECRET", { APP_SECRET_FILE: path }), "crlf");
writeFileSync(path, " kept as is ");
assert.equal(secretEnv("APP_SECRET", { APP_SECRET_FILE: path }), " kept as is ");
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
test("both set is refused, without the value in the message", () => {
assert.throws(
() => secretEnv("OAUTH_CLIENT_SECRET", { OAUTH_CLIENT_SECRET: "s3cret-value", OAUTH_CLIENT_SECRET_FILE: "/run/x" }, never),
(err: Error) => /both set/.test(err.message) && !err.message.includes("s3cret-value"),
);
});
test("a missing or empty file is refused, naming the variable and path", () => {
assert.throws(
() => secretEnv("APP_SECRET", { APP_SECRET_FILE: "/nonexistent/app-secret" }),
/APP_SECRET_FILE: can't read \/nonexistent\/app-secret \(ENOENT\)/,
);
assert.throws(() => secretEnv("APP_SECRET", { APP_SECRET_FILE: "/run/s" }, () => "\n"), /APP_SECRET_FILE: \/run\/s is empty/);
});