Skip to content

Commit b4a8d6a

Browse files
Merge c5556a5 into e487cd7
2 parents e487cd7 + c5556a5 commit b4a8d6a

6 files changed

Lines changed: 2778 additions & 4 deletions

File tree

‎knip.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"!utils/WorkspacePathResolver.ts!",
2727
"!test-utils/**!",
2828
"!utils/versionToken.ts!",
29+
"!services/file-safety/safeWriteText.ts!",
2930
"!services/tools/Bm25Ranker.ts!",
3031
"!services/tools/types.ts!",
3132
"!core/tools/helpers/toolResultFormatting.ts!"

‎src/core/tools/ApplyDiffTool.ts‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,29 @@ export class ApplyDiffTool extends BaseTool<"apply_diff"> {
7474
return
7575
}
7676

77-
const originalContent: string = await fs.readFile(absolutePath, "utf-8")
77+
// Bytes, not a decoded string: fs.readFile with a "utf-8" encoding replaces every byte sequence
78+
// that is not valid UTF-8 with U+FFFD, and every path below republishes the decoded string -
79+
// saveDirectly, saveChanges, and the baseline the diff view and the approval payload are built
80+
// from - so a file with undecodable bytes used to be saved back with the replacements in it,
81+
// silently rewriting bytes the model never asked to touch. Decoding fatally refuses instead,
82+
// which is the fail-closed decision the guarded publish already makes about a target it cannot
83+
// vouch for. The file is reported by name and the first undecodable byte is reported by offset,
84+
// because the refusal is only actionable if the model can find the byte.
85+
const originalBytes = await fs.readFile(absolutePath)
86+
let originalContent: string
87+
try {
88+
originalContent = new TextDecoder("utf-8", { fatal: true }).decode(originalBytes)
89+
} catch {
90+
const lossy = new TextDecoder("utf-8").decode(originalBytes)
91+
const undecodableByte = new TextEncoder().encode(lossy.slice(0, lossy.indexOf("\uFFFD"))).length
92+
task.consecutiveMistakeCount++
93+
task.recordToolError("apply_diff")
94+
const formattedError = `File ${absolutePath} is not valid UTF-8 (first undecodable byte at offset ${undecodableByte}).\n\n<error_details>\napply_diff decodes the file, applies the diff to the decoded text, and saves the result back. Bytes that cannot be decoded would have been saved back as U+FFFD alongside every byte the diff did not touch, so the edit was refused and nothing was written. Re-read the file and convert it to UTF-8 before applying the diff again.\n</error_details>`
95+
await task.say("error", formattedError)
96+
task.didToolFailInCurrentTurn = true
97+
pushToolResult(formattedError)
98+
return
99+
}
78100

