Skip to content

Fix CWE tag mapping for IDs with leading zeros#192

Merged
felickz merged 7 commits intomainfrom
copilot/fix-sarif-cwe-tag-bug
Jan 23, 2026
Merged

Fix CWE tag mapping for IDs with leading zeros#192
felickz merged 7 commits intomainfrom
copilot/fix-sarif-cwe-tag-bug

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jan 21, 2026

Fix CWE Leading Zero Mapping Bug

  • Explore repository and understand the issue
  • Identify the bug location in main.ts
  • Create a test case for CWE tags with leading zeros
  • Fix the bug by converting CWE IDs to integers to remove leading zeros
  • Run tests to verify the fix
  • Build and validate the solution
  • Address code review feedback
    • Added validation to skip non-numeric CWE IDs that would result in NaN
    • Fixed test to use correct prefix (external/cwe/cwe-)
    • Extracted normalization logic into helper function in tests
    • Added test for invalid CWE IDs
  • Run security scan - No vulnerabilities found
  • Fix CI test failure by regenerating expected output file
  • Extract normalization logic into shared utility function
    • Created normalizeCweId() function in utils.ts
    • Updated main.ts to use the shared function
    • Updated tests to import and test the actual implementation
    • Regenerated dist files
  • Add comprehensive edge case test coverage
    • Added tests for empty strings
    • Added tests for strings with only spaces
    • Added tests for strings with leading/trailing spaces
    • Added tests for negative numbers
    • Added tests for mixed alphanumeric strings
    • Added tests for zero and zero with leading zeros
    • Enhanced validation to reject negative CWE IDs (CWE IDs should be positive)

Summary

Issue: CWE tags in SARIF files with leading zeros (like "external/cwe/cwe-079") were not properly mapping to CWE XML documents (which store IDs as "99" without leading zeros). This caused the tool to miss valid CWE mappings.

Solution:

  1. Normalize CWE IDs by parsing them as integers and converting back to strings, which removes leading zeros (e.g., "099" → "99", "020" → "20")
  2. Added validation to skip invalid non-numeric CWE IDs that would result in NaN
  3. Added validation to reject negative numbers (CWE IDs should always be positive integers)
  4. Applied normalization to both the lookup and the category mapping
  5. Extracted normalization logic into a shared utility function for better code organization and testability
  6. Added comprehensive edge case test coverage (12 tests total) including empty strings, spaces, negative numbers, and zero handling

CI Test Fix:
The CI test was failing because the expected output file was generated with the buggy code and was missing tags for CWEs with leading zeros. Regenerated the expected output file using the fixed code, which now correctly includes OWASP tags for all matching CWE IDs (including those with leading zeros like cwe-079, cwe-020, etc.).

Original prompt

Fix the bug where a CWE tag in the SARIF is a number with a leading zero like

                            "properties": {
                                "tags": [
                                    "security",
                                    "external/cwe/cwe-099"
                                ],

It does not properly map to the CWE XML Doc that should link up like <Has_Member CWE_ID="99" View_ID="1344"/>

  <Category ID="1347" Name="OWASP Top Ten 2021 Category A03:2021 - Injection" Status="Incomplete">
     <Summary>Weaknesses in this category are related to the A03 category "Injection" in the OWASP Top Ten 2021.</Summary>
     <Relationships>
        <Has_Member CWE_ID="20" View_ID="1344"/>
        <Has_Member CWE_ID="74" View_ID="1344"/>
        <Has_Member CWE_ID="75" View_ID="1344"/>
        <Has_Member CWE_ID="77" View_ID="1344"/>
        <Has_Member CWE_ID="78" View_ID="1344"/>
        <Has_Member CWE_ID="79" View_ID="1344"/>
        <Has_Member CWE_ID="80" View_ID="1344"/>
        <Has_Member CWE_ID="83" View_ID="1344"/>
        <Has_Member CWE_ID="87" View_ID="1344"/>
        <Has_Member CWE_ID="88" View_ID="1344"/>
        <Has_Member CWE_ID="89" View_ID="1344"/>
        <Has_Member CWE_ID="90" View_ID="1344"/>
        <Has_Member CWE_ID="91" View_ID="1344"/>
        <Has_Member CWE_ID="93" View_ID="1344"/>
        <Has_Member CWE_ID="94" View_ID="1344"/>
        <Has_Member CWE_ID="95" View_ID="1344"/>
        <Has_Member CWE_ID="96" View_ID="1344"/>
        <Has_Member CWE_ID="97" View_ID="1344"/>
        <Has_Member CWE_ID="98" View_ID="1344"/>
        <Has_Member CWE_ID="99" View_ID="1344"/>

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits January 21, 2026 15:36
Co-authored-by: felickz <1760475+felickz@users.noreply.github.com>
Co-authored-by: felickz <1760475+felickz@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix bug with leading zero in CWE tags in SARIF Fix CWE tag mapping for IDs with leading zeros Jan 21, 2026
Copilot AI requested a review from felickz January 21, 2026 15:41
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jan 21, 2026

Dependency Review

✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.

Scanned Files

None

@felickz
Copy link
Copy Markdown
Contributor

felickz commented Jan 21, 2026

@copilot the ci test failed: https://github.com/advanced-security/codeql-sarif-security-standard-annotator/actions/runs/21215699118/job/61049679966?pr=192

{"text":"org.springframework.web.client.RestTemplate#exchange(String,HttpMethod,HttpEntity,Class,Object\[\])"}}],"semmle.formatSpecifier":"sarif-latest"}}]}
2026-01-21T17:31:59.0790156Z \ No newline at end of file
2026-01-21T17:31:59.0807764Z ##[error]Process completed with exit code 1.

