-
Notifications
You must be signed in to change notification settings - Fork 134
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
SPDX-2.2 validation #490
SPDX-2.2 validation #490
Changes from 9 commits
33ea7cb
264bb60
07a80f5
80ceb92
43ebce6
5bc9ac9
12ea66c
d87d372
ee0ab48
18a1dd2
dcfa0ad
b3798aa
1b886d6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,7 @@ | |
from spdx.validation.validation_message import ValidationMessage, ValidationContext, SpdxElementType | ||
|
||
|
||
def validate_creation_info(creation_info: CreationInfo) -> List[ValidationMessage]: | ||
def validate_creation_info(creation_info: CreationInfo, spdx_version: str) -> List[ValidationMessage]: | ||
validation_messages: List[ValidationMessage] = [] | ||
|
||
context = ValidationContext(spdx_id=creation_info.spdx_id, element_type=SpdxElementType.DOCUMENT) | ||
|
@@ -48,6 +48,6 @@ def validate_creation_info(creation_info: CreationInfo) -> List[ValidationMessag | |
|
||
validation_messages.extend(validate_actors(creation_info.creators, creation_info.spdx_id)) | ||
|
||
validation_messages.extend(validate_external_document_refs(creation_info.external_document_refs, creation_info.spdx_id)) | ||
validation_messages.extend(validate_external_document_refs(creation_info.external_document_refs, creation_info.spdx_id, spdx_version)) | ||
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. Auto formatting inserts a line break here. 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. done |
||
|
||
return validation_messages |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,16 +40,17 @@ | |
} | ||
|
||
|
||
def validate_external_package_refs(external_package_refs: List[ExternalPackageRef], parent_id: str) -> List[ | ||
def validate_external_package_refs(external_package_refs: List[ExternalPackageRef], parent_id: str, spdx_version: str) -> List[ | ||
ValidationMessage]: | ||
validation_messages = [] | ||
for external_package_ref in external_package_refs: | ||
validation_messages.extend(validate_external_package_ref(external_package_ref, parent_id)) | ||
validation_messages.extend(validate_external_package_ref(external_package_ref, parent_id, spdx_version)) | ||
|
||
return validation_messages | ||
|
||
|
||
def validate_external_package_ref(external_package_ref: ExternalPackageRef, parent_id: str) -> List[ValidationMessage]: | ||
def validate_external_package_ref(external_package_ref: ExternalPackageRef, parent_id: str, spdx_version: str) -> List[ValidationMessage]: | ||
validation_messages = [] | ||
context = ValidationContext(parent_id=parent_id, element_type=SpdxElementType.EXTERNAL_PACKAGE_REF, | ||
full_element=external_package_ref) | ||
|
||
|
@@ -59,31 +60,36 @@ def validate_external_package_ref(external_package_ref: ExternalPackageRef, pare | |
|
||
if category == ExternalPackageRefCategory.OTHER: | ||
if " " in locator: | ||
return [ValidationMessage( | ||
validation_messages.append(ValidationMessage( | ||
f"externalPackageRef locator in category OTHER must contain no spaces, but is: {locator}", | ||
context)] | ||
return [] | ||
context)) | ||
|
||
if reference_type not in CATEGORY_TO_EXTERNAL_PACKAGE_REF_TYPES[category]: | ||
return [ValidationMessage( | ||
elif reference_type not in CATEGORY_TO_EXTERNAL_PACKAGE_REF_TYPES[category]: | ||
validation_messages.append(ValidationMessage( | ||
f"externalPackageRef type in category {category.name} must be one of {CATEGORY_TO_EXTERNAL_PACKAGE_REF_TYPES[category]}, but is: {reference_type}", | ||
context)] | ||
context)) | ||
|
||
if reference_type in ["advisory", "fix", "url"]: | ||
elif reference_type in ["advisory", "fix", "url"]: | ||
if validate_url(locator): | ||
return [ValidationMessage( | ||
validation_messages.append(ValidationMessage( | ||
f'externalPackageRef locator of type "{reference_type}" must be a valid URL, but is: {locator}', | ||
context)] | ||
return [] | ||
context)) | ||
|
||
if reference_type == "swid": | ||
elif reference_type == "swid": | ||
if not uritools.isuri(locator) or not locator.startswith("swid"): | ||
return [ValidationMessage( | ||
validation_messages.append(ValidationMessage( | ||
f'externalPackageRef locator of type "swid" must be a valid URI with scheme swid, but is: {locator}', | ||
context)] | ||
return [] | ||
context)) | ||
|
||
return validate_against_regex(locator, reference_type, context) | ||
else: | ||
validation_messages.extend(validate_against_regex(locator, reference_type, context)) | ||
|
||
if spdx_version == "SPDX-2.2" and reference_type in ["advisory", "fix", "url", "swid"]: | ||
validation_messages.append( | ||
ValidationMessage(f'externalPackageRef type "{reference_type}" is not supported in SPDX-2.2', context) | ||
) | ||
|
||
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. I would move this validation to the beginning and return. I don't think that it makes sense to first validate references on a lower level and afterwards check if they are valid in general. 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. done |
||
return validation_messages | ||
|
||
|
||
def validate_against_regex(string_to_validate: str, reference_type: str, context: ValidationContext) -> List[ | ||
|
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.
I think we should return here as the algorithm is not valid.
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.
done