-
Notifications
You must be signed in to change notification settings - Fork 273
feat: Add script to generate OpenVEX file #684
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
ppkarwasz
wants to merge
2
commits into
master
Choose a base branch
from
feat/openvex-file
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| #!/usr/bin/env python3 | ||
| import xml.etree.ElementTree as ET | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This file is missing its Apache license header which blows up the build's RAT check. Run 'mvn' solo to run the default Maven goal 😉 |
||
| import json | ||
| from datetime import datetime, timezone | ||
|
|
||
| NAMESPACES = { | ||
| 'b': 'http://cyclonedx.org/schema/bom/1.6' | ||
| } | ||
|
|
||
|
|
||
| def _find_element(parent: ET.Element, tag: str) -> ET.Element | None: | ||
| return parent.find(tag, NAMESPACES) | ||
|
|
||
|
|
||
| def _find_stripped_text(parent: ET.Element, tag: str) -> str | None: | ||
| el = _find_element(parent, tag) | ||
| return el.text.strip() if el is not None else None | ||
|
|
||
|
|
||
| def _add_optional_date(parent: ET.Element, tag: str, target: dict, key: str) -> None: | ||
| el = _find_element(parent, tag) | ||
| if el is not None and el.text: | ||
| try: | ||
| dt = datetime.fromisoformat(el.text.strip()).astimezone(timezone.utc) | ||
| target[key] = dt.isoformat().replace('+00:00', 'Z') | ||
| except ValueError as e: | ||
| raise ValueError(f"Invalid ISO date format in <{tag}>: {el.text}") from e | ||
|
|
||
|
|
||
| def load_cyclonedx(path: str = 'VEX.cyclonedx.xml') -> ET.Element: | ||
| return ET.parse(path).getroot() | ||
|
|
||
|
|
||
| def to_openvex(root: ET.Element) -> dict: | ||
| serial_number = root.get('serialNumber') | ||
| if not serial_number: | ||
| raise ValueError("CycloneDX BOM must have a 'serialNumber' attribute") | ||
|
|
||
| version = int(root.get('version', '1')) | ||
|
|
||
| result = { | ||
| '@context': 'https://openvex.dev/ns/v0.2.0', | ||
| '@id': f"https://commons.apache.org/security/vex/{serial_number}", | ||
| 'author': 'Apache Commons Security Team <security@commons.apache.org>', | ||
| 'role': 'Security Team', | ||
| 'version': version, | ||
| 'tooling': ( | ||
| "This document was automatically converted from the `VEX.cyclonedx.xml` file.\n" | ||
| "Do not edit this file directly, run `generate_openvex.py` to regenerate it." | ||
| ) | ||
| } | ||
|
|
||
| _add_optional_date(root, 'b:metadata/b:timestamp', result, 'timestamp') | ||
|
|
||
| component = _find_element(root, 'b:metadata/b:component') | ||
| if component is None: | ||
| raise ValueError("Missing <component> in <metadata>") | ||
|
|
||
| product = to_openvex_product(component) | ||
|
|
||
| result['statements'] = [ | ||
| to_openvex_statement(vuln, product) | ||
| for vuln in root.findall('.//b:vulnerability', NAMESPACES) | ||
| ] | ||
|
|
||
| return result | ||
|
|
||
|
|
||
| def to_openvex_product(component: ET.Element) -> dict: | ||
| purl = _find_element(component, 'b:purl') | ||
| if purl is None or not purl.text: | ||
| raise ValueError("Component must include a non-empty <purl> element") | ||
|
|
||
| return { | ||
| '@id': purl.text, | ||
| 'identifiers': { | ||
| 'purl': purl.text | ||
| } | ||
| } | ||
|
|
||
|
|
||
| def to_openvex_vulnerability(vuln: ET.Element) -> dict: | ||
| cdx_id = _find_stripped_text(vuln, 'b:id') | ||
| if not cdx_id: | ||
| raise ValueError("Vulnerability must have an <id>") | ||
|
|
||
| entry = {'name': cdx_id} | ||
|
|
||
| source = _find_element(vuln, 'b:source') | ||
| if source is not None: | ||
| entry['@id'] = _find_stripped_text(source, 'b:url') | ||
|
|
||
| entry['aliases'] = [ | ||
| _find_stripped_text(ref, 'b:id') | ||
| for ref in vuln.findall('b:references/b:reference', NAMESPACES) | ||
| ] | ||
|
|
||
| return entry | ||
|
|
||
|
|
||
| def to_openvex_statement(vuln: ET.Element, product: dict) -> dict: | ||
| analysis = _find_element(vuln, 'b:analysis') | ||
| if analysis is None: | ||
| raise ValueError("Missing <analysis> in vulnerability") | ||
|
|
||
| state = _find_stripped_text(analysis, 'b:state') | ||
| if not state: | ||
| raise ValueError("Missing <state> in vulnerability analysis") | ||
|
|
||
| statement = { | ||
| 'products': [product], | ||
| 'vulnerability': to_openvex_vulnerability(vuln), | ||
| 'status': to_openvex_status(state) | ||
| } | ||
|
|
||
| justification = _find_stripped_text(analysis, 'b:justification') | ||
| if justification: | ||
| statement['justification'] = to_openvex_justification(justification) | ||
|
|
||
| detail = _find_stripped_text(analysis, 'b:detail') | ||
| if detail: | ||
| statement['status_notes'] = detail | ||
|
|
||
| remediation = _find_stripped_text(vuln, 'b:recommendation') | ||
| if remediation: | ||
| statement['action_statement'] = remediation | ||
| else: | ||
| if statement['status'] == 'affected': | ||
| raise ValueError("Affected vulnerabilities must have a <recommendation> element") | ||
|
|
||
| _add_optional_date(analysis, 'b:firstIssued', statement, 'timestamp') | ||
| _add_optional_date(analysis, 'b:lastUpdated', statement, 'last_updated') | ||
|
|
||
| return statement | ||
|
|
||
|
|
||
| def to_openvex_status(cdx_status: str) -> str: | ||
| mapping = { | ||
| "resolved": "fixed", | ||
| "exploitable": "affected", | ||
| "in_triage": "under_investigation", | ||
| "false_positive": "not_affected", | ||
| "not_affected": "not_affected" | ||
| } | ||
| status = mapping.get(cdx_status.strip().lower()) | ||
| if not status: | ||
| raise ValueError(f"Unknown CycloneDX status: '{cdx_status}'") | ||
| return status | ||
|
|
||
|
|
||
| def to_openvex_justification(cdx_justification: str) -> str: | ||
| mapping = { | ||
| "code_not_present": "vulnerable_code_not_present", | ||
| "code_not_reachable": "vulnerable_code_not_in_execute_path", | ||
| "requires_configuration": "vulnerable_code_cannot_be_controlled_by_adversary", | ||
| "requires_dependency": "component_not_present", | ||
| "requires_environment": "vulnerable_code_cannot_be_controlled_by_adversary", | ||
| "protected_by_compiler": "inline_mitigations_already_exist", | ||
| "protected_at_runtime": "inline_mitigations_already_exist", | ||
| "protected_by_mitigating_control": "inline_mitigations_already_exist" | ||
| } | ||
| result = mapping.get(cdx_justification.strip().lower()) | ||
| if not result: | ||
| raise ValueError(f"Unknown CycloneDX justification: '{cdx_justification}'") | ||
| return result | ||
|
|
||
|
|
||
| def main(): | ||
| cyclonedx_root = load_cyclonedx() | ||
| openvex_doc = to_openvex(cyclonedx_root) | ||
| with open('openvex.json', 'w') as f: | ||
| json.dump(openvex_doc, f, indent=2) | ||
| print("OpenVEX document written to 'openvex.json'") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| { | ||
| "@context": "https://openvex.dev/ns/v0.2.0", | ||
| "@id": "https://commons.apache.org/security/vex/urn:uuid:9d64577b-0376-4ee7-b154-5ec26a1803f4", | ||
| "author": "Apache Commons Security Team <security@commons.apache.org>", | ||
| "role": "Security Team", | ||
| "version": 2, | ||
| "tooling": "This document was automatically converted from the `VEX.cyclonedx.xml` file.\nDo not edit this file directly, run `generate_openvex.py` to regenerate it.", | ||
| "timestamp": "2025-07-29T12:26:42Z", | ||
| "statements": [ | ||
| { | ||
| "products": [ | ||
| { | ||
| "@id": "pkg:maven/org.apache.commons/commons-text?type=jar", | ||
| "identifiers": { | ||
| "purl": "pkg:maven/org.apache.commons/commons-text?type=jar" | ||
| } | ||
| } | ||
| ], | ||
| "vulnerability": { | ||
| "name": "CVE-2025-48924", | ||
| "@id": "https://nvd.nist.gov/vuln/detail/CVE-2025-48924", | ||
| "aliases": [ | ||
| "GHSA-j288-q9x7-2f5v" | ||
| ] | ||
| }, | ||
| "status": "affected", | ||
| "status_notes": "CVE-2025-48924 is exploitable in Apache Commons Text versions 1.5 and later, but only when all the following conditions are met:\n\n* The consuming project includes a vulnerable version of Commons Text on the classpath.\n As of version `1.14.1`, Commons Text no longer references a vulnerable version of the `commons-lang3` library in its POM file.\n* Unvalidated or unsanitized user input is passed to the `StringSubstitutor` or `StringLookup` classes.\n* An interpolator lookup created via `StringLookupFactory.interpolatorLookup()` is used.\n\nIf these conditions are satisfied, an attacker may cause an infinite loop by submitting a specially crafted input such as `${const:...}`.", | ||
| "action_statement": "Check if untrusted user input is passed to the `StringSubstitutor` or `StringLookup` classes,\nand if so, upgrade to Apache Commons Lang 3.18.0 or later.", | ||
| "timestamp": "2025-07-29T12:26:42Z", | ||
| "last_updated": "2025-07-29T12:26:42Z" | ||
| } | ||
| ] | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @ppkarwasz
This seems like a bad idea because all 20+ components will need this duplicated. It seems we should have an "empty" VEX statement for components without issues to affirm that we are OK there. We have TWO plug-ins already for this kind of housekeeping (we really should have a single one), can't we stick stuff like this in there?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Which plugin do you think would be the best?
As an alternative we can also move the VEX-es into a separate repo, where we can also store the Python scripts to generate them. What do you think? A single repo would also allow us to update VEX entries for all Commons components at once.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about putting this in commons-build-plugin?
That plugin generates files already like the release notes, read me, and some site XML files. This would allow us to also generate the security page with VEX information! Super 👌!
Requiring Python is not great, the plug-in generates files without Python.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I could write a Maven Site Plugin that generates the OpenVEX.
Since it is experimental and might be used by projects other than Commons, I'll write a prototype either in
sbom-enforcerorvex-generation-toolset: it will be easier to publish and it can be easier to drop support for it, if it ends up not being useful.