Why did you not catch this when you ran the test? Is there anything missing here in the ci testing or the CCA runtime that needs to be fixed so they match?

Co-authored-by: felickz <1760475+felickz@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Jan 21, 2026

@copilot the ci test failed: https://github.com/advanced-security/codeql-sarif-security-standard-annotator/actions/runs/21215699118/job/61049679966?pr=192

{"text":"org.springframework.web.client.Re...

The CI test failure was expected and has been fixed in commit 869c6d7.

Root cause: The fix correctly normalizes CWE IDs with leading zeros (e.g., "cwe-079" → "79"), allowing them to match entries in the XML file (CWE_ID="79"). This means the output now includes OWASP tags for CWE IDs that were previously missed due to the bug.

Why local tests didn't catch it: The local unit tests validated the normalization logic but didn't regenerate the integration test's expected output file. The expected file was created with the buggy code and was missing tags for ~12 CWE IDs with leading zeros.

Fix: Regenerated webgoat-with-security-standard-tag.sarif.expected using the corrected code. The new file now has 72 occurrences of "owasp-top10-2021" tags (vs. previously missing tags for cwe-020, cwe-022, cwe-079, cwe-089, etc.).

@felickz felickz marked this pull request as ready for review January 21, 2026 18:16
Copilot AI review requested due to automatic review settings January 21, 2026 18:16
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a bug where CWE tags with leading zeros (e.g., "external/cwe/cwe-079") were not properly mapping to CWE XML documents that store IDs without leading zeros (e.g., "99"). The fix normalizes CWE IDs by converting them to integers and back to strings, effectively removing leading zeros while maintaining proper mapping.

Changes:

  • Normalize CWE IDs by parsing as integers to strip leading zeros
  • Add validation to skip non-numeric CWE IDs that would result in NaN
  • Add comprehensive test coverage for CWE ID normalization including edge cases

Reviewed changes

Copilot reviewed 2 out of 5 changed files in this pull request and generated 2 comments.

File Description
src/main.ts Added CWE ID normalization logic to convert IDs to integers (removing leading zeros) and validation to skip invalid non-numeric IDs
tests/main.test.ts Added test suite for CWE ID normalization covering leading zeros, multiple leading zeros, no leading zeros, and invalid non-numeric IDs

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/main.ts Outdated
Comment thread __tests__/main.test.ts
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@felickz
Copy link
Copy Markdown
Contributor

felickz commented Jan 21, 2026

@copilot changes made - regenerate output Binary files a/dist/index.js.map and b/dist/index.js.map differ

Also, would you consider implementing this feedback - i am not clear this is correct:

https://github.com/apps/copilot-pull-request-reviewer AI
2 minutes ago
This test helper duplicates the normalization logic from main.ts. Consider extracting the normalization logic into a shared utility function that can be imported and tested directly, reducing duplication and ensuring tests verify the actual implementation.

… dist

Co-authored-by: felickz <1760475+felickz@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Jan 21, 2026

@copilot changes made - regenerate output Binary files a/dist/index.js.map and b/dist/index.js.map differ

Also, would you consider implementing this feedback - i am not clear this is correct:
...

Done in commit 77fb98c.

Changes made:

  1. Extracted normalization logic to shared utility - Created normalizeCweId() function in utils.ts that both main.ts and tests now import and use
  2. Regenerated dist files - Rebuilt and packaged dist/index.js and dist/index.js.map with the refactored code
  3. Updated tests - Tests now import and verify the actual implementation instead of duplicating the logic

This addresses the AI reviewer's feedback by eliminating code duplication and ensuring tests verify the real implementation.

@felickz felickz requested a review from Copilot January 21, 2026 18:40
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 6 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/utils.ts
@felickz felickz requested review from a team and adrienpessu and removed request for a team January 21, 2026 18:44
Co-authored-by: felickz <1760475+felickz@users.noreply.github.com>
@felickz felickz merged commit 720d2c8 into main Jan 23, 2026
8 checks passed
@felickz felickz deleted the copilot/fix-sarif-cwe-tag-bug branch January 23, 2026 14:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants