Skip to content

Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.

fix: A new note can silently fill up with another note's content

Chadpiha3d43a128589f
diff --git a/src/components/notes/PersonalNotesView.tsx b/src/components/notes/PersonalNotesView.tsx
index c72175cbc..92d80ad3b 100644
--- a/src/components/notes/PersonalNotesView.tsx
+++ b/src/components/notes/PersonalNotesView.tsx
@@ -102,6 +102,9 @@ export default function PersonalNotesView({
const saveTimeoutRef = useRef<NodeJS.Timeout | null>(null);
const enhancedSaveTimeoutRef = useRef<NodeJS.Timeout | null>(null);
const activeNoteRef = useRef<number | null>(null);
+ // The note the editor buffers below currently describe. Moves atomically with
+ // the buffers so a flush can refuse to write them to the wrong note id.
+ const bufferOwnerRef = useRef<number | null>(null);
const [syncedNoteId, setSyncedNoteIdState] = useState<number | null>(null);
const localContentRef = useRef(localContent);
const localTitleRef = useRef(localTitle);
@@ -140,6 +143,8 @@ export default function PersonalNotesView({
enhancedContent: localEnhancedContentRef.current,
documentPending: saveTimeoutRef.current != null,
enhancedPending: enhancedSaveTimeoutRef.current != null,
+ bufferOwnerId: bufferOwnerRef.current,
+ targetNoteId: noteId,
});
cancelPendingSaves();
if (!noteId || !updates) return;
@@ -307,6 +312,7 @@ export default function PersonalNotesView({
localTitleRef.current = activeNote.title;
localContentRef.current = activeNote.content;
localEnhancedContentRef.current = activeNote.enhanced_content ?? null;
+ bufferOwnerRef.current = activeNote.id;
} else if (
activeNote &&
activeNote.id === activeNoteRef.current &&
@@ -327,6 +333,13 @@ export default function PersonalNotesView({
setLocalTitle("");
setLocalContent("");
setLocalEnhancedContent(null);
+ // Clear the buffers synchronously (mirroring the switch branch), so the
+ // previously-open note's text can't leak into a note created from the
+ // overview before the async state->ref effect catches up.
+ localTitleRef.current = "";
+ localContentRef.current = "";
+ localEnhancedContentRef.current = null;
+ bufferOwnerRef.current = null;
}
}, [activeNote, flushPendingSaves]);
@@ -351,6 +364,7 @@ export default function PersonalNotesView({
const handleTitleChange = useCallback(
(title: string) => {
+ bufferOwnerRef.current = activeNoteRef.current;
localTitleRef.current = title;
setLocalTitle(title);
if (activeNoteRef.current)
@@ -361,6 +375,7 @@ export default function PersonalNotesView({
const handleContentChange = useCallback(
(content: string) => {
+ bufferOwnerRef.current = activeNoteRef.current;
localContentRef.current = content;
setLocalContent(content);
if (activeNoteRef.current)
@@ -370,6 +385,7 @@ export default function PersonalNotesView({
);
const handleEnhancedContentChange = useCallback((content: string) => {
+ bufferOwnerRef.current = activeNoteRef.current;
localEnhancedContentRef.current = content;
setLocalEnhancedContent(content);
if (!activeNoteRef.current) return;
diff --git a/src/lib/noteEditorPendingSave.ts b/src/lib/noteEditorPendingSave.ts
index f02cf613f..d65addd2c 100644
--- a/src/lib/noteEditorPendingSave.ts
+++ b/src/lib/noteEditorPendingSave.ts
@@ -4,6 +4,8 @@ export interface PendingNoteSnapshot {
enhancedContent: string | null;
documentPending: boolean;
enhancedPending: boolean;
+ bufferOwnerId: number | null;
+ targetNoteId: number | null;
}
export type PendingNoteUpdates = {
@@ -25,6 +27,11 @@ export function shouldCancelPendingSavesForDelete(
* note-to-overview navigation share the same lossless behavior.
*/
export function buildPendingNoteUpdates(snapshot: PendingNoteSnapshot): PendingNoteUpdates | null {
+ // Never write a buffer to a note it does not belong to. During
+ // create-from-overview + drag the active-note ref can be repointed at the
+ // new note while the buffers still hold the previously-open note's content;
+ // without this guard the flush would fill the new note with that text.
+ if (snapshot.bufferOwnerId !== snapshot.targetNoteId) return null;
const updates: PendingNoteUpdates = {};
if (snapshot.documentPending) {
updates.title = snapshot.title;
diff --git a/test/lib/noteEditorPendingSave.test.js b/test/lib/noteEditorPendingSave.test.js
index 91dd00688..3bb44a0c3 100644
--- a/test/lib/noteEditorPendingSave.test.js
+++ b/test/lib/noteEditorPendingSave.test.js
@@ -13,6 +13,8 @@ test("returns no update when the editor has no pending timers", async () => {
enhancedContent: "Enhanced",
documentPending: false,
enhancedPending: false,
+ bufferOwnerId: 5,
+ targetNoteId: 5,
}),
null
);
@@ -24,6 +26,8 @@ test("flushes only the editor buffers represented by pending timers", async () =
title: "Latest title",
content: "Latest body",
enhancedContent: "Latest enhancement",
+ bufferOwnerId: 5,
+ targetNoteId: 5,
};
assert.deepEqual(
@@ -56,6 +60,26 @@ test("flushes only the editor buffers represented by pending timers", async () =
);
});
+test("never writes a buffer to a note it does not belong to", async () => {
+ const { buildPendingNoteUpdates } = await load();
+
+ // The editor buffer still holds note A's content while the active-note ref
+ // has already been repointed at the freshly created note B. A flush here
+ // must write nothing, otherwise B silently fills with A's text.
+ assert.equal(
+ buildPendingNoteUpdates({
+ title: "A's title",
+ content: "A's body",
+ enhancedContent: "A's enhancement",
+ documentPending: true,
+ enhancedPending: true,
+ bufferOwnerId: 1,
+ targetNoteId: 2,
+ }),
+ null
+ );
+});
+
test("deleting another sidebar note keeps the active note's pending save", async () => {
const { shouldCancelPendingSavesForDelete } = await load();