()
| 93 | } |
| 94 | |
| 95 | async function main() { |
| 96 | const platform = getPlatform(); |
| 97 | const arch = getArch(); |
| 98 | const ext = platform === 'windows' ? 'zip' : 'tar.gz'; |
| 99 | const binName = platform === 'windows' ? 'codebase-memory-mcp.exe' : 'codebase-memory-mcp'; |
| 100 | const binPath = path.join(BIN_DIR, binName); |
| 101 | |
| 102 | if (fs.existsSync(binPath)) { |
| 103 | return; // already installed, nothing to do |
| 104 | } |
| 105 | |
| 106 | fs.mkdirSync(BIN_DIR, { recursive: true }); |
| 107 | |
| 108 | // Linux ships a fully-static "-portable" build; the standard linux binary |
| 109 | // dynamically links glibc 2.38+ and fails on older distros. macOS/Windows |
| 110 | // have no such variant. Keep in sync with install.sh / pypi _cli.py / cli.c. |
| 111 | const variant = platform === 'linux' ? '-portable' : ''; |
| 112 | const archive = `codebase-memory-mcp-${platform}-${arch}${variant}.${ext}`; |
| 113 | const url = `https://github.com/${REPO}/releases/download/v${VERSION}/${archive}`; |
| 114 | |
| 115 | process.stdout.write(`codebase-memory-mcp: downloading v${VERSION} for ${platform}/${arch}...\n`); |
| 116 | |
| 117 | const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cbm-install-')); |
| 118 | const tmpArchive = path.join(tmpDir, `cbm.${ext}`); |
| 119 | |
| 120 | try { |
| 121 | await download(url, tmpArchive); |
| 122 | await verifyChecksum(tmpArchive, archive); |
| 123 | |
| 124 | // Extract using execFileSync (array args — no shell injection). |
| 125 | if (ext === 'tar.gz') { |
| 126 | execFileSync('tar', ['-xzf', tmpArchive, '-C', tmpDir, '--no-same-owner']); |
| 127 | } else { |
| 128 | execFileSync('powershell', [ |
| 129 | '-NoProfile', '-Command', |
| 130 | `Expand-Archive -Path '${tmpArchive}' -DestinationPath '${tmpDir}' -Force`, |
| 131 | ]); |
| 132 | } |
| 133 | |
| 134 | // Validate extracted path doesn't escape tmpDir (tar-slip defense). |
| 135 | const extracted = path.join(tmpDir, binName); |
| 136 | const resolvedExtracted = path.resolve(extracted); |
| 137 | const resolvedTmpDir = path.resolve(tmpDir); |
| 138 | if (!resolvedExtracted.startsWith(resolvedTmpDir + path.sep)) { |
| 139 | throw new Error(`Path traversal detected in archive: ${binName}`); |
| 140 | } |
| 141 | if (!fs.existsSync(extracted)) { |
| 142 | throw new Error(`Binary not found after extraction at ${extracted}`); |
| 143 | } |
| 144 | |
| 145 | fs.copyFileSync(extracted, binPath); |
| 146 | fs.chmodSync(binPath, 0o755); |
| 147 | |
| 148 | process.stdout.write('codebase-memory-mcp: ready.\n'); |
| 149 | } finally { |
| 150 | fs.rmSync(tmpDir, { recursive: true, force: true }); |
| 151 | } |
| 152 | } |
no test coverage detected