-
Notifications
You must be signed in to change notification settings - Fork 9
feat(CSAF2.1): #447 add recommended test test 6.2.47 #458
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
rainer-exxcellent
wants to merge
2
commits into
main
Choose a base branch
from
feat/447-csaf-2.1_recommended_test_6.2.47
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| import Ajv from 'ajv/dist/jtd.js' | ||
| import { isCanonicalUrl } from '../../lib/shared/urlHelper.js' | ||
|
|
||
| /** @typedef {import('ajv/dist/jtd.js').JTDDataType<typeof inputSchema>} InputSchema */ | ||
|
|
||
| /** @typedef {InputSchema['vulnerabilities'][number]} Vulnerability */ | ||
|
|
||
| /** @typedef {NonNullable<Vulnerability['metrics']>[number]} Metric */ | ||
|
|
||
| /** @typedef {NonNullable<Metric['content']>} MetricContent */ | ||
|
|
||
| /** @typedef {{url?: string, category?: string}} Reference */ | ||
|
|
||
| const jtdAjv = new Ajv() | ||
|
|
||
| const inputSchema = /** @type {const} */ ({ | ||
| additionalProperties: true, | ||
| properties: { | ||
| document: { | ||
| additionalProperties: true, | ||
| optionalProperties: { | ||
| references: { | ||
| elements: { | ||
| additionalProperties: true, | ||
| optionalProperties: { | ||
| category: { type: 'string' }, | ||
| url: { type: 'string' }, | ||
| }, | ||
| }, | ||
| }, | ||
|
|
||
| tracking: { | ||
| additionalProperties: true, | ||
| optionalProperties: { | ||
| id: { type: 'string' }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| vulnerabilities: { | ||
| elements: { | ||
| additionalProperties: true, | ||
| optionalProperties: { | ||
| metrics: { | ||
| elements: { | ||
| additionalProperties: true, | ||
| optionalProperties: { | ||
| source: { | ||
| type: 'string', | ||
| }, | ||
| content: { | ||
| additionalProperties: true, | ||
| optionalProperties: { | ||
| qualitative_severity_rating: { | ||
| type: 'string', | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
|
|
||
| const validateInput = jtdAjv.compile(inputSchema) | ||
|
|
||
| /** | ||
| * Get the canonical url from the document | ||
| * @return {string} canonical url or empty when no canonical url exists | ||
| * @param {Array<{url?: string, category?: string}>|undefined} references | ||
| * @param {string|undefined} trackingId | ||
| */ | ||
| function getCanonicalUrl(references, trackingId) { | ||
| if (references && trackingId) { | ||
| // Find the reference that matches our criteria | ||
| /** @type {Reference| undefined} */ | ||
| const canonicalUrlReference = references.find((reference) => | ||
| isCanonicalUrl(reference, trackingId) | ||
| ) | ||
|
|
||
| // When we find a matching reference, we know it has the url property | ||
| // because isCanonicalUrl ensures it matches the Reference schema | ||
| return canonicalUrlReference?.url ?? '' | ||
| } else { | ||
| return '' | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * check whether metric has a qualitative_severity_rating | ||
| * and no `source` or `source` that is equal to the canonical URL. | ||
| * @param {Metric} metric | ||
| * @param {string} canonicalURL | ||
| * @return {boolean} | ||
| */ | ||
| function hasSeverityRatingAndNoSource(metric, canonicalURL) { | ||
| return ( | ||
| (!metric.source || metric.source === canonicalURL) && | ||
| !!metric?.content?.qualitative_severity_rating | ||
| ) | ||
| } | ||
|
|
||
| /** | ||
| * For each item in `metrics` provided by the issuing party it MUST be tested | ||
| * that it does not use the qualitative severity rating. | ||
| * This covers all items in `metrics` that do not have a `source` property and those where the `source` is equal to | ||
| * the canonical URL. | ||
| * | ||
| /** | ||
| * @param {any} doc | ||
| */ | ||
| export function recommendedTest_6_2_47(doc) { | ||
| const ctx = { | ||
| warnings: | ||
| /** @type {Array<{ instancePath: string; message: string }>} */ ([]), | ||
| } | ||
| if (!validateInput(doc)) { | ||
| return ctx | ||
| } | ||
|
|
||
| /** @type {Array<Vulnerability>} */ | ||
| const vulnerabilities = doc.vulnerabilities | ||
| const canonicalURL = getCanonicalUrl( | ||
| doc.document?.references, | ||
| doc.document?.tracking?.id | ||
| ) | ||
|
|
||
| vulnerabilities.forEach((vulnerabilityItem, vulnerabilityIndex) => { | ||
| /** @type {Array<Metric> | undefined} */ | ||
| const metrics = vulnerabilityItem.metrics | ||
| /** @type {Array<String> | undefined} */ | ||
| const invalidPaths = metrics | ||
| ?.map((metric, metricIndex) => | ||
| hasSeverityRatingAndNoSource(metric, canonicalURL) | ||
| ? `/vulnerabilities/${vulnerabilityIndex}/metrics/${metricIndex}/content/qualitative_severity_rating` | ||
| : null | ||
| ) | ||
| .filter((path) => path !== null) | ||
|
|
||
| if (!!invalidPaths) { | ||
| invalidPaths.forEach((path) => { | ||
| ctx.warnings.push({ | ||
| message: | ||
| 'a qualitative severity rating is used by the issuing party (as no "source" is given' + | ||
| ' or the source property equals to the canonical URL)', | ||
| instancePath: path, | ||
| }) | ||
| }) | ||
| } | ||
| }) | ||
|
|
||
| return ctx | ||
| } |
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,42 @@ | ||
| import Ajv from 'ajv/dist/jtd.js' | ||
|
|
||
| const ajv = new Ajv() | ||
|
|
||
| const referenceSchema = /** @type {const} */ ({ | ||
| additionalProperties: true, | ||
| properties: { | ||
| category: { type: 'string' }, | ||
| url: { type: 'string' }, | ||
| }, | ||
| }) | ||
| const validateReference = ajv.compile(referenceSchema) | ||
|
|
||
| /** | ||
| * Checks whether a reference contains a canonical URL | ||
| * It works for CSAF 2.0 and CSAF 2.1 | ||
| * A canonical URL fulfills all the following: | ||
| * - It has the category self | ||
| * - The url starts with https:// | ||
| * - The url ends with the valid filename for the CSAF document | ||
| * A filename must apply the following rules | ||
| * - The value /trackingId is converted into lower case | ||
| * - Any character sequence which is not part of one of the following groups MUST be replaced by a single underscore (_) | ||
| * Lower case ASCII letters (0x61 - 0x7A) | ||
| * digits (0x30 - 0x39) | ||
| * special characters: + (0x2B), - (0x2D) | ||
| * - The file extension .json MUST be appended. | ||
| * @param {{url?: string, category?: string}} reference | ||
| * @param {string} trackingId | ||
| * @return {boolean} | ||
| */ | ||
| export function isCanonicalUrl(reference, trackingId) { | ||
| return ( | ||
| validateReference(reference) && | ||
| reference.category === 'self' && | ||
| reference.url !== undefined && | ||
| reference.url.startsWith('https://') && | ||
| reference.url.endsWith( | ||
| trackingId.toLowerCase().replace(/[^+\-a-z0-9]+/g, '_') + '.json' | ||
| ) | ||
| ) | ||
| } |
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 |
|---|---|---|
|
|
@@ -52,7 +52,6 @@ const excluded = [ | |
| '6.2.44', | ||
| '6.2.45', | ||
| '6.2.46', | ||
| '6.2.47', | ||
| '6.3.12', | ||
| '6.3.13', | ||
| '6.3.14', | ||
|
|
||
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,44 @@ | ||
| import assert from 'node:assert' | ||
| import { recommendedTest_6_2_47 } from '../../csaf_2_1/recommendedTests.js' | ||
|
|
||
| describe('recommendedTest_6_2_47', function () { | ||
| it('only runs on relevant documents', function () { | ||
| assert.equal( | ||
| recommendedTest_6_2_47({ vulnerabilities: 'mydoc' }).warnings.length, | ||
| 0 | ||
| ) | ||
| }) | ||
|
|
||
| it('runs on references with empty category in reference', function () { | ||
| assert.equal( | ||
| recommendedTest_6_2_47({ | ||
| document: { | ||
| references: [ | ||
| { | ||
| category: 'self', | ||
| summary: 'The canonical URL for the CSAF document.', | ||
| url: 'https://example.com/.well-known/csaf/clear/2024/oasis_csaf_tc-csaf_2_1-2024-6-2-47-02.json', | ||
| }, | ||
| { url: 'https://some.other.url' }, | ||
| ], | ||
| tracking: { | ||
| id: 'OASIS_CSAF_TC-CSAF_2.1-2024-6-2-47-11', | ||
| }, | ||
| }, | ||
| vulnerabilities: [ | ||
| { | ||
| metrics: [ | ||
| { | ||
| content: { | ||
| qualitative_severity_rating: 'low', | ||
| }, | ||
| products: ['CSAFPID-9080700'], | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }).warnings.length, | ||
| 1 | ||
| ) | ||
| }) | ||
| }) |
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,60 @@ | ||
| import { isCanonicalUrl } from '../lib/shared/urlHelper.js' | ||
| import { expect } from 'chai' | ||
|
|
||
| describe('test url helper', function () { | ||
| it('test isCanonicalUrl', function () { | ||
| expect( | ||
| isCanonicalUrl( | ||
| { | ||
| url: 'https://example.com/.well-known/csaf/clear/2024/oasis_csaf_tc-csaf_2_1-2024-6-2-47-12.json', | ||
| category: 'self', | ||
| }, | ||
| 'OASIS_CSAF_TC-CSAF_2.1-2024-6-2-47-12' | ||
| ), | ||
| 'Valid canonical URL' | ||
| ).to.be.true | ||
|
|
||
| expect( | ||
| isCanonicalUrl( | ||
| { | ||
| url: 'https://example.com/.well-known/csaf/clear/2024/oasis_csaf_tc-csaf_2_1-2024-6-2-47-12.json', | ||
| category: 'not_self', | ||
| }, | ||
| 'OASIS_CSAF_TC-CSAF_2.1-2024-6-2-47-12' | ||
| ), | ||
| 'Invalid canonical URL - category not self' | ||
| ).to.be.false | ||
| }) | ||
|
|
||
| expect( | ||
| isCanonicalUrl( | ||
| { | ||
| url: 'http://example.com/.well-known/csaf/clear/2024/oasis_csaf_tc-csaf_2_1-2024-6-2-47-12.json', | ||
| category: 'self', | ||
| }, | ||
| 'OASIS_CSAF_TC-CSAF_2.1-2024-6-2-47-12' | ||
| ), | ||
| 'Invalid canonical URL - url starts not with https://' | ||
| ).to.be.false | ||
|
|
||
| expect( | ||
| isCanonicalUrl( | ||
| { | ||
| category: 'self', | ||
| }, | ||
| 'OASIS_CSAF_TC-CSAF_2.1-2024-6-2-47-12' | ||
| ), | ||
| 'Invalid canonical URL - no URL ' | ||
| ).to.be.false | ||
|
|
||
| expect( | ||
| isCanonicalUrl( | ||
| { | ||
| url: 'https://example.com/.well-known/csaf/clear/2024/oasis_csaf_tc-csaf_2_1-2024-6-2-47-12_invalid.json', | ||
| category: 'self', | ||
| }, | ||
| 'OASIS_CSAF_TC-CSAF_2.1-2024-6-2-47-12' | ||
| ), | ||
| 'Valid canonical URL - URL ends not with valid filename' | ||
| ).to.be.false | ||
| }) |
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.
Uh oh!
There was an error while loading. Please reload this page.