diff --git a/cliv2/go.mod b/cliv2/go.mod index 71c73706f8..3ef5944167 100644 --- a/cliv2/go.mod +++ b/cliv2/go.mod @@ -11,7 +11,7 @@ require ( github.com/rs/zerolog v1.32.0 github.com/snyk/cli-extension-dep-graph v0.0.0-20230926124856-b0fdf1ee6f73 github.com/snyk/cli-extension-iac-rules v0.0.0-20240404084125-0098857e0e1a - github.com/snyk/cli-extension-sbom v0.0.0-20240314090036-46535b380426 + github.com/snyk/cli-extension-sbom v0.0.0-20240418082712-4732b4b2d7b3 github.com/snyk/container-cli v0.0.0-20240322120441-6d9b9482f9b1 github.com/snyk/go-application-framework v0.0.0-20240417122153-755586b0312f github.com/snyk/go-httpauth v0.0.0-20240307114523-1f5ea3f55c65 diff --git a/cliv2/go.sum b/cliv2/go.sum index fe29333e56..6752d32301 100644 --- a/cliv2/go.sum +++ b/cliv2/go.sum @@ -243,6 +243,8 @@ github.com/bmatcuk/doublestar v1.3.4 h1:gPypJ5xD31uhX6Tf54sDPUOBXTqKH4c9aPY66CyQ github.com/bmatcuk/doublestar v1.3.4/go.mod h1:wiQtGV+rzVYxB7WIlirSN++5HPtPlXEo9MEoZQC/PmE= github.com/bmatcuk/doublestar/v4 v4.6.0 h1:HTuxyug8GyFbRkrffIpzNCSK4luc0TY3wzXvzIZhEXc= github.com/bmatcuk/doublestar/v4 v4.6.0/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc= +github.com/bradleyjkemp/cupaloy/v2 v2.8.0 h1:any4BmKE+jGIaMpnU8YgH/I2LPiLBufr6oMMlVBbn9M= +github.com/bradleyjkemp/cupaloy/v2 v2.8.0/go.mod h1:bm7JXdkRd4BHJk9HpwqAI8BoAY1lps46Enkdqw6aRX0= github.com/bwesterb/go-ristretto v1.2.3/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0= github.com/bytecodealliance/wasmtime-go/v3 v3.0.2 h1:3uZCA/BLTIu+DqCfguByNMJa2HVHpXvjfy0Dy7g6fuA= github.com/bytecodealliance/wasmtime-go/v3 v3.0.2/go.mod h1:RnUjnIXxEJcL6BgCvNyzCCRzZcxCgsZCi+RNlvYor5Q= @@ -727,8 +729,8 @@ github.com/snyk/cli-extension-dep-graph v0.0.0-20230926124856-b0fdf1ee6f73 h1:rw github.com/snyk/cli-extension-dep-graph v0.0.0-20230926124856-b0fdf1ee6f73/go.mod h1:QF3v8HBpOpyudYNCuR8LqfULutO76c91sBdLzD+pBJU= github.com/snyk/cli-extension-iac-rules v0.0.0-20240404084125-0098857e0e1a h1:pvj3bsgPMmYma56TU+rjFsulqS2kV1D2kBg1mVb8Et4= github.com/snyk/cli-extension-iac-rules v0.0.0-20240404084125-0098857e0e1a/go.mod h1:4c6XS4n6mWbJM9md3r4B2NFgjs2tyi8GzGlz1BbWIx0= -github.com/snyk/cli-extension-sbom v0.0.0-20240314090036-46535b380426 h1:MXbip3nmiOym3/9bNWlPISVOAEAAz4FDcPvqOMPcCc4= -github.com/snyk/cli-extension-sbom v0.0.0-20240314090036-46535b380426/go.mod h1:g2VgZU79btvZrAP3oHZGv3tHD9POVOx5a3DY894rS4w= +github.com/snyk/cli-extension-sbom v0.0.0-20240418082712-4732b4b2d7b3 h1:2dOzIy4L0LRH7EnMw//80K510ZgoXiDPpylpGNRSZTI= +github.com/snyk/cli-extension-sbom v0.0.0-20240418082712-4732b4b2d7b3/go.mod h1:lqmQT+QdzLdfi7qsqIH4qvCsSWu+P09GDFwQcmFfC0g= github.com/snyk/code-client-go v1.4.2 h1:Vy27Xr6CVAs0qKZlU8I/fxWWI6X2ppzan6IZnUJYmvg= github.com/snyk/code-client-go v1.4.2/go.mod h1:Kkr7pQc8ItsBZSYd6A1S4r4VHO6HNyTWZsqi18sAtwQ= github.com/snyk/container-cli v0.0.0-20240322120441-6d9b9482f9b1 h1:9RKY9NdX5DrJAoVXDP0JiqrXT+4Nb9NH8pjEcA0NsLA= diff --git a/test/acceptance/fake-server.ts b/test/acceptance/fake-server.ts index 1f14bc3205..71c20ce9ff 100644 --- a/test/acceptance/fake-server.ts +++ b/test/acceptance/fake-server.ts @@ -603,6 +603,57 @@ export const fakeServer = (basePath: string, snykToken: string): FakeServer => { res.status(200).send({}); }); + app.post(`/rest/orgs/:orgId/sbom_tests`, (req, res) => { + const response = { + data: { + id: '4b341b8a-4697-4e35-928b-4b9ae37f8ea8', + type: 'sbom_tests', + }, + jsonapi: { + version: '1.0', + }, + links: { + self: + '/rest/orgs/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/sbom_tests?version=2023-08-31~beta', + related: + '/rest/orgs/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/sbom_tests/4b341b8a-4697-4e35-928b-4b9ae37f8ea8?version=2023-08-31~beta', + }, + }; + res.status(201); + res.send(response); + }); + + app.get(`/rest/orgs/:orgId/sbom_tests/:id`, (req, res) => { + const response = { + data: { + id: '4b341b8a-4697-4e35-928b-4b9ae37f8ea8', + type: 'sbom_tests', + attributes: { + status: 'finished', + }, + }, + jsonapi: { + version: '1.0', + }, + links: { + self: + '/rest/orgs/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/sbom_tests/4b341b8a-4697-4e35-928b-4b9ae37f8ea8?version=2023-08-31~beta', + related: + '/rest/orgs/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/sbom_tests/4b341b8a-4697-4e35-928b-4b9ae37f8ea8/results?version=2023-08-31~beta', + }, + }; + res.status(303); + res.send(response); + }); + + app.get(`/rest/orgs/:orgId/sbom_tests/:id/results`, (req, res) => { + const body = fs.readFileSync( + path.resolve(getFixturePath('sbom'), 'npm-sbom-test-response.json'), + 'utf8', + ); + res.send(JSON.parse(body)); + }); + app.post( basePath.replace('v1', 'hidden') + '/orgs/:org/sbom', express.json(), diff --git a/test/fixtures/sbom/npm-sbom-cdx15.json b/test/fixtures/sbom/npm-sbom-cdx15.json new file mode 100644 index 0000000000..5f3408acd1 --- /dev/null +++ b/test/fixtures/sbom/npm-sbom-cdx15.json @@ -0,0 +1,2 @@ +{"$schema":"http://cyclonedx.org/schema/bom-1.5.schema.json","bomFormat":"CycloneDX","specVersion":"1.5","serialNumber":"urn:uuid:f441a916-51e8-4a53-b5e7-db914182fa7c","version":1,"metadata":{"timestamp":"2024-04-12T09:28:22Z","tools":{"components":[{"type":"application","author":"Snyk","name":"snyk-cli","version":"1.1288.0"}],"services":[{"provider":{"name":"Snyk"},"name":"SBOM Export API","version":"v1.84.1"}]},"component":{"bom-ref":"1-development@1.0.0","type":"application","name":"development","version":"1.0.0","purl":"pkg:npm/development@1.0.0"}},"components":[{"bom-ref":"2-minimatch@3.0.4","type":"library","name":"minimatch","version":"3.0.4","purl":"pkg:npm/minimatch@3.0.4"},{"bom-ref":"3-brace-expansion@1.1.11","type":"library","name":"brace-expansion","version":"1.1.11","purl":"pkg:npm/brace-expansion@1.1.11"},{"bom-ref":"4-balanced-match@1.0.2","type":"library","name":"balanced-match","version":"1.0.2","purl":"pkg:npm/balanced-match@1.0.2"},{"bom-ref":"5-concat-map@0.0.1","type":"library","name":"concat-map","version":"0.0.1","purl":"pkg:npm/concat-map@0.0.1"},{"bom-ref":"6-semver@7.3.5","type":"library","name":"semver","version":"7.3.5","purl":"pkg:npm/semver@7.3.5"},{"bom-ref":"7-lru-cache@6.0.0","type":"library","name":"lru-cache","version":"6.0.0","purl":"pkg:npm/lru-cache@6.0.0"},{"bom-ref":"8-yallist@4.0.0","type":"library","name":"yallist","version":"4.0.0","purl":"pkg:npm/yallist@4.0.0"}],"dependencies":[{"ref":"1-development@1.0.0","dependsOn":["2-minimatch@3.0.4","6-semver@7.3.5"]},{"ref":"2-minimatch@3.0.4","dependsOn":["3-brace-expansion@1.1.11"]},{"ref":"3-brace-expansion@1.1.11","dependsOn":["4-balanced-match@1.0.2","5-concat-map@0.0.1"]},{"ref":"4-balanced-match@1.0.2"},{"ref":"5-concat-map@0.0.1"},{"ref":"6-semver@7.3.5","dependsOn":["7-lru-cache@6.0.0"]},{"ref":"7-lru-cache@6.0.0","dependsOn":["8-yallist@4.0.0"]},{"ref":"8-yallist@4.0.0"}]} + diff --git a/test/fixtures/sbom/npm-sbom-test-response.json b/test/fixtures/sbom/npm-sbom-test-response.json new file mode 100644 index 0000000000..4569307727 --- /dev/null +++ b/test/fixtures/sbom/npm-sbom-test-response.json @@ -0,0 +1 @@ +{"data":{"id":"2c82ad1f-6868-40a9-acc6-f4a7b8bf0326","type":"sbom_test_results","attributes":{"sbom":{"format":"CycloneDX JSON"},"test_summary":{"tested":["2-minimatch@3.0.4","3-brace-expansion@1.1.11","4-balanced-match@1.0.2","5-concat-map@0.0.1","6-semver@7.3.5","7-lru-cache@6.0.0","8-yallist@4.0.0","1-development@1.0.0"],"untested":[],"total_issues":2,"total_license_issues":0,"total_vulnerabilities":2,"vulnerabilities_by_severity":{"critical":0,"high":1,"medium":1,"low":0}}},"relationships":{"affected_pkgs":{"data":[{"type":"packages","id":"semver@7.3.5"},{"type":"packages","id":"minimatch@3.0.4"}]},"vulnerabilities":{"data":[{"type":"vulnerabilities","id":"SNYK-JS-MINIMATCH-3050818"},{"type":"vulnerabilities","id":"SNYK-JS-SEMVER-3247795"}]},"remedies":{"data":[{"type":"remedies","id":"minimatch@3.0.4:SNYK-JS-MINIMATCH-3050818"},{"type":"remedies","id":"semver@7.3.5:SNYK-JS-SEMVER-3247795"}]}}},"jsonapi":{"version":"1.0"},"links":{"self":"/rest/orgs/0e10f26f-81f9-4bb0-8811-6c1b1b896a6c/sbom_tests/2c82ad1f-6868-40a9-acc6-f4a7b8bf0326/results?version=2023-08-31~beta"},"included":[{"type":"remedies","id":"minimatch@3.0.4:SNYK-JS-MINIMATCH-3050818","attributes":{"is_fixable":true,"upgrade_paths":[{"path":[{"name":"development","version":"1.0.0"},{"name":"minimatch","version":"3.0.4","new_version":"3.0.5"}]}]},"relationships":{"affected_package":{"data":{"type":"packages","id":"minimatch@3.0.4"}},"vulnerability":{"data":{"type":"vulnerabilities","id":"SNYK-JS-MINIMATCH-3050818"}}}},{"type":"remedies","id":"semver@7.3.5:SNYK-JS-SEMVER-3247795","attributes":{"is_fixable":true,"upgrade_paths":[{"path":[{"name":"development","version":"1.0.0"},{"name":"semver","version":"7.3.5","new_version":"7.5.2"}]}]},"relationships":{"affected_package":{"data":{"type":"packages","id":"semver@7.3.5"}},"vulnerability":{"data":{"type":"vulnerabilities","id":"SNYK-JS-SEMVER-3247795"}}}},{"id":"semver@7.3.5","type":"packages","attributes":{"name":"semver","version":"7.3.5","purl":"pkg:npm/semver@7.3.5"}},{"id":"minimatch@3.0.4","type":"packages","attributes":{"name":"minimatch","version":"3.0.4","purl":"pkg:npm/minimatch@3.0.4"}},{"id":"SNYK-JS-MINIMATCH-3050818","type":"vulnerabilities","attributes":{"title":"Regular Expression Denial of Service (ReDoS)","description":"## Overview\n[minimatch](https://www.npmjs.com/package/minimatch) is a minimal matching utility.\n\nAffected versions of this package are vulnerable to Regular Expression Denial of Service (ReDoS) via the `braceExpand` function in `minimatch.js`.\n\n## Details\n\nDenial of Service (DoS) describes a family of attacks, all aimed at making a system inaccessible to its original and legitimate users. There are many types of DoS attacks, ranging from trying to clog the network pipes to the system by generating a large volume of traffic from many machines (a Distributed Denial of Service - DDoS - attack) to sending crafted requests that cause a system to crash or take a disproportional amount of time to process.\n\nThe Regular expression Denial of Service (ReDoS) is a type of Denial of Service attack. Regular expressions are incredibly powerful, but they aren't very intuitive and can ultimately end up making it easy for attackers to take your site down.\n\nLet’s take the following regular expression as an example:\n```js\nregex = /A(B|C+)+D/\n```\n\nThis regular expression accomplishes the following:\n- `A` The string must start with the letter 'A'\n- `(B|C+)+` The string must then follow the letter A with either the letter 'B' or some number of occurrences of the letter 'C' (the `+` matches one or more times). The `+` at the end of this section states that we can look for one or more matches of this section.\n- `D` Finally, we ensure this section of the string ends with a 'D'\n\nThe expression would match inputs such as `ABBD`, `ABCCCCD`, `ABCBCCCD` and `ACCCCCD`\n\nIt most cases, it doesn't take very long for a regex engine to find a match:\n\n```bash\n$ time node -e '/A(B|C+)+D/.test(\"ACCCCCCCCCCCCCCCCCCCCCCCCCCCCD\")'\n0.04s user 0.01s system 95% cpu 0.052 total\n\n$ time node -e '/A(B|C+)+D/.test(\"ACCCCCCCCCCCCCCCCCCCCCCCCCCCCX\")'\n1.79s user 0.02s system 99% cpu 1.812 total\n```\n\nThe entire process of testing it against a 30 characters long string takes around ~52ms. But when given an invalid string, it takes nearly two seconds to complete the test, over ten times as long as it took to test a valid string. The dramatic difference is due to the way regular expressions get evaluated.\n\nMost Regex engines will work very similarly (with minor differences). The engine will match the first possible way to accept the current character and proceed to the next one. If it then fails to match the next one, it will backtrack and see if there was another way to digest the previous character. If it goes too far down the rabbit hole only to find out the string doesn’t match in the end, and if many characters have multiple valid regex paths, the number of backtracking steps can become very large, resulting in what is known as _catastrophic backtracking_.\n\nLet's look at how our expression runs into this problem, using a shorter string: \"ACCCX\". While it seems fairly straightforward, there are still four different ways that the engine could match those three C's:\n1. CCC\n2. CC+C\n3. C+CC\n4. C+C+C.\n\nThe engine has to try each of those combinations to see if any of them potentially match against the expression. When you combine that with the other steps the engine must take, we can use [RegEx 101 debugger](https://regex101.com/debugger) to see the engine has to take a total of 38 steps before it can determine the string doesn't match.\n\nFrom there, the number of steps the engine must use to validate a string just continues to grow.\n\n| String | Number of C's | Number of steps |\n| -------|-------------:| -----:|\n| ACCCX | 3 | 38\n| ACCCCX | 4 | 71\n| ACCCCCX | 5 | 136\n| ACCCCCCCCCCCCCCX | 14 | 65,553\n\n\nBy the time the string includes 14 C's, the engine has to take over 65,000 steps just to see if the string is valid. These extreme situations can cause them to work very slowly (exponentially related to input size, as shown above), allowing an attacker to exploit this and can cause the service to excessively consume CPU, resulting in a Denial of Service.\n\n## Remediation\nUpgrade `minimatch` to version 3.0.5 or higher.\n## References\n- [GitHub Commit](https://github.com/isaacs/minimatch/commit/a8763f4388e51956be62dc6025cec1126beeb5e6)\n","created_at":"2022-10-18T06:10:47Z","updated_at":"2024-03-11T09:53:57Z","problems":[{"id":"CVE-2022-3517","source":"CVE"},{"id":"CWE-1333","source":"CWE"}],"coordinates":[{"remedies":[{"description":"Upgrade to version 3.0.5 to fix this vulnerability","details":{"upgrade_package":"3.0.5"},"type":"indeterminate"}],"representation":[{"resource_path":"\u003c3.0.5"}]}],"severities":[{"source":"Snyk","level":"medium","score":5.3,"vector":"CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:L"}],"effective_severity_level":"medium","slots":[{"disclosure_time":"2022-10-18 06:00:25 +0000 UTC","exploit":"Not Defined","publication_time":"2022-10-18 06:29:18.07895 +0000 UTC","references":[{"url":"https://github.com/isaacs/minimatch/commit/a8763f4388e51956be62dc6025cec1126beeb5e6","title":"GitHub Commit"}]}]}},{"id":"SNYK-JS-SEMVER-3247795","type":"vulnerabilities","attributes":{"title":"Regular Expression Denial of Service (ReDoS)","description":"## Overview\n[semver](https://github.com/npm/node-semver) is a semantic version parser used by npm.\n\nAffected versions of this package are vulnerable to Regular Expression Denial of Service (ReDoS) via the function `new Range`, when untrusted user data is provided as a range.\r\n\r\n\r\n## PoC\r\n\r\n```js\r\n\r\nconst semver = require('semver')\r\nconst lengths_2 = [2000, 4000, 8000, 16000, 32000, 64000, 128000]\r\n\r\nconsole.log(\"n[+] Valid range - Test payloads\")\r\nfor (let i = 0; i =1.2.3' + ' '.repeat(lengths_2[i]) + '\u003c1.3.0';\r\nconst start = Date.now()\r\nsemver.validRange(value)\r\n// semver.minVersion(value)\r\n// semver.maxSatisfying([\"1.2.3\"], value)\r\n// semver.minSatisfying([\"1.2.3\"], value)\r\n// new semver.Range(value, {})\r\n\r\nconst end = Date.now();\r\nconsole.log('length=%d, time=%d ms', value.length, end - start);\r\n}\r\n```\n\n## Details\n\nDenial of Service (DoS) describes a family of attacks, all aimed at making a system inaccessible to its original and legitimate users. There are many types of DoS attacks, ranging from trying to clog the network pipes to the system by generating a large volume of traffic from many machines (a Distributed Denial of Service - DDoS - attack) to sending crafted requests that cause a system to crash or take a disproportional amount of time to process.\n\nThe Regular expression Denial of Service (ReDoS) is a type of Denial of Service attack. Regular expressions are incredibly powerful, but they aren't very intuitive and can ultimately end up making it easy for attackers to take your site down.\n\nLet’s take the following regular expression as an example:\n```js\nregex = /A(B|C+)+D/\n```\n\nThis regular expression accomplishes the following:\n- `A` The string must start with the letter 'A'\n- `(B|C+)+` The string must then follow the letter A with either the letter 'B' or some number of occurrences of the letter 'C' (the `+` matches one or more times). The `+` at the end of this section states that we can look for one or more matches of this section.\n- `D` Finally, we ensure this section of the string ends with a 'D'\n\nThe expression would match inputs such as `ABBD`, `ABCCCCD`, `ABCBCCCD` and `ACCCCCD`\n\nIt most cases, it doesn't take very long for a regex engine to find a match:\n\n```bash\n$ time node -e '/A(B|C+)+D/.test(\"ACCCCCCCCCCCCCCCCCCCCCCCCCCCCD\")'\n0.04s user 0.01s system 95% cpu 0.052 total\n\n$ time node -e '/A(B|C+)+D/.test(\"ACCCCCCCCCCCCCCCCCCCCCCCCCCCCX\")'\n1.79s user 0.02s system 99% cpu 1.812 total\n```\n\nThe entire process of testing it against a 30 characters long string takes around ~52ms. But when given an invalid string, it takes nearly two seconds to complete the test, over ten times as long as it took to test a valid string. The dramatic difference is due to the way regular expressions get evaluated.\n\nMost Regex engines will work very similarly (with minor differences). The engine will match the first possible way to accept the current character and proceed to the next one. If it then fails to match the next one, it will backtrack and see if there was another way to digest the previous character. If it goes too far down the rabbit hole only to find out the string doesn’t match in the end, and if many characters have multiple valid regex paths, the number of backtracking steps can become very large, resulting in what is known as _catastrophic backtracking_.\n\nLet's look at how our expression runs into this problem, using a shorter string: \"ACCCX\". While it seems fairly straightforward, there are still four different ways that the engine could match those three C's:\n1. CCC\n2. CC+C\n3. C+CC\n4. C+C+C.\n\nThe engine has to try each of those combinations to see if any of them potentially match against the expression. When you combine that with the other steps the engine must take, we can use [RegEx 101 debugger](https://regex101.com/debugger) to see the engine has to take a total of 38 steps before it can determine the string doesn't match.\n\nFrom there, the number of steps the engine must use to validate a string just continues to grow.\n\n| String | Number of C's | Number of steps |\n| -------|-------------:| -----:|\n| ACCCX | 3 | 38\n| ACCCCX | 4 | 71\n| ACCCCCX | 5 | 136\n| ACCCCCCCCCCCCCCX | 14 | 65,553\n\n\nBy the time the string includes 14 C's, the engine has to take over 65,000 steps just to see if the string is valid. These extreme situations can cause them to work very slowly (exponentially related to input size, as shown above), allowing an attacker to exploit this and can cause the service to excessively consume CPU, resulting in a Denial of Service.\n\n## Remediation\nUpgrade `semver` to version 5.7.2, 6.3.1, 7.5.2 or higher.\n## References\n- [GitHub Commit](https://github.com/npm/node-semver/commit/2f8fd41487acf380194579ecb6f8b1bbfe116be0)\n- [GitHub Commit](https://github.com/npm/node-semver/commit/717534ee353682f3bcf33e60a8af4292626d4441)\n- [GitHub Commit](https://github.com/npm/node-semver/commit/928e56d21150da0413a3333a3148b20e741a920c)\n- [GitHub PR](https://github.com/npm/node-semver/pull/564)\n- [Vulnerable Code](https://github.com/npm/node-semver/blob/main/classes/range.js#L97-L104)\n- [Vulnerable Code](https://github.com/npm/node-semver/blob/main/internal/re.js#L138)\n- [Vulnerable Code](https://github.com/npm/node-semver/blob/main/internal/re.js#L160)\n","created_at":"2023-01-25T16:16:50Z","updated_at":"2024-03-11T09:54:01Z","problems":[{"id":"CVE-2022-25883","source":"CVE"},{"id":"CWE-1333","source":"CWE"}],"coordinates":[{"remedies":[{"description":"Upgrade to version 5.7.2 to fix this vulnerability","details":{"upgrade_package":"5.7.2"},"type":"indeterminate"}],"representation":[{"resource_path":"\u003c5.7.2,\u003e=6.0.0 \u003c6.3.1,\u003e=7.0.0 \u003c7.5.2"}]}],"severities":[{"source":"Snyk","level":"high","score":7.5,"vector":"CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:H/E:P"}],"effective_severity_level":"high","slots":[{"disclosure_time":"2023-01-25 16:00:59 +0000 UTC","exploit":"Proof of Concept","publication_time":"2023-06-20 15:39:58.313421 +0000 UTC","references":[{"url":"https://github.com/npm/node-semver/commit/2f8fd41487acf380194579ecb6f8b1bbfe116be0","title":"GitHub Commit"},{"url":"https://github.com/npm/node-semver/commit/717534ee353682f3bcf33e60a8af4292626d4441","title":"GitHub Commit"},{"url":"https://github.com/npm/node-semver/commit/928e56d21150da0413a3333a3148b20e741a920c","title":"GitHub Commit"},{"url":"https://github.com/npm/node-semver/pull/564","title":"GitHub PR"},{"url":"https://github.com/npm/node-semver/blob/main/classes/range.js%23L97-L104","title":"Vulnerable Code"},{"url":"https://github.com/npm/node-semver/blob/main/internal/re.js%23L138","title":"Vulnerable Code"},{"url":"https://github.com/npm/node-semver/blob/main/internal/re.js%23L160","title":"Vulnerable Code"}]}]}}]} diff --git a/test/jest/acceptance/snyk-sbom-test/all-projects.spec.ts b/test/jest/acceptance/snyk-sbom-test/all-projects.spec.ts new file mode 100644 index 0000000000..fee363aaae --- /dev/null +++ b/test/jest/acceptance/snyk-sbom-test/all-projects.spec.ts @@ -0,0 +1,113 @@ +import { runSnykCLI } from '../../util/runSnykCLI'; +import { fakeServer } from '../../../acceptance/fake-server'; +import { getServerPort } from '../../util/getServerPort'; +import { getFixturePath } from '../../util/getFixturePath'; +import * as path from 'path'; + +jest.setTimeout(1000 * 60); + +describe('snyk sbom test (mocked server only)', () => { + let server: ReturnType; + let env: Record; + + beforeAll((done) => { + const port = getServerPort(process); + const baseApi = ''; + env = { + ...process.env, + SNYK_API: 'http://localhost:' + port + baseApi, + SNYK_API_REST_URL: 'http://localhost:' + port + baseApi, + SNYK_HOST: 'http://localhost:' + port, + SNYK_TOKEN: '123456789', + SNYK_DISABLE_ANALYTICS: '1', + }; + server = fakeServer(baseApi, env.SNYK_TOKEN); + server.listen(port, () => { + done(); + }); + }); + + afterEach(() => { + jest.resetAllMocks(); + server.restore(); + }); + + afterAll((done) => { + server.close(() => { + done(); + }); + }); + + test('`npm CycloneDX JSON`', async () => { + const fileToTest = path.resolve( + getFixturePath('sbom'), + 'npm-sbom-cdx15.json', + ); + + const { + code, + stdout, + stderr, + } = await runSnykCLI( + `sbom test --org aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee --experimental --file ${fileToTest}`, + { env }, + ); + + expect(stdout).toMatch( + '[MEDIUM] Regular Expression Denial of Service (ReDoS)', + ); + expect(stdout).toMatch('Introduced through: pkg:npm/minimatch@3.0.4'); + expect(stdout).toMatch( + 'URL: https://security.snyk.io/vuln/SNYK-JS-MINIMATCH-3050818', + ); + + expect(stdout).toMatch( + '[HIGH] Regular Expression Denial of Service (ReDoS)', + ); + expect(stdout).toMatch('Introduced through: pkg:npm/semver@7.3.5'); + expect(stdout).toMatch( + 'URL: https://security.snyk.io/vuln/SNYK-JS-SEMVER-3247795', + ); + + expect(code).toEqual(1); + + expect(stderr).toEqual(''); + }); + + test('`missing experimental flag`', async () => { + const fileToTest = path.resolve( + getFixturePath('sbom'), + 'npm-sbom-cdx15.json', + ); + + const { + stdout, + stderr, + } = await runSnykCLI( + `sbom test --org aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee --file ${fileToTest}`, + { env }, + ); + + expect(stdout).toMatch( + 'Flag `--experimental` is required to execute this command.', + ); + + expect(stderr).toEqual(''); + }); + + test('`missing file flag`', async () => { + const { + stdout, + stderr, + } = await runSnykCLI( + `sbom test --org aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee --experimental`, + { env }, + ); + + expect(stdout).toMatch( + 'Flag `--file` is required to execute this command. Value should point to a valid SBOM document.', + ); + + expect(stderr).toEqual(''); + }); +});