Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions convex/lib/clawpack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,32 @@ describe("clawpack", () => {
await expect(parseClawPack(pack)).rejects.toThrow("file/ancestor path collision");
});

it("inflates the full archive when the gzip size trailer is wrong", async () => {
const readme = "x".repeat(200_000);
const pack = npmPackFixture({
"package/package.json": JSON.stringify({ name: "demo", version: "1.0.0" }),
"package/README.md": readme,
});
// ISIZE is untrusted: an understated hint must grow the buffer, not truncate.
pack.set([1, 0, 0, 0], pack.byteLength - 4);

const parsed = await parseClawPack(pack);
const entry = parsed.entries.find((candidate) => candidate.path === "README.md");
expect(new TextDecoder().decode(entry?.bytes)).toBe(readme);
});

it("enforces the unpacked byte limit even when the size trailer understates it", async () => {
const pack = npmPackFixture({
"package/package.json": JSON.stringify({ name: "demo", version: "1.0.0" }),
"package/README.md": "x".repeat(200_000),
});
pack.set([1, 0, 0, 0], pack.byteLength - 4);

await expect(parseClawPack(pack, { maxTarBytes: 64 * 1024 })).rejects.toThrow(
"exceeds the requested byte limit",
);
});

it("uses npm-style tarball names", () => {
expect(npmTarballName("demo", "1.0.0")).toBe("demo-1.0.0.tgz");
expect(npmTarballName("@scope/demo", "1.0.0")).toBe("scope-demo-1.0.0.tgz");
Expand Down
36 changes: 23 additions & 13 deletions packages/clawhub/src/clawpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const MAX_TAR_BYTES = 64 * 1024 * 1024;
const MAX_FILE_BYTES = MAX_UNPACKED_BYTES;
const MAX_PACKAGE_JSON_BYTES = 256 * 1024;
const MAX_FILE_COUNT = 10_000;
const GZIP_INPUT_CHUNK_BYTES = 1_024;
const GZIP_INPUT_CHUNK_BYTES = 16_384;

function isRecord(value: unknown): value is Record<string, unknown> {
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
Expand Down Expand Up @@ -70,29 +70,39 @@ function normalizeTarPath(path: string) {
export class ClawPackTarLimitError extends Error {}

function gunzipBounded(bytes: Uint8Array, maxTarBytes: number): Uint8Array {
const chunks: Uint8Array[] = [];
// Inflate into one buffer: per-chunk copies held about 5x the unpacked size
// at peak and exhausted the 512 MB publish action on ~24 MB packages. The
// gzip trailer's size is only a hint; the limit check below stays authoritative.
const trailer = bytes.byteLength >= 4 ? bytes.byteLength - 4 : 0;
const sizeHint =
((bytes[trailer] ?? 0) |
((bytes[trailer + 1] ?? 0) << 8) |
((bytes[trailer + 2] ?? 0) << 16) |
((bytes[trailer + 3] ?? 0) << 24)) >>>
0;
let output = new Uint8Array(Math.min(maxTarBytes, Math.max(sizeHint, TAR_BLOCK_SIZE)));
let total = 0;
const gunzip = new Gunzip((chunk) => {
total += chunk.byteLength;
if (total > maxTarBytes)
const next = total + chunk.byteLength;
if (next > maxTarBytes)
throw new ClawPackTarLimitError(
maxTarBytes === MAX_TAR_BYTES
? "ClawPack unpacked archive exceeds 64MB limit"
: "ClawPack unpacked archive exceeds the requested byte limit",
);
chunks.push(Uint8Array.from(chunk));
if (next > output.byteLength) {
const grown = new Uint8Array(Math.min(maxTarBytes, Math.max(next, output.byteLength * 2)));
grown.set(output.subarray(0, total));
output = grown;
}
output.set(chunk, total);
total = next;
});
for (let offset = 0; offset < bytes.byteLength; offset += GZIP_INPUT_CHUNK_BYTES) {
const end = Math.min(offset + GZIP_INPUT_CHUNK_BYTES, bytes.byteLength);
gunzip.push(bytes.subarray(offset, end), end === bytes.byteLength);
}
const result = new Uint8Array(total);
let offset = 0;
for (const chunk of chunks) {
result.set(chunk, offset);
offset += chunk.byteLength;
}
return result;
return output.subarray(0, total);
}

function isZeroBlock(block: Uint8Array) {
Expand Down Expand Up @@ -138,7 +148,7 @@ function parseTarEntries(bytes: Uint8Array): ClawPackEntry[] {
if (entries.length >= MAX_FILE_COUNT) throw new Error("ClawPack exceeds 10000 file limit");
entries.push({
path: relPath,
bytes: Uint8Array.from(bytes.subarray(payloadOffset, payloadEnd)),
bytes: bytes.subarray(payloadOffset, payloadEnd),
});
} else if (typeflag !== "5") {
throw new Error("ClawPack may only contain regular files and directories");
Expand Down
Loading