From 8aa7d15c71c16b1a67c93554fc8bf8d5d9902b8d Mon Sep 17 00:00:00 2001 From: Simon Chrzanowski Date: Thu, 12 Nov 2026 19:02:31 +0100 Subject: [PATCH 1/3] Add the terraform type proposed in purl-spec#889 A local type allowance like bsd, so MakePURL splits Terraform addresses into a namespace and name instead of escaping the slash: hashicorp/random becomes pkg:terraform/hashicorp/random. A module address namespace/name/system splits at its last slash, as the existing parse tests read pkg:terraform/hashicorp/consul/aws. Names keep their case, and registry.terraform.io is the default registry. --- README.md | 2 +- ecosystem.go | 1 + makepurl.go | 13 +++++++++++++ makepurl_test.go | 11 +++++++++++ spec_test.go | 1 + types.json | 9 +++++++++ types_test.go | 3 +++ 7 files changed, 39 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 5e3a742..b89949d 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,7 @@ output stays stable for callers that store it: Type information is embedded in `types.json`. Type names and namespace requirements are checked against the pinned [purl-spec](https://github.com/package-url/purl-spec) submodule. Default registries and URL templates are maintained locally. -The local types `bioconductor`, `clojars`, `deno`, and `elm` are retained. `bsd` follows the FreeBSD type proposed in [purl-spec#496](https://github.com/package-url/purl-spec/pull/496); the `freebsd` ecosystem maps to `pkg:bsd/freebsd/...`. The legacy `homebrew` type keeps its existing PURLs and registry behavior; use `brew` explicitly for the registered type. Recognition of a type does not imply full support for its normalization or validation rules. +The local types `bioconductor`, `clojars`, `deno`, and `elm` are retained. `bsd` follows the FreeBSD type proposed in [purl-spec#496](https://github.com/package-url/purl-spec/pull/496); the `freebsd` ecosystem maps to `pkg:bsd/freebsd/...`. `terraform` follows the type proposed in [purl-spec#889](https://github.com/package-url/purl-spec/pull/889): a provider address `namespace/type` maps to `pkg:terraform/{namespace}/{type}`, a module address `namespace/name/system` to `pkg:terraform/{namespace}/{name}/{system}`, and the default registry is `https://registry.terraform.io`. The legacy `homebrew` type keeps its existing PURLs and registry behavior; use `brew` explicitly for the registered type. Recognition of a type does not imply full support for its normalization or validation rules. ```go purl.KnownTypes() // []string of all known PURL types diff --git a/ecosystem.go b/ecosystem.go index 6400cb4..74f7f91 100644 --- a/ecosystem.go +++ b/ecosystem.go @@ -21,6 +21,7 @@ const ( purlTypeGitHubActions = "githubactions" purlTypeVSCodeExtension = "vscode-extension" purlTypeBSD = "bsd" + purlTypeTerraform = "terraform" ) // purlTypeForEcosystem maps ecosystem names to PURL types. diff --git a/makepurl.go b/makepurl.go index fa3cddf..17560c4 100644 --- a/makepurl.go +++ b/makepurl.go @@ -241,6 +241,8 @@ func splitNamespace(ecosystem, name string) (namespace, pkgName string, ok bool) } case purlTypeVSCodeExtension: return splitVSCodeExtension(name) + case purlTypeTerraform: + return splitTerraform(name) case ecosystemSwift: return splitSwift(name) } @@ -269,6 +271,17 @@ func splitSwift(name string) (namespace, pkgName string, ok bool) { } // splitVSCodeExtension splits VS Code's publisher.name extension identifier. +// splitTerraform splits a Terraform address without its registry hostname: +// namespace/type for a provider, namespace/name/system for a module, whose +// namespace is then namespace/name. The type requires a namespace. +func splitTerraform(name string) (namespace, pkgName string, ok bool) { + i := strings.LastIndexByte(name, '/') + if i <= 0 || i == len(name)-1 { + return "", "", false + } + return name[:i], name[i+1:], true +} + func splitVSCodeExtension(name string) (publisher, extension string, ok bool) { publisher, extension, ok = strings.Cut(name, ".") if !ok || publisher == "" || extension == "" { diff --git a/makepurl_test.go b/makepurl_test.go index 86feb7d..f4915d7 100644 --- a/makepurl_test.go +++ b/makepurl_test.go @@ -95,6 +95,14 @@ func TestBuildPURLString(t *testing.T) { {"swift registry identity", "swift", "apple.swift-argument-parser", "1.8.2", "", "pkg:swift/apple/swift-argument-parser@1.8.2"}, {"swift registry path", "swift", "apple/swift-argument-parser", "1.8.2", "", ""}, {"swift registry URL path", "swift", "/apple/swift-argument-parser", "1.8.2", "", ""}, + {"terraform provider", "terraform", "hashicorp/random", "3.6.0", "", "pkg:terraform/hashicorp/random@3.6.0"}, + {"terraform module", "terraform", "hashicorp/consul/aws", "0.11.0", "", "pkg:terraform/hashicorp/consul/aws@0.11.0"}, + {"terraform case-sensitive", "terraform", "HashiCorp/Random", "3.6.0", "", "pkg:terraform/HashiCorp/Random@3.6.0"}, + {"terraform default registry ignored", "terraform", "hashicorp/random", "3.6.0", "https://registry.terraform.io", "pkg:terraform/hashicorp/random@3.6.0"}, + {"terraform other registry", "terraform", "hashicorp/random", "3.6.0", "https://registry.opentofu.org", "pkg:terraform/hashicorp/random@3.6.0?repository_url=https:%2F%2Fregistry.opentofu.org"}, + {"terraform without namespace", "terraform", "random", "3.6.0", "", ""}, + {"terraform empty name", "terraform", "hashicorp/", "3.6.0", "", ""}, + {"terraform empty namespace", "terraform", "/random", "3.6.0", "", ""}, } for _, tt := range tests { @@ -126,6 +134,9 @@ func TestBuildPURLStringMatchesMakePURL(t *testing.T) { {"npm", "lodash", "^1.0.0", ""}, {"npm", "pkg", "1.0.0", "https://custom.registry.com"}, {"swift", "github.com/apple/swift-argument-parser", "1.8.2", ""}, + {"terraform", "hashicorp/random", "3.6.0", ""}, + {"terraform", "hashicorp/consul/aws", "0.11.0", ""}, + {"terraform", "hashicorp/random", "3.6.0", "https://registry.opentofu.org"}, } for _, tt := range cases { diff --git a/spec_test.go b/spec_test.go index 75cd1be..928d3cf 100644 --- a/spec_test.go +++ b/spec_test.go @@ -33,6 +33,7 @@ func TestSpecTypeMetadata(t *testing.T) { "deno": "Deno registry support", "elm": "Elm registry support", "homebrew": "Legacy type retained alongside brew for compatibility", + "terraform": "Terraform providers and modules, proposed in purl-spec#889", } for _, name := range purl.KnownTypes() { if !registered[name] && localTypes[name] == "" { diff --git a/types.json b/types.json index 1c77a64..43aca02 100644 --- a/types.json +++ b/types.json @@ -585,6 +585,15 @@ } } }, + "terraform": { + "description": "Terraform providers and modules, proposed in purl-spec#889", + "default_registry": "https://registry.terraform.io", + "namespace_requirement": "required", + "examples": [ + "pkg:terraform/hashicorp/aws@6.0.0", + "pkg:terraform/terraform-aws-modules/vpc@6.0.1" + ] + }, "vcpkg": { "description": "Packages from the vcpkg C/C++ package manager.", "default_registry": null, diff --git a/types_test.go b/types_test.go index d1ff3a8..2593a93 100644 --- a/types_test.go +++ b/types_test.go @@ -81,6 +81,7 @@ func TestIsKnownType(t *testing.T) { {"npm", true}, {"pypi", true}, {"cargo", true}, + {"terraform", true}, {"unknown-type", false}, {"", false}, } @@ -102,6 +103,7 @@ func TestDefaultRegistry(t *testing.T) { {"npm", "https://registry.npmjs.org"}, {"pypi", "https://pypi.org"}, {"cargo", "https://crates.io"}, + {"terraform", "https://registry.terraform.io"}, {"apk", ""}, // no default registry {"deb", ""}, // no default registry {"unknown", ""}, @@ -128,6 +130,7 @@ func TestNamespaceRequirement(t *testing.T) { {"cran", false, true}, {"npm", false, false}, // optional {"cargo", false, true}, + {"terraform", true, false}, } for _, tt := range tests { From 3b4518c907fc9a26f8fecf825f1d468646e6e77d Mon Sep 17 00:00:00 2001 From: Simon Chrzanowski Date: Thu, 12 Nov 2026 19:10:49 +0100 Subject: [PATCH 2/3] Reject Terraform addresses with an empty segment //random would split into namespace / and name random, and the namespace normalizes to empty, so the PURL lost its required namespace. A leading slash or an empty segment anywhere now rejects the address. --- makepurl.go | 5 +++-- makepurl_test.go | 4 ++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/makepurl.go b/makepurl.go index 17560c4..95b11e1 100644 --- a/makepurl.go +++ b/makepurl.go @@ -273,10 +273,11 @@ func splitSwift(name string) (namespace, pkgName string, ok bool) { // splitVSCodeExtension splits VS Code's publisher.name extension identifier. // splitTerraform splits a Terraform address without its registry hostname: // namespace/type for a provider, namespace/name/system for a module, whose -// namespace is then namespace/name. The type requires a namespace. +// namespace is then namespace/name. The type requires a namespace, so an +// address without one, or with an empty segment, is rejected. func splitTerraform(name string) (namespace, pkgName string, ok bool) { i := strings.LastIndexByte(name, '/') - if i <= 0 || i == len(name)-1 { + if i <= 0 || i == len(name)-1 || name[0] == '/' || strings.Contains(name, "//") { return "", "", false } return name[:i], name[i+1:], true diff --git a/makepurl_test.go b/makepurl_test.go index f4915d7..1970800 100644 --- a/makepurl_test.go +++ b/makepurl_test.go @@ -103,6 +103,10 @@ func TestBuildPURLString(t *testing.T) { {"terraform without namespace", "terraform", "random", "3.6.0", "", ""}, {"terraform empty name", "terraform", "hashicorp/", "3.6.0", "", ""}, {"terraform empty namespace", "terraform", "/random", "3.6.0", "", ""}, + {"terraform slash-only namespace", "terraform", "//random", "3.6.0", "", ""}, + {"terraform leading slash", "terraform", "/hashicorp/random", "3.6.0", "", ""}, + {"terraform empty segment", "terraform", "hashicorp//random", "3.6.0", "", ""}, + {"terraform module empty segment", "terraform", "hashicorp/consul//aws", "0.11.0", "", ""}, } for _, tt := range tests { From 015783f7799f50cd3ebf35b235d426760bafcce1 Mon Sep 17 00:00:00 2001 From: Simon Chrzanowski Date: Sun, 15 Nov 2026 13:56:06 +0100 Subject: [PATCH 3/3] Reject Terraform addresses with a hostname and lowercase providers An address with a dot in its first segment carries a registry hostname, and one with more than three segments is no provider or module; both are now rejected. Terraform folds the case of a provider's namespace and type, so a two-segment address is lowercased. A module keeps its case. --- README.md | 2 +- makepurl.go | 32 +++++++++++++++--------- makepurl_test.go | 7 +++++- purl.go | 7 ++++++ terraform_test.go | 63 +++++++++++++++++++++++++++++++++++++++++++++++ types.json | 2 +- 6 files changed, 99 insertions(+), 14 deletions(-) create mode 100644 terraform_test.go diff --git a/README.md b/README.md index b89949d..31ab202 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,7 @@ output stays stable for callers that store it: Type information is embedded in `types.json`. Type names and namespace requirements are checked against the pinned [purl-spec](https://github.com/package-url/purl-spec) submodule. Default registries and URL templates are maintained locally. -The local types `bioconductor`, `clojars`, `deno`, and `elm` are retained. `bsd` follows the FreeBSD type proposed in [purl-spec#496](https://github.com/package-url/purl-spec/pull/496); the `freebsd` ecosystem maps to `pkg:bsd/freebsd/...`. `terraform` follows the type proposed in [purl-spec#889](https://github.com/package-url/purl-spec/pull/889): a provider address `namespace/type` maps to `pkg:terraform/{namespace}/{type}`, a module address `namespace/name/system` to `pkg:terraform/{namespace}/{name}/{system}`, and the default registry is `https://registry.terraform.io`. The legacy `homebrew` type keeps its existing PURLs and registry behavior; use `brew` explicitly for the registered type. Recognition of a type does not imply full support for its normalization or validation rules. +The local types `bioconductor`, `clojars`, `deno`, and `elm` are retained. `bsd` follows the FreeBSD type proposed in [purl-spec#496](https://github.com/package-url/purl-spec/pull/496); the `freebsd` ecosystem maps to `pkg:bsd/freebsd/...`. `terraform` follows the type proposed in [purl-spec#889](https://github.com/package-url/purl-spec/pull/889): a provider address `namespace/type` maps to `pkg:terraform/{namespace}/{type}`, a module address `namespace/name/system` to `pkg:terraform/{namespace}/{name}/{system}`, and the default registry is `https://registry.terraform.io`. A provider's namespace and type are lowercased, as Terraform folds their case; a module keeps its case. An address that carries a registry hostname, such as `registry.terraform.io/hashicorp/random`, is rejected. The legacy `homebrew` type keeps its existing PURLs and registry behavior; use `brew` explicitly for the registered type. Recognition of a type does not imply full support for its normalization or validation rules. ```go purl.KnownTypes() // []string of all known PURL types diff --git a/makepurl.go b/makepurl.go index 95b11e1..1ae8a23 100644 --- a/makepurl.go +++ b/makepurl.go @@ -271,24 +271,34 @@ func splitSwift(name string) (namespace, pkgName string, ok bool) { } // splitVSCodeExtension splits VS Code's publisher.name extension identifier. +func splitVSCodeExtension(name string) (publisher, extension string, ok bool) { + publisher, extension, ok = strings.Cut(name, ".") + if !ok || publisher == "" || extension == "" { + return "", "", false + } + return publisher, extension, true +} + // splitTerraform splits a Terraform address without its registry hostname: // namespace/type for a provider, namespace/name/system for a module, whose -// namespace is then namespace/name. The type requires a namespace, so an -// address without one, or with an empty segment, is rejected. +// namespace is then namespace/name. func splitTerraform(name string) (namespace, pkgName string, ok bool) { - i := strings.LastIndexByte(name, '/') - if i <= 0 || i == len(name)-1 || name[0] == '/' || strings.Contains(name, "//") { + const providerSegments, moduleSegments = 2, 3 + segments := strings.Split(name, "/") + if len(segments) < providerSegments || len(segments) > moduleSegments { return "", "", false } - return name[:i], name[i+1:], true -} - -func splitVSCodeExtension(name string) (publisher, extension string, ok bool) { - publisher, extension, ok = strings.Cut(name, ".") - if !ok || publisher == "" || extension == "" { + for _, segment := range segments { + if segment == "" { + return "", "", false + } + } + // Terraform allows no dot in a namespace, so one marks a hostname. + if strings.Contains(segments[0], ".") { return "", "", false } - return publisher, extension, true + i := strings.LastIndexByte(name, '/') + return name[:i], name[i+1:], true } // writeComponentEscaped writes s to b, percent-encoding characters that are not safe diff --git a/makepurl_test.go b/makepurl_test.go index 1970800..2a979ca 100644 --- a/makepurl_test.go +++ b/makepurl_test.go @@ -97,7 +97,12 @@ func TestBuildPURLString(t *testing.T) { {"swift registry URL path", "swift", "/apple/swift-argument-parser", "1.8.2", "", ""}, {"terraform provider", "terraform", "hashicorp/random", "3.6.0", "", "pkg:terraform/hashicorp/random@3.6.0"}, {"terraform module", "terraform", "hashicorp/consul/aws", "0.11.0", "", "pkg:terraform/hashicorp/consul/aws@0.11.0"}, - {"terraform case-sensitive", "terraform", "HashiCorp/Random", "3.6.0", "", "pkg:terraform/HashiCorp/Random@3.6.0"}, + {"terraform provider lowercased", "terraform", "HashiCorp/Random", "3.6.0", "", "pkg:terraform/hashicorp/random@3.6.0"}, + {"terraform module keeps case", "terraform", "HashiCorp/Consul/AWS", "0.11.0", "", "pkg:terraform/HashiCorp/Consul/AWS@0.11.0"}, + {"terraform provider with hostname", "terraform", "registry.terraform.io/hashicorp/random", "3.6.0", "", ""}, + {"terraform module with hostname", "terraform", "registry.terraform.io/hashicorp/consul/aws", "0.11.0", "", ""}, + {"terraform two segments with hostname", "terraform", "example.com/random", "3.6.0", "", ""}, + {"terraform four segments", "terraform", "a/b/c/d", "1.0.0", "", ""}, {"terraform default registry ignored", "terraform", "hashicorp/random", "3.6.0", "https://registry.terraform.io", "pkg:terraform/hashicorp/random@3.6.0"}, {"terraform other registry", "terraform", "hashicorp/random", "3.6.0", "https://registry.opentofu.org", "pkg:terraform/hashicorp/random@3.6.0?repository_url=https:%2F%2Fregistry.opentofu.org"}, {"terraform without namespace", "terraform", "random", "3.6.0", "", ""}, diff --git a/purl.go b/purl.go index 0a4df72..1b149b2 100644 --- a/purl.go +++ b/purl.go @@ -113,6 +113,13 @@ func lowercaseCaseInsensitive(p *packageurl.PackageURL) { p.Name = strings.ToLower(p.Name) case purlTypeBSD: p.Namespace = strings.ToLower(p.Namespace) + case purlTypeTerraform: + // Terraform folds a provider's namespace and type, and keeps a + // module's (namespace/name/system) as given. + if !strings.Contains(p.Namespace, "/") { + p.Namespace = strings.ToLower(p.Namespace) + p.Name = strings.ToLower(p.Name) + } } } diff --git a/terraform_test.go b/terraform_test.go new file mode 100644 index 0000000..e0d6b14 --- /dev/null +++ b/terraform_test.go @@ -0,0 +1,63 @@ +package purl_test + +import ( + "testing" + + "github.com/git-pkgs/purl" +) + +// Terraform folds the case of a provider's namespace and type, and keeps a +// module's as given. +func TestTerraformProviderIsLowercased(t *testing.T) { + const want = "pkg:terraform/hashicorp/random@3.6.0" + parsed, err := purl.Parse("pkg:terraform/HashiCorp/Random@3.6.0") + if err != nil { + t.Fatal(err) + } + for _, got := range []string{ + parsed.String(), + purl.New("terraform", "HashiCorp", "Random", "3.6.0", nil).String(), + purl.MakePURL("terraform", "HashiCorp/Random", "3.6.0").String(), + purl.MakePURLString("terraform", "HashiCorp/Random", "3.6.0"), + purl.BuildPURLString("terraform", "HashiCorp/Random", "3.6.0", ""), + } { + if got != want { + t.Errorf("got %q, want %q", got, want) + } + } +} + +func TestTerraformModuleKeepsCase(t *testing.T) { + const want = "pkg:terraform/HashiCorp/Consul/AWS@0.11.0" + parsed, err := purl.Parse(want) + if err != nil { + t.Fatal(err) + } + for _, got := range []string{ + parsed.String(), + purl.New("terraform", "HashiCorp/Consul", "AWS", "0.11.0", nil).String(), + purl.MakePURL("terraform", "HashiCorp/Consul/AWS", "0.11.0").String(), + purl.MakePURLString("terraform", "HashiCorp/Consul/AWS", "0.11.0"), + purl.BuildPURLString("terraform", "HashiCorp/Consul/AWS", "0.11.0", ""), + } { + if got != want { + t.Errorf("got %q, want %q", got, want) + } + } +} + +func TestTerraformRejectsHostnamesAndExtraSegments(t *testing.T) { + for _, name := range []string{ + "registry.terraform.io/hashicorp/random", + "registry.terraform.io/hashicorp/consul/aws", + "example.com/random", + "a/b/c/d", + } { + if got := purl.MakePURLString("terraform", name, "1.0.0"); got != "" { + t.Errorf("MakePURLString(%q) = %q, want empty", name, got) + } + if got := purl.MakePURL("terraform", name, "1.0.0"); got != nil { + t.Errorf("MakePURL(%q) = %v, want nil", name, got) + } + } +} diff --git a/types.json b/types.json index 43aca02..d3fd0c8 100644 --- a/types.json +++ b/types.json @@ -591,7 +591,7 @@ "namespace_requirement": "required", "examples": [ "pkg:terraform/hashicorp/aws@6.0.0", - "pkg:terraform/terraform-aws-modules/vpc@6.0.1" + "pkg:terraform/hashicorp/consul/aws@0.11.0" ] }, "vcpkg": {