Skip to content

Commit 077752b

Browse files
#22 - Added constraint verification of purl types, including a check for Maven that ensures namespace and name both exist. Added unit tests.
1 parent 4f637d1 commit 077752b

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

src/main/java/com/github/packageurl/PackageURL.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ public PackageURL(final String type, final String namespace, final String name,
104104
this.version = validateVersion(version);
105105
this.qualifiers = validateQualifiers(qualifiers);
106106
this.subpath = validatePath(subpath, true);
107+
verifyTypeConstraints(this.type, this.namespace, this.name);
107108
}
108109

109110
/**
@@ -533,11 +534,27 @@ private void parse(final String purl) throws MalformedPackageURLException {
533534
remainder.setLength(index);
534535
this.namespace = validateNamespace(parsePath(remainder.substring(start), false));
535536
}
537+
verifyTypeConstraints(this.type, this.namespace, this.name);
536538
} catch (URISyntaxException e) {
537539
throw new MalformedPackageURLException("Invalid purl: " + e.getMessage());
538540
}
539541
}
540542

543+
/**
544+
* Some purl types may have specific constraints. This method attempts to verify them.
545+
* @param type the purl type
546+
* @param namespace the purl namespace
547+
* @param name the purl name
548+
* @throws MalformedPackageURLException if constraints are not met
549+
*/
550+
private void verifyTypeConstraints(String type, String namespace, String name) throws MalformedPackageURLException {
551+
if (StandardTypes.MAVEN.equals(type)) {
552+
if (namespace == null || namespace.isEmpty() || name == null || name.isEmpty()) {
553+
throw new MalformedPackageURLException("The PackageURL specified is invalid. Maven requires both a namespace and name.");
554+
}
555+
}
556+
}
557+
541558
@SuppressWarnings("StringSplitter")//reason: surprising behavior is okay in this case
542559
private Map<String, String> parseQualifiers(final String encodedString) throws MalformedPackageURLException {
543560
try {

src/test/resources/test-suite-data.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,30 @@
227227
"subpath": null,
228228
"is_invalid": true
229229
},
230+
{
231+
"description": "a namespace is required",
232+
"purl": "pkg:maven/io@1.3.4",
233+
"canonical_purl": "pkg:maven/io@1.3.4",
234+
"type": "maven",
235+
"namespace": null,
236+
"name": null,
237+
"version": null,
238+
"qualifiers": null,
239+
"subpath": null,
240+
"is_invalid": true
241+
},
242+
{
243+
"description": "a namespace is required",
244+
"purl": "pkg:maven//io@1.3.4",
245+
"canonical_purl": "pkg:maven//io@1.3.4",
246+
"type": "maven",
247+
"namespace": null,
248+
"name": null,
249+
"version": null,
250+
"qualifiers": null,
251+
"subpath": null,
252+
"is_invalid": true
253+
},
230254
{
231255
"description": "slash / after scheme is not significant",
232256
"purl": "pkg:/maven/org.apache.commons/io",

0 commit comments

Comments
 (0)