Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
28 changes: 28 additions & 0 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,35 @@
import {normalizeCweId} from '../src/utils'

describe('main', () => {
it('placeholder test', () => {
// This is a placeholder test to prevent Jest from failing
// TODO: Add proper unit tests for the main module
expect(true).toBe(true)
})

describe('CWE ID normalization', () => {
it('should handle CWE IDs with leading zeros', () => {
// Test that 099 maps to 99
const normalizedId = normalizeCweId('099')
expect(normalizedId).toBe('99')
})

it('should handle CWE IDs without leading zeros', () => {
// Test that 89 maps to 89
const normalizedId = normalizeCweId('89')
expect(normalizedId).toBe('89')
})

it('should handle CWE IDs with multiple leading zeros', () => {
// Test that 020 maps to 20
const normalizedId = normalizeCweId('020')
expect(normalizedId).toBe('20')
})

it('should return null for non-numeric CWE IDs', () => {
// Test that invalid CWE IDs return null
const normalizedId = normalizeCweId('abc')
expect(normalizedId).toBeNull()
})
})
Comment thread
felickz marked this conversation as resolved.
})
23 changes: 21 additions & 2 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

12 changes: 9 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as core from '@actions/core'
import {DOMParser} from '@xmldom/xmldom'
import * as xpath from 'xpath'
import {JSONPath} from 'jsonpath-plus'
import {LogLevel, log} from './utils'
import {LogLevel, log, normalizeCweId} from './utils'

let sarifFilePath: string
let outputFilePath: string
Expand Down Expand Up @@ -103,9 +103,15 @@ JSONPath({
for (const tag of tags) {
if (tag.startsWith(codeQlCweTagPrefix)) {
const cweId = tag.replace(codeQlCweTagPrefix, '')
if (cweIds.includes(cweId)) {
// Normalize CWE ID by converting to integer to remove leading zeros
const normalizedCweId = normalizeCweId(cweId)
// Skip if the CWE ID is not a valid number
if (normalizedCweId === null) {
continue
}
if (cweIds.includes(normalizedCweId)) {
tags.push(securityStandardTag)
tags.push(...cweCategories[cweId])
tags.push(...cweCategories[normalizedCweId])
return
}
}
Expand Down
13 changes: 13 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,16 @@ export function log(message: string, level = LogLevel.Info): void {
}
}
}

/**
* Normalize a CWE ID by removing leading zeros
* @param cweId - The CWE ID string (e.g., "099", "020", "89")
* @returns The normalized CWE ID string (e.g., "99", "20", "89") or null if invalid
*/
export function normalizeCweId(cweId: string): string | null {
const parsedCweId = parseInt(cweId, 10)
if (Number.isNaN(parsedCweId)) {
return null
}
return String(parsedCweId)
}
Comment thread
felickz marked this conversation as resolved.

Large diffs are not rendered by default.

Loading