Skip to content

Commit

Permalink
fix(model): Normalize purl name(space segments)
Browse files Browse the repository at this point in the history
The algorithm description at [1] demands to "apply type-specific
normalization" to namespace segments and the name before applying
percent-encoding. In general, type-specific requirements are documented
at [2]. For Bazel the PR still pending, but in the current state
lowercasing of the name should be performed [3].

[1]: https://github.com/package-url/purl-spec/blob/master/PURL-SPECIFICATION.rst#how-to-build-purl-string-from-its-components
[2]: https://github.com/package-url/purl-spec/blob/master/PURL-TYPES.rst
[3]: package-url/purl-spec#317

Signed-off-by: Sebastian Schuberth <[email protected]>
  • Loading branch information
sschuberth committed Nov 22, 2024
1 parent af79e99 commit 558e7aa
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions model/src/main/kotlin/utils/PurlUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,35 +29,39 @@ import org.ossreviewtoolkit.utils.common.percentEncode
/**
* A subset of the Package URL types defined at https://github.com/package-url/purl-spec/blob/ad8a673/PURL-TYPES.rst.
*/
enum class PurlType(private val value: String) {
APK("apk"),
BAZEL("bazel"),
BITBUCKET("bitbucket"),
enum class PurlType(
private val value: String,
val nameNormalization: (String) -> String = { it },
val namespaceNormalization: (String) -> String = { it }
) {
APK("apk", { it.lowercase() }, { it.lowercase() }),
BAZEL("bazel", { it.lowercase() }),
BITBUCKET("bitbucket", { it.lowercase() }, { it.lowercase() }),
BOWER("bower"),
CARGO("cargo"),
CARTHAGE("carthage"),
COCOAPODS("cocoapods"),
COMPOSER("composer"),
COMPOSER("composer", { it.lowercase() }, { it.lowercase() }),
CONAN("conan"),
CONDA("conda"),
CRAN("cran"),
DEBIAN("deb"),
DEBIAN("deb", { it.lowercase() }, { it.lowercase() }),
DOCKER("docker"),
DRUPAL("drupal"),
GEM("gem"),
GENERIC("generic"),
GITHUB("github"),
GITHUB("github", { it.lowercase() }, { it.lowercase() }),
GITLAB("gitlab"),
GOLANG("golang"),
GOLANG("golang", { it.lowercase() }, { it.lowercase() }),
HACKAGE("hackage"),
HUGGING_FACE("huggingface"),
MAVEN("maven"),
MLFLOW("mlflow"),
NPM("npm"),
NPM("npm", { it.lowercase() }),
NUGET("nuget"),
PUB("pub"),
PYPI("pypi"),
RPM("rpm"),
PUB("pub", { it.lowercase() }),
PYPI("pypi", { it.lowercase() }),
RPM("rpm", namespaceNormalization = { it.lowercase() }),
SWIFT("swift");

init {
Expand Down Expand Up @@ -112,11 +116,15 @@ internal fun createPurl(
append('/')

if (namespace.isNotEmpty()) {
append(namespace.trim('/').split('/').joinToString("/") { it.percentEncode() })
append(
namespace.trim('/').split('/').joinToString("/") { segment ->
type.namespaceNormalization(segment).percentEncode()
}
)
append('/')
}

append(name.trim('/').percentEncode())
append(type.nameNormalization(name.trim('/')).percentEncode())

if (version.isNotEmpty()) {
append('@')
Expand Down

0 comments on commit 558e7aa

Please sign in to comment.