-
Notifications
You must be signed in to change notification settings - Fork 9
feat(CSAF2.1): add mandatoryTest_6_1_60_2.js #668
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
bendo-eXX
wants to merge
6
commits into
main
Choose a base branch
from
fest/197-csaf-2.1-mandatory-test-6.1.60.2
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
6 commits
Select commit
Hold shift + click to select a range
b38b34e
feat(CSAF2.1): add mandatoryTest_6_1_60.2.js
bendo-eXX 6afb8c4
feat(CSAF2.1): add dynamic ajv loadSchema
bendo-eXX 94ba900
Merge remote-tracking branch 'origin/main' into fest/197-csaf-2.1-man…
bendo-eXX 6bbbf29
feat(CSAF2.1): remove undici
bendo-eXX 9329537
Merge branch 'main' into fest/197-csaf-2.1-mandatory-test-6.1.60.2
bendo-eXX d5db1ac
feat(csaf2.1): update 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
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,67 @@ | ||
| import { walkPath } from '../../lib/walkPaths.js' | ||
| import csafAjv from '../csafAjv.js' | ||
|
|
||
| const X_EXTENSIONS_PATHS /** @type {string[]} */ = [ | ||
| '/document/x_extensions[]', | ||
| '/product_tree/branches[*]/product/x_extensions[]', | ||
| '/product_tree/full_product_names[]/x_extensions[]', | ||
| '/product_tree/product_paths[]/full_product_name/x_extensions[]', | ||
| '/vulnerabilities[]/metrics[]/content/x_extensions[]', | ||
| '/vulnerabilities[]/x_extensions[]', | ||
| '/x_extensions[]', | ||
| ] | ||
|
|
||
| /** | ||
| * This implements the mandatory test 6.1.60.2 of the CSAF 2.1 standard. | ||
| * | ||
| * @param {unknown} doc | ||
| */ | ||
| export async function mandatoryTest_6_1_60_2(doc) { | ||
| const ctx = { | ||
| errors: | ||
| /** @type {Array<{ instancePath: string; message: string }>} */ ([]), | ||
| warnings: | ||
| /** @type {Array<{ instancePath: string; message: string }>} */ ([]), | ||
| isValid: true, | ||
| } | ||
|
|
||
| for (const path of X_EXTENSIONS_PATHS) { | ||
| await walkPath(doc, path, async (instancePath, value) => { | ||
| const schemaUrl = | ||
| value && typeof value === 'object' && '$schema' in value | ||
| ? /** @type {{ $schema: unknown }} */ (value).$schema | ||
| : undefined | ||
|
|
||
| if (typeof schemaUrl !== 'string') return | ||
|
|
||
| let validateDeclaredSchema = csafAjv.getSchema(schemaUrl) | ||
| if (typeof validateDeclaredSchema !== 'function') { | ||
| try { | ||
| validateDeclaredSchema = await csafAjv.compileAsync({ | ||
| $ref: schemaUrl, | ||
| }) | ||
| } catch { | ||
| ctx.warnings.push({ | ||
| instancePath, | ||
| message: `declared CSAF Extension Schema "${schemaUrl}" is not supported and could not be validated`, | ||
| }) | ||
| return | ||
| } | ||
| } | ||
|
|
||
| if (!validateDeclaredSchema(value)) { | ||
| ctx.isValid = false | ||
| validateDeclaredSchema.errors?.forEach((err) => { | ||
| ctx.errors.push({ | ||
| instancePath: `${instancePath}${err.instancePath}`, | ||
| message: | ||
| err.message ?? | ||
| 'invalid according to declared CSAF Extension Schema', | ||
| }) | ||
| }) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import { mandatoryTest_6_1_60_2 } from '../../csaf_2_1/mandatoryTests.js' | ||
|
|
||
| describe('mandatoryTest_6_1_60_2', function () { | ||
| it('reports a warning when the declared schema is unknown', async function () { | ||
| const result = await mandatoryTest_6_1_60_2({ | ||
| x_extensions: [ | ||
| { | ||
| $schema: 'https://example.com/csaf/extension/unknown_1.0.0.json', | ||
| category: 'supplementary', | ||
| content: { note: 'unknown schema' }, | ||
| critical: false, | ||
| }, | ||
| ], | ||
| }) | ||
|
|
||
| expect(result.isValid).to.equal(true) | ||
| expect(result.errors.length).to.equal(0) | ||
| expect(result.warnings.length).to.equal(1) | ||
| expect(result.warnings[0].instancePath).to.equal('/x_extensions/0') | ||
| expect(result.warnings[0].message).to.match( | ||
| /https:\/\/example\.com\/csaf\/extension\/unknown_1\.0\.0\.json/ | ||
| ) | ||
| }) | ||
|
|
||
| it('skips extensions that have no $schema property', async function () { | ||
| const result = await mandatoryTest_6_1_60_2({ | ||
| x_extensions: [{ category: 'supplementary', content: {} }], | ||
| }) | ||
|
|
||
| expect(result.isValid).to.equal(true) | ||
| expect(result.errors.length).to.equal(0) | ||
| expect(result.warnings.length).to.equal(0) | ||
| }) | ||
| }) |
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,41 @@ | ||
| import { readFile } from 'node:fs/promises' | ||
| import { MockAgent } from 'undici' | ||
|
|
||
| // Pulled out of oasis.js because undici has no browser build - keeping it | ||
| // separate lets oasis.js load this only via dynamic import when running under | ||
| // Node. | ||
|
|
||
| const extensionDataBaseUrl = new URL( | ||
| '../../csaf/csaf_2.1/test/extension/data/valid/', | ||
| import.meta.url | ||
| ) | ||
|
|
||
| /** | ||
| * Mocks GitHub raw-content requests for extension schemas via csafAjv's | ||
| * dynamic loadSchema mechanism, so the test doesn't need real network access. | ||
| * | ||
| * @returns {Promise<MockAgent>} | ||
| */ | ||
| export async function extensionSchemaMockAgent() { | ||
| const mockAgent = new MockAgent() | ||
| mockAgent.disableNetConnect() | ||
|
|
||
| const pool = mockAgent.get('https://raw.githubusercontent.com') | ||
| for (const name of ['documentation-11', 'documentation-12']) { | ||
| const content = await readFile( | ||
| new URL(`${name}/${name}-content_1.0.0.json`, extensionDataBaseUrl), | ||
| 'utf-8' | ||
| ) | ||
| pool | ||
| .intercept({ | ||
| method: 'GET', | ||
| path: `/oasis-tcs/csaf/refs/heads/master/csaf_2.1/extension/data/valid/${name}/${name}-content_1.0.0.json`, | ||
| }) | ||
| .reply(200, content, { | ||
| headers: { 'content-type': 'application/json' }, | ||
| }) | ||
| .persist() | ||
| } | ||
|
|
||
| return mockAgent | ||
| } |
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.
This cast is not necessary.