@@ -8,7 +8,7 @@ const crypto = require('crypto');
88const fs = require ( 'fs' ) ;
99const path = require ( 'path' ) ;
1010const os = require ( 'os' ) ;
11- const { execFileSync } = require ( 'child_process' ) ;
11+ const { execFileSync, spawn } = require ( 'child_process' ) ;
1212const { pipeline } = require ( 'stream' ) ;
1313
1414const REPO = 'DeusData/codebase-memory-mcp' ;
@@ -18,6 +18,20 @@ const MAX_REDIRECTS = 5;
1818const DOWNLOAD_HOP_TIMEOUT_MS = 120_000 ;
1919const CANDIDATE_TIMEOUT_MS = 15_000 ;
2020const 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 ;
2135const WINDOWS_BINARY_NAME = 'codebase-memory-mcp.exe' ;
2236const 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
110234function 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
676874module . 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