feat(admin): verify the backoffice password with PBKDF2, not plaintext compare
- ui/auth.js: derive PBKDF2-HMAC-SHA256(name:password) with a per-deploy salt (250k iterations) and compare against the verifier in constant time - config/admin-credentials.json holds salt/iterations/verifier only — no password - server/set-admin-password.mjs generates/rotates that file (CLI or hidden prompt) - login is async now: button shows a pending state; config load errors surface in the form - SW precaches the credentials file; version bumped to 2026.11.19.4 - docs: README/AGENTS explain rotation and the residual limits of a client-side gate
This commit is contained in:
1 parent
cd5cb5dd08
commit
a629e67ca4
8 files changed
+249
-32
No files matched your search
+108
-20
@@ -1,16 +1,26 @@
|
||||
/* auth.js — ประตูหน้าหลังบ้าน (ชื่อ + รหัส)
|
||||
หมายเหตุความปลอดภัย: นี่คือ "ด่านหน้า" ฝั่งเบราว์เซอร์ ทำให้คนเดินผ่านหน้าจอ
|
||||
ไม่เข้าไปแก้ข้อความได้ง่าย ๆ เท่านั้น ไม่ใช่ระบบยืนยันตัวตนจริง
|
||||
การป้องกันจริงอยู่ที่ ADMIN_TOKEN ของ server/publish.mjs (ฝั่งเซิร์ฟเวอร์)
|
||||
ห้ามใช้รหัสเดียวกับระบบอื่น และอย่าเปิดหลังบ้านให้เครื่องอื่นโดยไม่ตั้ง ADMIN_TOKEN */
|
||||
/* auth.js — ด่านหน้าหลังบ้าน: ตรวจรหัสแบบ PBKDF2 (ไม่มีรหัสจริงอยู่ในไฟล์นี้)
|
||||
*
|
||||
* วิธีทำงาน
|
||||
* 1. อ่าน salt / iterations / verifier จาก config/admin-credentials.json (คนละไฟล์กับโค้ดนี้)
|
||||
* 2. ผู้ใช้พิมพ์ "ชื่อ" + "รหัส" → PBKDF2-HMAC-SHA256("ชื่อ:รหัส", salt, iterations)
|
||||
* 3. เทียบผลลัพธ์แบบ constant-time กับ verifier ในไฟล์ config
|
||||
*
|
||||
* ทำไมยังไม่ใช่ความปลอดภัยเต็มรูปแบบ
|
||||
* - ด่านนี้อยู่ฝั่งเบราว์เซอร์ ใครแก้ JS ใน devtools ก็ข้ามได้ และ verifier ที่ได้มาก็ถูกเดารหัส
|
||||
* แบบ offline ได้ (รหัสสั้น/เดาง่าย = แตกเร็ว) — สิ่งที่ได้คือ "ไม่มีรหัสจริงใน source"
|
||||
* - การป้องกันจริงคือ ADMIN_TOKEN ของ server/publish.mjs (ฝั่งเซิร์ฟเวอร์)
|
||||
*
|
||||
* เปลี่ยนรหัส: node server/set-admin-password.mjs admin <รหัสใหม่>
|
||||
*/
|
||||
|
||||
const ADMIN_NAME = "admin";
|
||||
const ADMIN_PASSWORD = "admin007";
|
||||
const CONFIG_URL = "config/admin-credentials.json";
|
||||
const SESSION_KEY = "hobbypj.admin.session.v1";
|
||||
|
||||
const MAX_ATTEMPTS = 5;
|
||||
const LOCK_MS = 30_000;
|
||||
|
||||
let cachedConfig = null;
|
||||
let configPromise = null;
|
||||
let failures = 0;
|
||||
let lockedUntil = 0;
|
||||
|
||||
@@ -22,15 +32,101 @@ export function isAuthed() {
|
||||
}
|
||||
}
|
||||
|
||||
/** @returns {{ok:boolean, locked?:boolean, retryIn?:number, remaining?:number}} */
|
||||
export function login(name, password) {
|
||||
export function logout() {
|
||||
try {
|
||||
sessionStorage.removeItem(SESSION_KEY);
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
}
|
||||
|
||||
async function loadConfig() {
|
||||
if (cachedConfig) return cachedConfig;
|
||||
if (!configPromise) {
|
||||
configPromise = fetch(CONFIG_URL, { cache: "no-store" })
|
||||
.then((res) => {
|
||||
if (!res.ok) throw new Error(`HTTP ${res.status}`);
|
||||
return res.json();
|
||||
})
|
||||
.then((json) => {
|
||||
cachedConfig = json;
|
||||
return json;
|
||||
})
|
||||
.catch((error) => {
|
||||
configPromise = null;
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
return configPromise;
|
||||
}
|
||||
|
||||
function base64ToBytes(value) {
|
||||
const binary = atob(String(value || ""));
|
||||
const bytes = new Uint8Array(binary.length);
|
||||
for (let i = 0; i < binary.length; i += 1) bytes[i] = binary.charCodeAt(i);
|
||||
return bytes;
|
||||
}
|
||||
|
||||
function equalConstantTime(a, b) {
|
||||
if (!a || !b || a.length !== b.length) return false;
|
||||
let diff = 0;
|
||||
for (let i = 0; i < a.length; i += 1) diff |= a[i] ^ b[i];
|
||||
return diff === 0;
|
||||
}
|
||||
|
||||
async function deriveVerifier(name, password, config) {
|
||||
const encoder = new TextEncoder();
|
||||
const material = await crypto.subtle.importKey(
|
||||
"raw",
|
||||
encoder.encode(`${name}:${password}`),
|
||||
"PBKDF2",
|
||||
false,
|
||||
["deriveBits"],
|
||||
);
|
||||
const bits = await crypto.subtle.deriveBits(
|
||||
{
|
||||
name: "PBKDF2",
|
||||
salt: base64ToBytes(config.salt),
|
||||
iterations: Number(config.iterations) || 250_000,
|
||||
hash: "SHA-256",
|
||||
},
|
||||
material,
|
||||
(Number(config.key_length) || 32) * 8,
|
||||
);
|
||||
return new Uint8Array(bits);
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {Promise<{ok:boolean, locked?:boolean, retryIn?:number, remaining?:number, error?:string}>}
|
||||
*/
|
||||
export async function login(name, password) {
|
||||
const now = Date.now();
|
||||
if (now < lockedUntil) {
|
||||
return { ok: false, locked: true, retryIn: Math.ceil((lockedUntil - now) / 1000) };
|
||||
}
|
||||
|
||||
const ok = String(name ?? "").trim() === ADMIN_NAME && String(password ?? "") === ADMIN_PASSWORD;
|
||||
if (!ok) {
|
||||
if (!globalThis.crypto?.subtle) {
|
||||
return {
|
||||
ok: false,
|
||||
error: "เบราว์เซอร์นี้ตรวจรหัสไม่ได้ — ต้องเปิดผ่าน https หรือ localhost",
|
||||
};
|
||||
}
|
||||
|
||||
let config;
|
||||
try {
|
||||
config = await loadConfig();
|
||||
} catch (error) {
|
||||
return { ok: false, error: `อ่านไฟล์รหัสไม่ได้ (${CONFIG_URL}) — ${error.message}` };
|
||||
}
|
||||
|
||||
let computed;
|
||||
try {
|
||||
computed = await deriveVerifier(String(name ?? "").trim(), String(password ?? ""), config);
|
||||
} catch (error) {
|
||||
return { ok: false, error: `ตรวจรหัสไม่สำเร็จ: ${error.message}` };
|
||||
}
|
||||
|
||||
if (!equalConstantTime(computed, base64ToBytes(config.verifier))) {
|
||||
failures += 1;
|
||||
if (failures >= MAX_ATTEMPTS) {
|
||||
failures = 0;
|
||||
@@ -45,15 +141,7 @@ export function login(name, password) {
|
||||
try {
|
||||
sessionStorage.setItem(SESSION_KEY, "1");
|
||||
} catch {
|
||||
/* โหมดส่วนตัวที่ปิด storage → ให้เข้าได้เฉพาะรอบนี้ */
|
||||
/* โหมดที่ปิด storage → เข้าได้เฉพาะรอบนี้ */
|
||||
}
|
||||
return { ok: true };
|
||||
}
|
||||
|
||||
export function logout() {
|
||||
try {
|
||||
sessionStorage.removeItem(SESSION_KEY);
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user