Skip to content

Commit 607ecba

Browse files
authoredNov 20, 2020
feat: retrieve memberships of a cwe by id (#7)
1 parent 60de4af commit 607ecba

File tree

5 files changed

+67
-4
lines changed

5 files changed

+67
-4
lines changed
 

‎__tests__/CweManager.test.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const { CweManager } = require('../index')
22

33
describe('Cwe Manager', () => {
4-
describe('Cwe Manager supports instnatiation with custom data', () => {
4+
describe('Cwe Manager supports instantiation with custom data', () => {
55
test('Cwe Manager instnatiated with custom hierarchy', () => {
66
const cweManager = new CweManager({
77
cweHierarchy: [{ weaknessId: '31337', parentId: '31338' }]
@@ -50,4 +50,18 @@ describe('Cwe Manager', () => {
5050
'A set of CWE IDs that are childs of another CWE ID should return true (one parent for all)'
5151
)
5252
})
53+
54+
describe('Cwe Manager Memberships', () => {
55+
test('A CWE ID that has no memberships should return null', () => {
56+
const cweManager = new CweManager()
57+
const result = cweManager.getMemberships({ weaknessId: 'notfoundid' })
58+
expect(result).toBe(null)
59+
})
60+
61+
test('A CWE ID with memberships should return an array of ids', () => {
62+
const cweManager = new CweManager()
63+
const result = cweManager.getMemberships({ weaknessId: '778' })
64+
expect(result).toStrictEqual(['1009', '1036', '1210', '1308'])
65+
})
66+
})
5367
})

‎build/build.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const RAW_INPUT_XML_FILENAME = 'cwe-archive.xml'
1313
const RAW_OUTPUT_JSON_FILENAME = 'cwe-archive.json'
1414
const OUTPUT_JSON_DICT_FILENAME = 'cwe-dictionary.json'
1515
const OUTPUT_JSON_HIERARCHY_FILENAME = 'cwe-hierarchy.json'
16+
const OUTPUT_JSON_MEMBERSHIPS_FILENAME = 'cwe-memberships.json'
1617
const ARCHIVE_DOWNLOAD_OPTIONS = {
1718
hostname: 'cwe.mitre.org',
1819
port: 443,
@@ -36,7 +37,9 @@ updateArchive()
3637
jsonData: rawJsonCweArchive
3738
})
3839

39-
const { cweDictionary, cweHierarchy } = createCweDictionary({ cweArchive: rawJsonCweArchive })
40+
const { cweDictionary, cweHierarchy, cweMemberships } = createCweDictionary({
41+
cweArchive: rawJsonCweArchive
42+
})
4043

4144
writeJsonToFile({
4245
jsonFilepath: path.join(__dirname, '..', 'raw', OUTPUT_JSON_DICT_FILENAME),
@@ -48,6 +51,11 @@ updateArchive()
4851
jsonData: cweHierarchy
4952
})
5053

54+
writeJsonToFile({
55+
jsonFilepath: path.join(__dirname, '..', 'raw', OUTPUT_JSON_MEMBERSHIPS_FILENAME),
56+
jsonData: cweMemberships
57+
})
58+
5159
debug('finished')
5260
})
5361
.catch(console.error)

‎build/xmlParser.js

+25-1
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,23 @@ const debug = require('debug')('cwe-sdk:build')
88

99
function createCweDictionary({ cweArchive }) {
1010
const allWeaknesses = cweArchive.Weakness_Catalog.Weaknesses.Weakness
11+
const allCategories = cweArchive.Weakness_Catalog.Categories.Category
12+
const membershipMap = new Map()
13+
allCategories
14+
.filter(category => category.Relationships)
15+
.forEach(category => {
16+
const memberIds = Array.from(category.Relationships.Has_Member).map(
17+
member => member.attr['@_CWE_ID']
18+
)
19+
memberIds.forEach(memberId => {
20+
const current = membershipMap.get(memberId) || []
21+
current.push(category.attr['@_ID'])
22+
membershipMap.set(memberId, current)
23+
})
24+
})
1125
const cweDictionary = {}
1226
const cweHierarchy = []
27+
const cweMemberships = []
1328

1429
allWeaknesses.forEach(function(weakness) {
1530
const weaknessId = weakness['attr']['@_ID']
@@ -37,12 +52,21 @@ function createCweDictionary({ cweArchive }) {
3752
})
3853
}
3954
}
55+
56+
const weaknessMembership = membershipMap.get(weaknessId)
57+
if (weaknessMembership) {
58+
cweMemberships.push({
59+
weaknessId,
60+
memberships: weaknessMembership
61+
})
62+
}
4063
}
4164
})
4265

4366
return {
4467
cweDictionary,
45-
cweHierarchy
68+
cweHierarchy,
69+
cweMemberships
4670
}
4771
}
4872

‎raw/cwe-memberships.json

+1
Large diffs are not rendered by default.

‎src/CweManager.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
const CWE_HIERARCHY = require('../raw/cwe-hierarchy.json')
44
const CWE_DICTIONARY = require('../raw/cwe-dictionary.json')
5+
const CWE_MEMBERSHIPS = require('../raw/cwe-memberships.json')
56
const debug = require('debug')('cwe-sdk:manager')
67

78
module.exports = class CweManager {
8-
constructor({ cweHierarchy = null, cweDictionary = null } = {}) {
9+
constructor({ cweHierarchy = null, cweDictionary = null, cweMemberships = null } = {}) {
910
if (cweHierarchy) {
1011
debug('manager received cweHierarchy to be used')
1112
this.cweHierarchy = cweHierarchy
@@ -19,6 +20,21 @@ module.exports = class CweManager {
1920
} else {
2021
this.cweDictionary = CWE_DICTIONARY
2122
}
23+
24+
if (cweMemberships) {
25+
debug('manager received cweMemberships to be used')
26+
this.cweMemberships = cweMemberships
27+
} else {
28+
this.cweMemberships = CWE_MEMBERSHIPS
29+
}
30+
}
31+
32+
getMemberships({ weaknessId }) {
33+
const weakness = this.cweMemberships.find(weakness => weakness.weaknessId === weaknessId)
34+
if (!weakness) {
35+
return null
36+
}
37+
return weakness.memberships
2238
}
2339

2440
isChildOf({ indirect = false, weaknessId, parentId }) {

0 commit comments

Comments
 (0)
Please sign in to comment.