Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,6 @@ The following tests are not yet implemented and therefore missing:
- Mandatory Test 6.1.26
- Mandatory Test 6.1.27.13
- Mandatory Test 6.1.27.18
- Mandatory Test 6.1.42
- Mandatory Test 6.1.44
- Mandatory Test 6.1.45
- Mandatory Test 6.1.46
Expand Down Expand Up @@ -436,6 +435,7 @@ export const mandatoryTest_6_1_38: DocumentTest
export const mandatoryTest_6_1_39: DocumentTest
export const mandatoryTest_6_1_40: DocumentTest
export const mandatoryTest_6_1_41: DocumentTest
export const mandatoryTest_6_1_42: DocumentTest
export const mandatoryTest_6_1_43: DocumentTest
```

Expand Down
1 change: 1 addition & 0 deletions csaf_2_1/mandatoryTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,5 @@ export { mandatoryTest_6_1_38 } from './mandatoryTests/mandatoryTests_6_1_38.js'
export { mandatoryTest_6_1_39 } from './mandatoryTests/mandatoryTest_6_1_39.js'
export { mandatoryTest_6_1_40 } from './mandatoryTests/mandatoryTest_6_1_40.js'
export { mandatoryTest_6_1_41 } from './mandatoryTests/mandatoryTest_6_1_41.js'
export { mandatoryTest_6_1_42 } from './mandatoryTests/mandatoryTest_6_1_42.js'
export { mandatoryTest_6_1_43 } from './mandatoryTests/mandatoryTest_6_1_43.js'
238 changes: 238 additions & 0 deletions csaf_2_1/mandatoryTests/mandatoryTest_6_1_42.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
import { PackageURL } from 'packageurl-js'
import Ajv from 'ajv/dist/jtd.js'

const ajv = new Ajv()

const branchSchema = /** @type {const} */ ({
additionalProperties: true,
optionalProperties: {
branches: {
elements: {
additionalProperties: true,
properties: {},
},
},
product: {
additionalProperties: true,
optionalProperties: {
product_identification_helper: {
additionalProperties: true,
optionalProperties: {
purls: { elements: { type: 'string' } },
},
},
},
},
},
})

const validateBranch = ajv.compile(branchSchema)

const fullProductNameSchema = /** @type {const} */ ({
additionalProperties: true,
optionalProperties: {
product_identification_helper: {
additionalProperties: true,
optionalProperties: {
purls: { elements: { type: 'string' } },
},
},
},
})

/*
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: {
branches: {
elements: branchSchema,
},
full_product_names: {
elements: fullProductNameSchema,
},
relationships: {
elements: {
additionalProperties: true,
optionalProperties: {
full_product_name: fullProductNameSchema,
},
},
},
},
},
},
})

const validate = ajv.compile(inputSchema)

/**
* @typedef {import('ajv/dist/core').JTDDataType<typeof branchSchema>} Branch
* @typedef {import('ajv/dist/core').JTDDataType<typeof fullProductNameSchema>} FullProductName
*/

/**
*
* @param {PackageURL | null} firstPurl
* @param {PackageURL | null} secondPurl
* @return {Array<string>} the parts of the PURLS that differ
*/
function purlPartsThatDifferExceptQualifiers(firstPurl, secondPurl) {
/** @type {Array<string>}*/
const partsThatDiffer = []

if (firstPurl && secondPurl) {
if (firstPurl.type !== secondPurl.type) {
partsThatDiffer.push('type')
}
if (firstPurl.namespace !== secondPurl.namespace) {
partsThatDiffer.push('namespace')
}
if (firstPurl.name !== secondPurl.name) {
partsThatDiffer.push('name')
}
if (firstPurl.version !== secondPurl.version) {
partsThatDiffer.push('version')
}
}
return partsThatDiffer
}

/**
* Validates all given PURLs and check whether the PURLs
* differ only in qualifiers to the first URL
*
* @param {Array<string> | undefined} purls PURLs to check
* @return {Array<{index:number, purlParts: Array<string> }>} indexes and parts of the PURLs that differ
*/
export function checkPurls(purls) {
/** @type {Array<{index:number, purlParts: Array<string> }>} */
const invalidPurls = []
if (purls) {
/** @type {Array<PackageURL | null>} */
const packageUrls = purls.map((purl) => {
try {
return PackageURL.fromString(purl)
} catch (e) {
// ignore, tested in CSAF 2.1 test 6.1.13
return null
}
})

/**
* @type {Array<PackageURL>}
*/
if (packageUrls.length > 1) {
const firstPurl = packageUrls[0]
for (let i = 1; i < packageUrls.length; i++) {
/** @type {Array<string>}*/
const purlParts = purlPartsThatDifferExceptQualifiers(
firstPurl,
packageUrls[i]
)
if (purlParts.length > 0) {
invalidPurls.push({ index: i, purlParts: purlParts })
}
}
}
}
return invalidPurls
}

/**
* For each product_identification_helper object containing multiple purls,
* it MUST be tested that the purls only differ in their qualifiers.
*
* @param {unknown} doc
*/
export function mandatoryTest_6_1_42(doc) {
/*
The `ctx` variable holds the state that is accumulated during the test ran and is
finally returned by the function.
*/
const ctx = {
errors:
/** @type {Array<{ instancePath: string; message: string }>} */ ([]),
isValid: true,
}

if (!validate(doc)) {
return ctx
}

doc.product_tree?.branches?.forEach((branch, index) => {
checkBranch(`/product_tree/branches/${index}`, branch)
})

doc.product_tree?.full_product_names?.forEach((fullProduceName, index) => {
checkFullProductName(
`/product_tree/full_product_names/${index}`,
fullProduceName
)
})

doc.product_tree?.relationships?.forEach((relationship, index) => {
const fullProductName = relationship.full_product_name
if (fullProductName) {
checkFullProductName(
`/product_tree/relationships/${index}/full_product_name`,
fullProductName
)
}
})

return ctx

/**
* Check whether the PURLs only differ in their qualifiers for a full product name.
*
* @param {string} prefix The instance path prefix of the "full product name". It is
* used to generate error messages.
* @param {FullProductName} fullProductName The "full product name" object.
*/
function checkFullProductName(prefix, fullProductName) {
const invalidPurls = checkPurls(
fullProductName.product_identification_helper?.purls
)
invalidPurls.forEach((invalidPurl) => {
ctx.isValid = false
ctx.errors.push({
instancePath: `${prefix}/product_identification_helper/purls/${invalidPurl.index}`,
message: `the PURL differs from the first PURL in the following part(s): ${invalidPurl.purlParts.join()}`,
})
})
}

/**
* Check whether the PURLs only differ in their qualifiers for the given branch object
* and its branch children.
*
* @param {string} prefix The instance path prefix of the "branch". It is
* used to generate error messages.
* @param {Branch} branch The "branch" object.
*/
function checkBranch(prefix, branch) {
const invalidPurls = checkPurls(
branch.product?.product_identification_helper?.purls
)
invalidPurls.forEach((invalidPurl) => {
ctx.isValid = false
ctx.errors.push({
instancePath: `${prefix}/product/product_identification_helper/purls/${invalidPurl.index}`,
message: `the PURL differs from the first PURL in the following parts: ${invalidPurl.purlParts.join()}`,
})
})
branch.branches?.forEach((branch, index) => {
if (validateBranch(branch)) {
checkBranch(`${prefix}/branches/${index}`, branch)
}
})
}
}
65 changes: 65 additions & 0 deletions tests/csaf_2_1/mandatoryTest_6_1_42.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import assert from 'node:assert/strict'
import { expect } from 'chai'

import {
mandatoryTest_6_1_42,
checkPurls,
} from '../../csaf_2_1/mandatoryTests/mandatoryTest_6_1_42.js'

describe('mandatoryTest_6_1_42', function () {
it('only runs on relevant documents', function () {
assert.equal(mandatoryTest_6_1_42({ product_tree: 'mydoc' }).isValid, true)
})

it('test checkPurls', function () {
expect(checkPurls([]), 'empty purl array').to.eql([])
expect(checkPurls(['invalid']), 'invalid PURL').to.eql([])
expect(
checkPurls([
'pkg:golang/google.golang.org/genproto#googleapis/api/annotations',
'pkg:golang/google.golang.org/genproto#googleapis/api/test',
]),
'only change in subpath'
).to.eql([])
expect(
checkPurls([
'pkg:deb/debian/[email protected]?arch=i386&distro=jessie',
'pkg:deb/debian/[email protected]?arch=i386&distro=buster',
]),
'only change in qualifier'
).to.eql([])
expect(
checkPurls([
'pkg:golang/google.golang.org/genproto#googleapis/api/annotations',
'pkg:golang/google.golang.com/genproto#googleapis/api/annotations',
]),
'change in namespace'
).to.eql([{ index: 1, purlParts: ['namespace'] }])
expect(
checkPurls([
'pkg:golang/google.golang.org/genproto#googleapis/api/annotations',
'pkg:npm/google.golang.org/genproto#googleapis/api/annotations',
]),
'change in type'
).to.eql([{ index: 1, purlParts: ['type'] }])
expect(
checkPurls([
'pkg:golang/google.golang.org/genproto#googleapis/api/annotations',
'pkg:golang/google.golang.org/genproto2#googleapis/api/annotations',
]),
'change in name'
).to.eql([{ index: 1, purlParts: ['name'] }])
expect(
checkPurls([
'pkg:npm/%40angular/[email protected]',
'invalid',
'pkg:npm/%40angular/[email protected]',
'pkg:golang/%40angular/[email protected]',
]),
'change in version and invalid PURL'
).to.eql([
{ index: 2, purlParts: ['version'] },
{ index: 3, purlParts: ['type', 'version'] },
])
})
})
1 change: 0 additions & 1 deletion tests/csaf_2_1/oasis.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ const excluded = [
'6.1.27.13',
'6.1.27.18',
'6.1.37',
'6.1.42',
'6.1.44',
'6.1.45',
'6.1.46',
Expand Down