The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 16:00 UTC
fix(server): prevent lossy edits of non-UTF-8 workspace files (#36545)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
Wout Stiens committed
Nov 14, 2026 at 09:05 UTC
5abf38367baf20ac658d00ee1e219c7a21dde71e
2 files changed
+17
-14
apps/server/src/workspace/WorkspaceFileSystem.test.ts
+5
-2
@@ -530,14 +530,17 @@ it.layer(layerTest, { excludeTestServices: true })("WorkspaceFileSystemLive", (i
530
}),
531
);
532
533
- it.effect("rejects binary files without leaking their contents into the error", () =>
533
+ it.effect.each([
534
+ { name: "a NUL byte", bytes: [0x61, 0, 0x62] },
535
+ { name: "invalid UTF-8", bytes: [0x63, 0x61, 0x66, 0xe9] },
536
+ ])("rejects files with $name without leaking their contents", ({ bytes }) =>
537
Effect.gen(function* () {
538
const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
539
const fileSystem = yield* FileSystem.FileSystem;
540
const path = yield* Path.Path;
541
const cwd = yield* makeTempDir;
542
const absolutePath = path.join(cwd, "asset.bin");
540
- yield* fileSystem.writeFile(absolutePath, Uint8Array.from([0x61, 0, 0x62]));
543
+ yield* fileSystem.writeFile(absolutePath, Uint8Array.from(bytes));
544
545
const error = yield* workspaceFileSystem
546
.readFile({ cwd, relativePath: "asset.bin" })
apps/server/src/workspace/WorkspaceFileSystem.ts
+12
-12
@@ -435,22 +435,22 @@ export const make = Effect.gen(function* () {
435
offset += bytesRead;
436
}
437
const fileBytes = buffer.subarray(0, offset);
438
- if (fileBytes.includes(0)) {
439
- return yield* new WorkspaceBinaryFileError({
438
+ const truncated = stat.size > PROJECT_READ_FILE_MAX_BYTES;
439
+ const binaryFileError = () =>
440
+ new WorkspaceBinaryFileError({
441
workspaceRoot: input.cwd,
442
relativePath: input.relativePath,
443
resolvedPath: realTargetPath,
444
});
444
- }
445
-
446
- const truncated = stat.size > PROJECT_READ_FILE_MAX_BYTES;
447
- return {
448
- relativePath: target.relativePath,
449
- // Streaming decode drops a character the byte limit split, instead of ending in U+FFFD.
450
- contents: new TextDecoder("utf-8").decode(fileBytes, { stream: truncated }),
451
- byteLength: stat.size,
452
- truncated,
453
- };
445
+ if (fileBytes.includes(0)) return yield* binaryFileError();
446
+ // Fatal decoding rejects invalid UTF-8, since U+FFFD contents would corrupt the file on save.
447
+ // Streaming decode drops a character the byte limit split, instead of rejecting it.
448
+ const contents = yield* Effect.try({
449
+ try: () =>
450
+ new TextDecoder("utf-8", { fatal: true }).decode(fileBytes, { stream: truncated }),
451
+ catch: binaryFileError,
452
+ });
453
+ return { relativePath: target.relativePath, contents, byteLength: stat.size, truncated };
454
}),
455
(handle) =>
456
handle === null