Skip to content

AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.

fix: validate demo password hash before reset

tianma-iffa60165c363d
diff --git a/apps/api/src/demo-mode.ts b/apps/api/src/demo-mode.ts
index 42b034ed..9d708235 100644
--- a/apps/api/src/demo-mode.ts
+++ b/apps/api/src/demo-mode.ts
@@ -1,3 +1,5 @@
+import { isSupportedPasswordHash } from "./auth-bootstrap";
+
type PasswordHasher = (password: string) => Promise<string>;
export const isDemoModeEnabled = (value: string | undefined) => value?.trim().toLowerCase() === "true";
@@ -22,7 +24,7 @@ export const resolveDemoPasswordHash = async (
hashPassword: PasswordHasher,
) => {
const passwordHash = configuredPasswordHash?.trim();
- if (passwordHash) {
+ if (passwordHash && isSupportedPasswordHash(passwordHash)) {
return passwordHash;
}
diff --git a/tests/demo-mode.test.ts b/tests/demo-mode.test.ts
index 2039084c..287c8896 100644
--- a/tests/demo-mode.test.ts
+++ b/tests/demo-mode.test.ts
@@ -14,9 +14,16 @@ describe("demo mode policy", () => {
expect(isDemoModeEnabled(undefined)).toBe(false);
});
- test("keeps the configured legacy hash authoritative", async () => {
+ test("keeps a valid configured hash authoritative", async () => {
const hashPassword = async () => "generated-hash";
- expect(await resolveDemoPasswordHash("plaintext", " legacy-hash ", hashPassword)).toBe("legacy-hash");
+ const configuredHash = "pbkdf2-sha256$100000$AAAAAAAAAAAAAAAAAAAAAA$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
+ expect(await resolveDemoPasswordHash("plaintext", ` ${configuredHash} `, hashPassword)).toBe(configuredHash);
+ });
+
+ test("repairs an invalid configured hash from the plaintext fallback", async () => {
+ const hashPassword = async (password: string) => `hashed:${password}`;
+ expect(await resolveDemoPasswordHash("plaintext", "legacy-hash", hashPassword)).toBe("hashed:plaintext");
+ expect(await resolveDemoPasswordHash(undefined, "legacy-hash", hashPassword)).toBeNull();
});
test("protects only the configured account while demo mode is enabled", () => {