From 6947ede0726ef926a7125484492fec2699f033a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Comb=C3=BCchen?= Date: Fri, 26 Apr 2024 08:59:13 +0200 Subject: [PATCH] fix(cli): Print message of errors wrapping exec.ExitError --- cliv2/cmd/cliv2/main.go | 3 +-- cliv2/cmd/cliv2/main_test.go | 14 ++++++++++++++ cliv2/go.mod | 4 ++-- cliv2/go.sum | 8 ++++---- test/fixtures/sbom/npm-sbom-test-response.json | 2 +- test/jest/acceptance/snyk-sbom/sbom.spec.ts | 5 ++++- 6 files changed, 26 insertions(+), 10 deletions(-) diff --git a/cliv2/cmd/cliv2/main.go b/cliv2/cmd/cliv2/main.go index 17032084de5..eb95141d86d 100644 --- a/cliv2/cmd/cliv2/main.go +++ b/cliv2/cmd/cliv2/main.go @@ -370,9 +370,8 @@ func handleError(err error) HandleError { func displayError(err error, output io.Writer, config configuration.Configuration) { if err != nil { - var exitError *exec.ExitError var exitCode *cli_errors.ErrorWithExitCode - isExitError := errors.As(err, &exitError) + _, isExitError := err.(*exec.ExitError) isErrorWithCode := errors.As(err, &exitCode) if isExitError || isErrorWithCode { return diff --git a/cliv2/cmd/cliv2/main_test.go b/cliv2/cmd/cliv2/main_test.go index a8f74f40729..2dd8960009a 100644 --- a/cliv2/cmd/cliv2/main_test.go +++ b/cliv2/cmd/cliv2/main_test.go @@ -400,4 +400,18 @@ func Test_displayError(t *testing.T) { assert.Equal(t, "", b.String()) }) } + + t.Run("prints messages of error wrapping exec.ExitError", func(t *testing.T) { + var b bytes.Buffer + config := configuration.NewInMemory() + err := &wrErr{wraps: &exec.ExitError{}} + displayError(err, &b, config) + + assert.Equal(t, "something went wrong\n", b.String()) + }) } + +type wrErr struct{ wraps error } + +func (e *wrErr) Error() string { return "something went wrong" } +func (e *wrErr) Unwrap() error { return e.wraps } diff --git a/cliv2/go.mod b/cliv2/go.mod index 9959afca492..0f128307d0e 100644 --- a/cliv2/go.mod +++ b/cliv2/go.mod @@ -9,9 +9,9 @@ require ( github.com/google/uuid v1.6.0 github.com/pkg/errors v0.9.1 github.com/rs/zerolog v1.32.0 - github.com/snyk/cli-extension-dep-graph v0.0.0-20230926124856-b0fdf1ee6f73 + github.com/snyk/cli-extension-dep-graph v0.0.0-20240426125928-8d56ac52821e github.com/snyk/cli-extension-iac-rules v0.0.0-20240422133948-ae17a4306672 - github.com/snyk/cli-extension-sbom v0.0.0-20240418082712-4732b4b2d7b3 + github.com/snyk/cli-extension-sbom v0.0.0-20240426130148-b83c2ebc75c4 github.com/snyk/container-cli v0.0.0-20240322120441-6d9b9482f9b1 github.com/snyk/go-application-framework v0.0.0-20240423090544-b0ba7f6ed181 github.com/snyk/go-httpauth v0.0.0-20240307114523-1f5ea3f55c65 diff --git a/cliv2/go.sum b/cliv2/go.sum index ec98432959a..0f9dc601a37 100644 --- a/cliv2/go.sum +++ b/cliv2/go.sum @@ -721,12 +721,12 @@ github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/skeema/knownhosts v1.2.2 h1:Iug2P4fLmDw9f41PB6thxUkNUkJzB5i+1/exaj40L3A= github.com/skeema/knownhosts v1.2.2/go.mod h1:xYbVRSPxqBZFrdmDyMmsOs+uX1UZC3nTN3ThzgDxUwo= -github.com/snyk/cli-extension-dep-graph v0.0.0-20230926124856-b0fdf1ee6f73 h1:rwdJzpPWkWWf7csbv871GoKgRGwJ4GVNXMSsO0cRuLk= -github.com/snyk/cli-extension-dep-graph v0.0.0-20230926124856-b0fdf1ee6f73/go.mod h1:QF3v8HBpOpyudYNCuR8LqfULutO76c91sBdLzD+pBJU= +github.com/snyk/cli-extension-dep-graph v0.0.0-20240426125928-8d56ac52821e h1:j17Ujw51/2SC3m1hbNCUwxFc8aNIFyfpnwFAszgEM8c= +github.com/snyk/cli-extension-dep-graph v0.0.0-20240426125928-8d56ac52821e/go.mod h1:QF3v8HBpOpyudYNCuR8LqfULutO76c91sBdLzD+pBJU= github.com/snyk/cli-extension-iac-rules v0.0.0-20240422133948-ae17a4306672 h1:AkLej8Lk//vFex1fiygSYFrQTUd0xP+GyRbsI+m2kwQ= github.com/snyk/cli-extension-iac-rules v0.0.0-20240422133948-ae17a4306672/go.mod h1:2vKTUsW73sVbDcyD19iNLfN0so2GSu9BE3k/fqG0mjA= -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/cli-extension-sbom v0.0.0-20240426130148-b83c2ebc75c4 h1:nLbplrdYB4ljdFSnpmw2rCqmBVxHcvZOFEqc8JF9OQc= +github.com/snyk/cli-extension-sbom v0.0.0-20240426130148-b83c2ebc75c4/go.mod h1:lqmQT+QdzLdfi7qsqIH4qvCsSWu+P09GDFwQcmFfC0g= github.com/snyk/code-client-go v1.4.5 h1:r112huvRXv6gsHNUkeFLMbEz8dOLBv+v/hZDJfuPZaA= github.com/snyk/code-client-go v1.4.5/go.mod h1:Kkr7pQc8ItsBZSYd6A1S4r4VHO6HNyTWZsqi18sAtwQ= github.com/snyk/container-cli v0.0.0-20240322120441-6d9b9482f9b1 h1:9RKY9NdX5DrJAoVXDP0JiqrXT+4Nb9NH8pjEcA0NsLA= diff --git a/test/fixtures/sbom/npm-sbom-test-response.json b/test/fixtures/sbom/npm-sbom-test-response.json index 45693077273..609fa155e26 100644 --- a/test/fixtures/sbom/npm-sbom-test-response.json +++ b/test/fixtures/sbom/npm-sbom-test-response.json @@ -1 +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"}]}]}}]} +{"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-18T06:00:25Z","exploit":"Not Defined","publication_time":"2022-10-18T06:29:18Z","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-25T16:00:59Z","exploit":"Proof of Concept","publication_time":"2023-06-20T15:39:58Z","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/sbom.spec.ts b/test/jest/acceptance/snyk-sbom/sbom.spec.ts index 64d93430d9d..23277dc87c6 100644 --- a/test/jest/acceptance/snyk-sbom/sbom.spec.ts +++ b/test/jest/acceptance/snyk-sbom/sbom.spec.ts @@ -177,7 +177,7 @@ describe('snyk sbom (mocked server only)', () => { test('`sbom` retains the exit error code of the underlying SCA process', async () => { const project = await createProject('empty'); - const { code } = await runSnykCLI( + const { code, stdout } = await runSnykCLI( `sbom --org aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee --format cyclonedx1.5+json --debug`, { cwd: project.path(), @@ -186,5 +186,8 @@ describe('snyk sbom (mocked server only)', () => { ); expect(code).toBe(3); + expect(stdout).toContain( + 'An error occurred while running the underlying analysis needed to generate the SBOM.', + ); }); });