Class: Homebrew::Vulns::Purl Private

Inherits:
Object
  • Object
show all
Defined in:
vulns/purl.rb

Overview

This class is part of a private API. This class may only be used in the Homebrew/brew repository. Third parties should avoid using this class if possible, as it may be removed or changed without warning.

A package URL per https://github.com/package-url/purl-spec.

Minimal builder for the registry types Homebrew derives from formula source URLs. Applies the spec's per-type name normalisation and RFC 3986 percent-encoding when serialised. Parsing, qualifiers and subpath are intentionally omitted.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type:, name:, namespace: nil, version: nil) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Raises:

  • (ArgumentError)


22
23
24
25
26
27
28
29
30
31
32
33
# File 'vulns/purl.rb', line 22

def initialize(type:, name:, namespace: nil, version: nil)
  raise ArgumentError, "type is required" if type.empty?
  raise ArgumentError, "name is required" if name.empty?

  @type = T.let(type.downcase.freeze, String)
  namespace = nil if namespace && namespace.empty?
  namespace, name = self.class.normalize(@type, namespace, name)
  @namespace = T.let(namespace && -namespace, T.nilable(String))
  @name = T.let(-name, String)
  version = nil if version && version.empty?
  @version = T.let(version && -version, T.nilable(String))
end

Instance Attribute Details

#nameString (readonly)

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



14
15
16
# File 'vulns/purl.rb', line 14

def name
  @name
end

#namespaceString? (readonly)

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



17
18
19
# File 'vulns/purl.rb', line 17

def namespace
  @namespace
end

#typeString (readonly)

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



14
15
16
# File 'vulns/purl.rb', line 14

def type
  @type
end

#versionString? (readonly)

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



17
18
19
# File 'vulns/purl.rb', line 17

def version
  @version
end

Class Method Details

.encode(component) ⇒ String

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Percent-encode a single purl component. The spec permits the RFC 3986 unreserved set plus : unencoded; + for space is forbidden so URI.encode_www_form_component is unsuitable.

Parameters:

Returns:



67
68
69
# File 'vulns/purl.rb', line 67

def self.encode(component)
  component.b.gsub(/[^A-Za-z0-9\-._~:]/n) { |c| format("%%%02X", c.ord) }
end

.normalize(type, namespace, name) ⇒ Array<([String, nil], String)>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Per-type normalisation from purl-spec PURL-TYPES.rst for the types we emit.

Parameters:

Returns:



76
77
78
79
80
81
82
83
84
85
86
87
# File 'vulns/purl.rb', line 76

def self.normalize(type, namespace, name)
  case type
  when "pypi"
    [namespace, name.downcase.tr("_", "-")]
  when "hex"
    [namespace&.downcase, name.downcase]
  when "cpan"
    [namespace&.upcase, name]
  else
    [namespace, name]
  end
end