Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: js2ts protect #1145

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
},
"devDependencies": {
"@types/agent-base": "^4.2.1",
"@types/diff": "^3.5.2",
"@types/diff": "^4.0.2",
"@types/needle": "^2.0.4",
"@types/node": "8.10.59",
"@types/restify": "^8.4.2",
Expand Down
2 changes: 1 addition & 1 deletion src/cli/commands/protect/wizard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ function processAnswers(answers, policy, options) {
const lbl = 'Updating npm-shrinkwrap.json...';
return (
spinner(lbl)
.then(() => npm('shrinkwrap', null, live, cwd, null))
.then(() => npm('shrinkwrap', null, live, cwd))
// clear spinner in case of success or failure
.then(spinner.clear(lbl))
.catch((error) => {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ function npm(
method: string,
packages: string[] | null,
live: boolean,
cwd: string | null,
flags: string[] | null,
cwd?: string,
flags?: string[],
): Promise<void> {
flags = flags || [];
if (!packages) {
Expand Down
42 changes: 22 additions & 20 deletions src/lib/protect/apply-patch.js → src/lib/protect/apply-patch.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
module.exports = applyPatch;
export = applyPatch;

const debug = require('debug')('snyk');
const diff = require('diff');
const exec = require('child_process').exec;
const path = require('path');
const fs = require('fs');
const uuid = require('uuid/v4');
const semver = require('semver');
const errorAnalytics = require('../analytics').single;
import * as debugModule from 'debug';
import * as diff from 'diff';
import { exec } from 'child_process';
import * as path from 'path';
import * as fs from 'fs';
import * as uuid from 'uuid/v4';
import * as semver from 'semver';
import { single as errorAnalytics } from '../analytics';

const debug = debugModule('snyk');

function applyPatch(patchFileName, vuln, live, patchUrl) {
let cwd = vuln.source;
Expand All @@ -21,11 +23,11 @@ function applyPatch(patchFileName, vuln, live, patchUrl) {
debug('DRY RUN: relative: %s', relative);

try {
let pkg = {};
let pkg: any = {};
const packageJsonPath = path.resolve(relative, 'package.json');
try {
const packageJson = fs.readFileSync(packageJsonPath);
pkg = JSON.parse(packageJson);
pkg = JSON.parse(packageJson.toString());
debug('package at patch target location: %s@%s', pkg.name, pkg.version);
} catch (err) {
debug(
Expand All @@ -46,7 +48,7 @@ function applyPatch(patchFileName, vuln, live, patchUrl) {

const isVersionMatch = semver.satisfies(
versionOfPackageToPatch,
semver.valid(semver.coerce(vuln.patches.version)),
semver.valid(semver.coerce(vuln.patches.version)!.toString())!,
);

if (isSemverMatch || isVersionMatch) {
Expand Down Expand Up @@ -84,7 +86,7 @@ function jsDiff(patchContent, relative, live) {
const patchedFiles = {};
return new Promise((resolve, reject) => {
diff.applyPatches(patchContent, {
loadFile: function(index, callback) {
loadFile(index, callback) {
try {
const fileName = trimUpToFirstSlash(index.oldFileName);
if (patchedFiles[fileName]) {
Expand All @@ -103,14 +105,14 @@ function jsDiff(patchContent, relative, live) {
} catch (err) {
// collect patch metadata for error analysis
err.patchIssue = JSON.stringify(index);
callback(err);
callback(err, '');
}
},
patched: function(index, content, callback) {
patched(index, content, callback) {
try {
if (content === false) {
if ((content as any) === false) {
// `false` means the patch does not match the original content.
const error = new Error('Found a mismatching patch');
const error: any = new Error('Found a mismatching patch');
error.patchIssue = JSON.stringify(index);
throw error;
}
Expand All @@ -120,19 +122,19 @@ function jsDiff(patchContent, relative, live) {
patchedFiles[oldFileName] = null;
}
patchedFiles[newFileName] = content;
callback();
callback(null);
} catch (err) {
callback(err);
}
},
compareLine: function(_, line, operation, patchContent) {
compareLine(_, line, operation, patchContent) {
if (operation === ' ') {
// Ignore when no patch operators as GNU patch does
return true;
}
return line === patchContent;
},
complete: function(error) {
complete(error) {
if (error) {
return reject(error);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
module.exports = dedupe;
export = dedupe;

const debug = require('debug')('snyk:patch');
const patchesForPackage = require('./patches-for-package');
import * as debugModule from 'debug';
import patchesForPackage = require('./patches-for-package');

const debug = debugModule('snyk:patch');

function dedupe(source) {
const removed = [];
const removed: any[] = [];

const names = source.reduce((acc, vuln) => {
if (Array.isArray(vuln.patches)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
module.exports = getVulnSource;
export = getVulnSource;

const debug = require('debug')('snyk');
const resolve = require('snyk-resolve');
const path = require('path');
const statSync = require('fs').statSync;
const { parsePackageString: moduleToObject } = require('snyk-module');
import * as debugModule from 'debug';
import * as resolve from 'snyk-resolve';
import * as path from 'path';
import { statSync } from 'fs';
import { parsePackageString as moduleToObject } from 'snyk-module';

const debug = debugModule('snyk');

function getVulnSource(vuln, live) {
const from = vuln.from.slice(1).map((pkg) => {
Expand Down
12 changes: 7 additions & 5 deletions src/lib/protect/ignore.js → src/lib/protect/ignore.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
module.exports = ignore;
export = ignore;

const debug = require('debug')('snyk');
const stripVersions = require('./strip-versions');
import * as debugModule from 'debug';
import stripVersions = require('./strip-versions');

const debug = debugModule('snyk');
const oneDay = 1000 * 60 * 60 * 24;

function ignore(data) {
return new Promise((resolve) => {
const config = {};
const config: any = {};
config.ignore = data
.map((res) => {
const vuln = res.vuln;
const days = res.meta.days || 30;
const ignoreRule = {};
const ignoreRule: any = {};
ignoreRule[stripVersions(vuln.from.slice(1)).join(' > ')] = {
reason: res.meta.reason,
expires: new Date(Date.now() + oneDay * days).toJSON(),
Expand Down
17 changes: 10 additions & 7 deletions src/lib/protect/index.js → src/lib/protect/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
const protect = (module.exports = {
const protect = {
ignore: require('./ignore'),
update: require('./update').update,
install: require('./update').install,
installDev: require('./update').installDev,
patch: require('./patch'),
patchesForPackage: require('./patches-for-package'),
generatePolicy: generatePolicy,
});
generatePolicy,
};

const debug = require('debug')('snyk');
const _ = require('@snyk/lodash');
export = protect;

function generatePolicy(policy, tasks, live, packageManager) {
import * as debugModule from 'debug';
import * as _ from '@snyk/lodash';
const debug = debugModule('snyk');

function generatePolicy(policy, tasks, live, packageManager?) {
const promises = ['ignore', 'update', 'patch']
.filter((task) => {
return tasks[task].length;
Expand All @@ -30,7 +33,7 @@ function generatePolicy(policy, tasks, live, packageManager) {
results.unshift(policy);
const newPolicy = _.merge(...results);

debug(JSON.stringify(newPolicy, '', 2));
debug(JSON.stringify(newPolicy, null, 2));

return newPolicy;
});
Expand Down
43 changes: 22 additions & 21 deletions src/lib/protect/patch.js → src/lib/protect/patch.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
module.exports = patch;
export = patch;

import * as debugModule from 'debug';
import chalk from 'chalk';
import * as glob from 'glob';
import * as tempfile from 'tempfile';
import * as fs from 'then-fs';
import * as path from 'path';
import * as _ from '@snyk/lodash';
import applyPatch = require('./apply-patch');
import stripVersions = require('./strip-versions');
import getVulnSource = require('./get-vuln-source');
import dedupe = require('./dedupe-patches');
import writePatchFlag = require('./write-patch-flag');
import * as spinner from '../spinner';
import * as errors from '../errors/legacy-errors';
import * as analytics from '../analytics';
import getPatchFile = require('./fetch-patch');

const now = new Date();

const debug = require('debug')('snyk');
const chalk = require('chalk');
const glob = require('glob');
const tempfile = require('tempfile');
const fs = require('then-fs');
const path = require('path');
const _ = require('@snyk/lodash');
const applyPatch = require('./apply-patch');
const stripVersions = require('./strip-versions');
const getVulnSource = require('./get-vuln-source');
const dedupe = require('./dedupe-patches');
const writePatchFlag = require('./write-patch-flag');
const spinner = require('../spinner');
const errors = require('../errors/legacy-errors');
const analytics = require('../analytics');
const getPatchFile = require('./fetch-patch');
const debug = debugModule('snyk');

function patch(vulns, live) {
const lbl = 'Applying patches...';
const errorList = [];
const errorList: any[] = [];

return (
spinner(lbl)
Expand Down Expand Up @@ -159,7 +160,7 @@ function patch(vulns, live) {
return Promise.all(patched);
})
.then((patched) => {
const config = {};
const config: any = {};

// this reduce function will look to see if the patch actually resolves
// more than one vulnerability, and if it does, it'll replicate the
Expand Down Expand Up @@ -203,7 +204,7 @@ function patch(vulns, live) {
// clear spinner in case of success or failure
.then(spinner.clear(lbl))
.catch((error) => {
spinner.clear(lbl)();
spinner.clear(lbl)(null);
throw error;
})
.then((res) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = patchesForPackage;
export = patchesForPackage;

const semver = require('semver');
import * as semver from 'semver';

function patchesForPackage(vuln) {
return (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = stripVersions;
export = stripVersions;

const { parsePackageString: moduleToObject } = require('snyk-module');
import { parsePackageString as moduleToObject } from 'snyk-module';

function stripVersions(packages) {
return packages.map((pkg) => {
Expand Down
40 changes: 21 additions & 19 deletions src/lib/protect/update.js → src/lib/protect/update.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
module.exports.update = update;
module.exports.install = install;
module.exports.installDev = installDev;

const debug = require('debug')('snyk');
const chalk = require('chalk');
const _ = require('@snyk/lodash');
const { parsePackageString: moduleToObject } = require('snyk-module');
const semver = require('semver');
const errors = require('../errors/legacy-errors');
const npm = require('../npm');
const { yarn } = require('../yarn');
const spinner = require('../spinner');
const analytics = require('../analytics');
export { update };
export { install };
export { installDev };

import * as debugModule from 'debug';
import chalk from 'chalk';
import * as _ from '@snyk/lodash';
import { parsePackageString as moduleToObject } from 'snyk-module';
import * as semver from 'semver';
import * as errors from '../errors/legacy-errors';
import * as npm from '../npm';
import { yarn } from '../yarn';
import * as spinner from '../spinner';
import * as analytics from '../analytics';

const debug = debugModule('snyk');

function update(packages, live, pkgManager) {
pkgManager = pkgManager || 'npm';
const lbl = 'Applying updates using ' + pkgManager + '...';
let error = false;
let error: any = false;

return (
spinner(lbl)
Expand Down Expand Up @@ -101,9 +103,9 @@ function update(packages, live, pkgManager) {
return promise;
})
// clear spinner in case of success or failure
.then(spinner.clear(lbl))
.then(() => spinner.clear(lbl)(null))
.catch((error) => {
spinner.clear(lbl)();
spinner.clear(lbl)(null);
throw error;
})
.then((res) => {
Expand All @@ -124,8 +126,8 @@ function install(pkgManager, upgrades, live) {

function installDev(pkgManager, upgrades, live) {
return pkgManager === 'yarn'
? yarn('add', upgrades, live, null, ['--dev'])
: npm('install', upgrades, live, null, ['--save-dev']);
? yarn('add', upgrades, live, undefined, ['--dev'])
: npm('install', upgrades, live, undefined, ['--save-dev']);
}

function uninstall(pkgManager, toUninstall, live) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
module.exports = writePatchFlag;
export = writePatchFlag;

const debug = require('debug')('snyk');
const fs = require('then-fs');
const path = require('path');
import * as debugModule from 'debug';
import * as fs from 'then-fs';
import * as path from 'path';

const debug = debugModule('snyk');

function writePatchFlag(now, vuln) {
if (!vuln) {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/yarn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ export function yarn(
method: string,
packages: string[],
live: boolean,
cwd: string,
flags: string[],
cwd?: string,
flags?: string[],
) {
flags = flags || [];
if (!packages) {
Expand Down