Skip to content

Commit

Permalink
fix: skip patch when dep package.json is malformed
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton Drukh committed Jul 10, 2019
1 parent 80c3717 commit 3f617b6
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/lib/protect/apply-patch.js
Expand Up @@ -20,25 +20,25 @@ function applyPatch(patchFileName, vuln, live, patchUrl) {
const relative = path.relative(process.cwd(), cwd);
debug('DRY RUN: relative: %s', relative);

let pkg;
let pkg = {};
const packageJsonPath = path.resolve(relative, 'package.json');
try {
const packageJson = fs.readFileSync(path.resolve(relative, 'package.json'));
const packageJson = fs.readFileSync(packageJsonPath);
pkg = JSON.parse(packageJson);
debug('package at patch target location: %s@%s', pkg.name, pkg.version);
} catch (err) {
debug('Failed loading package.json of package about to be patched', err);
debug('Failed loading package.json at %s. Skipping patch!', packageJsonPath, err);
return resolve();
}

let foundVersionMatchToPatch;
const versionOfPackageToPatch = pkg.version;
const patchableVersionsRange = vuln.patches.version;
if (semver.satisfies(versionOfPackageToPatch, patchableVersionsRange)) {
debug(`found patchable version range ${patchableVersionsRange}`);
foundVersionMatchToPatch = true;
}

if (!foundVersionMatchToPatch) {
debug('could not find package on disk that satisfies the vuln to patch, nothing to do');
debug('Patch version range %s matches package version %s',
patchableVersionsRange, versionOfPackageToPatch);
} else {
debug('Patch version range %s does not match package version %s. Skipping patch!',
patchableVersionsRange, versionOfPackageToPatch);
return resolve();
}

Expand Down

0 comments on commit 3f617b6

Please sign in to comment.