-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0bd3f08
commit b01dd0d
Showing
9 changed files
with
247 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import fs from 'fs' | ||
import lf from '@yarnpkg/lockfile' | ||
import { keyBy } from 'lodash' | ||
import sv from 'semver' | ||
|
||
import {TAuditEntry, TAuditReport, TContext, TLockfileObject} from './ifaces' | ||
import {attempt, getNpm, getYarn, invoke} from './util' | ||
|
||
export const read = (name: string): TLockfileObject => { | ||
const data = lf.parse(fs.readFileSync(name, 'utf-8')) | ||
|
||
if (data.type !== 'success') { | ||
throw new Error('Merge conflict in yarn lockfile, aborting') | ||
} | ||
|
||
return data.object | ||
} | ||
|
||
export const write = (name: string, lockfile: TLockfileObject): void => { | ||
fs.writeFileSync(name, lf.stringify(lockfile)) | ||
} | ||
|
||
export const patch = (lockfile: TLockfileObject, report: TAuditReport, { flags }: TContext): TLockfileObject => { | ||
if (Object.keys(report).length < 1) { | ||
!flags.silent && console.log('Audit check found no issues') | ||
return lockfile | ||
} | ||
|
||
const upgraded: string [] = [] | ||
|
||
for (let depSpec of Object.keys(lockfile)) { | ||
let [pkgName, desiredRange] = depSpec.split('@') | ||
let pkgAudit = report[pkgName] | ||
if (!pkgAudit) continue | ||
let pkgSpec = lockfile[depSpec] | ||
if (sv.satisfies(pkgSpec.version, pkgAudit.vulnerable_versions)) { | ||
let fix = sv.minVersion(pkgAudit.patched_versions)?.format() | ||
if (fix == null) { | ||
console.error( | ||
'Can\'t find satisfactory version for', | ||
pkgAudit.module_name, | ||
pkgAudit.patched_versions | ||
); | ||
continue | ||
} | ||
if (!sv.satisfies(fix, desiredRange)) { | ||
console.error( | ||
'Cant find patched version that satisfies', | ||
depSpec, | ||
'in', | ||
pkgAudit.patched_versions | ||
); | ||
continue | ||
} | ||
upgraded.push(`${pkgName}@${fix}`) | ||
pkgSpec.version = fix | ||
pkgSpec.dependencies = [] | ||
pkgSpec.integrity = '' | ||
pkgSpec.resolved = '' | ||
} | ||
} | ||
|
||
!flags.silent && console.log('Upgraded deps:', upgraded.join(', ')); | ||
|
||
return lockfile | ||
} | ||
|
||
export const audit = ({ flags, temp }: TContext): TAuditReport => { | ||
const cmd = flags.reporter === 'npm' | ||
? getNpm(flags['npm-path']) | ||
: getYarn() | ||
const report = invoke(cmd, ['audit', '--json'], temp, !!flags.silent, true) | ||
|
||
return keyBy( | ||
report | ||
.toString() | ||
.split('\n') | ||
.map((item) => attempt(() => JSON.parse(item)) as TAuditEntry) | ||
.map((item) => item?.data?.advisory) | ||
.filter((item) => item != null) | ||
.map((item) => ({ | ||
module_name: item.module_name, | ||
vulnerable_versions: item.vulnerable_versions, | ||
patched_versions: item.patched_versions, | ||
})), | ||
(item) => item.module_name | ||
) | ||
} | ||
|
||
export default { | ||
audit, | ||
patch, | ||
read, | ||
write, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.