diff --git a/domain/snyk/issues.go b/domain/snyk/issues.go index c394a1b0e..7934eff29 100644 --- a/domain/snyk/issues.go +++ b/domain/snyk/issues.go @@ -138,32 +138,49 @@ type MarkerPosition struct { } type OssIssueData struct { - Key string `json:"key"` - Title string `json:"title"` - Name string `json:"name"` - LineNumber int `json:"lineNumber"` - Identifiers Identifiers `jsom:"identifiers"` - Description string `json:"description"` - References []Reference `json:"references,omitempty"` - Version string `json:"version"` - License string `json:"license,omitempty"` - PackageManager string `json:"packageManager"` - PackageName string `json:"packageName"` - From []string `json:"from"` - FixedIn []string `json:"fixedIn,omitempty"` - UpgradePath []any `json:"upgradePath,omitempty"` - IsUpgradable bool `json:"isUpgradable,omitempty"` - CVSSv3 string `json:"CVSSv3,omitempty"` - CvssScore float64 `json:"cvssScore,omitempty"` - Exploit string `json:"exploit,omitempty"` - IsPatchable bool `json:"isPatchable"` - ProjectName string `json:"projectName"` - DisplayTargetFile string `json:"displayTargetFile"` - Language string `json:"language"` - Details string `json:"details"` - MatchingIssues []OssIssueData `json:"matchingIssues"` - Lesson string `json:"lesson,omitempty"` - Remediation string `json:"remediation"` + Key string `json:"key"` + Title string `json:"title"` + Name string `json:"name"` + LineNumber int `json:"lineNumber"` + Identifiers Identifiers `jsom:"identifiers"` + Description string `json:"description"` + References []Reference `json:"references,omitempty"` + Version string `json:"version"` + License string `json:"license,omitempty"` + PackageManager string `json:"packageManager"` + PackageName string `json:"packageName"` + From []string `json:"from"` + FixedIn []string `json:"fixedIn,omitempty"` + UpgradePath []any `json:"upgradePath,omitempty"` + IsUpgradable bool `json:"isUpgradable,omitempty"` + CVSSv3 string `json:"CVSSv3,omitempty"` + CvssScore float64 `json:"cvssScore,omitempty"` + Exploit string `json:"exploit,omitempty"` + IsPatchable bool `json:"isPatchable"` + ProjectName string `json:"projectName"` + DisplayTargetFile string `json:"displayTargetFile"` + Language string `json:"language"` + Details string `json:"details"` + MatchingIssues []OssIssueData `json:"matchingIssues"` + Lesson string `json:"lesson,omitempty"` + Remediation string `json:"remediation"` + AppliedPolicyRules AppliedPolicyRules `json:"appliedPolicyRules,omitempty"` +} + +type SeverityChange struct { + OriginalSeverity string `json:"originalSeverity"` + NewSeverity string `json:"newSeverity"` + Reason string `json:"reason"` +} + +type AppliedPolicyRules struct { + Annotation Annotation `json:"annotation,omitempty"` + SeverityChange SeverityChange `json:"severityChange,omitempty"` +} + +type Annotation struct { + Value string `json:"value,omitempty"` + Reason string `json:"reason,omitempty"` } type Identifiers struct { diff --git a/infrastructure/oss/testdata/policyAnnotationsInputData.json b/infrastructure/oss/testdata/policyAnnotationsInputData.json new file mode 100644 index 000000000..c762a984a --- /dev/null +++ b/infrastructure/oss/testdata/policyAnnotationsInputData.json @@ -0,0 +1,168 @@ +{ + "id": "SNYK-JS-SEMVER-3247795", + "title": "Regular Expression Denial of Service (ReDoS)", + "CVSSv3": "CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:H/E:P", + "credit": [ + "Alessio Della Libera - Snyk Research Team" + ], + "semver": { + "vulnerable": [ + "<5.7.2", + ">=6.0.0 <6.3.1", + ">=7.0.0 <7.5.2" + ] + }, + "exploit": "Proof of Concept", + "fixedIn": [ + "5.7.2", + "6.3.1", + "7.5.2" + ], + "patches": [], + "insights": { + "triageAdvice": null + }, + "language": "js", + "severity": "high", + "cvssScore": 7.5, + "functions": [], + "malicious": false, + "isDisputed": false, + "moduleName": "semver", + "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" + } + ], + "cvssDetails": [ + { + "assigner": "NVD", + "severity": "high", + "cvssV3Vector": "CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:H", + "cvssV3BaseScore": 7.5, + "modificationTime": "2024-03-11T09:53:22.600372Z" + }, + { + "assigner": "Red Hat", + "severity": "high", + "cvssV3Vector": "CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:H", + "cvssV3BaseScore": 7.5, + "modificationTime": "2024-03-11T09:54:01.668389Z" + } + ], + "cvssSources": [ + { + "type": "primary", + "vector": "CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:H/E:P", + "assigner": "Snyk", + "severity": "high", + "baseScore": 7.5, + "cvssVersion": "3.1", + "modificationTime": "2024-03-06T14:06:32.773457Z" + }, + { + "type": "secondary", + "vector": "CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:H", + "assigner": "NVD", + "severity": "high", + "baseScore": 7.5, + "cvssVersion": "3.1", + "modificationTime": "2024-03-11T09:53:22.600372Z" + }, + { + "type": "secondary", + "vector": "CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:H", + "assigner": "Red Hat", + "severity": "high", + "baseScore": 7.5, + "cvssVersion": "3.1", + "modificationTime": "2024-03-11T09:54:01.668389Z" + } + ], + "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]) + '<1.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", + "epssDetails": { + "percentile": "0.51115", + "probability": "0.00148", + "modelVersion": "v2023.03.01" + }, + "identifiers": { + "CVE": [ + "CVE-2022-25883" + ], + "CWE": [ + "CWE-1333" + ] + }, + "packageName": "semver", + "proprietary": true, + "creationTime": "2023-01-25T16:16:50.073158Z", + "functions_new": [], + "alternativeIds": [], + "disclosureTime": "2023-01-25T16:00:59Z", + "exploitDetails": { + "sources": [ + "Snyk" + ], + "maturityLevels": [ + { + "type": "secondary", + "level": "Proof of Concept", + "format": "CVSSv3" + }, + { + "type": "primary", + "level": "Proof of Concept", + "format": "CVSSv4" + } + ] + }, + "packageManager": "npm", + "publicationTime": "2023-06-20T15:39:58.313421Z", + "severityBasedOn": "CVSS", + "modificationTime": "2024-03-11T09:54:01.668389Z", + "socialTrendAlert": false, + "appliedPolicyRules": { + "annotation": { + "value": "annotate on this SNYK-JS-SEMVER-3247795 09/08/2024", + "reason": "SNYK-JS-SEMVER-3247795 annotating..." + } + }, + "severityWithCritical": "high", + "from": [ + "restify@2.6.1", + "semver@2.2.1" + ], + "upgradePath": [ + false, + "semver@5.7.2" + ], + "isUpgradable": true, + "isPatchable": false, + "name": "semver", + "version": "2.2.1" +} diff --git a/infrastructure/oss/testdata/policySeverityChangeInputData.json b/infrastructure/oss/testdata/policySeverityChangeInputData.json new file mode 100644 index 000000000..e43a1d337 --- /dev/null +++ b/infrastructure/oss/testdata/policySeverityChangeInputData.json @@ -0,0 +1,170 @@ +{ + "id": "SNYK-JS-SEMVER-3247795", + "title": "Regular Expression Denial of Service (ReDoS)", + "CVSSv3": "CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:H/E:P", + "credit": [ + "Alessio Della Libera - Snyk Research Team" + ], + "semver": { + "vulnerable": [ + "<5.7.2", + ">=6.0.0 <6.3.1", + ">=7.0.0 <7.5.2" + ] + }, + "exploit": "Proof of Concept", + "fixedIn": [ + "5.7.2", + "6.3.1", + "7.5.2" + ], + "patches": [], + "insights": { + "triageAdvice": null + }, + "language": "js", + "severity": "high", + "cvssScore": 7.5, + "functions": [], + "malicious": false, + "isDisputed": false, + "moduleName": "semver", + "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" + } + ], + "cvssDetails": [ + { + "assigner": "NVD", + "severity": "high", + "cvssV3Vector": "CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:H", + "cvssV3BaseScore": 7.5, + "modificationTime": "2024-03-11T09:53:22.600372Z" + }, + { + "assigner": "Red Hat", + "severity": "high", + "cvssV3Vector": "CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:H", + "cvssV3BaseScore": 7.5, + "modificationTime": "2024-03-11T09:54:01.668389Z" + } + ], + "cvssSources": [ + { + "type": "primary", + "vector": "CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:H/E:P", + "assigner": "Snyk", + "severity": "high", + "baseScore": 7.5, + "cvssVersion": "3.1", + "modificationTime": "2024-03-06T14:06:32.773457Z" + }, + { + "type": "secondary", + "vector": "CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:H", + "assigner": "NVD", + "severity": "high", + "baseScore": 7.5, + "cvssVersion": "3.1", + "modificationTime": "2024-03-11T09:53:22.600372Z" + }, + { + "type": "secondary", + "vector": "CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:H", + "assigner": "Red Hat", + "severity": "high", + "baseScore": 7.5, + "cvssVersion": "3.1", + "modificationTime": "2024-03-11T09:54:01.668389Z" + } + ], + "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]) + '<1.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", + "epssDetails": { + "percentile": "0.51115", + "probability": "0.00148", + "modelVersion": "v2023.03.01" + }, + "identifiers": { + "CVE": [ + "CVE-2022-25883" + ], + "CWE": [ + "CWE-1333" + ] + }, + "packageName": "semver", + "proprietary": true, + "creationTime": "2023-01-25T16:16:50.073158Z", + "functions_new": [], + "alternativeIds": [], + "disclosureTime": "2023-01-25T16:00:59Z", + "exploitDetails": { + "sources": [ + "Snyk" + ], + "maturityLevels": [ + { + "type": "secondary", + "level": "Proof of Concept", + "format": "CVSSv3" + }, + { + "type": "primary", + "level": "Proof of Concept", + "format": "CVSSv4" + } + ] + }, + "packageManager": "npm", + "publicationTime": "2023-06-20T15:39:58.313421Z", + "severityBasedOn": "CVSS", + "modificationTime": "2024-03-11T09:54:01.668389Z", + "socialTrendAlert": false, + "originalSeverity": "high", + "severityWithCritical": "critical", + "appliedPolicyRules": { + "severityChange": { + "originalSeverity": "high", + "newSeverity": "critical", + "reason": "Changing the severity" + } + }, + "from": [ + "restify@2.6.1", + "semver@2.2.1" + ], + "upgradePath": [ + false, + "semver@5.7.2" + ], + "isUpgradable": true, + "isPatchable": false, + "name": "semver", + "version": "2.2.1" +} diff --git a/infrastructure/oss/types.go b/infrastructure/oss/types.go index 6598fb507..7bf94c42a 100644 --- a/infrastructure/oss/types.go +++ b/infrastructure/oss/types.go @@ -48,28 +48,66 @@ type reference struct { } type ossIssue struct { - Id string `json:"id"` - Name string `json:"name"` - Title string `json:"title"` - Severity string `json:"severity"` - LineNumber int `json:"lineNumber"` - Description string `json:"description"` - References []reference `json:"references,omitempty"` - Version string `json:"version"` - PackageManager string `json:"packageManager"` - PackageName string `json:"packageName"` - From []string `json:"from"` - Identifiers identifiers `json:"identifiers,omitempty"` - FixedIn []string `json:"fixedIn,omitempty"` - UpgradePath []any `json:"upgradePath,omitempty"` - IsUpgradable bool `json:"isUpgradable,omitempty"` - CVSSv3 string `json:"CVSSv3,omitempty"` - CvssScore float64 `json:"cvssScore,omitempty"` - Exploit string `json:"exploit,omitempty"` - IsPatchable bool `json:"isPatchable"` - License string `json:"license,omitempty"` - Language string `json:"language,omitempty"` - lesson *learn.Lesson `json:"-"` + Id string `json:"id"` + Name string `json:"name"` + Title string `json:"title"` + Severity string `json:"severity"` + LineNumber int `json:"lineNumber"` + Description string `json:"description"` + References []reference `json:"references,omitempty"` + Version string `json:"version"` + PackageManager string `json:"packageManager"` + PackageName string `json:"packageName"` + From []string `json:"from"` + Identifiers identifiers `json:"identifiers,omitempty"` + FixedIn []string `json:"fixedIn,omitempty"` + UpgradePath []any `json:"upgradePath,omitempty"` + IsUpgradable bool `json:"isUpgradable,omitempty"` + CVSSv3 string `json:"CVSSv3,omitempty"` + CvssScore float64 `json:"cvssScore,omitempty"` + Exploit string `json:"exploit,omitempty"` + IsPatchable bool `json:"isPatchable"` + License string `json:"license,omitempty"` + Language string `json:"language,omitempty"` + lesson *learn.Lesson `json:"-"` + OriginalSeverity string `json:"originalSeverity,omitempty"` + SeverityWithCritical string `json:"severityWithCritical,omitempty"` + AppliedPolicyRules AppliedPolicyRules `json:"appliedPolicyRules,omitempty"` +} + +type SeverityChange struct { + OriginalSeverity string `json:"originalSeverity"` + NewSeverity string `json:"newSeverity"` + Reason string `json:"reason"` +} + +type AppliedPolicyRules struct { + Annotation Annotation `json:"annotation,omitempty"` + SeverityChange SeverityChange `json:"severityChange,omitempty"` +} + +func (r AppliedPolicyRules) toAppliedPolicyRules() snyk.AppliedPolicyRules { + if r.SeverityChange.NewSeverity != "" { + return snyk.AppliedPolicyRules{ + SeverityChange: snyk.SeverityChange{ + OriginalSeverity: r.SeverityChange.OriginalSeverity, + NewSeverity: r.SeverityChange.NewSeverity, + Reason: r.SeverityChange.Reason, + }, + } + } + + return snyk.AppliedPolicyRules{ + Annotation: snyk.Annotation{ + Value: r.Annotation.Value, + Reason: r.Annotation.Reason, + }, + } +} + +type Annotation struct { + Value string `json:"value,omitempty"` + Reason string `json:"reason,omitempty"` } func (i *ossIssue) toAdditionalData(scanResult *scanResult, matchingIssues []snyk.OssIssueData) snyk.OssIssueData { @@ -104,7 +142,7 @@ func (i *ossIssue) toAdditionalData(scanResult *scanResult, matchingIssues []sny additionalData.Lesson = i.lesson.Url } additionalData.Remediation = i.GetRemediation() - + additionalData.AppliedPolicyRules = i.AppliedPolicyRules.toAppliedPolicyRules() return additionalData } diff --git a/infrastructure/oss/types_test.go b/infrastructure/oss/types_test.go new file mode 100644 index 000000000..845710f27 --- /dev/null +++ b/infrastructure/oss/types_test.go @@ -0,0 +1,93 @@ +/* + * © 2024 Snyk Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package oss + +import ( + "encoding/json" + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/require" + + "github.com/snyk/snyk-ls/domain/snyk" + "github.com/snyk/snyk-ls/internal/testutil" +) + +func Test_ossIssueContainsPolicyAnnotations(t *testing.T) { + testutil.UnitTest(t) + + issue := setupOssIssueWithPolicyAnnotations(t) + + require.NotEmpty(t, issue.AppliedPolicyRules.Annotation.Value) + require.NotEmpty(t, issue.AppliedPolicyRules.Annotation.Reason) +} + +func Test_ossIssue_toAdditionalData_ConvertsPolicyAnnotations(t *testing.T) { + testutil.UnitTest(t) + + issue := setupOssIssueWithPolicyAnnotations(t) + fakeScanResult := &scanResult{ + DisplayTargetFile: "testFile", + ProjectName: "test", + } + + convertedIssue := issue.toAdditionalData(fakeScanResult, []snyk.OssIssueData{}) + + require.NotEmpty(t, convertedIssue.AppliedPolicyRules.Annotation.Value) + require.NotEmpty(t, convertedIssue.AppliedPolicyRules.Annotation.Reason) +} + +func Test_ossIssue_toAdditionalData_ConvertsSeverityChange(t *testing.T) { + testutil.UnitTest(t) + issue := setupOssIssueWithSeverityChangePolicy(t) + fakeScanResult := &scanResult{ + DisplayTargetFile: "testFile", + ProjectName: "test", + } + + convertedIssue := issue.toAdditionalData(fakeScanResult, []snyk.OssIssueData{}) + + require.NotEmpty(t, convertedIssue.AppliedPolicyRules.SeverityChange.OriginalSeverity) + require.NotEmpty(t, convertedIssue.AppliedPolicyRules.SeverityChange.NewSeverity) + require.NotEmpty(t, convertedIssue.AppliedPolicyRules.SeverityChange.Reason) +} + +func setupOssIssueWithPolicyAnnotations(t *testing.T) ossIssue { + t.Helper() + name := filepath.Join("testdata", "policyAnnotationsInputData.json") + issue := unmarshalFromFile(t, name) + return issue +} + +func setupOssIssueWithSeverityChangePolicy(t *testing.T) ossIssue { + t.Helper() + name := filepath.Join("testdata", "policySeverityChangeInputData.json") + issue := unmarshalFromFile(t, name) + return issue +} + +func unmarshalFromFile(t *testing.T, name string) ossIssue { + t.Helper() + inputJson, err := os.ReadFile(name) + require.NoError(t, err) + + var issue ossIssue + err = json.Unmarshal(inputJson, &issue) + require.NoError(t, err) + return issue +}