|
| 1 | +import fs from "node:fs"; |
| 2 | +import os from "node:os"; |
| 3 | +import path from "node:path"; |
| 4 | +import { execFileSync } from "node:child_process"; |
| 5 | + |
| 6 | +export class ArchiveResolveError extends Error {} |
| 7 | + |
| 8 | +export type ArchiveFormat = "zip" | "tar.gz"; |
| 9 | + |
| 10 | +export type ResolveHttpArchiveOptions = { |
| 11 | + tempRoot?: string; |
| 12 | + maxDownloadBytes?: number; |
| 13 | + maxEntries?: number; |
| 14 | + maxExtractedBytes?: number; |
| 15 | +}; |
| 16 | + |
| 17 | +export type ResolvedHttpArchive = { |
| 18 | + format: ArchiveFormat; |
| 19 | + archivePath: string; |
| 20 | + extractPath: string; |
| 21 | + contentPath: string; |
| 22 | + finalUrl: string; |
| 23 | +}; |
| 24 | + |
| 25 | +const DEFAULT_MAX_DOWNLOAD_BYTES = 50 * 1024 * 1024; |
| 26 | +const DEFAULT_MAX_ENTRIES = 10_000; |
| 27 | +const DEFAULT_MAX_EXTRACTED_BYTES = 250 * 1024 * 1024; |
| 28 | + |
| 29 | +export async function resolveHttpArchive( |
| 30 | + sourceUrl: string, |
| 31 | + options: ResolveHttpArchiveOptions = {} |
| 32 | +): Promise<ResolvedHttpArchive> { |
| 33 | + const tempRoot = path.resolve(options.tempRoot ?? os.tmpdir()); |
| 34 | + fs.mkdirSync(tempRoot, { recursive: true }); |
| 35 | + const workDir = fs.mkdtempSync(path.join(tempRoot, "skillet-archive-")); |
| 36 | + |
| 37 | + const maxDownloadBytes = options.maxDownloadBytes ?? DEFAULT_MAX_DOWNLOAD_BYTES; |
| 38 | + const maxEntries = options.maxEntries ?? DEFAULT_MAX_ENTRIES; |
| 39 | + const maxExtractedBytes = options.maxExtractedBytes ?? DEFAULT_MAX_EXTRACTED_BYTES; |
| 40 | + |
| 41 | + const downloaded = await downloadArchive(sourceUrl, maxDownloadBytes); |
| 42 | + const format = detectArchiveFormat(downloaded.finalUrl, downloaded.contentType, downloaded.buffer); |
| 43 | + |
| 44 | + const archivePath = path.join(workDir, format === "zip" ? "archive.zip" : "archive.tar.gz"); |
| 45 | + fs.writeFileSync(archivePath, downloaded.buffer); |
| 46 | + |
| 47 | + const entries = listArchiveEntries(archivePath, format); |
| 48 | + if (entries.length > maxEntries) { |
| 49 | + throw new ArchiveResolveError(`Archive entry count exceeds limit (${maxEntries})`); |
| 50 | + } |
| 51 | + |
| 52 | + for (const entry of entries) { |
| 53 | + validateArchiveEntryPath(entry); |
| 54 | + } |
| 55 | + |
| 56 | + const extractPath = path.join(workDir, "extract"); |
| 57 | + fs.mkdirSync(extractPath, { recursive: true }); |
| 58 | + extractArchive(archivePath, format, extractPath); |
| 59 | + |
| 60 | + const extractedBytes = computeDirectorySize(extractPath); |
| 61 | + if (extractedBytes > maxExtractedBytes) { |
| 62 | + throw new ArchiveResolveError(`Extracted archive size exceeds limit (${maxExtractedBytes} bytes)`); |
| 63 | + } |
| 64 | + |
| 65 | + return { |
| 66 | + format, |
| 67 | + archivePath, |
| 68 | + extractPath, |
| 69 | + contentPath: normalizeContentPath(extractPath), |
| 70 | + finalUrl: downloaded.finalUrl, |
| 71 | + }; |
| 72 | +} |
| 73 | + |
| 74 | +type DownloadedArchive = { |
| 75 | + buffer: Buffer; |
| 76 | + contentType: string; |
| 77 | + finalUrl: string; |
| 78 | +}; |
| 79 | + |
| 80 | +async function downloadArchive(url: string, maxDownloadBytes: number): Promise<DownloadedArchive> { |
| 81 | + let response: Response; |
| 82 | + try { |
| 83 | + response = await fetch(url, { redirect: "follow" }); |
| 84 | + } catch (error) { |
| 85 | + const message = error instanceof Error ? error.message : String(error); |
| 86 | + throw new ArchiveResolveError(`Failed to fetch archive: ${message}`); |
| 87 | + } |
| 88 | + |
| 89 | + if (!response.ok) { |
| 90 | + throw new ArchiveResolveError(`HTTP archive request failed: ${response.status} ${response.statusText}`); |
| 91 | + } |
| 92 | + |
| 93 | + if (!response.body) { |
| 94 | + throw new ArchiveResolveError("HTTP archive response did not include a body"); |
| 95 | + } |
| 96 | + |
| 97 | + const reader = response.body.getReader(); |
| 98 | + const chunks: Buffer[] = []; |
| 99 | + let totalBytes = 0; |
| 100 | + |
| 101 | + while (true) { |
| 102 | + const { done, value } = await reader.read(); |
| 103 | + if (done) { |
| 104 | + break; |
| 105 | + } |
| 106 | + |
| 107 | + const chunk = Buffer.from(value); |
| 108 | + totalBytes += chunk.length; |
| 109 | + |
| 110 | + if (totalBytes > maxDownloadBytes) { |
| 111 | + throw new ArchiveResolveError(`Archive download exceeds limit (${maxDownloadBytes} bytes)`); |
| 112 | + } |
| 113 | + |
| 114 | + chunks.push(chunk); |
| 115 | + } |
| 116 | + |
| 117 | + return { |
| 118 | + buffer: Buffer.concat(chunks), |
| 119 | + contentType: response.headers.get("content-type") ?? "", |
| 120 | + finalUrl: response.url, |
| 121 | + }; |
| 122 | +} |
| 123 | + |
| 124 | +function detectArchiveFormat(finalUrl: string, contentType: string, buffer: Buffer): ArchiveFormat { |
| 125 | + const lowerUrl = finalUrl.toLowerCase(); |
| 126 | + const lowerContentType = contentType.toLowerCase(); |
| 127 | + |
| 128 | + if (lowerUrl.endsWith(".tar.gz") || lowerUrl.endsWith(".tgz")) { |
| 129 | + return "tar.gz"; |
| 130 | + } |
| 131 | + |
| 132 | + if (lowerUrl.endsWith(".zip")) { |
| 133 | + return "zip"; |
| 134 | + } |
| 135 | + |
| 136 | + if (lowerContentType.includes("application/zip")) { |
| 137 | + return "zip"; |
| 138 | + } |
| 139 | + |
| 140 | + if (lowerContentType.includes("application/gzip") || lowerContentType.includes("application/x-gzip")) { |
| 141 | + return "tar.gz"; |
| 142 | + } |
| 143 | + |
| 144 | + if (buffer.length >= 2 && buffer[0] === 0x1f && buffer[1] === 0x8b) { |
| 145 | + return "tar.gz"; |
| 146 | + } |
| 147 | + |
| 148 | + if ( |
| 149 | + buffer.length >= 4 && |
| 150 | + buffer[0] === 0x50 && |
| 151 | + buffer[1] === 0x4b && |
| 152 | + (buffer[2] === 0x03 || buffer[2] === 0x05 || buffer[2] === 0x07) && |
| 153 | + (buffer[3] === 0x04 || buffer[3] === 0x06 || buffer[3] === 0x08) |
| 154 | + ) { |
| 155 | + return "zip"; |
| 156 | + } |
| 157 | + |
| 158 | + throw new ArchiveResolveError("Unable to detect archive format (expected .zip or .tar.gz)"); |
| 159 | +} |
| 160 | + |
| 161 | +function listArchiveEntries(archivePath: string, format: ArchiveFormat): string[] { |
| 162 | + const args = format === "zip" ? ["-Z1", archivePath] : ["-tzf", archivePath]; |
| 163 | + const cmd = format === "zip" ? "unzip" : "tar"; |
| 164 | + const output = runCommand(cmd, args, "Failed to inspect archive contents"); |
| 165 | + return output |
| 166 | + .split(/\r?\n/) |
| 167 | + .map((entry) => entry.trim()) |
| 168 | + .filter((entry) => entry.length > 0); |
| 169 | +} |
| 170 | + |
| 171 | +function extractArchive(archivePath: string, format: ArchiveFormat, extractPath: string): void { |
| 172 | + if (format === "zip") { |
| 173 | + runCommand("unzip", ["-q", archivePath, "-d", extractPath], "Failed to extract zip archive"); |
| 174 | + return; |
| 175 | + } |
| 176 | + |
| 177 | + runCommand("tar", ["-xzf", archivePath, "-C", extractPath], "Failed to extract tar.gz archive"); |
| 178 | +} |
| 179 | + |
| 180 | +function validateArchiveEntryPath(entryPath: string): void { |
| 181 | + const sanitized = entryPath.replace(/\\/g, "/"); |
| 182 | + |
| 183 | + if (sanitized.length === 0) { |
| 184 | + return; |
| 185 | + } |
| 186 | + |
| 187 | + if (sanitized.startsWith("/") || /^[a-zA-Z]:/.test(sanitized)) { |
| 188 | + throw new ArchiveResolveError(`Archive entry uses absolute path: ${entryPath}`); |
| 189 | + } |
| 190 | + |
| 191 | + const normalized = path.posix.normalize(sanitized); |
| 192 | + if (normalized === ".." || normalized.startsWith("../") || normalized.includes("/../")) { |
| 193 | + throw new ArchiveResolveError(`Archive entry escapes extraction root: ${entryPath}`); |
| 194 | + } |
| 195 | +} |
| 196 | + |
| 197 | +function normalizeContentPath(extractPath: string): string { |
| 198 | + const entries = fs |
| 199 | + .readdirSync(extractPath) |
| 200 | + .filter((entry) => entry !== ".DS_Store" && entry !== "__MACOSX"); |
| 201 | + |
| 202 | + if (entries.length === 1) { |
| 203 | + const candidate = path.join(extractPath, entries[0]); |
| 204 | + if (fs.statSync(candidate).isDirectory()) { |
| 205 | + return candidate; |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + return extractPath; |
| 210 | +} |
| 211 | + |
| 212 | +function computeDirectorySize(directoryPath: string): number { |
| 213 | + let total = 0; |
| 214 | + const stack = [directoryPath]; |
| 215 | + |
| 216 | + while (stack.length > 0) { |
| 217 | + const current = stack.pop(); |
| 218 | + if (!current) { |
| 219 | + continue; |
| 220 | + } |
| 221 | + |
| 222 | + for (const entry of fs.readdirSync(current, { withFileTypes: true })) { |
| 223 | + const fullPath = path.join(current, entry.name); |
| 224 | + if (entry.isDirectory()) { |
| 225 | + stack.push(fullPath); |
| 226 | + continue; |
| 227 | + } |
| 228 | + |
| 229 | + if (entry.isFile()) { |
| 230 | + total += fs.statSync(fullPath).size; |
| 231 | + } |
| 232 | + } |
| 233 | + } |
| 234 | + |
| 235 | + return total; |
| 236 | +} |
| 237 | + |
| 238 | +function runCommand(cmd: string, args: string[], errorPrefix: string): string { |
| 239 | + try { |
| 240 | + return execFileSync(cmd, args, { |
| 241 | + encoding: "utf8", |
| 242 | + stdio: ["ignore", "pipe", "pipe"], |
| 243 | + }).trim(); |
| 244 | + } catch (error) { |
| 245 | + const stderr = |
| 246 | + error instanceof Error && "stderr" in error && typeof error.stderr === "string" |
| 247 | + ? error.stderr.trim() |
| 248 | + : ""; |
| 249 | + const details = stderr ? `: ${stderr}` : ""; |
| 250 | + throw new ArchiveResolveError(`${errorPrefix}${details}`); |
| 251 | + } |
| 252 | +} |
0 commit comments