Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package purl

import (
"errors"
"net/url"
"regexp"
"strings"
"sync"
Expand Down Expand Up @@ -86,9 +87,9 @@ func expandTemplate(rc *RegistryConfig, namespace, name, version string) (string

// Expand template variables
result := template
result = strings.ReplaceAll(result, "{namespace}", displayNamespace)
result = strings.ReplaceAll(result, "{name}", name)
result = strings.ReplaceAll(result, "{version}", version)
result = strings.ReplaceAll(result, "{namespace}", url.PathEscape(displayNamespace))
result = strings.ReplaceAll(result, "{name}", url.PathEscape(name))
result = strings.ReplaceAll(result, "{version}", url.PathEscape(version))

return result, nil
}
Expand Down
13 changes: 13 additions & 0 deletions registry_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package purl

import (
"strings"
"testing"
)

Expand Down Expand Up @@ -191,6 +192,18 @@ func TestParseRegistryURLWithType(t *testing.T) {
}
}

func TestRegistryURLEscapesSpecialChars(t *testing.T) {
// A name with path traversal characters should be escaped in the URL
p := New("npm", "", "evil/../../secret", "", nil)
got, err := p.RegistryURL()
if err != nil {
t.Fatalf("RegistryURL() error: %v", err)
}
if strings.Contains(got, "../../") {
t.Errorf("expected path traversal to be escaped in URL, got %s", got)
}
}

func TestParseRegistryURL(t *testing.T) {
tests := []struct {
url string
Expand Down