This commit is contained in:
+101
-20
@@ -1,7 +1,7 @@
|
||||
import { stdin as input, stdout as output } from "node:process";
|
||||
import { mkdirSync } from "node:fs";
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { openDatabase } from "../db/index.js";
|
||||
import { StringDecoder } from "node:string_decoder";
|
||||
import { openDatabase, openDatabaseReadOnly } from "../db/index.js";
|
||||
import { acquireInstanceLock, loadConfig, prepareDataDirectories } from "../config.js";
|
||||
import { hashPassword, normalizeUsername, validateNewPassword, temporaryPassword } from "../security.js";
|
||||
import { writeAudit } from "../audit.js";
|
||||
@@ -11,42 +11,120 @@ function arg(name: string): string | undefined {
|
||||
return index >= 0 ? process.argv[index + 1] : undefined;
|
||||
}
|
||||
|
||||
// A terminal paste can contain more than one line. Keep the unread tail for
|
||||
// the next prompt instead of silently discarding credentials after the first
|
||||
// newline.
|
||||
let pendingInput = "";
|
||||
let pendingSkipLf = false;
|
||||
|
||||
async function readSecret(prompt: string): Promise<string> {
|
||||
if (!input.isTTY) throw new Error("admin:init 需要交互式 TTY,不能通过管道传入密码");
|
||||
output.write(prompt);
|
||||
return await new Promise<string>((resolve, reject) => {
|
||||
let value = "";
|
||||
let escapeSequence = false;
|
||||
let cleaned = false;
|
||||
const decoder = new StringDecoder("utf8");
|
||||
const wasRaw = Boolean(input.isRaw);
|
||||
const onData = (chunk: Buffer) => {
|
||||
const text = chunk.toString("utf8");
|
||||
if (text === "\u0003") {
|
||||
cleanup();
|
||||
reject(new Error("已取消"));
|
||||
} else if (text === "\r" || text === "\n") {
|
||||
cleanup();
|
||||
output.write("\n");
|
||||
resolve(value);
|
||||
} else if (text === "\u007f") {
|
||||
value = value.slice(0, -1);
|
||||
} else if (!text.includes("\u001b")) {
|
||||
value += text;
|
||||
}
|
||||
};
|
||||
const initialInput = pendingInput;
|
||||
pendingInput = "";
|
||||
let onData: (chunk: Buffer | string) => void;
|
||||
let onSignal: () => void;
|
||||
const cleanup = () => {
|
||||
if (cleaned) return;
|
||||
cleaned = true;
|
||||
input.off("data", onData);
|
||||
input.off("error", onInputError);
|
||||
process.off("SIGINT", onSignal);
|
||||
process.off("SIGTERM", onSignal);
|
||||
input.setRawMode?.(wasRaw);
|
||||
input.pause();
|
||||
};
|
||||
const finish = (error?: Error) => {
|
||||
cleanup();
|
||||
if (error) reject(error);
|
||||
else {
|
||||
output.write("\n");
|
||||
resolve(value);
|
||||
}
|
||||
};
|
||||
const onInputError = (error: Error) => finish(error);
|
||||
onSignal = () => finish(new Error("已取消"));
|
||||
const consume = (text: string) => {
|
||||
let offset = 0;
|
||||
for (const character of text) {
|
||||
offset += character.length;
|
||||
if (pendingSkipLf) {
|
||||
if (character === "\n") {
|
||||
pendingSkipLf = false;
|
||||
continue;
|
||||
}
|
||||
pendingSkipLf = false;
|
||||
}
|
||||
if (character === "\u0003") {
|
||||
finish(new Error("已取消"));
|
||||
return;
|
||||
}
|
||||
if (escapeSequence) {
|
||||
if (/[A-Za-z~]/.test(character)) escapeSequence = false;
|
||||
continue;
|
||||
}
|
||||
if (character === "\u001b") {
|
||||
escapeSequence = true;
|
||||
} else if (character === "\r" || character === "\n") {
|
||||
const tail = text.slice(offset);
|
||||
pendingInput = tail.startsWith("\n") && character === "\r" ? tail.slice(1) : tail;
|
||||
pendingSkipLf = character === "\r" && !tail.startsWith("\n");
|
||||
finish();
|
||||
return;
|
||||
} else if (character === "\u007f" || character === "\b") {
|
||||
value = value.slice(0, -1);
|
||||
} else {
|
||||
value += character;
|
||||
}
|
||||
}
|
||||
};
|
||||
onData = (chunk) => {
|
||||
consume(typeof chunk === "string" ? chunk : decoder.write(chunk));
|
||||
};
|
||||
input.resume();
|
||||
input.setRawMode?.(true);
|
||||
process.once("SIGINT", onSignal);
|
||||
process.once("SIGTERM", onSignal);
|
||||
input.once("error", onInputError);
|
||||
input.on("data", onData);
|
||||
if (initialInput) consume(initialInput);
|
||||
});
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const config = loadConfig();
|
||||
const checkOnly = process.argv.includes("--check");
|
||||
if (checkOnly) {
|
||||
let database;
|
||||
try {
|
||||
database = openDatabaseReadOnly(config);
|
||||
} catch (error) {
|
||||
if (error && typeof error === "object" && "code" in error && (error as NodeJS.ErrnoException).code === "ENOENT") {
|
||||
console.log("empty");
|
||||
return;
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
try {
|
||||
const hasAdminsTable = database.sqlite
|
||||
.prepare("SELECT 1 AS present FROM sqlite_master WHERE type = 'table' AND name = 'admins'")
|
||||
.get();
|
||||
const existing = hasAdminsTable
|
||||
? database.sqlite.prepare("SELECT COUNT(*) AS count FROM admins").get() as { count: number }
|
||||
: { count: 0 };
|
||||
console.log(existing.count > 0 ? "initialized" : "empty");
|
||||
} finally {
|
||||
database.sqlite.close();
|
||||
}
|
||||
return;
|
||||
}
|
||||
prepareDataDirectories(config);
|
||||
mkdirSync(config.dataDir, { recursive: true, mode: 0o700 });
|
||||
const release = acquireInstanceLock(config);
|
||||
const database = openDatabase(config);
|
||||
try {
|
||||
@@ -64,6 +142,9 @@ async function main() {
|
||||
if (policyError) throw new Error(policyError);
|
||||
const normalized = normalizeUsername(username);
|
||||
if ([...normalized].length < 3) throw new Error("用户名至少需要 3 个字符");
|
||||
if ([...normalized].length > 64) throw new Error("用户名最多 64 个字符");
|
||||
const normalizedDisplayName = displayName.normalize("NFKC").trim();
|
||||
if ([...normalizedDisplayName].length < 1 || [...normalizedDisplayName].length > 80) throw new Error("显示名称必须为 1-80 个字符");
|
||||
const passwordHash = await hashPassword(password);
|
||||
const id = randomUUID();
|
||||
const now = Date.now();
|
||||
@@ -74,14 +155,14 @@ async function main() {
|
||||
INSERT INTO admins(id, username, username_norm, display_name, password_hash, status,
|
||||
must_change_password, auth_version, version, created_at)
|
||||
VALUES (?, ?, ?, ?, ?, 'active', 1, 1, 1, ?)
|
||||
`).run(id, username.normalize("NFKC").trim(), normalized, displayName.trim(), passwordHash, now);
|
||||
`).run(id, username.normalize("NFKC").trim(), normalized, normalizedDisplayName, passwordHash, now);
|
||||
writeAudit(database.sqlite, {
|
||||
requestId: `cli:${randomUUID()}`,
|
||||
actorUsername: "cli",
|
||||
action: "admin.initialized",
|
||||
targetType: "admin",
|
||||
targetId: id,
|
||||
after: { username: normalized, displayName: displayName.trim(), status: "active" },
|
||||
after: { username: normalized, displayName: normalizedDisplayName, status: "active" },
|
||||
});
|
||||
})();
|
||||
console.log(generate ? `已创建首位管理员。一次性密码:${password}` : "已创建首位管理员。");
|
||||
|
||||
+22
-1
@@ -1,6 +1,6 @@
|
||||
import Database from "better-sqlite3";
|
||||
import { drizzle, type BetterSQLite3Database } from "drizzle-orm/better-sqlite3";
|
||||
import { readdirSync, readFileSync } from "node:fs";
|
||||
import { lstatSync, readdirSync, readFileSync } from "node:fs";
|
||||
import { chmodSync, existsSync } from "node:fs";
|
||||
import path from "node:path";
|
||||
import type { AppConfig } from "../config.js";
|
||||
@@ -44,3 +44,24 @@ export function openDatabase(config: AppConfig): DatabaseContext {
|
||||
if (foreignKeys !== 1) throw new Error("SQLite 外键未启用");
|
||||
return { sqlite, db: drizzle(sqlite, { schema }) };
|
||||
}
|
||||
|
||||
/**
|
||||
* Open an existing database without creating directories, changing journal
|
||||
* mode, running migrations, or changing file permissions. This is used by
|
||||
* administrative status checks that must be side-effect free.
|
||||
*/
|
||||
export function openDatabaseReadOnly(config: AppConfig): DatabaseContext {
|
||||
const info = lstatSync(config.dbPath);
|
||||
if (!info.isFile() || info.isSymbolicLink()) throw new Error(`数据库文件不是安全的普通文件:${config.dbPath}`);
|
||||
const sqlite = new Database(config.dbPath, { readonly: true, fileMustExist: true });
|
||||
sqlite.pragma("foreign_keys = ON");
|
||||
sqlite.pragma("busy_timeout = 5000");
|
||||
sqlite.pragma("temp_store = MEMORY");
|
||||
sqlite.pragma("query_only = ON");
|
||||
const foreignKeys = sqlite.pragma("foreign_keys", { simple: true });
|
||||
if (foreignKeys !== 1) {
|
||||
sqlite.close();
|
||||
throw new Error("SQLite 外键未启用");
|
||||
}
|
||||
return { sqlite, db: drizzle(sqlite, { schema }) };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user