Skip to content
Open
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
14 changes: 11 additions & 3 deletions src/spdx_tools/spdx/validation/license_expression_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,16 @@ def validate_license_expression(
license_ref_ids: List[str] = [license_ref.license_id for license_ref in document.extracted_licensing_info]

for non_spdx_token in spdx_licensing.validate(license_expression).invalid_symbols:
if ":" in non_spdx_token:
split_token: List[str] = non_spdx_token.split(":")
token = non_spdx_token
if token.endswith("+"):
base_token = token[:-1]
known_sym = spdx_licensing.known_symbols.get(base_token)
if known_sym and not getattr(known_sym, "is_exception", False):
continue
token = base_token

if ":" in token:
split_token: List[str] = token.split(":")
if len(split_token) != 2:
validation_messages.append(
ValidationMessage(
Expand Down Expand Up @@ -72,7 +80,7 @@ def validate_license_expression(
)
)

elif non_spdx_token not in license_ref_ids:
elif token not in license_ref_ids:
validation_messages.append(
ValidationMessage(
f"Unrecognized license reference: {non_spdx_token}. license_expression must only use IDs from the "
Expand Down
7 changes: 7 additions & 0 deletions tests/spdx/validation/test_license_expression_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
"expression_string",
[
"MIT",
"Apache-1.0+",
"Apache-2.0+",
f"{FIXTURE_LICENSE_ID}+",
f"{EXTERNAL_DOCUMENT_ID}:LicenseRef-007+",
"Apache-1.0+ or MIT",
FIXTURE_LICENSE_ID,
f"GPL-2.0-only with GPL-CC-1.0 and {FIXTURE_LICENSE_ID} with 389-exception or Beerware",
f"{EXTERNAL_DOCUMENT_ID}:LicenseRef-007",
Expand Down Expand Up @@ -71,6 +76,8 @@ def test_valid_license_expressions(expression_list):
[
(f"{FIXTURE_LICENSE_ID} or LicenseRef-22", ["LicenseRef-22"]),
("nope with 389-exception and _.- or LicenseRef-10", ["nope", "_.-", "LicenseRef-10"]),
("InvalidLicense+", ["InvalidLicense+"]),
("LLVM-exception+", ["LLVM-exception+"]),
],
)
def test_invalid_license_expression_with_unknown_symbols(expression_string, unknown_symbols):
Expand Down