Skip to content

Commit

Permalink
fix(protect): handle carriage returns when parsing .snyk file
Browse files Browse the repository at this point in the history
  • Loading branch information
Jahed Ahmed committed May 19, 2021
1 parent 33d7560 commit 63e4818
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/snyk-protect/src/lib/snyk-file.ts
Expand Up @@ -10,7 +10,7 @@ export function extractPatchMetadata(
const patches = dotSnykFileContent
.split('\n')
.filter((l) => l.length && !l.trimStart().startsWith('#'))
.map((line) => lineRegex.exec(line))
.map((line) => lineRegex.exec(line.trimEnd()))
.filter(Boolean)
.reduce((acc, thing) => {
const [, prefix, key, value] = thing as RegExpExecArray;
Expand Down
28 changes: 28 additions & 0 deletions packages/snyk-protect/test/unit/snyk-file.spec.ts
Expand Up @@ -27,6 +27,34 @@ patch:
expect(packageNames).toEqual(['lodash']);
});

it('handles carriage returns in line endings', () => {
const dotSnykFileContents = `
# Snyk (https://snyk.io) policy file, patches or ignores known vulnerabilities.
version: v1.19.0
ignore: {}
# patches apply the minimum changes required to fix a vulnerability
patch:
SNYK-JS-LODASH-567746:
- lodash:
patched: '2021-02-17T13:43:51.857Z'
`
.split('\n')
.join('\r\n');
const snykFilePatchMetadata = extractPatchMetadata(dotSnykFileContents);
const vulnIds = Object.keys(snykFilePatchMetadata);

// can't use .flat() because it's not supported in Node 10
const packageNames: string[] = [];
for (const nextArrayOfPackageNames of Object.values(
snykFilePatchMetadata,
)) {
packageNames.push(...nextArrayOfPackageNames);
}

expect(vulnIds).toEqual(['SNYK-JS-LODASH-567746']);
expect(packageNames).toEqual(['lodash']);
});

it('extracts a transitive dependency', () => {
const dotSnykFileContents = `
# Snyk (https://snyk.io) policy file, patches or ignores known vulnerabilities.
Expand Down

0 comments on commit 63e4818

Please sign in to comment.