1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
|
# SPDX-FileCopyrightText: 2022 spdx contributors
#
# SPDX-License-Identifier: Apache-2.0
from typing import List
import pytest
from spdx_tools.spdx.model import ExternalPackageRef, ExternalPackageRefCategory
from spdx_tools.spdx.validation.external_package_ref_validator import (
BOWER_REGEX,
CPE22TYPE_REGEX,
CPE23TYPE_REGEX,
GITOID_REGEX,
MAVEN_CENTRAL_REGEX,
NPM_REGEX,
NUGET_REGEX,
PURL_REGEX,
SWH_REGEX,
validate_external_package_ref,
)
from spdx_tools.spdx.validation.validation_message import SpdxElementType, ValidationContext, ValidationMessage
@pytest.mark.parametrize(
"category, reference_type, locator",
[
(ExternalPackageRefCategory.SECURITY, "cpe22Type", "cpe:/o:canonical:ubuntu_linux:10.04:-:lts"),
(ExternalPackageRefCategory.SECURITY, "cpe23Type", "cpe:2.3:o:canonical:ubuntu_linux:10.04:-:lts:*:*:*:*:*"),
(ExternalPackageRefCategory.SECURITY, "advisory", "https://nvd.nist.gov/vuln/detail/CVE-2020-28498"),
(ExternalPackageRefCategory.SECURITY, "fix", "https://github.com/indutny/elliptic/commit/441b7428"),
(
ExternalPackageRefCategory.SECURITY,
"url",
"https://github.com/christianlundkvist/blog/blob/master/2020_05_26_secp256k1_twist_attacks/"
"secp256k1_twist_attacks.md",
),
(ExternalPackageRefCategory.SECURITY, "swid", "swid:2df9de35-0aff-4a86-ace6-f7dddd1ade4c"),
(ExternalPackageRefCategory.PACKAGE_MANAGER, "maven-central", "org.apache.tomcat:tomcat:9.0.0.M4"),
(ExternalPackageRefCategory.PACKAGE_MANAGER, "npm", "http-server@0.3.0"),
(ExternalPackageRefCategory.PACKAGE_MANAGER, "nuget", "Microsoft.AspNet.MVC/5.0.0"),
(ExternalPackageRefCategory.PACKAGE_MANAGER, "bower", "modernizr#2.6.2"),
(
ExternalPackageRefCategory.PACKAGE_MANAGER,
"purl",
"pkg:docker/debian@sha256:2f04d3d33b6027bb74ecc81397abe780649ec89f1a2af18d7022737d0482cefe",
),
(
ExternalPackageRefCategory.PACKAGE_MANAGER,
"purl",
"pkg:bitbucket/birkenfeld/pygments-main@244fd47e07d1014f0aed9c",
),
(ExternalPackageRefCategory.PACKAGE_MANAGER, "purl", "pkg:deb/debian/curl@7.50.3-1?arch=i386&distro=jessie"),
(
ExternalPackageRefCategory.PACKAGE_MANAGER,
"purl",
"pkg:docker/customer/dockerimage@sha256:244fd47e07d1004f0aed9c?repository_url=gcr.io",
),
(ExternalPackageRefCategory.PACKAGE_MANAGER, "purl", "pkg:gem/jruby-launcher@1.1.2?platform=java"),
(ExternalPackageRefCategory.PACKAGE_MANAGER, "purl", "pkg:gem/ruby-advisory-db-check@0.12.4"),
(
ExternalPackageRefCategory.PACKAGE_MANAGER,
"purl",
"pkg:github/package-url/purl-spec@244fd47e07d1004f0aed9c",
),
(
ExternalPackageRefCategory.PACKAGE_MANAGER,
"purl",
"pkg:golang/google.golang.org/genproto#googleapis/api/annotations",
),
(
ExternalPackageRefCategory.PACKAGE_MANAGER,
"purl",
"pkg:maven/org.apache.xmlgraphics/batik-anim@1.9.1?repository_url=repo.spring.io%2Frelease",
),
(ExternalPackageRefCategory.PACKAGE_MANAGER, "purl", "pkg:npm/%40angular/animation@12.3.1"),
(ExternalPackageRefCategory.PACKAGE_MANAGER, "purl", "pkg:nuget/EnterpriseLibrary.Common@6.0.1304"),
(
ExternalPackageRefCategory.PACKAGE_MANAGER,
"purl",
"pkg:rpm/fedora/curl@7.50.3-1.fc25?arch=i386&distro=fedora-25",
),
(ExternalPackageRefCategory.PERSISTENT_ID, "swh", "swh:1:cnt:94a9ed024d3859793618152ea559a168bbcbb5e2"),
(ExternalPackageRefCategory.PERSISTENT_ID, "swh", "swh:1:dir:d198bc9d7a6bcf6db04f476d29314f157507d505"),
(ExternalPackageRefCategory.PERSISTENT_ID, "swh", "swh:1:rev:309cf2674ee7a0749978cf8265ab91a60aea0f7d"),
(ExternalPackageRefCategory.PERSISTENT_ID, "swh", "swh:1:rel:22ece559cc7cc2364edc5e5593d63ae8bd229f9f"),
(ExternalPackageRefCategory.PERSISTENT_ID, "swh", "swh:1:snp:c7c108084bc0bf3d81436bf980b46e98bd338453"),
(
ExternalPackageRefCategory.PERSISTENT_ID,
"gitoid",
"gitoid:blob:sha1:261eeb9e9f8b2b4b0d119366dda99c6fd7d35c64",
),
(
ExternalPackageRefCategory.PERSISTENT_ID,
"gitoid",
"gitoid:blob:sha256:3557f7eb43c621c71483743d4b37059bb80933e7f71277c0c3b3846159d1f61c",
),
(ExternalPackageRefCategory.OTHER, "some idstring", "#//string-withOUT!Spaces\\?"),
],
)
def test_valid_external_package_ref(category, reference_type, locator):
external_package_ref = ExternalPackageRef(category, reference_type, locator, "externalPackageRef comment")
validation_messages: List[ValidationMessage] = validate_external_package_ref(
external_package_ref, "parent_id", "SPDX-2.3"
)
assert validation_messages == []
@pytest.mark.parametrize(
"category, reference_type, locator, expected_message",
[
(
ExternalPackageRefCategory.SECURITY,
"cpe22Typo",
"cpe:/o:canonical:ubuntu_linux:10.04:-:lts",
"externalPackageRef type in category SECURITY must be one of ['cpe22Type', 'cpe23Type', 'advisory', 'fix'"
", 'url', 'swid'], but is: cpe22Typo",
),
(
ExternalPackageRefCategory.PACKAGE_MANAGER,
"nugat",
"cpe:/o:canonical:ubuntu_linux:10.04:-:lts",
"externalPackageRef type in category PACKAGE_MANAGER must be one of ['maven-central', 'npm', 'nuget',"
" 'bower', 'purl'], but is: nugat",
),
(
ExternalPackageRefCategory.PERSISTENT_ID,
"git-oid",
"cpe:/o:canonical:ubuntu_linux:10.04:-:lts",
"externalPackageRef type in category PERSISTENT_ID must be one of ['swh', 'gitoid'], but is: git-oid",
),
],
)
def test_invalid_external_package_ref_types(category, reference_type, locator, expected_message):
external_package_ref = ExternalPackageRef(category, reference_type, locator, "externalPackageRef comment")
parent_id = "SPDXRef-Package"
validation_messages: List[ValidationMessage] = validate_external_package_ref(
external_package_ref, parent_id, "SPDX-2.3"
)
expected = ValidationMessage(
expected_message,
ValidationContext(
parent_id=parent_id, element_type=SpdxElementType.EXTERNAL_PACKAGE_REF, full_element=external_package_ref
),
)
assert validation_messages == [expected]
@pytest.mark.parametrize(
"category, reference_type, locator, expected_message",
[
(
ExternalPackageRefCategory.SECURITY,
"cpe22Type",
"cpe:o:canonical:ubuntu_linux:10.04:-:lts",
f'externalPackageRef locator of type "cpe22Type" must conform with the regex {CPE22TYPE_REGEX}, but is: '
f"cpe:o:canonical:ubuntu_linux:10.04:-:lts",
),
(
ExternalPackageRefCategory.SECURITY,
"cpe23Type",
"cpe:2.3:/o:canonical:ubuntu_linux:10.04:-:lts:*:*:*:*:*",
f'externalPackageRef locator of type "cpe23Type" must conform with the regex {CPE23TYPE_REGEX}, but is: '
f"cpe:2.3:/o:canonical:ubuntu_linux:10.04:-:lts:*:*:*:*:*",
),
(
ExternalPackageRefCategory.SECURITY,
"advisory",
"http://locatorurl",
'externalPackageRef locator of type "advisory" must be a valid URL, but is: http://locatorurl',
),
(
ExternalPackageRefCategory.SECURITY,
"fix",
"http://fixurl",
'externalPackageRef locator of type "fix" must be a valid URL, but is: http://fixurl',
),
(
ExternalPackageRefCategory.SECURITY,
"url",
"http://url",
'externalPackageRef locator of type "url" must be a valid URL, but is: http://url',
),
(
ExternalPackageRefCategory.SECURITY,
"swid",
"2df9de35-0aff-4a86-ace6-f7dddd1ade4c",
'externalPackageRef locator of type "swid" must be a valid URI with scheme swid, but is: '
"2df9de35-0aff-4a86-ace6-f7dddd1ade4c",
),
(
ExternalPackageRefCategory.PACKAGE_MANAGER,
"maven-central",
"org.apache.tomcat:tomcat:tomcat:9.0.0.M4",
f'externalPackageRef locator of type "maven-central" must conform with the regex {MAVEN_CENTRAL_REGEX}, '
f"but is: org.apache.tomcat:tomcat:tomcat:9.0.0.M4",
),
(
ExternalPackageRefCategory.PACKAGE_MANAGER,
"npm",
"http-server:0.3.0",
f'externalPackageRef locator of type "npm" must conform with the regex {NPM_REGEX}, '
f"but is: http-server:0.3.0",
),
(
ExternalPackageRefCategory.PACKAGE_MANAGER,
"nuget",
"Microsoft.AspNet.MVC@5.0.0",
f'externalPackageRef locator of type "nuget" must conform with the regex {NUGET_REGEX}, '
f"but is: Microsoft.AspNet.MVC@5.0.0",
),
(
ExternalPackageRefCategory.PACKAGE_MANAGER,
"bower",
"modernizr:2.6.2",
f'externalPackageRef locator of type "bower" must conform with the regex {BOWER_REGEX}, '
f"but is: modernizr:2.6.2",
),
(
ExternalPackageRefCategory.PACKAGE_MANAGER,
"purl",
"pkg:npm@12.3.1",
f'externalPackageRef locator of type "purl" must conform with the regex {PURL_REGEX}, '
f"but is: pkg:npm@12.3.1",
),
(
ExternalPackageRefCategory.PERSISTENT_ID,
"swh",
"swh:cnt:94a9ed024d3859793618152ea559a168bbcbb5e2",
f'externalPackageRef locator of type "swh" must conform with the regex {SWH_REGEX}, '
f"but is: swh:cnt:94a9ed024d3859793618152ea559a168bbcbb5e2",
),
(
ExternalPackageRefCategory.PERSISTENT_ID,
"gitoid",
"gitoid:blob:sha1:3557f7eb43c621c71483743d4b37059bb80933e7f71277c0c3b3846159d1f61c",
f'externalPackageRef locator of type "gitoid" must conform with the regex {GITOID_REGEX}, '
f"but is: gitoid:blob:sha1:3557f7eb43c621c71483743d4b37059bb80933e7f71277c0c3b3846159d1f61c",
),
(
ExternalPackageRefCategory.PERSISTENT_ID,
"gitoid",
"gitoid:blob:sha256:261eeb9e9f8b2b4b0d119366dda99c6fd7d35c64",
f'externalPackageRef locator of type "gitoid" must conform with the regex {GITOID_REGEX},'
f" but is: gitoid:blob:sha256:261eeb9e9f8b2b4b0d119366dda99c6fd7d35c64",
),
(
ExternalPackageRefCategory.OTHER,
"id string",
"locator string",
"externalPackageRef locator in category OTHER must contain no spaces, but is: locator string",
),
],
)
def test_invalid_external_package_ref_locators(category, reference_type, locator, expected_message):
external_package_ref = ExternalPackageRef(category, reference_type, locator, "externalPackageRef comment")
parent_id = "SPDXRef-Package"
validation_messages: List[ValidationMessage] = validate_external_package_ref(
external_package_ref, parent_id, "SPDX-2.3"
)
expected = ValidationMessage(
expected_message,
ValidationContext(
parent_id=parent_id, element_type=SpdxElementType.EXTERNAL_PACKAGE_REF, full_element=external_package_ref
),
)
assert validation_messages == [expected]
@pytest.mark.parametrize(
"category, reference_type, locator",
[
(ExternalPackageRefCategory.SECURITY, "advisory", "https://nvd.nist.gov/vuln/detail/CVE-2020-28498"),
(ExternalPackageRefCategory.SECURITY, "fix", "https://github.com/indutny/elliptic/commit/441b7428"),
(
ExternalPackageRefCategory.SECURITY,
"url",
"https://github.com/christianlundkvist/blog/blob/master/2020_05_26_secp256k1_twist_attacks/"
"secp256k1_twist_attacks.md",
),
(ExternalPackageRefCategory.SECURITY, "swid", "swid:2df9de35-0aff-4a86-ace6-f7dddd1ade4c"),
],
)
def test_v2_3only_external_package_ref_types(category, reference_type, locator):
external_package_ref = ExternalPackageRef(category, reference_type, locator, "externalPackageRef comment")
parent_id = "SPDXRef-Package"
validation_messages: List[ValidationMessage] = validate_external_package_ref(
external_package_ref, parent_id, "SPDX-2.2"
)
expected = ValidationMessage(
f'externalPackageRef type "{reference_type}" is not supported in SPDX-2.2',
ValidationContext(
parent_id=parent_id, element_type=SpdxElementType.EXTERNAL_PACKAGE_REF, full_element=external_package_ref
),
)
assert validation_messages == [expected]
|