Skip to content

Commit 458c8a9

Browse files
authored
feat: throw NotSupportedEcosystem for NX Build Project (#265)
1 parent 07bc69b commit 458c8a9

File tree

9 files changed

+118
-0
lines changed

9 files changed

+118
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
dist
44
node_modules
55
package-lock.json
6+
!test/fixtures/**/package-lock.json
67

78
# Diagnostic reports (https://nodejs.org/api/report.html)
89
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

lib/errors/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ export { FileNotFoundError } from './file-not-found-error';
44
export { FileNotProcessableError } from './file-not-processable-error';
55
export { InvalidManifestError } from './invalid-manifest-error';
66
export { InvalidTargetFile } from './invalid-target-file';
7+
export { NotSupportedEcosystem } from './not-supported-ecosystem-error';
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export class NotSupportedEcosystem extends Error {
2+
public code = 422;
3+
public name = 'NotSupportedEcosystem';
4+
5+
public constructor(...args) {
6+
super(...args);
7+
Error.captureStackTrace(this, NotSupportedEcosystem);
8+
}
9+
}

lib/nuget-parser/index.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
CliCommandError,
1414
FileNotProcessableError,
1515
InvalidManifestError,
16+
NotSupportedEcosystem,
1617
} from '../errors';
1718
import {
1819
AssemblyVersions,
@@ -539,6 +540,27 @@ export async function buildDepTreeFromFiles(
539540
projectRootFolder,
540541
);
541542

543+
if (manifestType === ManifestType.PROJECT_JSON) {
544+
let json: any;
545+
try {
546+
json = JSON.parse(fileContent);
547+
} catch (err) {
548+
throw new FileNotProcessableError(`Failed to parse project.json: ${err}`);
549+
}
550+
551+
const hasAnyRequiredProp = [
552+
'dependencies',
553+
'frameworks',
554+
'runtimes',
555+
'supports',
556+
].some((prop) => prop in json);
557+
if (!hasAnyRequiredProp) {
558+
throw new NotSupportedEcosystem(
559+
'project.json file is not a valid project.json file',
560+
);
561+
}
562+
}
563+
542564
const tree = {
543565
dependencies: {},
544566
meta: {},
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"$schema": "./node_modules/nx/schemas/nx-schema.json"
3+
}

test/fixtures/npm-nx-build-platform/package-lock.json

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "with-vulnerable-lodash-dep",
3+
"version": "1.2.3",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [],
10+
"license": "ISC",
11+
"dependencies": {
12+
"lodash": "4.17.15"
13+
}
14+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "with-vulnerable-lodash-dep",
3+
"$schema": "./node_modules/nx/schemas/project-schema.json",
4+
"sourceRoot": "src",
5+
"projectType": "application",
6+
"targets": {
7+
"build": {
8+
"executor": "nx:run-commands",
9+
"options": {
10+
"command": "npm run build"
11+
},
12+
"inputs": ["default"],
13+
"outputs": ["{projectRoot}/dist"]
14+
},
15+
"test": {
16+
"executor": "nx:run-commands",
17+
"options": {
18+
"command": "npm test"
19+
},
20+
"inputs": ["default"]
21+
},
22+
"lint": {
23+
"executor": "nx:run-commands",
24+
"options": {
25+
"command": "npm run lint"
26+
},
27+
"inputs": ["default"]
28+
}
29+
},
30+
"tags": []
31+
}

test/inspect.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as dotnet from '../lib/nuget-parser/cli/dotnet';
66
import * as depGraphLib from '@snyk/dep-graph';
77
import * as depGraphLegacyLib from '@snyk/dep-graph/dist/legacy';
88
import { legacyPlugin as pluginApi } from '@snyk/cli-interface';
9+
import { NotSupportedEcosystem } from '../lib/errors';
910

1011
const INSPECT_OPTIONS = {
1112
useFixForImprovedDotnetFalsePositives: true,
@@ -67,6 +68,15 @@ describe('when calling plugin.inspect with various configs', () => {
6768
).rejects.toThrow('Could not find a <packages> tag');
6869
});
6970

71+
it('fails gracefully on NX build platform project', async () => {
72+
const filePath = './test/fixtures/npm-nx-build-platform/';
73+
const manifestFile = 'project.json';
74+
75+
await expect(
76+
async () => await plugin.inspect(filePath, manifestFile, INSPECT_OPTIONS),
77+
).rejects.toThrow(NotSupportedEcosystem);
78+
});
79+
7080
it('should parse dotnet-cli project with packages.config only', async () => {
7181
const packagesConfigOnlyPath =
7282
'./test/fixtures/packages-config/config-only/';

0 commit comments

Comments
 (0)