Skip to content

Commit

Permalink
feat: add mandatory test 6.1.40
Browse files Browse the repository at this point in the history
  • Loading branch information
domachine committed Mar 6, 2025
1 parent 4828789 commit b7db4eb
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 1 deletion.
1 change: 1 addition & 0 deletions csaf_2_1/mandatoryTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ export {
mandatoryTest_6_1_33,
} from '../mandatoryTests.js'
export { mandatoryTest_6_1_34 } from './mandatoryTests/mandatoryTest_6_1_34.js'
export { mandatoryTest_6_1_40 } from './mandatoryTests/mandatoryTest_6_1_40.js'
92 changes: 92 additions & 0 deletions csaf_2_1/mandatoryTests/mandatoryTest_6_1_40.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import Ajv from 'ajv/dist/jtd.js'

/**
* The max uuid to check the sharing_group.id for.
*/
const MAX_UUID = 'ffffffff-ffff-ffff-ffff-ffffffffffff'

/**
* The nil uuid to check the sharing_group.id for.
*/
const NIL_UUID = '00000000-0000-0000-0000-000000000000'

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: {
distribution: {
additionalProperties: true,
optionalProperties: {
sharing_group: {
additionalProperties: true,
properties: {
id: {
type: 'string',
},
},
},
tlp: {
additionalProperties: true,
optionalProperties: {
label: { type: 'string' },
},
},
},
},
},
},
},
})

const validate = ajv.compile(inputSchema)

/**
* This implements the mandatory test 6.1.40 of the CSAF 2.1 standard.
*
* @param {any} doc
*/
export function mandatoryTest_6_1_40(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

const sharingGroupId = doc.document.distribution.sharing_group?.id
const sharingGroupName = doc.document.distribution.sharing_group?.name

if (sharingGroupName === 'Public' && sharingGroupId !== MAX_UUID) {
ctx.isValid = false
ctx.errors.push({
instancePath: '/document/distribution/sharing_group/id',
message: `the sharing group name is "Public" but it does not use the max UUID`,
})
} else if (
sharingGroupName === 'No sharing allowed' &&
sharingGroupId !== NIL_UUID
) {
ctx.isValid = false
ctx.errors.push({
instancePath: '/document/distribution/sharing_group/id',
message: `the sharing group name is "No sharing allowed" but it does not use the nil UUID`,
})
}

return ctx
}
8 changes: 8 additions & 0 deletions tests/csaf_2_1/mandatoryTest_6_1_40.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import assert from 'node:assert/strict'
import { mandatoryTest_6_1_40 } from '../../csaf_2_1/mandatoryTests/mandatoryTest_6_1_40.js'

describe('mandatoryTest_6_1_40', function () {
it('only runs on relevant documents', function () {
assert.equal(mandatoryTest_6_1_40({ document: 'mydoc' }).isValid, true)
})
})
1 change: 0 additions & 1 deletion tests/csaf_2_1/oasis.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ const excluded = [
'6.1.37',
'6.1.38',
'6.1.39',
'6.1.40',
'6.1.41',
'6.1.42',
'6.2.6',
Expand Down

0 comments on commit b7db4eb

Please sign in to comment.