Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(zip): finish a short synchronous read before calling the archive truncated
DeryFerdcfe0dc16750b
diff --git a/server/zip-archive.ts b/server/zip-archive.tsindex aada588406..9b6786cb10 100644--- a/server/zip-archive.ts+++ b/server/zip-archive.ts@@ -36,6 +36,18 @@ async function fillBuffer(handle: FileHandle, buffer: Buffer, position: number):return true;}+/** `fillBuffer`'s synchronous twin: a filesystem may answer with fewer bytes than asked, so keep reading until the+ * buffer is full, and report only a real end of file. */+function fillBufferSync(fd: number, buffer: Buffer, position: number): boolean {+ let filled = 0;+ while (filled < buffer.length) {+ const bytesRead = readSync(fd, buffer, filled, buffer.length - filled, position + filled);+ if (bytesRead === 0) return false;+ filled += bytesRead;+ }+ return true;+}+/** A central-directory record; `member` is null for one that cannot be read plainly (encrypted, ZIP64, on another* disk, or compressed some other way). */export interface ZipRecord {@@ -141,10 +153,10 @@ export function listZipSync(path: string, limits: { maxEntries: number; maxDirectry {const size = fstatSync(fd).size;const tail = Buffer.alloc(Math.min(size, MAX_EOCD_BYTES));- if (readSync(fd, tail, 0, tail.length, size - tail.length) !== tail.length) throw new Error("The archive was truncated.");+ if (!fillBufferSync(fd, tail, size - tail.length)) throw new Error("The archive was truncated.");const { entryCount, directoryBytes, directoryOffset } = endOfDirectory(tail, size - tail.length);const directory = Buffer.alloc(Math.min(directoryBytes, limits.maxDirectoryBytes));- if (readSync(fd, directory, 0, directory.length, directoryOffset) !== directory.length) throw new Error("The archive directory was truncated.");+ if (!fillBufferSync(fd, directory, directoryOffset)) throw new Error("The archive directory was truncated.");const wanted = Math.min(entryCount, limits.maxEntries);const records = directoryRecords(directory, wanted);if (records.length !== wanted) throw new Error("The archive directory is invalid.");@@ -168,14 +180,14 @@ export function readZipMemberSync(archivePath: string, entry: ZipMember): Bufferconst fd = openSync(archivePath, "r");try {const header = Buffer.alloc(30);- if (readSync(fd, header, 0, header.length, entry.localHeaderOffset) !== header.length) throw new Error("An archive member header was truncated.");+ if (!fillBufferSync(fd, header, entry.localHeaderOffset)) throw new Error("An archive member header was truncated.");const { nameBytes, extraBytes } = localHeaderLengths(header, entry);const name = Buffer.alloc(nameBytes);- if (readSync(fd, name, 0, nameBytes, entry.localHeaderOffset + 30) !== nameBytes || name.toString("utf8") !== entry.name) {+ if (!fillBufferSync(fd, name, entry.localHeaderOffset + 30) || name.toString("utf8") !== entry.name) {throw new Error("An archive member name did not match its directory.");}const data = Buffer.alloc(entry.compressedBytes);- if (readSync(fd, data, 0, data.length, entry.localHeaderOffset + 30 + nameBytes + extraBytes) !== data.length) {+ if (!fillBufferSync(fd, data, entry.localHeaderOffset + 30 + nameBytes + extraBytes)) {throw new Error("An archive member was truncated.");}// zlib refuses a maxOutputLength of 0.diff --git a/server/zip-short-reads-sync.test.ts b/server/zip-short-reads-sync.test.tsnew file mode 100644index 0000000000..e26906dcb4--- /dev/null+++ b/server/zip-short-reads-sync.test.ts@@ -0,0 +1,61 @@+import { mkdtempSync, writeFileSync } from "node:fs";+import { tmpdir } from "node:os";+import { join } from "node:path";+import { afterEach, expect, it, vi } from "vitest";+import { listZipSync, readZipMemberSync } from "./zip-archive.ts";+import { removeTempDir } from "./testing/cleanup.ts";+import { buildZip } from "./testing/zip-fixture.ts";++/** The sync readers share `fillBuffer`'s duty with the async ones: a filesystem may answer with fewer bytes than asked+ * (network, FUSE, an antivirus filter) and the read must carry on rather than report a sound archive as truncated. */+const reads = vi.hoisted(() => ({ limit: 3, shortcuts: 0 }));++vi.mock("node:fs", async (importOriginal) => {+ const fs = await importOriginal<typeof import("node:fs")>();+ const readSync = ((fd: number, buffer: Buffer, offset: number, length: number, position: number) => {+ const capped = Math.min(length, reads.limit);+ const bytesRead = fs.readSync(fd, buffer, offset, capped, position);+ if (bytesRead < length) reads.shortcuts += 1;+ return bytesRead;+ }) as typeof fs.readSync;+ return { ...fs, readSync };+});++let scratch = "";+afterEach(async () => {+ reads.limit = 3;+ reads.shortcuts = 0;+ if (scratch) await removeTempDir(scratch);+ scratch = "";+});++function archive(): { file: string; contents: string } {+ scratch = mkdtempSync(join(tmpdir(), "omb-zip-sync-short-"));+ const file = join(scratch, "fixture.zip");+ const contents = "complete contents\n";+ writeFileSync(file, buildZip([{ name: "nested/世界.txt", data: contents, method: 8 }]));+ return { file, contents };+}++it("lists a ZIP whose every synchronous read stops after three bytes", () => {+ const { file } = archive();+ const { total, records } = listZipSync(file, { maxEntries: 10, maxDirectoryBytes: 1 << 20 });+ expect(total).toBe(1);+ expect(records.map((record) => record.name)).toEqual(["nested/世界.txt"]);+ expect(reads.shortcuts).toBeGreaterThan(0);+});++it("reads a member whose every synchronous read stops after three bytes", () => {+ const { file, contents } = archive();+ const { records } = listZipSync(file, { maxEntries: 10, maxDirectoryBytes: 1 << 20 });+ const member = records[0]!.member!;+ expect(readZipMemberSync(file, member).toString("utf8")).toBe(contents);+});++it("still rejects a member whose declared size the file does not hold", () => {+ const { file } = archive();+ const { records } = listZipSync(file, { maxEntries: 10, maxDirectoryBytes: 1 << 20 });+ const member = records[0]!.member!;+ const beyond = { ...member, compressedBytes: member.compressedBytes + 4096 };+ expect(() => readZipMemberSync(file, beyond)).toThrow();+});\ No newline at end of file