From ef9a9f07bc522c30e0705d4942e4aab6182373b6 Mon Sep 17 00:00:00 2001 From: Julian Date: Thu, 16 Mar 2023 02:07:45 -0400 Subject: [PATCH] Fix issues handling golang packages with multi-part namespaces. (#20) * Fix issues handling golang packages with multi-part namespaces. Added a test for it as well. * Update src/PackageUrl.cs Match packageurl-js implementation for namespace encoding. Co-authored-by: Adeel Mujahid <3840695+am11@users.noreply.github.com> --------- Co-authored-by: Adeel Mujahid <3840695+am11@users.noreply.github.com> --- src/PackageUrl.cs | 10 ++++++++-- tests/TestAssets/test-suite-data.json | 12 ++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/PackageUrl.cs b/src/PackageUrl.cs index 2a7a0e8..3d20bd1 100644 --- a/src/PackageUrl.cs +++ b/src/PackageUrl.cs @@ -42,6 +42,11 @@ namespace PackageUrl [Serializable] public sealed class PackageURL { + /// + /// The url encoding of /. + /// + private const string EncodedSlash = "%2F"; + private static readonly Regex s_typePattern = new Regex("^[a-zA-Z][a-zA-Z0-9.+-]+$", RegexOptions.Compiled); /// @@ -135,7 +140,8 @@ public override string ToString() purl.Append('/'); if (Namespace != null) { - purl.Append(WebUtility.UrlEncode(Namespace)); + string encodedNamespace = WebUtility.UrlEncode(Namespace).Replace(EncodedSlash, "/"); + purl.Append(encodedNamespace); purl.Append('/'); } if (Name != null) @@ -238,7 +244,7 @@ private void Parse(string purl) int i; for (i = 1; i < firstPartArray.Length - 2; ++i) { - @namespace += firstPartArray[i] + ','; + @namespace += firstPartArray[i] + '/'; } @namespace += firstPartArray[i]; diff --git a/tests/TestAssets/test-suite-data.json b/tests/TestAssets/test-suite-data.json index 4793dab..42182f1 100644 --- a/tests/TestAssets/test-suite-data.json +++ b/tests/TestAssets/test-suite-data.json @@ -47,6 +47,18 @@ "subpath": "googleapis/api/annotations", "is_invalid": false }, + { + "description": "valid go purl with version, subpath, and multi-part namespace", + "purl": "pkg:GOLANG/github.com/gorilla/context@234fd47e07d1004f0aed9c#api/", + "canonical_purl": "pkg:golang/github.com/gorilla/context@234fd47e07d1004f0aed9c#api", + "type": "golang", + "namespace": "github.com/gorilla", + "name": "context", + "version": "234fd47e07d1004f0aed9c", + "qualifiers": null, + "subpath": "api", + "is_invalid": false + }, { "description": "bitbucket namespace and name should be lowercased", "purl": "pkg:bitbucket/birKenfeld/pyGments-main@244fd47e07d1014f0aed9c",