Skip to content

Commit 52ab4e5

Browse files
committed
fix(pkg): apply the Go wrapper's archive size limits to npm and PyPI
The Go wrapper caps the compressed release download at 256 MiB and bounds extraction to 64 archive members, 256 MiB per member and 512 MiB expanded in total. The npm and PyPI wrappers had the download cap parameter but never passed it for the release archive, and extracted members without any size bound. Both wrappers now use the same four limits as named constants. The download rejects an over-limit Content-Length before writing (only plain ASCII digits count as a length), stops as soon as the received body exceeds the limit, and removes the partial file. The PyPI extractor checks the member count and the declared sizes before writing and copies each member through a counted loop that reads at most one byte past the limit and compares the actual size with the declared one. The npm tar path keeps the system tar listing and the exact four-name check as the member bound, then extracts only the executable through `tar -xzOf` piped into a file the wrapper creates itself, counting bytes: past the per-member limit the child is stopped and the file removed; a non-zero tar exit carries tar's own diagnostic text; an empty result is a failure. The file is removed only when this run created it, so a pre-existing target is never deleted. The companions are listed but never written. The PowerShell zip path counts entries and declared sizes and copies the executable through a counted loop instead of ExtractToFile, with its own failure text in the install error. Any limit hit fails with a message naming the limit and leaves no partial output behind. Tests cover an over-limit download (declared, actual, and non-numeric declared lengths), a member stopped in the counted writer, a pre-existing target left untouched, tar failing mid-stream, an empty member, too many members and over-limit declared sizes for zip, and a normal archive installing byte-identical with the executable bit set. The PowerShell cases run on Windows hosts only. Signed-off-by: Martin Vogel <[email protected]>
1 parent cb7d937 commit 52ab4e5

6 files changed

Lines changed: 1223 additions & 69 deletions

File tree

‎pkg/npm/install.js‎

Lines changed: 231 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const crypto = require('crypto');
88
const fs = require('fs');
99
const path = require('path');
1010
const os = require('os');
11-
const { execFileSync } = require('child_process');
11+
const { execFileSync, spawn } = require('child_process');
1212
const { pipeline } = require('stream');
1313

1414
const REPO = 'DeusData/codebase-memory-mcp';
@@ -18,6 +18,20 @@ const MAX_REDIRECTS = 5;
1818
const DOWNLOAD_HOP_TIMEOUT_MS = 120_000;
1919
const CANDIDATE_TIMEOUT_MS = 15_000;
2020
const MAX_CHECKSUM_MANIFEST_BYTES = 1024 * 1024;
21+
// Release archive resource limits. Keep in sync with the Go wrapper
22+
// (pkg/go/cmd/codebase-memory-mcp/main.go) and the PyPI wrapper (_cli.py).
23+
const MAX_RELEASE_ARCHIVE_BYTES = 256 * 1024 * 1024;
24+
const MAX_ARCHIVE_MEMBERS = 64;
25+
const MAX_ARCHIVE_MEMBER_BYTES = 256 * 1024 * 1024;
26+
const MAX_ARCHIVE_EXPANDED_BYTES = 512 * 1024 * 1024;
27+
const DEFAULT_ARCHIVE_LIMITS = Object.freeze({
28+
compressedBytes: MAX_RELEASE_ARCHIVE_BYTES,
29+
members: MAX_ARCHIVE_MEMBERS,
30+
memberBytes: MAX_ARCHIVE_MEMBER_BYTES,
31+
expandedBytes: MAX_ARCHIVE_EXPANDED_BYTES,
32+
});
33+
// Bytes of tar's own diagnostics kept for the failure message.
34+
const MAX_TAR_STDERR_CHARS = 4096;
2135
const WINDOWS_BINARY_NAME = 'codebase-memory-mcp.exe';
2236
const UNIX_ARCHIVE_NAMES = [
2337
'codebase-memory-mcp',
@@ -90,21 +104,131 @@ function validateExactTarMemberListing(listing, expectedNames) {
90104
}
91105
}
92106

