-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for yarn 2 lock files
Add metadata to inform Snyk CLI which version of yarn has been detected. Refactor yarn 1 and 2 tests. Co-authored-by: Daniel <[email protected]> Co-authored-by: Mega Bean (gel) <[email protected]> Co-authored-by: Or Sagie <[email protected]> Co-authored-by: Antonio Gomes <[email protected]> Co-authored-by: Eleanor Kavanagh-Brown <[email protected]> Co-authored-by: Regev Brody <[email protected]>
- Loading branch information
1 parent
f8814e0
commit e56d2af
Showing
81 changed files
with
50,601 additions
and
741 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
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,87 @@ | ||
import { structUtils } from '@yarnpkg/core'; | ||
import * as _ from 'lodash'; | ||
|
||
const BUILTIN_PLACEHOLDER = 'builtin'; | ||
const MULTIPLE_KEYS_REGEXP = / *, */g; | ||
|
||
export type ParseDescriptor = typeof structUtils.parseDescriptor; | ||
export type ParseRange = typeof structUtils.parseRange; | ||
|
||
const keyNormalizer = ( | ||
parseDescriptor: ParseDescriptor, | ||
parseRange: ParseRange, | ||
) => (rawDescriptor: string): string[] => { | ||
// See https://yarnpkg.com/features/protocols | ||
const descriptors: string[] = [rawDescriptor]; | ||
const descriptor = parseDescriptor(rawDescriptor); | ||
const name = `${descriptor.scope ? '@' + descriptor.scope + '/' : ''}${ | ||
descriptor.name | ||
}`; | ||
const range = parseRange(descriptor.range); | ||
const protocol = range.protocol; | ||
switch (protocol) { | ||
case 'npm:': | ||
case 'file:': | ||
descriptors.push(`${name}@${range.selector}`); | ||
descriptors.push(`${name}@${protocol}${range.selector}`); | ||
break; | ||
case 'git:': | ||
case 'git+ssh:': | ||
case 'git+http:': | ||
case 'git+https:': | ||
case 'github:': | ||
if (range.source) { | ||
descriptors.push( | ||
`${name}@${protocol}${range.source}${ | ||
range.selector ? '#' + range.selector : '' | ||
}`, | ||
); | ||
} else { | ||
descriptors.push(`${name}@${protocol}${range.selector}`); | ||
} | ||
break; | ||
case 'patch:': | ||
if (range.source && range.selector.indexOf(BUILTIN_PLACEHOLDER) === 0) { | ||
descriptors.push(range.source); | ||
} else { | ||
descriptors.push( | ||
`${name}@${protocol}${range.source}${ | ||
range.selector ? '#' + range.selector : '' | ||
}`, | ||
); | ||
} | ||
break; | ||
case null: | ||
case undefined: | ||
if (range.source) { | ||
descriptors.push(`${name}@${range.source}#${range.selector}`); | ||
} else { | ||
descriptors.push(`${name}@${range.selector}`); | ||
} | ||
break; | ||
case 'http:': | ||
case 'https:': | ||
case 'link:': | ||
case 'portal:': | ||
case 'exec:': | ||
case 'workspace:': | ||
case 'virtual:': | ||
default: | ||
// For user defined plugins | ||
descriptors.push(`${name}@${protocol}${range.selector}`); | ||
break; | ||
} | ||
return descriptors; | ||
}; | ||
|
||
export type YarnLockFileKeyNormalizer = (fullDescriptor: string) => Set<string>; | ||
|
||
export const yarnLockFileKeyNormalizer = ( | ||
parseDescriptor: ParseDescriptor, | ||
parseRange: ParseRange, | ||
): YarnLockFileKeyNormalizer => (fullDescriptor: string) => { | ||
const allKeys = fullDescriptor | ||
.split(MULTIPLE_KEYS_REGEXP) | ||
.map(keyNormalizer(parseDescriptor, parseRange)); | ||
return new Set<string>(_.flatMap(allKeys)); | ||
}; |
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,55 @@ | ||
import * as _ from 'lodash'; | ||
import * as yaml from 'yaml'; | ||
|
||
import { LockfileType } from './'; | ||
import getRuntimeVersion from '../get-node-runtime-version'; | ||
import { InvalidUserInputError, UnsupportedRuntimeError } from '../errors'; | ||
import { YarnLockBase, YarnLockDeps } from './yarn-lock-parse-base'; | ||
import { YarnLockParseBase } from './yarn-lock-parse-base'; | ||
import { | ||
YarnLockFileKeyNormalizer, | ||
yarnLockFileKeyNormalizer, | ||
} from './yarn-utils'; | ||
|
||
export type Yarn2Lock = YarnLockBase<LockfileType.yarn2>; | ||
|
||
export class Yarn2LockParser extends YarnLockParseBase<LockfileType.yarn2> { | ||
private keyNormalizer: YarnLockFileKeyNormalizer; | ||
|
||
constructor() { | ||
super(LockfileType.yarn2); | ||
// @yarnpkg/core doesn't work with Node.js < 10 | ||
if (getRuntimeVersion() < 10) { | ||
throw new UnsupportedRuntimeError( | ||
`yarn.lock parsing is supported for Node.js v10 and higher.`, | ||
); | ||
} | ||
const structUtils = require('@yarnpkg/core').structUtils; | ||
const parseDescriptor = structUtils.parseDescriptor; | ||
const parseRange = structUtils.parseRange; | ||
this.keyNormalizer = yarnLockFileKeyNormalizer(parseDescriptor, parseRange); | ||
} | ||
|
||
public parseLockFile(lockFileContents: string): Yarn2Lock { | ||
try { | ||
const rawYarnLock: any = yaml.parse(lockFileContents); | ||
delete rawYarnLock.__metadata; | ||
const dependencies: YarnLockDeps = {}; | ||
_.forEach(rawYarnLock, (versionData, fullDescriptor) => { | ||
this.keyNormalizer(fullDescriptor).forEach((descriptor) => { | ||
dependencies[descriptor] = versionData; | ||
}); | ||
}); | ||
return { | ||
dependencies, | ||
lockfileType: LockfileType.yarn2, | ||
object: dependencies, | ||
type: LockfileType.yarn2, | ||
}; | ||
} catch (e) { | ||
throw new InvalidUserInputError( | ||
`yarn.lock parsing failed with an error: ${e.message}`, | ||
); | ||
} | ||
} | ||
} |
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
File renamed without changes.
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 @@ | ||
yarnPath: .yarn/releases/yarn-rc.js |
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,23 @@ | ||
# This file is generated by running "yarn install" inside your project. | ||
# Manual changes might be lost - proceed with caution! | ||
|
||
__metadata: | ||
version: 4 | ||
|
||
"debug@npm:2.0.x": | ||
version: 2.0.0 | ||
resolution: "debug@npm:2.0.0" | ||
dependencies: | ||
ms: 0.6.2 | ||
checksum: 2/0d3aafea6f4d2fac3e1ad295c17927aeb1188b51184cc1a39a061911fde969c1ff9139a14f895b592f2a82035fc507ef9ec78ebddcba6aba4be0fe6e195abde7 | ||
languageName: node | ||
linkType: hard | ||
|
||
"ms@npm:0.6.2": | ||
version: 0.6.2 | ||
resolution: "ms@npm:0.6.2" | ||
checksum: 2/58b15f75a33f042ce241d435c6eb218f46cd835f74db4f85b4dacf4143a92638cc0887be38423e79411da27dae103db3048a0d9aa4629607ab10c7d037b6f9e7 | ||
languageName: node | ||
linkType: hard | ||
dependencies: | ||
debug: 2.0.x |
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 |
---|---|---|
|
@@ -21,4 +21,4 @@ | |
} | ||
} | ||
} | ||
} | ||
} |
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,27 @@ | ||
{ | ||
"name": "pkg-dev-deps-only", | ||
"size": 3, | ||
"version": "0.0.1", | ||
"hasDevDependencies": true, | ||
"dependencies": { | ||
"debug": { | ||
"name": "debug", | ||
"version": "2.6.9", | ||
"labels": { | ||
"scope": "dev" | ||
}, | ||
"dependencies": { | ||
"ms": { | ||
"name": "ms", | ||
"version": "2.0.0", | ||
"labels": { | ||
"scope": "dev" | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"meta": { | ||
"packageManagerVersion": "1" | ||
} | ||
} |
File renamed without changes.
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 @@ | ||
yarnPath: .yarn/releases/yarn-rc.js |
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,27 @@ | ||
{ | ||
"name": "pkg-dev-deps-only", | ||
"size": 3, | ||
"version": "0.0.1", | ||
"hasDevDependencies": true, | ||
"dependencies": { | ||
"debug": { | ||
"name": "debug", | ||
"version": "2.6.9", | ||
"labels": { | ||
"scope": "dev" | ||
}, | ||
"dependencies": { | ||
"ms": { | ||
"name": "ms", | ||
"version": "2.0.0", | ||
"labels": { | ||
"scope": "dev" | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"meta": { | ||
"packageManagerVersion": "2" | ||
} | ||
} |
Oops, something went wrong.