packageurl.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*
  2. Copyright (c) the purl authors
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in all
  10. copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. SOFTWARE.
  18. */
  19. // Package packageurl implements the package-url spec
  20. package packageurl
  21. import (
  22. "errors"
  23. "fmt"
  24. "net/url"
  25. "regexp"
  26. "sort"
  27. "strings"
  28. )
  29. var (
  30. // QualifierKeyPattern describes a valid qualifier key:
  31. //
  32. // - The key must be composed only of ASCII letters and numbers, '.',
  33. // '-' and '_' (period, dash and underscore).
  34. // - A key cannot start with a number.
  35. QualifierKeyPattern = regexp.MustCompile(`^[A-Za-z\.\-_][0-9A-Za-z\.\-_]*$`)
  36. )
  37. // These are the known purl types as defined in the spec. Some of these require
  38. // special treatment during parsing.
  39. // https://github.com/package-url/purl-spec#known-purl-types
  40. var (
  41. // TypeBitbucket is a pkg:bitbucket purl.
  42. TypeBitbucket = "bitbucket"
  43. // TypeCocoapods is a pkg:cocoapods purl.
  44. TypeCocoapods = "cocoapods"
  45. // TypeCargo is a pkg:cargo purl.
  46. TypeCargo = "cargo"
  47. // TypeComposer is a pkg:composer purl.
  48. TypeComposer = "composer"
  49. // TypeConan is a pkg:conan purl.
  50. TypeConan = "conan"
  51. // TypeConda is a pkg:conda purl.
  52. TypeConda = "conda"
  53. // TypeCran is a pkg:cran purl.
  54. TypeCran = "cran"
  55. // TypeDebian is a pkg:deb purl.
  56. TypeDebian = "deb"
  57. // TypeDocker is a pkg:docker purl.
  58. TypeDocker = "docker"
  59. // TypeGem is a pkg:gem purl.
  60. TypeGem = "gem"
  61. // TypeGeneric is a pkg:generic purl.
  62. TypeGeneric = "generic"
  63. // TypeGithub is a pkg:github purl.
  64. TypeGithub = "github"
  65. // TypeGolang is a pkg:golang purl.
  66. TypeGolang = "golang"
  67. // TypeHackage is a pkg:hackage purl.
  68. TypeHackage = "hackage"
  69. // TypeHex is a pkg:hex purl.
  70. TypeHex = "hex"
  71. // TypeMaven is a pkg:maven purl.
  72. TypeMaven = "maven"
  73. // TypeNPM is a pkg:npm purl.
  74. TypeNPM = "npm"
  75. // TypeNuget is a pkg:nuget purl.
  76. TypeNuget = "nuget"
  77. // TypeOCI is a pkg:oci purl
  78. TypeOCI = "oci"
  79. // TypePyPi is a pkg:pypi purl.
  80. TypePyPi = "pypi"
  81. // TypeRPM is a pkg:rpm purl.
  82. TypeRPM = "rpm"
  83. // TypeSwift is pkg:swift purl
  84. TypeSwift = "swift"
  85. )
  86. // Qualifier represents a single key=value qualifier in the package url
  87. type Qualifier struct {
  88. Key string
  89. Value string
  90. }
  91. func (q Qualifier) String() string {
  92. // A value must be a percent-encoded string
  93. return fmt.Sprintf("%s=%s", q.Key, url.PathEscape(q.Value))
  94. }
  95. // Qualifiers is a slice of key=value pairs, with order preserved as it appears
  96. // in the package URL.
  97. type Qualifiers []Qualifier
  98. // QualifiersFromMap constructs a Qualifiers slice from a string map. To get a
  99. // deterministic qualifier order (despite maps not providing any iteration order
  100. // guarantees) the returned Qualifiers are sorted in increasing order of key.
  101. func QualifiersFromMap(mm map[string]string) Qualifiers {
  102. q := Qualifiers{}
  103. for k, v := range mm {
  104. q = append(q, Qualifier{Key: k, Value: v})
  105. }
  106. // sort for deterministic qualifier order
  107. sort.Slice(q, func(i int, j int) bool { return q[i].Key < q[j].Key })
  108. return q
  109. }
  110. // Map converts a Qualifiers struct to a string map.
  111. func (qq Qualifiers) Map() map[string]string {
  112. m := make(map[string]string)
  113. for i := 0; i < len(qq); i++ {
  114. k := qq[i].Key
  115. v := qq[i].Value
  116. m[k] = v
  117. }
  118. return m
  119. }
  120. func (qq Qualifiers) String() string {
  121. var kvPairs []string
  122. for _, q := range qq {
  123. kvPairs = append(kvPairs, q.String())
  124. }
  125. return strings.Join(kvPairs, "&")
  126. }
  127. // PackageURL is the struct representation of the parts that make a package url
  128. type PackageURL struct {
  129. Type string
  130. Namespace string
  131. Name string
  132. Version string
  133. Qualifiers Qualifiers
  134. Subpath string
  135. }
  136. // NewPackageURL creates a new PackageURL struct instance based on input
  137. func NewPackageURL(purlType, namespace, name, version string,
  138. qualifiers Qualifiers, subpath string) *PackageURL {
  139. return &PackageURL{
  140. Type: purlType,
  141. Namespace: namespace,
  142. Name: name,
  143. Version: version,
  144. Qualifiers: qualifiers,
  145. Subpath: subpath,
  146. }
  147. }
  148. // ToString returns the human-readable instance of the PackageURL structure.
  149. // This is the literal purl as defined by the spec.
  150. func (p *PackageURL) ToString() string {
  151. // Start with the type and a colon
  152. purl := fmt.Sprintf("pkg:%s/", p.Type)
  153. // Add namespaces if provided
  154. if p.Namespace != "" {
  155. var ns []string
  156. for _, item := range strings.Split(p.Namespace, "/") {
  157. ns = append(ns, url.QueryEscape(item))
  158. }
  159. purl = purl + strings.Join(ns, "/") + "/"
  160. }
  161. // The name is always required and must be a percent-encoded string
  162. // Use url.QueryEscape instead of PathEscape, as it handles @ signs
  163. purl = purl + url.QueryEscape(p.Name)
  164. // If a version is provided, add it after the at symbol
  165. if p.Version != "" {
  166. // A name must be a percent-encoded string
  167. purl = purl + "@" + url.PathEscape(p.Version)
  168. }
  169. // Iterate over qualifiers and make groups of key=value
  170. var qualifiers []string
  171. for _, q := range p.Qualifiers {
  172. qualifiers = append(qualifiers, q.String())
  173. }
  174. // If there are one or more key=value pairs, append on the package url
  175. if len(qualifiers) != 0 {
  176. purl = purl + "?" + strings.Join(qualifiers, "&")
  177. }
  178. // Add a subpath if available
  179. if p.Subpath != "" {
  180. purl = purl + "#" + p.Subpath
  181. }
  182. return purl
  183. }
  184. func (p PackageURL) String() string {
  185. return p.ToString()
  186. }
  187. // FromString parses a valid package url string into a PackageURL structure
  188. func FromString(purl string) (PackageURL, error) {
  189. initialIndex := strings.Index(purl, "#")
  190. // Start with purl being stored in the remainder
  191. remainder := purl
  192. substring := ""
  193. if initialIndex != -1 {
  194. initialSplit := strings.SplitN(purl, "#", 2)
  195. remainder = initialSplit[0]
  196. rightSide := initialSplit[1]
  197. rightSide = strings.TrimLeft(rightSide, "/")
  198. rightSide = strings.TrimRight(rightSide, "/")
  199. var rightSides []string
  200. for _, item := range strings.Split(rightSide, "/") {
  201. item = strings.Replace(item, ".", "", -1)
  202. item = strings.Replace(item, "..", "", -1)
  203. if item != "" {
  204. i, err := url.PathUnescape(item)
  205. if err != nil {
  206. return PackageURL{}, fmt.Errorf("failed to unescape path: %s", err)
  207. }
  208. rightSides = append(rightSides, i)
  209. }
  210. }
  211. substring = strings.Join(rightSides, "/")
  212. }
  213. qualifiers := Qualifiers{}
  214. index := strings.LastIndex(remainder, "?")
  215. // If we don't have anything to split then return an empty result
  216. if index != -1 {
  217. qualifier := remainder[index+1:]
  218. for _, item := range strings.Split(qualifier, "&") {
  219. kv := strings.Split(item, "=")
  220. key := strings.ToLower(kv[0])
  221. key, err := url.PathUnescape(key)
  222. if err != nil {
  223. return PackageURL{}, fmt.Errorf("failed to unescape qualifier key: %s", err)
  224. }
  225. if !validQualifierKey(key) {
  226. return PackageURL{}, fmt.Errorf("invalid qualifier key: '%s'", key)
  227. }
  228. // TODO
  229. // - If the `key` is `checksums`, split the `value` on ',' to create
  230. // a list of `checksums`
  231. if kv[1] == "" {
  232. continue
  233. }
  234. value, err := url.PathUnescape(kv[1])
  235. if err != nil {
  236. return PackageURL{}, fmt.Errorf("failed to unescape qualifier value: %s", err)
  237. }
  238. qualifiers = append(qualifiers, Qualifier{key, value})
  239. }
  240. remainder = remainder[:index]
  241. }
  242. nextSplit := strings.SplitN(remainder, ":", 2)
  243. if len(nextSplit) != 2 || nextSplit[0] != "pkg" {
  244. return PackageURL{}, errors.New("scheme is missing")
  245. }
  246. // leading slashes after pkg: are to be ignored (pkg://maven is
  247. // equivalent to pkg:maven)
  248. remainder = strings.TrimLeft(nextSplit[1], "/")
  249. nextSplit = strings.SplitN(remainder, "/", 2)
  250. if len(nextSplit) != 2 {
  251. return PackageURL{}, errors.New("type is missing")
  252. }
  253. // purl type is case-insensitive, canonical form is lower-case
  254. purlType := strings.ToLower(nextSplit[0])
  255. remainder = nextSplit[1]
  256. index = strings.LastIndex(remainder, "/")
  257. name := typeAdjustName(purlType, remainder[index+1:])
  258. version := ""
  259. atIndex := strings.Index(name, "@")
  260. if atIndex != -1 {
  261. v, err := url.PathUnescape(name[atIndex+1:])
  262. if err != nil {
  263. return PackageURL{}, fmt.Errorf("failed to unescape purl version: %s", err)
  264. }
  265. version = v
  266. unecapeName, err := url.PathUnescape(name[:atIndex])
  267. if err != nil {
  268. return PackageURL{}, fmt.Errorf("failed to unescape purl name: %s", err)
  269. }
  270. name = unecapeName
  271. }
  272. var namespaces []string
  273. if index != -1 {
  274. remainder = remainder[:index]
  275. for _, item := range strings.Split(remainder, "/") {
  276. if item != "" {
  277. unescaped, err := url.PathUnescape(item)
  278. if err != nil {
  279. return PackageURL{}, fmt.Errorf("failed to unescape path: %s", err)
  280. }
  281. namespaces = append(namespaces, unescaped)
  282. }
  283. }
  284. }
  285. namespace := strings.Join(namespaces, "/")
  286. namespace = typeAdjustNamespace(purlType, namespace)
  287. // Fail if name is empty at this point
  288. if name == "" {
  289. return PackageURL{}, errors.New("name is required")
  290. }
  291. err := validCustomRules(purlType, name, namespace, version, qualifiers)
  292. if err != nil {
  293. return PackageURL{}, err
  294. }
  295. return PackageURL{
  296. Type: purlType,
  297. Namespace: namespace,
  298. Name: name,
  299. Version: version,
  300. Qualifiers: qualifiers,
  301. Subpath: substring,
  302. }, nil
  303. }
  304. // Make any purl type-specific adjustments to the parsed namespace.
  305. // See https://github.com/package-url/purl-spec#known-purl-types
  306. func typeAdjustNamespace(purlType, ns string) string {
  307. switch purlType {
  308. case TypeBitbucket, TypeDebian, TypeGithub, TypeGolang, TypeNPM, TypeRPM:
  309. return strings.ToLower(ns)
  310. }
  311. return ns
  312. }
  313. // Make any purl type-specific adjustments to the parsed name.
  314. // See https://github.com/package-url/purl-spec#known-purl-types
  315. func typeAdjustName(purlType, name string) string {
  316. switch purlType {
  317. case TypeBitbucket, TypeDebian, TypeGithub, TypeGolang, TypeNPM:
  318. return strings.ToLower(name)
  319. case TypePyPi:
  320. return strings.ToLower(strings.ReplaceAll(name, "_", "-"))
  321. }
  322. return name
  323. }
  324. // validQualifierKey validates a qualifierKey against our QualifierKeyPattern.
  325. func validQualifierKey(key string) bool {
  326. return QualifierKeyPattern.MatchString(key)
  327. }
  328. // validCustomRules evaluates additional rules for each package url type, as specified in the package-url specification.
  329. // On success, it returns nil. On failure, a descriptive error will be returned.
  330. func validCustomRules(purlType, name, ns, version string, qualifiers Qualifiers) error {
  331. q := qualifiers.Map()
  332. switch purlType {
  333. case TypeConan:
  334. if ns != "" {
  335. if val, ok := q["channel"]; ok {
  336. if val == "" {
  337. return errors.New("the qualifier channel must be not empty if namespace is present")
  338. }
  339. } else {
  340. return errors.New("channel qualifier does not exist")
  341. }
  342. } else {
  343. if val, ok := q["channel"]; ok {
  344. if val != "" {
  345. return errors.New("namespace is required if channel is non empty")
  346. }
  347. }
  348. }
  349. case TypeSwift:
  350. if ns == "" {
  351. return errors.New("namespace is required")
  352. }
  353. if version == "" {
  354. return errors.New("version is required")
  355. }
  356. case TypeCran:
  357. if version == "" {
  358. return errors.New("version is required")
  359. }
  360. }
  361. return nil
  362. }