9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
# File 'lib/bundler/sbom.rb', line 9
def self.generate_sbom
lockfile_path = Bundler.default_lockfile
unless lockfile_path.exist?
abort "No Gemfile.lock found. Run `bundle install` first."
end
lockfile = Bundler::LockfileParser.new(lockfile_path.read)
document_name = File.basename(Dir.pwd)
spdx_id = SecureRandom.uuid
sbom = {
"SPDXID" => "SPDXRef-DOCUMENT",
"spdxVersion" => "SPDX-2.2",
"creationInfo" => {
"created" => Time.now.utc.strftime("%Y-%m-%dT%H:%M:%SZ"),
"creators" => ["Tool: bundle-sbom"],
"licenseListVersion" => "3.17"
},
"name" => document_name,
"dataLicense" => "CC0-1.0",
"documentNamespace" => "https://spdx.org/spdxdocs/#{document_name}-#{spdx_id}",
"packages" => []
}
lockfile.specs.each do |spec|
begin
gemspec = Gem::Specification.find_by_name(spec.name, spec.version)
licenses = []
if gemspec
if gemspec.license && !gemspec.license.empty?
licenses << gemspec.license
end
if gemspec.licenses && !gemspec.licenses.empty?
licenses.concat(gemspec.licenses)
end
licenses.uniq!
end
license_string = licenses.empty? ? "NOASSERTION" : licenses.join(", ")
rescue Gem::LoadError
license_string = "NOASSERTION"
end
package = {
"SPDXID" => "SPDXRef-Package-#{spec.name}",
"name" => spec.name,
"versionInfo" => spec.version.to_s,
"downloadLocation" => "NOASSERTION",
"filesAnalyzed" => false,
"licenseConcluded" => license_string,
"licenseDeclared" => license_string,
"supplier" => "NOASSERTION",
"externalRefs" => [
{
"referenceCategory" => "PACKAGE_MANAGER",
"referenceType" => "purl",
"referenceLocator" => "pkg:gem/#{spec.name}@#{spec.version}"
}
]
}
sbom["packages"] << package
end
sbom
end
|