Repository navigation
feat: add citations to Bom for CycloneDX v1.7 #1064
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
Open
juliosuas
wants to merge
3
commits into
CycloneDX:main
Choose a base branch
from
juliosuas:feat/1.7-citations
base: main
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.
Open
Changes from all commits
Commits
Show all changes
3 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,239 @@ | ||
| # This file is part of CycloneDX Python Library | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # Copyright (c) OWASP Foundation. All Rights Reserved. | ||
|
|
||
|
|
||
| """ | ||
| This set of classes represents the data about Citations. | ||
|
|
||
| .. note:: | ||
| Introduced in CycloneDX v1.7 | ||
|
|
||
| .. note:: | ||
| See the CycloneDX Schema definition https://cyclonedx.org/docs/1.7/xml/#type_citationType | ||
| """ | ||
|
|
||
| from collections.abc import Iterable | ||
| from datetime import datetime | ||
| from typing import Any, Optional, Union | ||
|
|
||
| import py_serializable as serializable | ||
| from sortedcontainers import SortedSet | ||
|
|
||
| from .._internal.bom_ref import bom_ref_from_str as _bom_ref_from_str | ||
| from .._internal.compare import ComparableTuple as _ComparableTuple | ||
| from ..exception.model import MutuallyExclusivePropertiesException, NoPropertiesProvidedException | ||
| from .bom_ref import BomRef | ||
|
|
||
|
|
||
| @serializable.serializable_class(ignore_unknown_during_deserialization=True) | ||
| class Citation: | ||
| """ | ||
| Details a specific attribution of data within the BOM to a contributing entity or process. | ||
|
|
||
| Exactly one of `pointers` or `expressions` must be provided. | ||
| At least one of `attributed_to` or `process` must be provided. | ||
|
|
||
| .. note:: | ||
| Introduced in CycloneDX v1.7 | ||
|
|
||
| .. note:: | ||
| See the CycloneDX Schema definition: https://cyclonedx.org/docs/1.7/xml/#type_citationType | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, *, | ||
| timestamp: datetime, | ||
| bom_ref: Optional[Union[str, BomRef]] = None, | ||
| pointers: Optional[Iterable[str]] = None, | ||
| expressions: Optional[Iterable[str]] = None, | ||
| attributed_to: Optional[Union[str, BomRef]] = None, | ||
| process: Optional[Union[str, BomRef]] = None, | ||
| note: Optional[str] = None, | ||
| # TODO: signature | ||
| ) -> None: | ||
| pointers = SortedSet(pointers or ()) | ||
| expressions = SortedSet(expressions or ()) | ||
| if not pointers and not expressions: | ||
| raise NoPropertiesProvidedException( | ||
| 'One of `pointers` or `expressions` must be supplied - neither supplied' | ||
| ) | ||
| if pointers and expressions: | ||
| raise MutuallyExclusivePropertiesException( | ||
| 'Only one of `pointers` or `expressions` must be supplied - both supplied' | ||
| ) | ||
| if attributed_to is None and process is None: | ||
| raise NoPropertiesProvidedException( | ||
| 'At least one of `attributed_to` or `process` must be supplied - neither supplied' | ||
| ) | ||
| self._bom_ref = _bom_ref_from_str(bom_ref) | ||
| self.pointers = pointers | ||
| self.expressions = expressions | ||
| self.timestamp = timestamp | ||
| self.attributed_to = attributed_to | ||
| self.process = process | ||
| self.note = note | ||
| # TODO: signature | ||
|
|
||
| @property | ||
| @serializable.type_mapping(BomRef) | ||
| @serializable.json_name('bom-ref') | ||
| @serializable.xml_name('bom-ref') | ||
| @serializable.xml_attribute() | ||
| def bom_ref(self) -> BomRef: | ||
| """ | ||
| An optional identifier which can be used to reference the citation elsewhere in the BOM. | ||
| Every bom-ref MUST be unique within the BOM. | ||
|
|
||
| Returns: | ||
| `BomRef` | ||
| """ | ||
| return self._bom_ref | ||
|
|
||
| @property | ||
| @serializable.xml_array(serializable.XmlArraySerializationType.NESTED, 'pointer') | ||
| @serializable.xml_sequence(1) | ||
| def pointers(self) -> 'SortedSet[str]': | ||
| """ | ||
| One or more JSON Pointers (RFC 6901) identifying the BOM fields to which the attribution applies. | ||
| Users of other serialization formats (e.g. XML) shall use the JSON Pointer format as well. | ||
|
|
||
| Returns: | ||
| Set of `str` | ||
| """ | ||
| return self._pointers | ||
|
|
||
| @pointers.setter | ||
| def pointers(self, pointers: Iterable[str]) -> None: | ||
| self._pointers = SortedSet(pointers) | ||
|
|
||
| @property | ||
| @serializable.xml_array(serializable.XmlArraySerializationType.NESTED, 'expression') | ||
| @serializable.xml_sequence(2) | ||
| def expressions(self) -> 'SortedSet[str]': | ||
| """ | ||
| One or more path expressions used to locate values within a BOM. | ||
| Use JSONPath for JSON and XPath for XML. | ||
|
|
||
| Returns: | ||
| Set of `str` | ||
| """ | ||
| return self._expressions | ||
|
|
||
| @expressions.setter | ||
| def expressions(self, expressions: Iterable[str]) -> None: | ||
| self._expressions = SortedSet(expressions) | ||
|
|
||
| @property | ||
| @serializable.type_mapping(serializable.helpers.XsdDateTime) | ||
| @serializable.xml_sequence(3) | ||
| def timestamp(self) -> datetime: | ||
| """ | ||
| The date and time when the attribution was made or the information was supplied. | ||
|
|
||
| Returns: | ||
| `datetime` | ||
| """ | ||
| return self._timestamp | ||
|
|
||
| @timestamp.setter | ||
| def timestamp(self, timestamp: datetime) -> None: | ||
| self._timestamp = timestamp | ||
|
|
||
| @property | ||
| @serializable.type_mapping(BomRef) | ||
| @serializable.json_name('attributedTo') | ||
| @serializable.xml_name('attributedTo') | ||
| @serializable.xml_sequence(4) | ||
| def attributed_to(self) -> Optional[BomRef]: | ||
| """ | ||
| The `bom-ref` of an object, such as a component, service, tool, organisational entity, or person | ||
| that supplied the cited information. | ||
|
|
||
| Returns: | ||
| `BomRef` if set else `None` | ||
| """ | ||
| return self._attributed_to | ||
|
|
||
| @attributed_to.setter | ||
| def attributed_to(self, attributed_to: Optional[Union[str, BomRef]]) -> None: | ||
| self._attributed_to = _bom_ref_from_str(attributed_to, optional=True) | ||
|
|
||
| @property | ||
| @serializable.type_mapping(BomRef) | ||
| @serializable.xml_sequence(5) | ||
| def process(self) -> Optional[BomRef]: | ||
| """ | ||
| The `bom-ref` to a process (such as a formula, workflow, task, or step) defined in the `formulation` | ||
| section that executed or generated the attributed data. | ||
|
|
||
| Returns: | ||
| `BomRef` if set else `None` | ||
| """ | ||
| return self._process | ||
|
|
||
| @process.setter | ||
| def process(self, process: Optional[Union[str, BomRef]]) -> None: | ||
| self._process = _bom_ref_from_str(process, optional=True) | ||
|
|
||
| @property | ||
| @serializable.xml_sequence(6) | ||
| def note(self) -> Optional[str]: | ||
| """ | ||
| A description or comment about the context or quality of the data attribution. | ||
|
|
||
| Returns: | ||
| `str` if set else `None` | ||
| """ | ||
| return self._note | ||
|
|
||
| @note.setter | ||
| def note(self, note: Optional[str]) -> None: | ||
| self._note = note | ||
|
|
||
| # @property | ||
| # # MUST NOT RENDER FOR XML -- this is JSON only | ||
| # def signature(self) -> ...: | ||
| # ... | ||
| # | ||
| # @signature.setter | ||
| # def signature(self, signature: ...) -> None: | ||
| # ... | ||
|
|
||
| def __comparable_tuple(self) -> _ComparableTuple: | ||
| return _ComparableTuple(( | ||
| self.timestamp, | ||
| _ComparableTuple(self.pointers), _ComparableTuple(self.expressions), | ||
| self.attributed_to, self.process, | ||
| self.bom_ref.value, self.note, | ||
| )) | ||
|
|
||
| def __lt__(self, other: Any) -> bool: | ||
| if isinstance(other, Citation): | ||
| return self.__comparable_tuple() < other.__comparable_tuple() | ||
| return NotImplemented | ||
|
|
||
| def __eq__(self, other: object) -> bool: | ||
| if isinstance(other, Citation): | ||
| return self.__comparable_tuple() == other.__comparable_tuple() | ||
| return False | ||
|
|
||
| def __hash__(self) -> int: | ||
| return hash(self.__comparable_tuple()) | ||
|
|
||
| def __repr__(self) -> str: | ||
| return f'<Citation bom-ref={self.bom_ref!r}, timestamp={self.timestamp!r}, ' \ | ||
| f'attributed_to={self.attributed_to!r}, process={self.process!r}>' | ||
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
11 changes: 11 additions & 0 deletions
11
tests/_data/snapshots/get_bom_with_citations_1_7-1.0.xml.bin
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,11 @@ | ||
| <?xml version="1.0" ?> | ||
| <bom xmlns="http://cyclonedx.org/schema/bom/1.0" version="1"> | ||
| <components> | ||
| <component type="library"> | ||
| <name>external-lib</name> | ||
| <version>1.0.0</version> | ||
| <scope>required</scope> | ||
| <modified>false</modified> | ||
| </component> | ||
| </components> | ||
| </bom> |
10 changes: 10 additions & 0 deletions
10
tests/_data/snapshots/get_bom_with_citations_1_7-1.1.xml.bin
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,10 @@ | ||
| <?xml version="1.0" ?> | ||
| <bom xmlns="http://cyclonedx.org/schema/bom/1.1" serialNumber="urn:uuid:1441d33a-e0fc-45b5-af3b-61ee52a88bac" version="1"> | ||
| <components> | ||
| <component type="library" bom-ref="external-lib-1.0.0"> | ||
| <name>external-lib</name> | ||
| <version>1.0.0</version> | ||
| <scope>required</scope> | ||
| </component> | ||
| </components> | ||
| </bom> |
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.
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.
In
cyclonedx/model/citation.py(Citation.__init__, lines 78-87),if attributed_to is None and process is None:runs beforeself.attributed_toandself.processare normalized via_bom_ref_from_str(..., optional=True). Passingattributed_to=''(orprocess='') bypassesNoPropertiesProvidedExceptionand sets both fields toNone, soJsonV1Dot7emits acitationentry withoutattributedToorprocessthat failsJsonStrictValidator(SchemaVersion.V1_7). Passing an emptyBomRef()similarly serializesattributedToasNoneand causesCitation(..., attributed_to=BomRef(), process='p1') == Citation(..., attributed_to=None, process='p1')to evaluate toFalse. Assigningself.attributed_toandself.processbefore checkingif not self.attributed_to and not self.process:keeps constructor validation and equality aligned with the 1.7 schema.Disclosure: Review and test verification conducted with assistance from Gemini 4 Argon.