Skip to content
Merged
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
72 changes: 72 additions & 0 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,79 @@
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()
})

it('should return null for empty strings', () => {
// Test that empty strings return null
const normalizedId = normalizeCweId('')
expect(normalizedId).toBeNull()
})

it('should return null for strings with only spaces', () => {
// Test that strings with only spaces return null
const normalizedId = normalizeCweId(' ')
expect(normalizedId).toBeNull()
})

it('should handle strings with leading/trailing spaces', () => {
// Test that strings with spaces are parsed correctly
const normalizedId = normalizeCweId(' 99 ')
expect(normalizedId).toBe('99')
})

it('should return null for negative numbers', () => {
// Test that negative numbers return null (CWE IDs should be positive)
const normalizedId = normalizeCweId('-99')
expect(normalizedId).toBeNull()
})

it('should handle strings with mixed alphanumeric characters (parseInt is lenient)', () => {
// Test that mixed alphanumeric strings are parsed leniently
// parseInt stops at first non-numeric character, so '99abc' becomes 99
// This is acceptable for our use case as malformed tags would be rare
const normalizedId = normalizeCweId('99abc')
expect(normalizedId).toBe('99')
})

it('should handle zero', () => {
// Test that zero is handled correctly
const normalizedId = normalizeCweId('0')
expect(normalizedId).toBe('0')
})

it('should handle zero with leading zeros', () => {
// Test that zero with leading zeros is handled correctly
const normalizedId = normalizeCweId('000')
expect(normalizedId).toBe('0')
})
})
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) || parsedCweId < 0) {
return null
}
return String(parsedCweId)
}
Comment thread
felickz marked this conversation as resolved.

Large diffs are not rendered by default.

Loading