Skip to content

Commit cf347bd

Browse files
Merge pull request #93 from codefresh-io/CR-14111
fixing vulnerabilities
2 parents 5aec75c + 39a4665 commit cf347bd

3 files changed

Lines changed: 133 additions & 243 deletions

File tree

lib/logic/Spwan.helper.js

Lines changed: 61 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,69 @@
11
const fs = require('fs');
22
const { resolve, join } = require('path');
33
const { homedir, arch } = require('os');
4+
let { pipeline } = require('stream');
5+
const { createGunzip } = require('zlib');
6+
const { promisify } = require('util');
47

58
const _ = require('lodash');
69
const rp = require('request-promise');
710
const request = require('request');
8-
const decompress = require('decompress');
9-
const decompressTargz = require('decompress-targz');
10-
const decompressUnzip = require('decompress-unzip');
11+
const tarStream = require('tar-stream');
12+
const zip = require('zip');
1113
const compareVersions = require('compare-versions');
1214

15+
pipeline = promisify(pipeline);
16+
1317
const CODEFRESH_PATH = resolve(homedir(), '.Codefresh');
1418

19+
async function unzipFile(zipPath, outputPath) {
20+
const zipBuffer = await fs.promises.readFile(zipPath);
21+
const zr = zip.Reader(zipBuffer);
22+
23+
const fileWrites = [];
24+
zr.forEach((entry) => {
25+
if (!entry.isFile()) {
26+
return;
27+
}
28+
29+
const outputFilePath = join(outputPath, entry.getName());
30+
fileWrites.push(fs.promises.writeFile(outputFilePath, entry.getData(), { mode: entry.getMode() }));
31+
});
32+
33+
return Promise.all(fileWrites);
34+
}
35+
36+
async function untarFile(tarPath, outputPath) {
37+
const zipFile = fs.createReadStream(tarPath);
38+
const unzipStream = createGunzip();
39+
const extract = tarStream.extract();
40+
41+
extract.on('entry', async (headers, stream, next) => {
42+
if (headers.type !== 'file') {
43+
return next();
44+
}
45+
46+
try {
47+
const outputFilePath = join(outputPath, headers.name);
48+
const outputFile = fs.createWriteStream(outputFilePath, { mode: headers.mode });
49+
await pipeline(stream, outputFile);
50+
return next();
51+
} catch (error) {
52+
return next(error);
53+
}
54+
});
55+
56+
await pipeline(
57+
zipFile,
58+
unzipStream,
59+
extract,
60+
);
61+
}
62+
1563
const prepareSpwan = async ({ name, repoName, pathName, branch = 'master', excludeVersionPrefix = false, events }) => {
1664
const dirPath = join(CODEFRESH_PATH, name);
1765
const versionPath = join(CODEFRESH_PATH, name, 'version.txt');
66+
const outputPath = join(CODEFRESH_PATH, name);
1867
const filePath = join(CODEFRESH_PATH, name, repoName);
1968
const fullPath = pathName ? join(repoName, branch, pathName) : join(repoName, branch);
2069
const versionUrl = `https://raw.githubusercontent.com/codefresh-io/${fullPath}/VERSION`;
@@ -70,25 +119,15 @@ const prepareSpwan = async ({ name, repoName, pathName, branch = 'master', exclu
70119
});
71120
}
72121

73-
req.pipe(fs.createWriteStream(zipPath));
74-
const p = new Promise((resolveFn, rejectFn) => {
75-
req.on('end', () => {
76-
decompress(zipPath, join(homedir(), '.Codefresh', name), {
77-
plugins: [
78-
decompressTargz(),
79-
decompressUnzip(),
80-
],
81-
}).then(() => {
82-
fs.writeFile(versionPath, version, (err) => {
83-
if (err) {
84-
rejectFn(err);
85-
}
86-
});
87-
resolveFn(filePath);
88-
});
89-
});
90-
});
91-
return p;
122+
await pipeline(req, fs.createWriteStream(zipPath));
123+
124+
if (zipPath.endsWith('.zip')) {
125+
await unzipFile(zipPath, outputPath);
126+
} else {
127+
await untarFile(zipPath, outputPath);
128+
}
129+
130+
await fs.promises.writeFile(versionPath, version);
92131
}
93132
return filePath;
94133
};

package.json

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "codefresh-sdk",
3-
"version": "1.10.0",
3+
"version": "1.11.0",
44
"description": "Codefresh_api_swagger_3_0_specification",
55
"main": "index.js",
66
"author": {
@@ -25,22 +25,21 @@
2525
"cf-errors": "^0.1.16",
2626
"compare-versions": "^3.4.0",
2727
"debug": "^4.1.1",
28-
"decompress": "^4.2.1",
29-
"decompress-targz": "^4.1.1",
30-
"decompress-unzip": "^4.0.1",
3128
"firebase": "git+https://github.com/codefresh-io/firebase.git#80b2ed883ff281cd67b53bd0f6a0bbd6f330fed5",
3229
"fs-extra": "^7.0.1",
3330
"js-yaml": "^3.13.1",
3431
"jsonwebtoken": "^8.4.0",
3532
"lodash": "^4.17.21",
36-
"moment": "^2.24.0",
33+
"moment": "^2.29.4",
3734
"recursive-readdir": "^2.2.2",
3835
"request": "2.88.2",
3936
"request-promise": "4.2.6",
4037
"requestretry": "^7.0.2",
4138
"swagger-client": "~3.13.7",
39+
"tar-stream": "^2.2.0",
4240
"uniqid": "^5.4.0",
43-
"uuid": "^3.3.2"
41+
"uuid": "^3.3.2",
42+
"zip": "^1.2.0"
4443
},
4544
"devDependencies": {
4645
"eslint": "^7.32.0",

0 commit comments

Comments
 (0)