Skip to content

Commit 0623b45

Browse files
Copilotfelickz
andcommitted
Fix CWE leading zero mapping bug and add tests
Co-authored-by: felickz <1760475+felickz@users.noreply.github.com>
1 parent 54787ff commit 0623b45

File tree

4 files changed

+41
-5
lines changed

4 files changed

+41
-5
lines changed

__tests__/main.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,36 @@ describe('main', () => {
44
// TODO: Add proper unit tests for the main module
55
expect(true).toBe(true)
66
})
7+
8+
describe('CWE ID normalization', () => {
9+
it('should handle CWE IDs with leading zeros', () => {
10+
// Test that cwe-099 maps to 99
11+
const cweIdWithLeadingZero = 'cwe-099'
12+
const cweIdPrefix = 'cwe-'
13+
const extractedId = cweIdWithLeadingZero.replace(cweIdPrefix, '')
14+
const normalizedId = String(parseInt(extractedId, 10))
15+
16+
expect(normalizedId).toBe('99')
17+
})
18+
19+
it('should handle CWE IDs without leading zeros', () => {
20+
// Test that cwe-89 maps to 89
21+
const cweIdNoLeadingZero = 'cwe-89'
22+
const cweIdPrefix = 'cwe-'
23+
const extractedId = cweIdNoLeadingZero.replace(cweIdPrefix, '')
24+
const normalizedId = String(parseInt(extractedId, 10))
25+
26+
expect(normalizedId).toBe('89')
27+
})
28+
29+
it('should handle CWE IDs with multiple leading zeros', () => {
30+
// Test that cwe-020 maps to 20
31+
const cweIdWithLeadingZeros = 'cwe-020'
32+
const cweIdPrefix = 'cwe-'
33+
const extractedId = cweIdWithLeadingZeros.replace(cweIdPrefix, '')
34+
const normalizedId = String(parseInt(extractedId, 10))
35+
36+
expect(normalizedId).toBe('20')
37+
})
38+
})
739
})

dist/index.js

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,11 @@ JSONPath({
103103
for (const tag of tags) {
104104
if (tag.startsWith(codeQlCweTagPrefix)) {
105105
const cweId = tag.replace(codeQlCweTagPrefix, '')
106-
if (cweIds.includes(cweId)) {
106+
// Normalize CWE ID by converting to integer to remove leading zeros
107+
const normalizedCweId = String(parseInt(cweId, 10))
108+
if (cweIds.includes(normalizedCweId)) {
107109
tags.push(securityStandardTag)
108-
tags.push(...cweCategories[cweId])
110+
tags.push(...cweCategories[normalizedCweId])
109111
return
110112
}
111113
}

0 commit comments

Comments
 (0)