Skip to content

Commit 29bfda2

Browse files
committed
fix: raise a clear error for an spdx_id with multiple colons
get_full_element_spdx_id unpacked spdx_id.split(":") into exactly two values, so an id containing more than one colon raised "ValueError: too many values to unpack (expected 2)" during bump_spdx_document. validate_spdx_id already rejects such ids with a descriptive message, but the bump path can run without validation (pyspdxtools3 --novalidation, or a direct library call). Check the split length and raise the same descriptive error instead. Fixes #901 Signed-off-by: Atishyy27 <sethatishayjain@gmail.com>
1 parent cef432a commit 29bfda2

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

src/spdx_tools/spdx/spdx_element_utils.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,14 @@ def get_full_element_spdx_id(
4040
if ":" not in element.spdx_id:
4141
return f"{document_namespace}#{element.spdx_id}"
4242

43-
external_id, local_id = element.spdx_id.split(":")
43+
split_id = element.spdx_id.split(":")
44+
if len(split_id) > 2:
45+
raise ValueError(
46+
f"spdx_id must not contain more than one colon in order to separate the external document "
47+
f"reference id from the internal SPDX id, but is: {element.spdx_id}"
48+
)
49+
50+
external_id, local_id = split_id
4451
external_uri = None
4552
for entry in external_document_refs:
4653
if entry.document_ref_id == external_id:

tests/spdx3/bump/test_external_element_bump.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44

55
from unittest import TestCase
66

7+
import pytest
8+
79
from spdx_tools.spdx3.bump_from_spdx2.checksum import bump_checksum
810
from spdx_tools.spdx3.bump_from_spdx2.spdx_document import bump_spdx_document
911
from spdx_tools.spdx3.model import ExternalMap
1012
from spdx_tools.spdx3.payload import Payload
1113
from spdx_tools.spdx.model import ExternalDocumentRef
1214
from spdx_tools.spdx.model.document import Document as Spdx2_Document
15+
from spdx_tools.spdx.spdx_element_utils import get_full_element_spdx_id
1316
from tests.spdx.fixtures import (
1417
checksum_fixture,
1518
creation_info_fixture,
@@ -53,3 +56,17 @@ def test_bump_external_elements():
5356
assert f"{external_doc_uri}#SPDXRef-Snippet" in payload.get_full_map()
5457

5558
TestCase().assertCountEqual(spdx_document.imports, expected_imports)
59+
60+
61+
def test_bump_external_element_with_malformed_spdx_id():
62+
"""An spdx_id with more than one colon must raise a clear error instead of
63+
crashing with an unpack ValueError from split(":")."""
64+
document_namespace = document_fixture().creation_info.document_namespace
65+
malformed_id = "DocumentRef-external:SPDXRef-Package:extra"
66+
67+
with pytest.raises(ValueError, match="must not contain more than one colon"):
68+
get_full_element_spdx_id(
69+
package_fixture(spdx_id=malformed_id),
70+
document_namespace,
71+
[ExternalDocumentRef("DocumentRef-external", "https://external-document.uri", checksum_fixture())],
72+
)

0 commit comments

Comments
 (0)