Skip to content
Closed
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
34 changes: 33 additions & 1 deletion npm/src/install.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,35 @@ function extractFileFromTarball(tarballBuffer, filepath) {
throw new Error(`File ${filepath} not found in tarball`)
}

/**
* Gunzip with an explicit upper bound to avoid decompression bombs.
* @param {Buffer<ArrayBufferLike>} buffer
* @param {number} maxBytes
* @returns {Promise<Buffer<ArrayBufferLike>>}
*/
function gunzipWithLimit(buffer, maxBytes) {
return new Promise((resolve, reject) => {
const gunzip = NodeZlib.createGunzip()
/** @type {Array<Buffer>} */
const chunks = []
let total = 0

gunzip.on('data', chunk => {
total += chunk.length
if (total > maxBytes) {
gunzip.destroy(new Error('Decompressed tarball exceeds maximum allowed size'))
return
}
chunks.push(chunk)
})

gunzip.on('error', reject)
gunzip.on('end', () => resolve(Buffer.concat(chunks)))

gunzip.end(buffer)
})
}

async function downloadBinaryFromRegistry() {
if (!platformSpecificPackageName)
throw new Error('Platform-specific package name is not defined')
Expand Down Expand Up @@ -330,7 +359,10 @@ async function downloadBinaryFromRegistry() {
}

// Unpack and write binary
const tarballBuffer = NodeZlib.gunzipSync(tarballDownloadBuffer)
const tarballBuffer = await gunzipWithLimit(
tarballDownloadBuffer,
MAX_BINARY_BYTES
)

NodeFS.writeFileSync(
fallbackBinaryPath,
Expand Down
Loading