Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Merge #7881: fix(attachments): bots without a shell can read uploaded zips and text files
milind-soni858ce0c1b784
diff --cc server/zip-archive.tsindex 78dac9340d,cc38967bfa..aada588406--- a/server/zip-archive.ts+++ b/server/zip-archive.ts@@@ -1,8 -1,9 +1,9 @@@// Reads a ZIP that was already checked against a pinned SHA-256, without loading it into memory: the central directory// is parsed from the end of the file, and each member is inflated straight to disk, never past the size it declares.- // No ZIP64, no encryption, no links: a pinned vendor archive needs none of them.- import { createReadStream, createWriteStream } from "node:fs";+ // No ZIP64, no encryption, no links: a pinned vendor archive needs none of them. listZipSync and readZipMemberSync+ // read archives nobody vouched for, in slices the caller bounds.+ import { closeSync, createReadStream, createWriteStream, fstatSync, openSync, readSync } from "node:fs";-import { mkdir, open, writeFile } from "node:fs/promises";+import { mkdir, open, writeFile, type FileHandle } from "node:fs/promises";import { join } from "node:path";import { Transform } from "node:stream";import { pipeline } from "node:stream/promises";@@@ -25,16 -26,76 +26,86 @@@ const UNIX_TYPE_MASK = 0o170000const UNIX_FILE = 0o100000;const UNIX_DIRECTORY = 0o040000;+async function fillBuffer(handle: FileHandle, buffer: Buffer, position: number): Promise<boolean> {+ let filled = 0;+ while (filled < buffer.length) {+ const { bytesRead } = await handle.read(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 {+ name: string;+ uncompressedBytes: number;+ member: ZipMember | null;+ }++ function endOfDirectory(tail: Buffer, tailOffset: number): { entryCount: number; directoryBytes: number; directoryOffset: number } {+ let eocd = -1;+ for (let cursor = tail.length - 22; cursor >= 0; cursor -= 1) {+ if (+ tail.readUInt32LE(cursor) === EOCD_SIGNATURE+ && cursor + 22 + tail.readUInt16LE(cursor + 20) === tail.length+ ) {+ eocd = cursor;+ break;+ }+ }+ if (eocd < 0) throw new Error("The verified archive has no valid ZIP directory.");+ const disk = tail.readUInt16LE(eocd + 4);+ const directoryDisk = tail.readUInt16LE(eocd + 6);+ const diskEntries = tail.readUInt16LE(eocd + 8);+ const entryCount = tail.readUInt16LE(eocd + 10);+ const directoryBytes = tail.readUInt32LE(eocd + 12);+ const directoryOffset = tail.readUInt32LE(eocd + 16);+ if (+ disk !== 0 || directoryDisk !== 0 || diskEntries !== entryCount+ || directoryBytes === 0xffffffff || directoryOffset === 0xffffffff+ || directoryOffset + directoryBytes > tailOffset + eocd+ ) throw new Error("The verified archive uses an unsupported ZIP layout.");+ return { entryCount, directoryBytes, directoryOffset };+ }++ /** At most `limit` records from the start of a central directory. */+ function directoryRecords(directory: Buffer, limit: number): ZipRecord[] {+ const records: ZipRecord[] = [];+ let cursor = 0;+ while (cursor < directory.length && records.length < limit) {+ if (cursor + 46 > directory.length || directory.readUInt32LE(cursor) !== CENTRAL_SIGNATURE) {+ throw new Error("The verified archive directory is invalid.");+ }+ const flags = directory.readUInt16LE(cursor + 8);+ const compression = directory.readUInt16LE(cursor + 10);+ const compressedBytes = directory.readUInt32LE(cursor + 20);+ const uncompressedBytes = directory.readUInt32LE(cursor + 24);+ const nameBytes = directory.readUInt16LE(cursor + 28);+ const extraBytes = directory.readUInt16LE(cursor + 30);+ const commentBytes = directory.readUInt16LE(cursor + 32);+ const startDisk = directory.readUInt16LE(cursor + 34);+ const externalAttributes = directory.readUInt32LE(cursor + 38);+ const localHeaderOffset = directory.readUInt32LE(cursor + 42);+ const next = cursor + 46 + nameBytes + extraBytes + commentBytes;+ if (next > directory.length) throw new Error("The verified archive directory is invalid.");+ const name = directory.subarray(cursor + 46, cursor + 46 + nameBytes).toString("utf8");+ const plain = startDisk === 0 && (flags & 1) === 0+ && compressedBytes !== 0xffffffff && uncompressedBytes !== 0xffffffff+ && localHeaderOffset !== 0xffffffff;+ records.push({+ name,+ uncompressedBytes,+ member: plain && (compression === 0 || compression === 8)+ ? { name, compression, compressedBytes, uncompressedBytes, localHeaderOffset, externalAttributes }+ : null,+ });+ cursor = next;+ }+ return records;+ }+/** The members a ZIP's central directory lists; more than `maxEntries` is refused. */export async function readZipDirectory(path: string, maxEntries: number): Promise<ZipMember[]> {const handle = await open(path, "r");@@@ -43,72 -104,22 +114,22 @@@const tailBytes = Math.min(info.size, MAX_EOCD_BYTES);const tail = Buffer.alloc(tailBytes);const tailOffset = info.size - tailBytes;- if ((await handle.read(tail, 0, tailBytes, tailOffset)).bytesRead !== tailBytes) {+ if (!await fillBuffer(handle, tail, tailOffset)) {throw new Error("The verified archive was truncated.");}- let eocd = -1;- for (let cursor = tail.length - 22; cursor >= 0; cursor -= 1) {- if (- tail.readUInt32LE(cursor) === EOCD_SIGNATURE- && cursor + 22 + tail.readUInt16LE(cursor + 20) === tail.length- ) {- eocd = cursor;- break;- }- }- if (eocd < 0) throw new Error("The verified archive has no valid ZIP directory.");- const disk = tail.readUInt16LE(eocd + 4);- const directoryDisk = tail.readUInt16LE(eocd + 6);- const diskEntries = tail.readUInt16LE(eocd + 8);- const entryCount = tail.readUInt16LE(eocd + 10);- const directoryBytes = tail.readUInt32LE(eocd + 12);- const directoryOffset = tail.readUInt32LE(eocd + 16);- const eocdOffset = tailOffset + eocd;- if (- disk !== 0 || directoryDisk !== 0 || diskEntries !== entryCount || entryCount > maxEntries- || directoryBytes === 0xffffffff || directoryOffset === 0xffffffff- || directoryOffset + directoryBytes > eocdOffset- ) throw new Error("The verified archive uses an unsupported ZIP layout.");+ const { entryCount, directoryBytes, directoryOffset } = endOfDirectory(tail, tailOffset);+ if (entryCount > maxEntries) throw new Error("The verified archive uses an unsupported ZIP layout.");const directory = Buffer.alloc(directoryBytes);- if ((await handle.read(directory, 0, directoryBytes, directoryOffset)).bytesRead !== directoryBytes) {+ if (!await fillBuffer(handle, directory, directoryOffset)) {throw new Error("The verified archive directory was truncated.");}- const entries: ZipMember[] = [];- let cursor = 0;- while (cursor < directory.length) {- if (cursor + 46 > directory.length || directory.readUInt32LE(cursor) !== CENTRAL_SIGNATURE) {- throw new Error("The verified archive directory is invalid.");- }- const flags = directory.readUInt16LE(cursor + 8);- const compression = directory.readUInt16LE(cursor + 10);- const compressedBytes = directory.readUInt32LE(cursor + 20);- const uncompressedBytes = directory.readUInt32LE(cursor + 24);- const nameBytes = directory.readUInt16LE(cursor + 28);- const extraBytes = directory.readUInt16LE(cursor + 30);- const commentBytes = directory.readUInt16LE(cursor + 32);- const startDisk = directory.readUInt16LE(cursor + 34);- const externalAttributes = directory.readUInt32LE(cursor + 38);- const localHeaderOffset = directory.readUInt32LE(cursor + 42);- const next = cursor + 46 + nameBytes + extraBytes + commentBytes;- if (- next > directory.length || startDisk !== 0 || (flags & 1) !== 0- || (compression !== 0 && compression !== 8)- || compressedBytes === 0xffffffff || uncompressedBytes === 0xffffffff- || localHeaderOffset === 0xffffffff- ) throw new Error("The verified archive contains an unsupported ZIP member.");- entries.push({- name: directory.subarray(cursor + 46, cursor + 46 + nameBytes).toString("utf8"),- compression,- compressedBytes,- uncompressedBytes,- localHeaderOffset,- externalAttributes,- });- cursor = next;- }- if (entries.length !== entryCount) throw new Error("The verified archive member count is invalid.");- return entries;+ const records = directoryRecords(directory, Infinity);+ if (records.length !== entryCount) throw new Error("The verified archive member count is invalid.");+ return records.map(({ member }) => {+ if (!member) throw new Error("The verified archive contains an unsupported ZIP member.");+ return member;+ });} finally {await handle.close();}@@@ -118,18 -181,12 +191,12 @@@ async function memberDataOffset(path: sconst handle = await open(path, "r");try {const header = Buffer.alloc(30);- if ((await handle.read(header, 0, header.length, entry.localHeaderOffset)).bytesRead !== header.length) {+ if (!await fillBuffer(handle, header, entry.localHeaderOffset)) {throw new Error("A verified archive member header was truncated.");}- const nameBytes = header.readUInt16LE(26);- const extraBytes = header.readUInt16LE(28);- if (- header.readUInt32LE(0) !== LOCAL_SIGNATURE- || header.readUInt16LE(8) !== entry.compression- || (header.readUInt16LE(6) & 1) !== 0- ) throw new Error("A verified archive member header is invalid.");+ const { nameBytes, extraBytes } = localHeaderLengths(header, entry);const name = Buffer.alloc(nameBytes);- if ((await handle.read(name, 0, nameBytes, entry.localHeaderOffset + 30)).bytesRead !== nameBytes) {+ if (!await fillBuffer(handle, name, entry.localHeaderOffset + 30)) {throw new Error("A verified archive member name was truncated.");}if (name.toString("utf8") !== entry.name) throw new Error("A verified archive member name did not match its directory.");