M001: replace bootstrap setup with forced password rotation

This commit is contained in:
illusia committed 2026-09-01 13:09:51 +08:00
1 parent 34ef16b737
commit 7dcbcaae78
14 files changed
+369 -329

No files matched your search

+12 -2
View File
@@ -5,6 +5,8 @@ const SCRYPT_PARAMETERS = Object.freeze({ N: 32768, r: 8, p: 1, maxmem: 64 * 102
export interface AdminCredential {
version: 1;
username: string;
mustChangePassword: boolean;
salt: string;
hash: string;
parameters: {
@@ -14,19 +16,27 @@ export interface AdminCredential {
};
}
export interface AdminPasswordOptions {
username?: string;
mustChangePassword?: boolean;
allowWeakPassword?: boolean;
}
export function validateAdminPassword(password: string): string | null {
if (password.length < 12) return "Admin password must contain at least 12 characters";
if (password.length > 1024) return "Admin password is too long";
return null;
}
export async function hashAdminPassword(password: string): Promise<AdminCredential> {
const validationError = validateAdminPassword(password);
export async function hashAdminPassword(password: string, options: AdminPasswordOptions = {}): Promise<AdminCredential> {
const validationError = options.allowWeakPassword ? null : validateAdminPassword(password);
if (validationError) throw new Error(validationError);
const salt = randomBytes(16);
const hash = await derive(password, salt, SCRYPT_PARAMETERS);
return {
version: 1,
username: options.username ?? "admin",
mustChangePassword: options.mustChangePassword ?? false,
salt: salt.toString("base64url"),
hash: hash.toString("base64url"),
parameters: {
-54
View File
@@ -1,54 +0,0 @@
import { chmodSync, mkdirSync, readFileSync, unlinkSync, writeFileSync } from "node:fs";
import { dirname } from "node:path";
import { randomBytes, timingSafeEqual } from "node:crypto";
export class BootstrapManager {
private code: string | null = null;
constructor(private readonly path: string) {}
ensure(): string {
if (this.code) return this.code;
try {
const stored = readFileSync(this.path, "utf8").trim();
if (normalize(stored).length >= 20) {
this.code = stored;
return stored;
}
} catch {
// A missing or invalid bootstrap file is replaced below.
}
const raw = randomBytes(16).toString("hex").toUpperCase();
this.code = raw.match(/.{1,4}/g)!.join("-");
mkdirSync(dirname(this.path), { recursive: true });
writeFileSync(this.path, `${this.code}\n`, { encoding: "utf8", mode: 0o600 });
try {
chmodSync(this.path, 0o600);
} catch {
// POSIX permissions are best-effort on non-POSIX filesystems.
}
return this.code;
}
verify(candidate: string): boolean {
const expected = normalize(this.ensure());
const actual = normalize(candidate);
const expectedBytes = Buffer.from(expected, "utf8");
const actualBytes = Buffer.from(actual, "utf8");
return expectedBytes.length === actualBytes.length && timingSafeEqual(expectedBytes, actualBytes);
}
consume(): void {
this.code = null;
try {
unlinkSync(this.path);
} catch (error: unknown) {
if (!(error instanceof Error && "code" in error && error.code === "ENOENT")) throw error;
}
}
}
function normalize(value: string): string {
return value.replace(/[^a-fA-F0-9]/g, "").toUpperCase();
}