79101
// Apply the diff to the original content
80102
const diffResult = (await task.diffStrategy?.applyDiff(

‎src/core/tools/__tests__/applyDiffTool.failureReporting.spec.ts‎

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,15 @@ vi.mock("@roo-code/telemetry", () => ({
1414

1515
vi.mock("fs/promises", () => ({
1616
default: {
17-
readFile: vi.fn().mockResolvedValue(""),
17+
readFile: vi.fn().mockResolvedValue(Buffer.alloc(0)),
1818
},
1919
}))
2020

21+
// ApplyDiffTool reads the file with no encoding, so the call the double stands in for is the
22+
// byte-returning one: fs.readFile returns Buffer unless an encoding is passed. vi.mocked resolves
23+
// the encoding overload, so the mock is typed against the signature the tool actually calls.
24+
const mockedReadFile = vi.mocked(fs.readFile as (path: string) => Promise<Buffer>)
25+
2126
vi.mock("../../../utils/fs", () => ({
2227
fileExistsAtPath: vi.fn().mockResolvedValue(true),
2328
}))
@@ -63,7 +68,7 @@ describe("ApplyDiffTool failure reporting", () => {
6368
vi.clearAllMocks()
6469
pushedResult = undefined
6570
vi.mocked(fileExistsAtPath).mockResolvedValue(true)
66-
vi.mocked(fs.readFile).mockResolvedValue("line one\nline two\nline three\n")
71+
mockedReadFile.mockResolvedValue(Buffer.from("line one\nline two\nline three\n", "utf8"))
6772

6873
mockAskApproval = vi.fn().mockResolvedValue(true)
6974
mockHandleError = vi.fn().mockResolvedValue(undefined)
@@ -150,6 +155,65 @@ describe("ApplyDiffTool failure reporting", () => {
150155
return failures
151156
}
152157

158+
describe("bytes the decoder cannot read", () => {
159+
async function runOnBytes(bytes: Buffer): Promise<string> {
160+
mockedReadFile.mockResolvedValue(bytes)
161+
const applyDiff = vi.fn().mockResolvedValue({ success: true, content: "unused" })
162+
mockTask.diffStrategy = { getName: () => "test", applyDiff }
163+
await applyDiffTool.execute({ path: testFilePath, diff: buildDiffContent(1) }, mockTask, {
164+
askApproval: mockAskApproval,
165+
handleError: mockHandleError,
166+
toolCallId: "tool-call-1",
167+
pushToolResult: vi.fn((result: ToolResponse) => {
168+
pushedResult = result
169+
}),
170+
})
171+
return String(pushedResult ?? "")
172+
}
173+
174+
// A byte the UTF-8 decoder rejects sits at index 3; everything around it is plain ASCII, so the
175+
// offset the tool reports has to be that index and nothing else.
176+
const UNDECODABLE = Buffer.from([0x68, 0x69, 0x20, 0xff, 0x61, 0x0a])
177+
178+
it("refuses the edit and names the file and the first undecodable byte", async () => {
179+
const output = await runOnBytes(UNDECODABLE)
180+
181+
expect(output).toContain("is not valid UTF-8")
182+
expect(output).toContain("first undecodable byte at offset 3")
183+
expect(mockTask.say).toHaveBeenCalledWith("error", output)
184+
expect(mockTask.recordToolError).toHaveBeenCalledWith("apply_diff")
185+
expect(mockTask.consecutiveMistakeCount).toBe(1)
186+
expect(mockTask.didToolFailInCurrentTurn).toBe(true)
187+
})
188+
189+
it("refuses before the diff is computed, approved, or written", async () => {
190+
await runOnBytes(UNDECODABLE)
191+
192+
expect(mockedReadFile).toHaveBeenCalledWith(path.resolve("/workspace", testFilePath))
193+
expect(mockAskApproval).not.toHaveBeenCalled()
194+
expect(mockTask.diffViewProvider.open).not.toHaveBeenCalled()
195+
expect(mockTask.diffViewProvider.saveChanges).not.toHaveBeenCalled()
196+
expect(mockTask.diffViewProvider.saveDirectly).not.toHaveBeenCalled()
197+
expect(mockTask.didEditFile).toBe(false)
198+
})
199+
200+
it("still edits a file whose bytes are valid multi-byte UTF-8", async () => {
201+
const output = await runOnBytes(Buffer.from("héllo 中文 ✅\n", "utf8"))
202+
203+
expect(output).not.toContain("is not valid UTF-8")
204+
expect(output).toContain("Changes successfully applied.")
205+
expect(mockTask.consecutiveMistakeCount).toBe(0)
206+
expect(mockTask.diffViewProvider.saveChanges).toHaveBeenCalled()
207+
})
208+
209+
it("reports the first undecodable byte when invalid bytes repeat", async () => {
210+
const output = await runOnBytes(Buffer.from([0x41, 0xc3, 0x28, 0x42, 0xfe, 0x43, 0xff]))
211+
212+
expect(output).toContain("first undecodable byte at offset 1")
213+
expect(output).not.toContain("offset 4")
214+
})
215+
})
216+
153217
describe("failure accumulation and rendering", () => {
154218
it("reports every failed block, not just the last one", async () => {
155219
const output = await runApplyDiff({ success: false, failParts: makeFailureSet(2) }, 2)
@@ -330,7 +394,7 @@ describe("ApplyDiffTool failure reporting", () => {
330394
const applyingBlock = "<<<<<<< SEARCH\nconst header = 1\n=======\nconst header = 3\n>>>>>>> REPLACE"
331395

332396
async function runRealStrategy(diff: string): Promise<string> {
333-
vi.mocked(fs.readFile).mockResolvedValue(fileText)
397+
mockedReadFile.mockResolvedValue(Buffer.from(fileText, "utf8"))
334398
const strategy = new MultiSearchReplaceDiffStrategy()
335399
const realResult = await strategy.applyDiff(fileText, diff)
336400
// Synthetic details exercise the tool's own guard against forwarding strategy detail objects.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import * as fs from "fs/promises"
2+
import * as os from "os"
3+
import * as path from "path"
4+
5+
import { safeWriteText } from "../safeWriteText"
6+
7+
// No fs mocks in this file: the point is to assert what a real filesystem ends up
8+
// holding after a publish attempt, which the mocked spec cannot show. The failure is
9+
// provoked with real filesystem semantics rather than with a stubbed call.
10+
describe("safeWriteText against a real filesystem", () => {
11+
let dir: string
12+
13+
beforeEach(async () => {
14+
dir = await fs.mkdtemp(path.join(os.tmpdir(), "safe-write-text-int-"))
15+
})
16+
17+
afterEach(async () => {
18+
await fs.rm(dir, { recursive: true, force: true })
19+
})
20+
21+
// The commit-rename failure mode is covered deterministically in safeWriteText.spec.ts
22+
// ('a failed commit does not move the target, so nothing has to be rolled back'): on a real
23+
// filesystem there is no portable way to make only the rename fail - the ESM fs namespace
24+
// cannot be spied, and read-only-parent / sticky-bit / cross-device setups are not portable.
25+
it("publishes the new bytes and leaves no staging or backup residue", async () => {
26+
const targetPath = path.join(dir, "target.txt")
27+
await fs.writeFile(targetPath, "old bytes")
28+
29+
// No platform override: the real platform's own durability and ACL steps run, and this
30+
// case is the successful restore. A failed restore now throws DaclRestoreError after the
31+
// commit rename has already happened (the focused unit test "win32 DACL: a failed restore
32+
// is an error and the dump is still unlinked" covers that path), so on a machine where
33+
// icacls cannot put a saved ACL back into a throwaway temp directory this publish
34+
// reports that error instead of resolving, and the residue assertions below are reached
35+
// only where the restore worked.
36+
await safeWriteText(targetPath, "new bytes", { backup: true })
37+
38+
expect(await fs.readFile(targetPath, "utf8")).toBe("new bytes")
39+
expect(await fs.readdir(dir)).toEqual(["target.txt"])
40+
})
41+
42+
it("leaves the target bytes untouched when the backup copy cannot be made", async () => {
43+
// A regular file cannot be renamed over a directory, so the step-3 backup copy fails
44+
// on a real filesystem with no mocking. Note what this case does NOT cover: the commit
45+
// rename is never reached, because the backup failure aborts the write first.
46+
const targetPath = path.join(dir, "target-dir")
47+
await fs.mkdir(targetPath)
48+
const inside = path.join(targetPath, "payload.txt")
49+
await fs.writeFile(inside, "original bytes")
50+
51+
await expect(safeWriteText(targetPath, "new data", { backup: true })).rejects.toThrow()
52+
53+
// The directory and its content are exactly as they were, and no backup copy
54+
// or staging directory was left behind next to them.
55+
expect(await fs.readFile(inside, "utf8")).toBe("original bytes")
56+
expect(await fs.readdir(dir)).toEqual(["target-dir"])
57+
})
58+
})

0 commit comments

Comments
 (0)