Skip to content

Commit 56d80e6

Browse files
committed
fix(web): prevent task checkboxes from overwriting large Markdown files
1 parent b54bfc9 commit 56d80e6

3 files changed

Lines changed: 67 additions & 12 deletions

File tree

‎apps/web/src/components/files/FilePreviewPanel.test.ts‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ import {
55
normalizeFileCommentRange,
66
remapFileCommentAnnotations,
77
} from "./fileCommentAnnotations";
8-
import { isMarkdownPreviewFile, setMarkdownTaskChecked } from "./filePreviewMode";
8+
import {
9+
isMarkdownPreviewFile,
10+
resolveMarkdownTaskPreviewUpdate,
11+
setMarkdownTaskChecked,
12+
} from "./filePreviewMode";
913

1014
describe("file comment annotations", () => {
1115
it("normalizes and formats selected line ranges", () => {
@@ -79,4 +83,31 @@ describe("setMarkdownTaskChecked", () => {
7983
expect(setMarkdownTaskChecked(markdown, 0, true)).toBe(markdown);
8084
expect(setMarkdownTaskChecked(markdown, 200, true)).toBe(markdown);
8185
});
86+
87+
it("refuses rendered task-list mutations for a truncated Markdown read", () => {
88+
const fullMarkdown = `- [ ] Keep the complete file\n${"x".repeat(1024 * 1024)}`;
89+
const truncatedMarkdown = fullMarkdown.slice(0, 1024 * 1024);
90+
91+
expect(
92+
resolveMarkdownTaskPreviewUpdate({
93+
markdown: truncatedMarkdown,
94+
markerOffset: 2,
95+
checked: true,
96+
truncated: true,
97+
}),
98+
).toBeNull();
99+
expect(fullMarkdown.startsWith(truncatedMarkdown)).toBe(true);
100+
expect(fullMarkdown.length).toBeGreaterThan(truncatedMarkdown.length);
101+
});
102+
103+
it("returns a task-list update for a complete Markdown read", () => {
104+
expect(
105+
resolveMarkdownTaskPreviewUpdate({
106+
markdown,
107+
markerOffset: 2,
108+
checked: true,
109+
truncated: false,
110+
}),
111+
).toBe("- [x] First\n- [x] Second\n");
112+
});
82113
});

‎apps/web/src/components/files/FilePreviewPanel.tsx‎

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ import { resolveCenteredFileLineScrollTop } from "./fileLineReveal";
5555
import { DiffCommentAnnotation } from "../diffs/DiffCommentAnnotation";
5656
import { projectFileCacheKey, projectFileEditorCacheKey } from "./fileContentRevision";
5757
import { fileBreadcrumbs } from "./filePath";
58-
import { isMarkdownPreviewFile, setMarkdownTaskChecked } from "./filePreviewMode";
58+
import { isMarkdownPreviewFile, resolveMarkdownTaskPreviewUpdate } from "./filePreviewMode";
5959
import { FileSaveCoordinator } from "./fileSaveCoordinator";
6060
import {
6161
confirmProjectFileQueryData,
@@ -704,6 +704,7 @@ function RenderedMarkdownSurface({
704704
cwd,
705705
relativePath,
706706
contents,
707+
truncated,
707708
threadRef,
708709
onPendingChange,
709710
}: Omit<
@@ -715,6 +716,7 @@ function RenderedMarkdownSurface({
715716
| "wordWrap"
716717
| "onPostRender"
717718
> & {
719+
truncated: boolean;
718720
threadRef: ScopedThreadRef;
719721
}) {
720722
const saveCoordinator = useFileSaveCoordinator({
@@ -731,15 +733,24 @@ function RenderedMarkdownSurface({
731733
cwd={cwd}
732734
threadRef={threadRef}
733735
className="mx-auto max-w-4xl px-6 py-5"
734-
onTaskListChange={({ markerOffset, checked }) => {
735-
const currentContents =
736-
getOptimisticProjectFileQueryData(environmentId, cwd, relativePath)?.contents ??
737-
contents;
738-
const nextContents = setMarkdownTaskChecked(currentContents, markerOffset, checked);
739-
if (nextContents === currentContents) return;
740-
setProjectFileQueryData(environmentId, cwd, relativePath, nextContents);
741-
saveCoordinator.change(nextContents);
742-
}}
736+
onTaskListChange={
737+
truncated
738+
? undefined
739+
: ({ markerOffset, checked }) => {
740+
const currentContents =
741+
getOptimisticProjectFileQueryData(environmentId, cwd, relativePath)?.contents ??
742+
contents;
743+
const nextContents = resolveMarkdownTaskPreviewUpdate({
744+
markdown: currentContents,
745+
markerOffset,
746+
checked,
747+
truncated,
748+
});
749+
if (nextContents === null) return;
750+
setProjectFileQueryData(environmentId, cwd, relativePath, nextContents);
751+
saveCoordinator.change(nextContents);
752+
}
753+
}
743754
/>
744755
</ScrollArea>
745756
);
@@ -970,7 +981,8 @@ export default function FilePreviewPanel({
970981
) : null}
971982
{relativePath && file.data?.truncated ? (
972983
<div className="shrink-0 border-b border-warning/20 bg-warning-surface px-3 py-1.5 text-[11px] text-warning-foreground">
973-
Preview limited to the first 1 MB of a {file.data.byteLength.toLocaleString()} byte file.
984+
Read-only preview limited to the first 1 MB of a {file.data.byteLength.toLocaleString()}{" "}
985+
byte file.
974986
</div>
975987
) : null}
976988
<div className="flex min-h-0 flex-1 overflow-hidden">
@@ -1004,6 +1016,7 @@ export default function FilePreviewPanel({
10041016
relativePath={relativePath}
10051017
threadRef={threadRef}
10061018
contents={file.data.contents}
1019+
truncated={file.data.truncated}
10071020
onPendingChange={onPendingChange}
10081021
/>
10091022
) : file.data.truncated ? (

‎apps/web/src/components/files/filePreviewMode.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,14 @@ export function setMarkdownTaskChecked(
1616

1717
return `${markdown.slice(0, markerOffset + 1)}${checked ? "x" : " "}${markdown.slice(markerOffset + 2)}`;
1818
}
19+
20+
export function resolveMarkdownTaskPreviewUpdate(input: {
21+
readonly markdown: string;
22+
readonly markerOffset: number;
23+
readonly checked: boolean;
24+
readonly truncated: boolean;
25+
}): string | null {
26+
if (input.truncated) return null;
27+
const nextMarkdown = setMarkdownTaskChecked(input.markdown, input.markerOffset, input.checked);
28+
return nextMarkdown === input.markdown ? null : nextMarkdown;
29+
}

0 commit comments

Comments
 (0)