Skip to content

Commit

Permalink
Fix issues handling golang packages with multi-part namespaces. (#20)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
jpinz and am11 committed Mar 16, 2023
1 parent 7b27f39 commit ef9a9f0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/PackageUrl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ namespace PackageUrl
[Serializable]
public sealed class PackageURL
{
/// <summary>
/// The url encoding of /.
/// </summary>
private const string EncodedSlash = "%2F";

private static readonly Regex s_typePattern = new Regex("^[a-zA-Z][a-zA-Z0-9.+-]+$", RegexOptions.Compiled);

/// <summary>
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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];

Expand Down
12 changes: 12 additions & 0 deletions tests/TestAssets/test-suite-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit ef9a9f0

Please sign in to comment.