Skip to content

Commit

Permalink
Catch exceptions in tar handling (#4515)
Browse files Browse the repository at this point in the history
- adds error handlers for all streams
- catches exceptions raised in the non-trivial `entry` handler
- rejects in all cases

Partially addresses #4512
  • Loading branch information
mattgodbolt committed Jan 4, 2023
1 parent 2f0daa3 commit 184a607
Showing 1 changed file with 37 additions and 18 deletions.
55 changes: 37 additions & 18 deletions lib/buildenvsetup/ceconan.ts
Expand Up @@ -111,33 +111,47 @@ export class BuildEnvSetupCeConanDirect extends BuildEnvSetupBase {
}

async downloadAndExtractPackage(libId, version, downloadPath, packageUrl): Promise<BuildEnvDownloadInfo> {
return new Promise(resolve => {
return new Promise((resolve, reject) => {
const startTime = process.hrtime.bigint();
const extract = tar.extract();
const gunzip = zlib.createGunzip();

extract.on('entry', async (header, stream, next) => {
let filepath = '';
if (this.extractAllToRoot) {
const filename = path.basename(header.name);
filepath = path.join(downloadPath, filename);
} else {
const filename = header.name;
filepath = path.join(downloadPath, filename);
const resolved = path.resolve(path.dirname(filepath));
if (!resolved.startsWith(downloadPath)) {
logger.error(`Library ${libId}/${version} is using a zip-slip, skipping file`);
next();
return;
try {
let filepath = '';
if (this.extractAllToRoot) {
const filename = path.basename(header.name);
filepath = path.join(downloadPath, filename);
} else {
const filename = header.name;
filepath = path.join(downloadPath, filename);
const resolved = path.resolve(path.dirname(filepath));
if (!resolved.startsWith(downloadPath)) {
logger.error(`Library ${libId}/${version} is using a zip-slip, skipping file`);
next();
return;
}

await mkdirp(path.dirname(filepath));
}

await mkdirp(path.dirname(filepath));
const filestream = fs.createWriteStream(filepath);
stream.pipe(filestream);
stream.on('error', error => {
logger.error(`Error in stream handling: ${error}`);
reject(error);
});
stream.on('end', next);
stream.resume();
} catch (error) {
logger.error(`Error in entry handling: ${error}`);
reject(error);
}
});

const filestream = fs.createWriteStream(filepath);
stream.pipe(filestream);
stream.on('end', next);
stream.resume();
extract.on('error', error => {
logger.error(`Error in tar handling: ${error}`);
reject(error);
});

extract.on('finish', () => {
Expand All @@ -149,6 +163,11 @@ export class BuildEnvSetupCeConanDirect extends BuildEnvSetupBase {
});
});

gunzip.on('error', error => {
logger.error(`Error in gunzip handling: ${error}`);
reject(error);
});

gunzip.pipe(extract);

const settings = {
Expand Down

0 comments on commit 184a607

Please sign in to comment.