93-
function extractExactTarArchive(
94-
archivePath, destPath, expectedNames, targetName, runFile = execFileSync,
107+
function validateArchiveLimits(limits) {
108+
if (!limits || !(limits.compressedBytes > 0) || !(limits.members > 0) ||
109+
!(limits.memberBytes > 0) || !(limits.expandedBytes > 0)) {
110+
throw new Error('invalid archive resource safety limits');
111+
}
112+
}
113+
114+
function requireCompressedArchiveWithinLimit(archivePath, limits) {
115+
const status = fs.statSync(archivePath);
116+
if (!status.isFile()) {
117+
throw new Error('release archive is not a regular file');
118+
}
119+
if (status.size > limits.compressedBytes) {
120+
throw new Error(
121+
`release archive exceeds the ${limits.compressedBytes}-byte compressed safety limit`,
122+
);
123+
}
124+
}
125+
126+
// Streams one member through `tar -xzOf` into a file this call creates,
127+
// counting every byte against the per-member limit. The file is removed only
128+
// when this call created it: a pre-existing target fails untouched.
129+
function extractTarMemberWithLimit(
130+
archivePath, target, memberName, limits, spawnFile = spawn,
95131
) {
132+
return new Promise((resolve, reject) => {
133+
let fd;
134+
try {
135+
fd = fs.openSync(target, 'wx', 0o600);
136+
} catch (err) {
137+
reject(err);
138+
return;
139+
}
140+
let written = 0;
141+
let failure = null;
142+
let settled = false;
143+
let stderr = '';
144+
const finish = (err) => {
145+
if (settled) return;
146+
settled = true;
147+
try { fs.closeSync(fd); } catch (closeError) { err = err || closeError; }
148+
if (err) {
149+
try { fs.unlinkSync(target); } catch (_) { /* already gone */ }
150+
reject(err);
151+
return;
152+
}
153+
resolve(written);
154+
};
155+
const child = spawnFile('tar', ['-xzOf', archivePath, memberName], {
156+
stdio: ['ignore', 'pipe', 'pipe'],
157+
windowsHide: true,
158+
});
159+
const fail = (err) => {
160+
if (failure) return;
161+
failure = err;
162+
child.kill();
163+
};
164+
child.stdout.on('data', (chunk) => {
165+
if (failure) return;
166+
written += chunk.length;
167+
if (written > limits.memberBytes) {
168+
fail(new Error(
169+
`archive member ${JSON.stringify(memberName)} exceeds the ${limits.memberBytes}-byte expanded safety limit`,
170+
));
171+
return;
172+
}
173+
try {
174+
let offset = 0;
175+
while (offset < chunk.length) {
176+
offset += fs.writeSync(fd, chunk, offset, chunk.length - offset);
177+
}
178+
} catch (err) {
179+
fail(err);
180+
}
181+
});
182+
child.stderr.setEncoding('utf8');
183+
child.stderr.on('data', (text) => {
184+
if (stderr.length < MAX_TAR_STDERR_CHARS) stderr += text;
185+
});
186+
child.on('error', (err) => {
187+
failure = failure || err;
188+
// A process that never started (no tar on PATH) has no exit to wait for.
189+
if (child.pid === undefined) finish(failure);
190+
});
191+
child.on('close', (code, signal) => {
192+
const detail = stderr.trim();
193+
let err = failure;
194+
if (!err && signal) {
195+
err = new Error(
196+
`tar was terminated by ${signal} while extracting ${memberName}: ${detail}`,
197+
);
198+
} else if (!err && code !== 0) {
199+
err = new Error(
200+
`tar exited with status ${code} while extracting ${memberName}: ${detail}`,
201+
);
202+
} else if (!err && written === 0) {
203+
err = new Error(`archive member ${memberName} is empty`);
204+
}
205+
finish(err);
206+
});
207+
});
208+
}
209+
210+
async function extractExactTarArchive(
211+
archivePath, destPath, expectedNames, targetName,
212+
runFile = execFileSync, limits = DEFAULT_ARCHIVE_LIMITS, spawnFile = spawn,
213+
) {
214+
validateArchiveLimits(limits);
215+
requireCompressedArchiveWithinLimit(archivePath, limits);
216+
// The exact root namespace is the member bound: its four names lie well
217+
// within MAX_ARCHIVE_MEMBERS, which the zip path enforces by count.
218+
if (expectedNames.length > limits.members) {
219+
throw new Error(`archive namespace exceeds the ${limits.members}-member safety limit`);
220+
}
96221
const listing = runFile('tar', ['-tzf', archivePath], {
97222
encoding: 'utf8',
98223
maxBuffer: MAX_CHECKSUM_MANIFEST_BYTES,
99224
windowsHide: true,
100225
});
101226
validateExactTarMemberListing(listing, expectedNames);
102-
// Extract only the fixed root executable. Even a malformed tar implementation
103-
// cannot write an unvalidated companion member outside the private temp tree.
104-
runFile('tar', ['-xzf', archivePath, '-C', destPath, targetName], {
105-
stdio: 'inherit',
106-
windowsHide: true,
107-
});
227+
// Only the fixed root executable is ever written, through the counted
228+
// writer; the companions are listed but never extracted.
229+
const target = path.join(destPath, targetName);
230+
await extractTarMemberWithLimit(archivePath, target, targetName, limits, spawnFile);
231+
fs.chmodSync(target, 0o755);
108232
}
109233

110234
function downloadHop(url, dest, maxBytes) {
@@ -123,7 +247,11 @@ function downloadHop(url, dest, maxBytes) {
123247

124248
const req = https.get(url, (res) => {
125249
response = res;
126-
res.on('error', (err) => finish(err));
250+
let piped = false;
251+
// Before the body is piped, a response error ends the hop directly;
252+
// afterwards the pipeline reports it, so the partial file is discarded
253+
// before the hop settles.
254+
res.on('error', (err) => { if (!piped) finish(err); });
127255
const redirectCodes = new Set([301, 302, 303, 307, 308]);
128256
if (redirectCodes.has(res.statusCode)) {
129257
const location = res.headers.location;
@@ -149,6 +277,12 @@ function downloadHop(url, dest, maxBytes) {
149277
finish(new Error(`HTTP ${res.statusCode} for ${url}`));
150278
return;
151279
}
280+
const declared = Number(res.headers['content-length']);
281+
if (maxBytes && Number.isFinite(declared) && declared > maxBytes) {
282+
res.destroy();
283+
finish(new Error(`Download exceeds the ${maxBytes}-byte safety limit`));
284+
return;
285+
}
152286

153287
let received = 0;
154288
res.on('data', (chunk) => {
@@ -158,8 +292,20 @@ function downloadHop(url, dest, maxBytes) {
158292
}
159293
});
160294
const file = fs.createWriteStream(dest, { flags: 'w' });
295+
piped = true;
161296
pipeline(res, file, (err) => {
162-
finish(err, { redirect: null });
297+
if (!err) {
298+
finish(null, { redirect: null });
299+
return;
300+
}
301+
// Discard the partial file once its handle is closed (Windows refuses
302+
// to unlink an open file), then report the failure.
303+
const discard = () => {
304+
try { fs.unlinkSync(dest); } catch (_) { /* nothing was written */ }
305+
finish(err);
306+
};
307+
if (file.closed) discard();
308+
else file.once('close', discard);
163309
});
164310
});
165311
req.on('error', (err) => finish(err));
@@ -457,19 +603,37 @@ function installWindowsBinaryAtomically(sourceDir, destDir, verifier = verifyCan
457603
}
458604
}
459605

460-
function extractZipOnWindows(archivePath, destPath, requiredNames, extractNames) {
606+
function extractZipOnWindows(
607+
archivePath, destPath, requiredNames, extractNames,
608+
limits = DEFAULT_ARCHIVE_LIMITS, shell = 'powershell',
609+
) {
610+
validateArchiveLimits(limits);
611+
requireCompressedArchiveWithinLimit(archivePath, limits);
461612
// -EncodedCommand is a constant program. Paths travel only through the child
462613
// environment and are consumed with -LiteralPath, so PowerShell never parses
463614
// user/TEMP path bytes as source code or wildcard syntax.
464615
const script = [
465616
"$ErrorActionPreference = 'Stop'",
466617
'Add-Type -AssemblyName System.IO.Compression.FileSystem',
618+
'$maxMembers = [int]$env:CBM_NPM_MAX_MEMBERS',
619+
'$maxMemberBytes = [long]$env:CBM_NPM_MAX_MEMBER_BYTES',
620+
'$maxExpandedBytes = [long]$env:CBM_NPM_MAX_EXPANDED_BYTES',
621+
'$memberCount = 0',
622+
'$declaredExpanded = [long]0',
467623
'$zip = [System.IO.Compression.ZipFile]::OpenRead($env:CBM_NPM_ARCHIVE_PATH)',
468624
"$seen = New-Object 'System.Collections.Generic.HashSet[string]' ([System.StringComparer]::OrdinalIgnoreCase)",
469625
"$requiredNames = @($env:CBM_NPM_REQUIRED_NAMES.Split('|'))",
470626
'$targetCounts = @{}',
471627
'foreach ($requiredName in $requiredNames) { $targetCounts[$requiredName] = 0 }',
472628
'try { foreach ($entry in $zip.Entries) { ' +
629+
'$memberCount = $memberCount + 1; ' +
630+
'if ($memberCount -gt $maxMembers) { ' +
631+
'throw "archive exceeds the $maxMembers-member safety limit" }; ' +
632+
'if ($entry.Length -gt $maxMemberBytes) { ' +
633+
'throw "archive member $($entry.FullName) exceeds the $maxMemberBytes-byte expanded safety limit" }; ' +
634+
'if ($declaredExpanded -gt ($maxExpandedBytes - $entry.Length)) { ' +
635+
'throw "archive exceeds the $maxExpandedBytes-byte aggregate expanded safety limit" }; ' +
636+
'$declaredExpanded = $declaredExpanded + $entry.Length; ' +
473637
"$name = $entry.FullName.Replace('\\', '/'); " +
474638
"$directory = $name.EndsWith('/'); " +
475639
"$segmentsPath = if ($directory) { $name.TrimEnd('/') } else { $name }; " +
@@ -491,27 +655,61 @@ function extractZipOnWindows(archivePath, destPath, requiredNames, extractNames)
491655
'if ($seen.Count -ne $requiredNames.Count) { ' +
492656
'throw "archive does not match the exact release root-file allowlist" }',
493657
"$extractNames = @($env:CBM_NPM_EXTRACT_NAMES.Split('|'))",
658+
'$actualExpanded = [long]0',
659+
'$buffer = New-Object byte[] 65536',
494660
'$extractZip = [System.IO.Compression.ZipFile]::OpenRead($env:CBM_NPM_ARCHIVE_PATH)',
661+
// Copy by hand with a byte counter: ExtractToFile would write whatever the
662+
// compressed stream yields, regardless of the declared size.
495663
'try { foreach ($extractName in $extractNames) { ' +
496664
'$entry = @($extractZip.Entries | Where-Object { $_.FullName -ceq $extractName })[0]; ' +
497-
'[System.IO.Compression.ZipFileExtensions]::ExtractToFile(' +
498-
'$entry, (Join-Path $env:CBM_NPM_DEST_PATH $extractName), $false) ' +
665+
'$targetPath = Join-Path $env:CBM_NPM_DEST_PATH $extractName; ' +
666+
'$remaining = $maxExpandedBytes - $actualExpanded; ' +
667+
'$copied = [long]0; ' +
668+
'$source = $entry.Open(); ' +
669+
'$target = [System.IO.File]::Open($targetPath, [System.IO.FileMode]::CreateNew, [System.IO.FileAccess]::Write); ' +
670+
'try { while ($true) { ' +
671+
'$count = $source.Read($buffer, 0, $buffer.Length); ' +
672+
'if ($count -le 0) { break }; ' +
673+
'$copied = $copied + $count; ' +
674+
'if ($copied -gt $maxMemberBytes) { ' +
675+
'throw "archive member $extractName exceeds the $maxMemberBytes-byte actual expanded safety limit" }; ' +
676+
'if ($copied -gt $remaining) { ' +
677+
'throw "archive exceeds the $maxExpandedBytes-byte aggregate actual expanded safety limit" }; ' +
678+
'$target.Write($buffer, 0, $count) } ' +
679+
'} catch { $target.Dispose(); ' +
680+
'Remove-Item -LiteralPath $targetPath -Force -ErrorAction SilentlyContinue; throw ' +
681+
'} finally { $target.Dispose(); $source.Dispose() }; ' +
682+
'if ($copied -ne $entry.Length) { ' +
683+
'Remove-Item -LiteralPath $targetPath -Force -ErrorAction SilentlyContinue; ' +
684+
'throw "archive member $extractName actual size $copied does not match declared size $($entry.Length)" }; ' +
685+
'$actualExpanded = $actualExpanded + $copied ' +
499686
'} } finally { $extractZip.Dispose() }',
500687
].join('; ');
501688
const encoded = Buffer.from(script, 'utf16le').toString('base64');
502-
execFileSync('powershell', [
503-
'-NoLogo', '-NoProfile', '-NonInteractive', '-EncodedCommand', encoded,
504-
], {
505-
env: {
506-
...process.env,
507-
CBM_NPM_ARCHIVE_PATH: archivePath,
508-
CBM_NPM_DEST_PATH: destPath,
509-
CBM_NPM_REQUIRED_NAMES: requiredNames.join('|'),
510-
CBM_NPM_EXTRACT_NAMES: extractNames.join('|'),
511-
},
512-
stdio: 'inherit',
513-
windowsHide: true,
514-
});
689+
try {
690+
execFileSync(shell, [
691+
'-NoLogo', '-NoProfile', '-NonInteractive', '-EncodedCommand', encoded,
692+
], {
693+
env: {
694+
...process.env,
695+
CBM_NPM_ARCHIVE_PATH: archivePath,
696+
CBM_NPM_DEST_PATH: destPath,
697+
CBM_NPM_REQUIRED_NAMES: requiredNames.join('|'),
698+
CBM_NPM_EXTRACT_NAMES: extractNames.join('|'),
699+
CBM_NPM_MAX_MEMBERS: String(limits.members),
700+
CBM_NPM_MAX_MEMBER_BYTES: String(limits.memberBytes),
701+
CBM_NPM_MAX_EXPANDED_BYTES: String(limits.expandedBytes),
702+
},
703+
encoding: 'utf8',
704+
stdio: ['ignore', 'inherit', 'pipe'],
705+
windowsHide: true,
706+
});
707+
} catch (err) {
708+
// Surface the script's own reason (for example which limit was hit)
709+
// instead of a bare "Command failed".
710+
const detail = err && err.stderr ? String(err.stderr).trim() : '';
711+
throw new Error(detail ? `zip extraction failed: ${detail}` : err.message);
712+
}
515713
}
516714

517715
// Fetch checksums.txt and verify the archive hash.
@@ -592,12 +790,12 @@ async function main() {
592790
const tmpArchive = path.join(tmpDir, `cbm.${ext}`);
593791

594792
try {
595-
await download(url, tmpArchive);
793+
await download(url, tmpArchive, MAX_RELEASE_ARCHIVE_BYTES);
596794
await verifyChecksum(tmpArchive, archive);
597795

598796
// Validate the complete archive namespace, then extract only executables.
599797
if (ext === 'tar.gz') {
600-
extractExactTarArchive(
798+
await extractExactTarArchive(
601799
tmpArchive, tmpDir, archiveNames, binName,
602800
);
603801
} else {
@@ -674,9 +872,13 @@ if (require.main === module || module.parent == null) {
674872
}
675873

676874
module.exports = {
875+
DEFAULT_ARCHIVE_LIMITS,
677876
UNIX_ARCHIVE_NAMES,
877+
WINDOWS_ARCHIVE_NAMES,
678878
WINDOWS_BINARY_NAME,
879+
download,
679880
extractExactTarArchive,
881+
extractZipOnWindows,
680882
installWindowsBinaryAtomically,
681883
validateExactTarMemberListing,
682884
windowsBinaryReady,

0 commit comments

Comments
 (0)