AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.
HTML ai-agentcloudflarecloudflare-d1cloudflare-r2cloudflare-workersdockerelectronevernoteevernote-alternativeknowledge-basemcpnote-takingobsidian-alternativeoffline-firstpersonal-knowledge-basepwaself-hostedsqlitetiptapweb-clipper
fix(sharing): require password verification for logged-in users
tianma-ifeda356a824d9
diff --git a/apps/api/src/notebook-share-routes.test.mjs b/apps/api/src/notebook-share-routes.test.mjsindex 75975565..1b4a8b47 100644--- a/apps/api/src/notebook-share-routes.test.mjs+++ b/apps/api/src/notebook-share-routes.test.mjs@@ -470,10 +470,10 @@ describe("password-protected notebook shares", () => {});});-test("only an authenticated session in the shared workspace can preview without the share password", async () => {+test("workspace login cannot bypass the notebook password; an unlock grant is still required", async () => {const { createHash } = await import("node:crypto");const f = fixture();- await f.app.request("/api/v1/notebooks/root/share", {+ const enabled = await f.app.request("/api/v1/notebooks/root/share", {method: "PATCH", headers: { "content-type": "application/json" }, body: JSON.stringify({ passwordProtected: true }),}, f.environment);for (const [user, workspace] of [["owner", "ws_test"], ["other", "ws_other"]]) {@@ -483,7 +483,18 @@ test("only an authenticated session in the shared workspace can preview without[user, user, createHash("sha256").update(user).digest("hex"), new Date(Date.now()+3600000).toISOString()]);}for (const path of ["", "/memos/memo_child", "/resources/res_memo_child/blob"]) {- expect((await f.request(path, { headers: { Cookie: "edgeever_session=owner" } })).status).toBe(200);+ expect((await f.request(path, { headers: { Cookie: "edgeever_session=owner" } })).status).toBe(403);expect((await f.request(path, { headers: { Cookie: "edgeever_session=other" } })).status).toBe(403);+ expect((await f.request(path, { headers: { Authorization: "Bearer owner" } })).status).toBe(403);+ }+ const password = (await enabled.json()).share.password;+ const unlocked = await f.request("/unlock", {+ method: "POST", headers: { "content-type": "application/json", Cookie: "edgeever_session=owner" },+ body: JSON.stringify({ password }),+ });+ expect(unlocked.status).toBe(200);+ const cookie = unlocked.headers.get("set-cookie").split(";")[0];+ for (const path of ["", "/memos/memo_child", "/resources/res_memo_child/blob"]) {+ expect((await f.request(path, { headers: { Cookie: `edgeever_session=owner; ${cookie}` } })).status).toBe(200);}});diff --git a/apps/api/src/notebook-share-routes.ts b/apps/api/src/notebook-share-routes.tsindex d9533f38..89aadcfb 100644--- a/apps/api/src/notebook-share-routes.ts+++ b/apps/api/src/notebook-share-routes.ts@@ -184,7 +184,7 @@ export const registerPublicNotebookShareRoutes = (app: Hono<AppEnv>) => {if (!gate) return notFound(c, "Shared notebook not found");// Only the password submission bypasses the gate; all reads, including blobs, share it.const unlocking = c.req.method === "POST" && c.req.path === `/api/public/notebook-shares/${token}/unlock`;- if (!unlocking && gate.password_hash && !(await allowPasswordProtectedShare(c, token, gate.workspace_id, gate.password_hash))) {+ if (!unlocking && gate.password_hash && !(await allowPasswordProtectedShare(c, token, gate.password_hash))) {return apiError(c, "share_password_required", "Password required to view this shared notebook", 403);}await next();diff --git a/apps/api/src/share-http-access.ts b/apps/api/src/share-http-access.tsindex 29303851..0a90624e 100644--- a/apps/api/src/share-http-access.ts+++ b/apps/api/src/share-http-access.ts@@ -1,6 +1,5 @@import { getCookie, setCookie } from "hono/cookie";import type { AppContext } from "./api-context";-import { authenticateRequest } from "./auth-service";import { createShareAccessCookieValue, isValidShareAccessCookieValue, SHARE_ACCESS_COOKIE, SHARE_ACCESS_MAX_AGE_SECONDS } from "./share-access";const shareAccessCookiePath = (token: string) => `/api/public/shares/${encodeURIComponent(token)}`;@@ -16,18 +15,9 @@ export const setShareAccessCookie = async (c: AppContext, token: string, passwor});};-const hasShareAccessCookie = async (c: AppContext, token: string, passwordHash: string) =>- isValidShareAccessCookieValue(getCookie(c, SHARE_ACCESS_COOKIE), passwordHash, token);--const isShareOwner = async (c: AppContext, workspaceId: string) => {- const auth = await authenticateRequest(c, false);- return auth?.workspaceId === workspaceId;-};-+/** Public share access requires a password unlock grant, even for the author. */export const allowPasswordProtectedShare = async (c: AppContext,token: string,- workspaceId: string,passwordHash: string,-) => (await hasShareAccessCookie(c, token, passwordHash)) || isShareOwner(c, workspaceId);-+) => isValidShareAccessCookieValue(getCookie(c, SHARE_ACCESS_COOKIE), passwordHash, token);diff --git a/apps/api/src/share-routes.test.mjs b/apps/api/src/share-routes.test.mjsindex ef00550c..315fb976 100644--- a/apps/api/src/share-routes.test.mjs+++ b/apps/api/src/share-routes.test.mjs@@ -489,3 +489,44 @@ describe("password-protected memo shares", () => {sqlite.close();});});+++test("workspace login cannot bypass a memo's password or attachment gate", async () => {+ const { createHash } = await import("node:crypto");+ const { sqlite, environment } = createDatabaseEnvironment();+ try {+ const app = createShareApp(environment);+ const password = "testPass";+ sqlite.query("UPDATE memo_shares SET password_hash = ? WHERE token = ?").run(await hashPassword(password), sourceToken);+ sqlite.query("INSERT INTO users (id, username, password_hash) VALUES ('owner', 'owner', 'unused')").run();+ sqlite.query("INSERT INTO workspace_members (workspace_id, user_id, role) VALUES ('ws_member', 'owner', 'owner')").run();+ sqlite.query("INSERT INTO sessions (id, user_id, token_hash, expires_at) VALUES (?, 'owner', ?, ?)")+ .run("session_owner", createHash("sha256").update("owner-session").digest("hex"), new Date(Date.now()+3600000).toISOString());+ sqlite.query("INSERT INTO resources (id, memo_id, object_key, kind, byte_size) VALUES ('res_owner', 'memo_source', 'owner-key', 'image', 10)").run();+ let blobReads = 0;+ environment.storage.resources = { get: async () => {+ blobReads++;+ return { body: new Blob([new Uint8Array(10)]).stream(), size: 10, writeHttpMetadata: () => {} };+ } };+ const base = `/api/public/shares/${sourceToken}`;+ for (const path of [base, `${base}/resources/res_owner/blob`]) {+ for (const headers of [{ Cookie: "edgeever_session=owner-session" }, { Authorization: "Bearer owner-session" }]) {+ const response = await app.request(path, { headers }, environment);+ expect(response.status).toBe(403);+ expect((await response.json()).error.code).toBe("share_password_required");+ }+ }+ expect(blobReads).toBe(0);+ const unlocked = await app.request(`${base}/unlock`, {+ method: "POST", headers: { "Content-Type": "application/json", Cookie: "edgeever_session=owner-session" },+ body: JSON.stringify({ password }),+ }, environment);+ expect(unlocked.status).toBe(200);+ const cookie = readCookieValue(unlocked, "ee_share");+ for (const path of [base, `${base}/resources/res_owner/blob`]) {+ expect((await app.request(path, { headers: { Cookie: `edgeever_session=owner-session; ee_share=${cookie}` } }, environment)).status).toBe(200);+ }+ } finally {+ sqlite.close();+ }+});diff --git a/apps/api/src/share-routes.ts b/apps/api/src/share-routes.tsindex a15dac8b..7d5b568e 100644--- a/apps/api/src/share-routes.ts+++ b/apps/api/src/share-routes.ts@@ -179,7 +179,7 @@ export const registerPublicShareRoutes = (app: Hono<AppEnv>) => {LIMIT 1`).bind(token).first<PublicMemoShareRow>();if (!row) return notFound(c, "Shared note not found");- if (row.password_hash && !(await allowPasswordProtectedShare(c, token, row.workspace_id, row.password_hash))) {+ if (row.password_hash && !(await allowPasswordProtectedShare(c, token, row.password_hash))) {return sharePasswordRequired(c);}@@ -264,7 +264,7 @@ export const registerPublicShareRoutes = (app: Hono<AppEnv>) => {LIMIT 1`).bind(token, c.req.param("resourceId")).first<SharedResourceRow>();if (!resource) return notFound(c, "Shared resource not found");- if (resource.password_hash && !(await allowPasswordProtectedShare(c, token, resource.workspace_id, resource.password_hash))) {+ if (resource.password_hash && !(await allowPasswordProtectedShare(c, token, resource.password_hash))) {return sharePasswordRequired(c);}diff --git a/apps/web/src/components/PublicNotebookSharePage.tsx b/apps/web/src/components/PublicNotebookSharePage.tsxindex db0650e8..f99da502 100644--- a/apps/web/src/components/PublicNotebookSharePage.tsx+++ b/apps/web/src/components/PublicNotebookSharePage.tsx@@ -69,7 +69,6 @@ export const PublicNotebookSharePage = () => {return <PublicSharePasswordFormkey={token}token={token}- hintKey="sharing.notebookPasswordRequiredHint"unlock={(password) => api.unlockPublicNotebookShare(token, password)}onUnlocked={async () => {await directory.refetch();diff --git a/apps/web/src/components/PublicSharePage.tsx b/apps/web/src/components/PublicSharePage.tsxindex e5f384f1..dc567e75 100644--- a/apps/web/src/components/PublicSharePage.tsx+++ b/apps/web/src/components/PublicSharePage.tsx@@ -172,11 +172,9 @@ export const PublicSharePasswordForm = ({token,onUnlocked,unlock,- hintKey = "sharing.passwordRequiredHint",}: {token: string;unlock?: (password: string) => Promise<unknown>;- hintKey?: string;onUnlocked: () => Promise<unknown>;}) => {const { t } = useTranslation();@@ -201,7 +199,6 @@ export const PublicSharePasswordForm = ({<section className="w-full max-w-md rounded-2xl border border-slate-200 bg-card p-8 shadow-sm"><LockKeyhole className="mx-auto h-9 w-9 text-slate-700" /><h1 className="mt-4 text-center text-xl font-semibold text-slate-900">{t("sharing.passwordRequiredTitle")}</h1>- <p className="mt-2 text-center text-sm leading-6 text-slate-500">{t(hintKey)}</p><form className="mt-6 space-y-3" onSubmit={submit}><Inputtype="password"