-
Notifications
You must be signed in to change notification settings - Fork 9
fix(CSAF2.1): #197 fix mandatoryTest_6_1_3.js #573
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
7
commits into
main
Choose a base branch
from
fix/csaf-2.1_mandatory_test_6.1.3
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
7 commits
Select commit
Hold shift + click to select a range
9389d50
fix(CSAF2.1): fix mandatoryTest_6_1_3.js
bendo-eXX 38aa8d1
Merge branch 'main' into fix/csaf-2.1_mandatory_test_6.1.3
bendo-eXX 443ca57
fix(CSAF2.1): add test case for circular definition
bendo-eXX 8795973
Merge branch 'main' into fix/csaf-2.1_mandatory_test_6.1.3
bendo-eXX 53837ba
fix(CSAF2.1): update import
bendo-eXX e7fa125
fix(CSAF2.1): improve TestCoverage
bendo-eXX 397783f
Merge branch 'main' into fix/csaf-2.1_mandatory_test_6.1.3
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,201 @@ | ||
| import { Ajv } from 'ajv/dist/jtd.js' | ||
|
|
||
| const ajv = new Ajv() | ||
|
|
||
| const fullProductNameSchema = /** @type {const} */ ({ | ||
| additionalProperties: true, | ||
| properties: { | ||
| name: { type: 'string' }, | ||
| product_id: { type: 'string' }, | ||
| }, | ||
| }) | ||
|
|
||
| const subpathSchema = /** @type {const} */ ({ | ||
| additionalProperties: false, | ||
| optionalProperties: { | ||
| category: { type: 'string' }, | ||
| next_product_reference: { type: 'string' }, | ||
| }, | ||
| }) | ||
|
|
||
| const productPathSchema = /** @type {const} */ ({ | ||
| additionalProperties: false, | ||
| properties: { | ||
| beginning_product_reference: { type: 'string' }, | ||
| full_product_name: fullProductNameSchema, | ||
| subpaths: { | ||
| elements: subpathSchema, | ||
| }, | ||
| }, | ||
| }) | ||
|
|
||
| /* | ||
| 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, | ||
| optionalProperties: { | ||
| product_tree: { | ||
| additionalProperties: true, | ||
| optionalProperties: { | ||
| product_paths: { | ||
| elements: productPathSchema, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
|
|
||
| const validate = ajv.compile(inputSchema) | ||
|
|
||
| /** | ||
| * @typedef {import('ajv/dist/core.js').JTDDataType<typeof productPathSchema>} ProductPath | ||
| * @typedef {{dependencies: Array<{ productId: string, pathIndex: number, type: "main" | "sub", subIndex: number | null }> }} GraphNode | ||
| * @typedef {Map<string, GraphNode>} DependencyGraph | ||
| * @typedef {{cycleProductId: string, pathIndex: number, type: string, subIndex: number | null } | null} CircularDependency | ||
| */ | ||
|
|
||
| /** | ||
| * This implements the mandatory test 6.1.3 of the CSAF 2.1 standard. | ||
| * | ||
| * @param {any} doc | ||
| */ | ||
| export function mandatoryTest_6_1_3(doc) { | ||
| const ctx = { | ||
| errors: | ||
| /** @type {Array<{ instancePath: string; message: string }>} */ ([]), | ||
| isValid: true, | ||
| } | ||
|
|
||
| if (!validate(doc)) { | ||
| return ctx | ||
| } | ||
|
|
||
| if (!Array.isArray(doc.product_tree?.product_paths)) { | ||
| return ctx | ||
| } | ||
|
|
||
| const productPaths = doc.product_tree.product_paths | ||
| const graph = buildDependencyGraph(productPaths) | ||
|
|
||
| const visited = new Set() | ||
| for (const [productId] of graph) { | ||
| if (!visited.has(productId)) { | ||
| /** @type {CircularDependency} */ | ||
| const circle = hasCircle(productId, graph, visited) | ||
| if (circle) { | ||
| ctx.isValid = false | ||
| const instancePath = | ||
| circle.type === 'sub' | ||
| ? `/product_tree/product_paths/${circle.pathIndex}/subpaths/${circle.subIndex}/next_product_reference` | ||
| : `/product_tree/product_paths/${circle.pathIndex}/full_product_name/product_id` | ||
|
|
||
| ctx.errors.push({ | ||
| instancePath, | ||
| message: `circular reference detected for product_id: ${circle.cycleProductId}`, | ||
| }) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return ctx | ||
| } | ||
|
|
||
| /** | ||
| * Adds a directed edge from `from` to `to` in the graph. The edge is annotated with the path index and type. | ||
| * @param {DependencyGraph} graph | ||
| * @param {string} from | ||
| * @param {string} to | ||
| * @param {number} pathIndex | ||
| * @param {"main" | "sub"} type the 'type' specifies is it from the main part of a product_path or from a subpath | ||
| * @param {number | null} subIndex | ||
| */ | ||
| function addEdge(graph, from, to, pathIndex, type, subIndex = null) { | ||
| if (!graph.has(from)) { | ||
| graph.set(from, { dependencies: [] }) | ||
| } | ||
|
|
||
| const deps = graph.get(from)?.dependencies | ||
|
|
||
| const exists = deps?.some( | ||
| (d) => d.productId === to && d.pathIndex === pathIndex | ||
| ) | ||
|
|
||
| if (!exists) { | ||
| deps?.push({ productId: to, pathIndex, type, subIndex }) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Builds a dependency graph from the given product paths. | ||
| * @param {ProductPath[]} productPaths | ||
| */ | ||
| function buildDependencyGraph(productPaths) { | ||
| /** @type {DependencyGraph} */ | ||
| const graph = new Map() | ||
|
|
||
| productPaths.forEach((path, index) => { | ||
| const beginningProductReference = path.beginning_product_reference | ||
| const productId = path.full_product_name.product_id | ||
|
|
||
| if (!beginningProductReference || !productId) return | ||
|
|
||
| addEdge(graph, productId, beginningProductReference, index, 'main') | ||
|
|
||
| path.subpaths?.forEach((sub, subIndex) => { | ||
| if (sub.next_product_reference) { | ||
| addEdge( | ||
| graph, | ||
| productId, | ||
| sub.next_product_reference, | ||
| index, | ||
| 'sub', | ||
| subIndex | ||
| ) | ||
| } | ||
| }) | ||
| }) | ||
| return graph | ||
| } | ||
|
|
||
| /** | ||
| * Detects if there is a circular reference starting from the given productId in the dependency graph. | ||
| * @param {string} productId | ||
| * @param {DependencyGraph} graph | ||
| * @param {Set<string>} visited | ||
| * @param {Set<string>} recursionStack | ||
| * @returns {CircularDependency} | ||
| */ | ||
| function hasCircle( | ||
| productId, | ||
| graph, | ||
| visited = new Set(), | ||
| recursionStack = new Set() | ||
| ) { | ||
| if (visited.has(productId)) return null | ||
|
bendo-eXX marked this conversation as resolved.
|
||
|
|
||
| visited.add(productId) | ||
| recursionStack.add(productId) | ||
| const node = graph.get(productId) | ||
|
|
||
| if (node) { | ||
|
bendo-eXX marked this conversation as resolved.
|
||
| for (const dep of node.dependencies) { | ||
| if (recursionStack.has(dep.productId)) { | ||
| return { | ||
| cycleProductId: dep.productId, | ||
| pathIndex: dep.pathIndex, | ||
| type: dep.type, | ||
| subIndex: dep.subIndex ?? null, | ||
| } | ||
| } | ||
| const result = hasCircle(dep.productId, graph, visited, recursionStack) | ||
| if (result) return result | ||
| } | ||
| } | ||
|
|
||
| recursionStack.delete(productId) | ||
| return null | ||
| } | ||
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,130 @@ | ||
| import assert from 'node:assert/strict' | ||
| import { mandatoryTest_6_1_3 } from '../../csaf_2_1/mandatoryTests/mandatoryTest_6_1_3.js' | ||
|
|
||
| describe('mandatoryTest_6_1_3 (CSAF 2.1)', function () { | ||
| it('only runs on relevant documents', function () { | ||
| assert.equal(mandatoryTest_6_1_3({ document: 'mydoc' }).isValid, true) | ||
| }) | ||
|
|
||
| it('returns valid when product_paths is not an array', function () { | ||
| const doc = mandatoryTest_6_1_3({ | ||
| document: 'mydoc', | ||
| product_tree: { | ||
| product_paths: 'not_an_array', | ||
| }, | ||
| }) | ||
| assert.equal(doc.isValid, true) | ||
| assert.equal(doc.errors.length, 0) | ||
| }) | ||
|
|
||
| it('detects a circular definition across product paths', function () { | ||
| const doc = mandatoryTest_6_1_3({ | ||
| product_tree: { | ||
| product_paths: [ | ||
| { | ||
| beginning_product_reference: 'A', | ||
| full_product_name: { | ||
| name: 'B', | ||
| product_id: 'B', | ||
| }, | ||
| subpaths: [ | ||
| { | ||
| category: 'installed_on', | ||
| next_product_reference: 'C', | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| beginning_product_reference: 'B', | ||
| full_product_name: { | ||
| name: 'C', | ||
| product_id: 'C', | ||
| }, | ||
| subpaths: [ | ||
| { | ||
| category: 'installed_with', | ||
| next_product_reference: 'D', | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }, | ||
| }) | ||
|
|
||
| assert.equal(doc.isValid, false) | ||
| assert.equal(doc.errors.length, 1) | ||
| assert.equal( | ||
| doc.errors[0].instancePath, | ||
| '/product_tree/product_paths/1/full_product_name/product_id' | ||
| ) | ||
| }) | ||
|
|
||
| it('returns valid when beginning_product_reference or product_id is missing', function () { | ||
| const doc = mandatoryTest_6_1_3({ | ||
| product_tree: { | ||
| product_paths: [ | ||
| { | ||
| beginning_product_reference: '', | ||
| full_product_name: { | ||
| name: 'A', | ||
| product_id: 'A', | ||
| }, | ||
| subpaths: [], | ||
| }, | ||
| { | ||
| beginning_product_reference: 'A', | ||
| full_product_name: { | ||
| name: 'B', | ||
| product_id: '', | ||
| }, | ||
| subpaths: [], | ||
| }, | ||
| ], | ||
| }, | ||
| }) | ||
| assert.equal(doc.isValid, true) | ||
| assert.equal(doc.errors.length, 0) | ||
| }) | ||
|
|
||
| it('returns valid when two paths share a common dependency', function () { | ||
| const doc = mandatoryTest_6_1_3({ | ||
| product_tree: { | ||
| product_paths: [ | ||
| { | ||
| beginning_product_reference: 'A', | ||
| full_product_name: { name: 'B', product_id: 'B' }, | ||
| subpaths: [], | ||
| }, | ||
| { | ||
| beginning_product_reference: 'A', | ||
| full_product_name: { name: 'C', product_id: 'C' }, | ||
| subpaths: [], | ||
| }, | ||
| ], | ||
| }, | ||
| }) | ||
| assert.equal(doc.isValid, true) | ||
| assert.equal(doc.errors.length, 0) | ||
| }) | ||
|
|
||
| it('does not report duplicate edges as circular (same target in beginning_product_reference and subpath)', function () { | ||
| const doc = mandatoryTest_6_1_3({ | ||
| product_tree: { | ||
| product_paths: [ | ||
| { | ||
| beginning_product_reference: 'A', | ||
| full_product_name: { name: 'B', product_id: 'B' }, | ||
| subpaths: [ | ||
| { | ||
| category: 'installed_on', | ||
| next_product_reference: 'A', | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }, | ||
| }) | ||
| assert.equal(doc.isValid, true) | ||
| assert.equal(doc.errors.length, 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
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.
Uh oh!
There was an error while loading. Please reload this page.