Repository navigation
196 csaf 2.1 optional test 6.2.24 #266
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
christopher-exx
wants to merge
13
commits into
main
Choose a base branch
from
196-csaf-2.1_optional_test_6.2.24
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
4b5d580
feat: add optional test 6.2.24
christopher-exx c299e78
feat: add test for validating inputSchema of optional test 6.2.24
christopher-exx 2607e20
fix: fix wrong typedef in cwe-catalogue
christopher-exx dcb7fe2
feat: adapt recommended test 6.2.24
christopher-exx 6fc05bb
feat: add test on empty objects
christopher-exx 17f1078
Merge remote-tracking branch 'origin/main' into 196-csaf-2.1_optional…
bendo-eXX cb486ce
feat(CSAF2.1): fix import
bendo-eXX 9580d5b
feat(CSAF2.1): fix import
bendo-eXX 0a349d3
feat(CSAF2.1): add test for unknown version
bendo-eXX 1851e5e
Merge branch 'main' into 196-csaf-2.1_optional_test_6.2.24
bendo-eXX 989c603
feat(CSAF2.1): change dateHelper
bendo-eXX 814b29d
Merge branch 'main' into 196-csaf-2.1_optional_test_6.2.24
bendo-eXX 139a6a8
feat(csaf2.1): change unit test to vitest
bendo-eXX File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| import { Ajv } from 'ajv/dist/jtd.js' | ||
| import { cwecMap } from '../../lib/cwec.js' | ||
| import { | ||
| compareZonedDateTimes, | ||
| localDateToTimeZonedDate, | ||
| } from '../dateHelper.js' | ||
|
|
||
| const ajv = new Ajv() | ||
|
|
||
| /* | ||
| This is the jtd schema that needs to match the input document so that the | ||
| test is activated. If this schema doesn't match it normally means that the input | ||
| document does not validate against the csaf json schema or optional fields that | ||
| the test checks are not present. | ||
| */ | ||
| const inputSchema = /** @type {const} */ ({ | ||
| additionalProperties: true, | ||
| properties: { | ||
| document: { | ||
| additionalProperties: true, | ||
| properties: { | ||
| tracking: { | ||
| additionalProperties: true, | ||
| properties: { | ||
| current_release_date: { type: 'string' }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| vulnerabilities: { | ||
| elements: { | ||
| additionalProperties: true, | ||
| optionalProperties: { | ||
| cwes: { | ||
| elements: { | ||
| additionalProperties: true, | ||
| properties: {}, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
|
|
||
| const validateInput = ajv.compile(inputSchema) | ||
|
|
||
| const cweSchema = /** @type {const} */ ({ | ||
| additionalProperties: true, | ||
| properties: { | ||
| id: { type: 'string' }, | ||
| version: { type: 'string' }, | ||
| name: { type: 'string' }, | ||
| }, | ||
| }) | ||
|
|
||
| const validateCWE = ajv.compile(cweSchema) | ||
|
|
||
| /** @type {Promise<Array<[string, string]>> | undefined} */ | ||
| let sortedCwecReleases | ||
|
|
||
| /** | ||
| * Loads the release dates of all known CWE versions once (loading all catalogues is expensive) | ||
| * and returns them sorted descending by date (newest first). Versions with the same date | ||
| * keep the order of the `cwecMap`, which lists newer versions first. | ||
| * | ||
| * @returns {Promise<Array<[string, string]>>} | ||
| */ | ||
| function getSortedCwecReleases() { | ||
| sortedCwecReleases ??= (async () => { | ||
| /** @type {Array<[string, string]>} */ | ||
| const releases = [] | ||
| for (const [version, load] of cwecMap) { | ||
| const date = (await load()).default.date | ||
| releases.push([version, localDateToTimeZonedDate(date)]) | ||
| } | ||
| return releases.sort((a, b) => compareZonedDateTimes(b[1], a[1])) | ||
| })() | ||
| return sortedCwecReleases | ||
| } | ||
|
|
||
| /** | ||
| * This implements the recommended test 6.2.24 of the CSAF 2.1 standard. | ||
| * | ||
| * @param {any} doc | ||
| */ | ||
| export async function recommendedTest_6_2_24(doc) { | ||
| /** @type {Array<{ message: string; instancePath: string }>} */ | ||
| const warnings = [] | ||
| const context = { warnings } | ||
|
|
||
| if (!validateInput(doc)) { | ||
| return context | ||
| } | ||
|
|
||
| const currentReleaseDate = doc.document.tracking.current_release_date | ||
| const cwecArray = await getSortedCwecReleases() | ||
|
|
||
| for (let i = 0; i < doc.vulnerabilities.length; ++i) { | ||
| const vulnerability = doc.vulnerabilities[i] | ||
| if (vulnerability.cwes) { | ||
| for (let j = 0; j < vulnerability.cwes.length; ++j) { | ||
| const cwe = vulnerability.cwes.at(j) | ||
| if (validateCWE(cwe)) { | ||
| const idx = cwecArray.findIndex((row) => row[0] === cwe.version) | ||
| if (idx === -1) continue | ||
| const zonedCweReleaseDate = cwecArray[idx][1] | ||
| // Case 1: Check if the used cwe version was already published | ||
| // at the current release date of the CSAF document | ||
| if ( | ||
| compareZonedDateTimes(currentReleaseDate, zonedCweReleaseDate) < 0 | ||
| ) { | ||
| context.warnings.push({ | ||
| instancePath: `/vulnerabilities/${i}/cwes/${j}/version`, | ||
| message: `at release date ${currentReleaseDate} the used version ${cwe.version} was not published yet`, | ||
| }) | ||
| // Case 2: Check if a newer cwe version than the used one had already been published | ||
| // at the current release date of the CSAF document | ||
| } else { | ||
| for (let k = idx - 1; k >= 0; k--) { | ||
| const zonedCweReleaseDateNewer = cwecArray[k][1] | ||
|
|
||
| if ( | ||
| compareZonedDateTimes( | ||
| currentReleaseDate, | ||
| zonedCweReleaseDateNewer | ||
| ) > 0 | ||
| ) { | ||
| context.warnings.push({ | ||
| instancePath: `/vulnerabilities/${i}/cwes/${j}/version`, | ||
| message: | ||
| `the used cwe version ${cwe.version} ` + | ||
| `was not the newest one available at release date ${currentReleaseDate}`, | ||
| }) | ||
| break | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return context | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,7 +17,6 @@ const excluded = [ | |
| '6.1.60.2', | ||
| '6.1.60.3', | ||
| '6.2.20', | ||
| '6.2.24', | ||
| '6.2.31', | ||
| '6.2.39.1', | ||
| '6.2.46', | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| import { recommendedTest_6_2_24 } from '../../csaf_2_1/recommendedTests.js' | ||
|
|
||
| describe('recommendedTest_6_2_24', function () { | ||
| it('only runs on relevant documents', async function () { | ||
| expect( | ||
| (await recommendedTest_6_2_24({ vulnerabilities: 'mydoc' })).warnings | ||
| .length | ||
| ).toBe(0) | ||
| }) | ||
|
|
||
| it('skips cwe entries with an unknown version (not in cwecMap)', async function () { | ||
| expect( | ||
| ( | ||
| await recommendedTest_6_2_24({ | ||
| document: { | ||
| tracking: { | ||
| current_release_date: '2024-01-21T10:00:00.000Z', | ||
| }, | ||
| }, | ||
| vulnerabilities: [ | ||
| { | ||
| cwes: [ | ||
| { | ||
| id: 'CWE-256', | ||
| name: 'Plaintext Storage of a Password', | ||
| version: '99.99', | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }) | ||
| ).warnings.length | ||
| ).toBe(0) | ||
| }) | ||
|
|
||
| it('skips empty objects', async function () { | ||
| expect( | ||
| ( | ||
| await recommendedTest_6_2_24({ | ||
| document: { | ||
| tracking: { | ||
| current_release_date: '2024-01-21T10:00:00.000Z', | ||
| }, | ||
| }, | ||
| vulnerabilities: [ | ||
| { | ||
| cwes: [ | ||
| { | ||
| id: 'CWE-256', | ||
| name: 'Plaintext Storage of a Password', | ||
| version: '4.12', | ||
| }, | ||
| ], | ||
| }, | ||
| {}, // should be ignored | ||
| ], | ||
| }) | ||
| ).warnings.length | ||
| ).toBe(1) | ||
| }) | ||
|
|
||
| it('skips cwe entries not matching the cwe schema but checks valid ones', async function () { | ||
| const result = await recommendedTest_6_2_24({ | ||
| document: { | ||
| tracking: { current_release_date: '2024-01-21T10:00:00.000Z' }, | ||
| }, | ||
| vulnerabilities: [ | ||
| { | ||
| cwes: [ | ||
| { id: 'CWE-256', name: 'Plaintext Storage of a Password' }, // version missing | ||
| { | ||
| id: 'CWE-256', | ||
| name: 'Plaintext Storage of a Password', | ||
| version: 4.12, | ||
| }, // wrong type | ||
| 'CWE-256', // not an object | ||
| { | ||
| id: 'CWE-256', | ||
| name: 'Plaintext Storage of a Password', | ||
| version: '4.12', | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }) | ||
|
|
||
| expect(result.warnings[0].instancePath).to.equal( | ||
| '/vulnerabilities/0/cwes/3/version' | ||
| ) | ||
| }) | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this might throw an exception if the given cwe version is not found in the catalogue since
idxis then-1which here evaluates toundefined[1]which throws.