Skip to content

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

Prevent cross-note overwrite during image resize

yoma97ba6d01a296
diff --git a/apps/api/src/index.ts b/apps/api/src/index.ts
index 391f98c1..453ee1cd 100644
--- a/apps/api/src/index.ts
+++ b/apps/api/src/index.ts
@@ -8,6 +8,7 @@ import {
DeleteMemosSchema,
LoginSchema,
markdownToDoc,
+ isSuspiciousMemoOverwrite,
MemoCreateSchema,
MemoUpdateSchema,
MergeMemosSchema,
@@ -1534,6 +1535,20 @@ const updateMemoFromInput = async (c: AppContext, id: string, input: MemoUpdateI
const contentText = docToText(contentJson);
const title =
input.title !== undefined ? normalizeMemoTitle(input.title) : normalizeMemoTitle(current.title);
+ if (
+ !input.allowDestructiveOverwrite &&
+ isSuspiciousMemoOverwrite(current.title, current.content_text, title, contentText)
+ ) {
+ return c.json(
+ {
+ error: {
+ code: "suspicious_memo_overwrite",
+ message: "Save blocked because the title changed while most of the note content disappeared.",
+ },
+ },
+ 409
+ );
+ }
const tags = input.tags === undefined ? parseJsonArray(current.tags_json) : normalizeTags(input.tags);
const excerpt = createExcerpt(contentText);
const notebookId = input.notebookId ?? current.notebook_id;
@@ -4436,6 +4451,7 @@ const updateMemoRecord = async (
tags?: string[];
createdAt?: string;
updatedAt?: string;
+ allowDestructiveOverwrite?: boolean;
},
actor: { actorType: "user" | "agent"; actorId: string | null },
actorLabel: string
@@ -4505,6 +4521,15 @@ const updateMemoRecord = async (
const contentText = docToText(contentJson);
const title =
input.title !== undefined ? normalizeMemoTitle(input.title) : normalizeMemoTitle(current.title);
+ if (
+ !input.allowDestructiveOverwrite &&
+ isSuspiciousMemoOverwrite(current.title, current.content_text, title, contentText)
+ ) {
+ return {
+ error: "suspicious_memo_overwrite",
+ message: "Save blocked because the title changed while most of the note content disappeared.",
+ };
+ }
const tags = input.tags === undefined ? parseJsonArray(current.tags_json) : normalizeTags(input.tags);
const excerpt = createExcerpt(contentText);
const notebookId = input.notebookId ?? current.notebook_id;
diff --git a/apps/web/src/components/EditorPane.tsx b/apps/web/src/components/EditorPane.tsx
index fa4c9d54..9059516e 100644
--- a/apps/web/src/components/EditorPane.tsx
+++ b/apps/web/src/components/EditorPane.tsx
@@ -235,7 +235,9 @@ const parseImageWidth = (value: unknown) => {
const ResizableImageNodeView = ({ editor, node, selected, updateAttributes }: NodeViewProps) => {
const wrapperRef = useRef<HTMLDivElement | null>(null);
- const width = parseImageWidth(node.attrs.width) ?? DEFAULT_IMAGE_WIDTH_PERCENT;
+ const [previewWidth, setPreviewWidth] = useState<number | null>(null);
+ const nodeWidth = parseImageWidth(node.attrs.width) ?? DEFAULT_IMAGE_WIDTH_PERCENT;
+ const width = previewWidth ?? nodeWidth;
const editable = editor.isEditable;
const alt = typeof node.attrs.alt === "string" ? node.attrs.alt : "";
const title = typeof node.attrs.title === "string" ? node.attrs.title : "";
@@ -268,24 +270,32 @@ const ResizableImageNodeView = ({ editor, node, selected, updateAttributes }: No
return;
}
- const updateFromPointer = (clientX: number) => {
+ let pendingWidth = nodeWidth;
+ const previewFromPointer = (clientX: number) => {
const wrapperLeft = wrapper.getBoundingClientRect().left;
- updateWidth(((clientX - wrapperLeft) / parentWidth) * 100);
+ pendingWidth = clampImageWidth(((clientX - wrapperLeft) / parentWidth) * 100);
+ setPreviewWidth(pendingWidth);
};
- const handlePointerMove = (moveEvent: PointerEvent) => updateFromPointer(moveEvent.clientX);
- const stopResize = () => {
+ const handlePointerMove = (moveEvent: PointerEvent) => previewFromPointer(moveEvent.clientX);
+ const stopResize = (commit: boolean) => {
window.removeEventListener("pointermove", handlePointerMove);
- window.removeEventListener("pointerup", stopResize);
- window.removeEventListener("pointercancel", stopResize);
+ window.removeEventListener("pointerup", handlePointerUp);
+ window.removeEventListener("pointercancel", handlePointerCancel);
+ setPreviewWidth(null);
+ if (commit && pendingWidth !== nodeWidth) {
+ updateWidth(pendingWidth);
+ }
};
+ const handlePointerUp = () => stopResize(true);
+ const handlePointerCancel = () => stopResize(false);
window.addEventListener("pointermove", handlePointerMove);
- window.addEventListener("pointerup", stopResize);
- window.addEventListener("pointercancel", stopResize);
- updateFromPointer(event.clientX);
+ window.addEventListener("pointerup", handlePointerUp);
+ window.addEventListener("pointercancel", handlePointerCancel);
+ previewFromPointer(event.clientX);
},
- [editable, updateWidth]
+ [editable, nodeWidth, updateWidth]
);
return (
@@ -1092,6 +1102,7 @@ const RichEditorPane = ({
const noteSearchInputRef = useRef<HTMLInputElement | null>(null);
const noteReplaceInputRef = useRef<HTMLInputElement | null>(null);
const hydratingRef = useRef(false);
+ const hydratedMemoIdRef = useRef<string | null>(null);
const hasUnsavedChangesRef = useRef(false);
const editingMemoIdRef = useRef<string | null>(memo?.id ?? null);
const imageCompressionEnabledRef = useRef(imageCompressionEnabled);
@@ -1514,7 +1525,12 @@ const RichEditorPane = ({
const markDirty = useCallback(() => {
const currentMemo = memoRef.current;
- if (hydratingRef.current || currentMemo?.isDeleted) {
+ if (
+ hydratingRef.current ||
+ currentMemo?.isDeleted ||
+ !currentMemo ||
+ hydratedMemoIdRef.current !== currentMemo.id
+ ) {
return;
}
@@ -1556,6 +1572,7 @@ const RichEditorPane = ({
if (!memo) {
memoRef.current = null;
+ hydratedMemoIdRef.current = null;
editingMemoIdRef.current = null;
hasUnsavedChangesRef.current = false;
setHasUnsavedChanges(false);
@@ -1573,6 +1590,10 @@ const RichEditorPane = ({
const sameMemo = editingMemoIdRef.current === memo.id;
memoRef.current = memo;
+ if (!sameMemo) {
+ hydratedMemoIdRef.current = null;
+ }
+
if (sameMemo && hasUnsavedChangesRef.current && !memo.isDeleted) {
return;
}
@@ -1612,6 +1633,8 @@ const RichEditorPane = ({
currentEditor.commands.setContent(nextContent);
}
+ hydratedMemoIdRef.current = memo.id;
+
window.setTimeout(() => {
hydratingRef.current = false;
}, 0);
@@ -1696,7 +1719,7 @@ const RichEditorPane = ({
const currentMemo = memoRef.current;
const contentJson = getCurrentContentJson();
- if (!currentMemo || !contentJson) {
+ if (!currentMemo || !contentJson || hydratedMemoIdRef.current !== currentMemo.id) {
throw new Error("No memo selected");
}
diff --git a/packages/shared/src/index.ts b/packages/shared/src/index.ts
index eca5ce26..09b3d6be 100644
--- a/packages/shared/src/index.ts
+++ b/packages/shared/src/index.ts
@@ -1,3 +1,4 @@
export * from "./content";
+export * from "./memo-safety";
export * from "./schemas";
export * from "./types";
diff --git a/packages/shared/src/memo-safety.test.mjs b/packages/shared/src/memo-safety.test.mjs
new file mode 100644
index 00000000..b1401d5e
--- /dev/null
+++ b/packages/shared/src/memo-safety.test.mjs
@@ -0,0 +1,31 @@
+import { describe, expect, test } from "bun:test";
+import { isSuspiciousMemoOverwrite } from "./memo-safety.ts";
+
+describe("isSuspiciousMemoOverwrite", () => {
+ test("blocks the production incident shape", () => {
+ expect(
+ isSuspiciousMemoOverwrite(
+ "英国 giffgaff 卡激活",
+ "原".repeat(550),
+ "号商 松松 GPT 菲区",
+ "错".repeat(70)
+ )
+ ).toBe(true);
+ });
+
+ test("allows image-width-only saves", () => {
+ expect(
+ isSuspiciousMemoOverwrite("同一标题", "正文".repeat(300), "同一标题", "正文".repeat(300))
+ ).toBe(false);
+ });
+
+ test("allows a title rename when content remains intact", () => {
+ expect(
+ isSuspiciousMemoOverwrite("旧标题", "正文".repeat(300), "新标题", "正文".repeat(290))
+ ).toBe(false);
+ });
+
+ test("does not interfere with short notes", () => {
+ expect(isSuspiciousMemoOverwrite("旧标题", "短内容", "新标题", "")).toBe(false);
+ });
+});
diff --git a/packages/shared/src/memo-safety.ts b/packages/shared/src/memo-safety.ts
new file mode 100644
index 00000000..7beb4c7f
--- /dev/null
+++ b/packages/shared/src/memo-safety.ts
@@ -0,0 +1,12 @@
+export const isSuspiciousMemoOverwrite = (
+ currentTitle: string | null,
+ currentContentText: string,
+ nextTitle: string | null,
+ nextContentText: string
+) => {
+ const currentLength = currentContentText.trim().length;
+ const nextLength = nextContentText.trim().length;
+ const titleChanged = (currentTitle ?? "").trim() !== (nextTitle ?? "").trim();
+
+ return titleChanged && currentLength >= 200 && nextLength < currentLength * 0.35;
+};
diff --git a/packages/shared/src/schemas.ts b/packages/shared/src/schemas.ts
index 2ecc8126..dbbfd834 100644
--- a/packages/shared/src/schemas.ts
+++ b/packages/shared/src/schemas.ts
@@ -30,6 +30,7 @@ export const MemoUpdateSchema = z.object({
tags: z.array(z.string()).optional(),
createdAt: z.string().datetime().optional(),
updatedAt: z.string().datetime().optional(),
+ allowDestructiveOverwrite: z.boolean().optional(),
});
export const MoveMemosSchema = z.object({