This repository was archived by the owner on Jan 22, 2026. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathintegrity.rb
More file actions
288 lines (239 loc) · 9.36 KB
/
Copy pathintegrity.rb
File metadata and controls
288 lines (239 loc) · 9.36 KB
1
2
3
4
5
6
7
8
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# frozen_string_literal: true
module Git
module Pkgs
module Commands
class Integrity
include Output
def self.description
"Show and verify lockfile integrity hashes"
end
def initialize(args)
@args = args
@options = parse_options
end
def run
repo = Repository.new
use_stateless = @options[:stateless] || !Database.exists?(repo.git_dir)
if @options[:drift]
error "--drift requires database (run 'git pkgs init' first)" if use_stateless
run_drift_detection(repo)
else
run_show(repo, use_stateless)
end
end
def run_show(repo, use_stateless)
if use_stateless
deps = run_stateless(repo)
else
deps = run_with_database(repo)
end
# Filter to only lockfile deps with integrity
deps = Analyzer.lockfile_dependencies(deps)
deps = deps.select { |d| d[:integrity] }
if @options[:ecosystem]
deps = deps.select { |d| d[:ecosystem] == @options[:ecosystem] }
end
if deps.empty?
empty_result "No dependencies with integrity hashes found"
return
end
if @options[:format] == "json"
require "json"
puts JSON.pretty_generate(deps.map { |d| format_dep_json(d) })
else
paginate { output_text(deps) }
end
end
def run_stateless(repo)
commit_sha = @options[:ref] || repo.head_sha
rugged_commit = repo.lookup(repo.rev_parse(commit_sha))
error "Could not resolve '#{commit_sha}'" unless rugged_commit
analyzer = Analyzer.new(repo)
analyzer.dependencies_at_commit(rugged_commit)
end
def run_with_database(repo)
Database.connect(repo.git_dir)
commit_sha = @options[:ref] || repo.head_sha
target_commit = Models::Commit.first(sha: commit_sha)
error "Commit not in database. Run 'git pkgs update' first." unless target_commit
compute_dependencies_at_commit(target_commit, repo)
end
def run_drift_detection(repo)
Database.connect(repo.git_dir)
# Get unique (purl, requirement, integrity) from snapshots
results = Database.db[:dependency_snapshots]
.exclude(integrity: nil)
.select(:purl, :requirement, :integrity)
.distinct
.all
# Build versioned purls and group
by_versioned_purl = {}
results.each do |r|
versioned_purl = "#{r[:purl]}@#{r[:requirement]}"
by_versioned_purl[versioned_purl] ||= { purl: r[:purl], version: r[:requirement], lockfile_integrities: [] }
by_versioned_purl[versioned_purl][:lockfile_integrities] << r[:integrity]
end
# Dedupe lockfile integrities
by_versioned_purl.each { |_, v| v[:lockfile_integrities].uniq! }
# Find internal drift (same version with different lockfile hashes)
internal_drifts = by_versioned_purl.select { |_, v| v[:lockfile_integrities].size > 1 }
# Fetch registry integrity for comparison
registry_mismatches = []
purls_to_check = by_versioned_purl.keys
if purls_to_check.any?
Spinner.with_spinner("Fetching registry integrity...") do
client = EcosystemsClient.new
purls_to_check.each do |versioned_purl|
data = by_versioned_purl[versioned_purl]
version_info = client.lookup_version(versioned_purl)
next unless version_info && version_info["integrity"]
registry_integrity = version_info["integrity"]
lockfile_integrity = data[:lockfile_integrities].first
unless integrity_match?(lockfile_integrity, registry_integrity)
registry_mismatches << {
purl: versioned_purl,
lockfile: lockfile_integrity,
registry: registry_integrity
}
end
end
end
end
if internal_drifts.empty? && registry_mismatches.empty?
info "No integrity drift detected"
return
end
if @options[:format] == "json"
require "json"
output = {
internal_drift: internal_drifts.map { |purl, v| { purl: purl, integrity_values: v[:lockfile_integrities] } },
registry_mismatch: registry_mismatches
}
puts JSON.pretty_generate(output)
else
paginate { output_drift_text(internal_drifts, registry_mismatches) }
end
end
def integrity_match?(lockfile, registry)
normalize_integrity(lockfile) == normalize_integrity(registry)
end
def normalize_integrity(integrity)
return nil unless integrity
# Normalize sha256= vs sha256- format
integrity.gsub(/^sha256[-=]/, "sha256:")
end
def output_text(deps)
grouped = deps.group_by { |d| d[:ecosystem] }
grouped.each do |ecosystem, ecosystem_deps|
puts "#{ecosystem}:"
ecosystem_deps.sort_by { |d| d[:name] }.each do |dep|
puts " #{dep[:name]} #{dep[:requirement]}"
puts " #{dep[:integrity]}"
end
puts
end
end
def output_drift_text(internal_drifts, registry_mismatches)
if internal_drifts.any?
puts Color.red("Internal drift (same version, different lockfile hashes):")
puts
internal_drifts.each do |purl, data|
puts " #{purl}"
data[:lockfile_integrities].each do |integrity|
puts " #{integrity}"
end
puts
end
end
if registry_mismatches.any?
puts Color.red("Registry mismatch (lockfile differs from registry):")
puts
registry_mismatches.each do |mismatch|
puts " #{mismatch[:purl]}"
puts " lockfile: #{mismatch[:lockfile]}"
puts " registry: #{mismatch[:registry]}"
puts
end
end
total = internal_drifts.size + registry_mismatches.size
puts "#{total} integrity issue(s) found"
end
def format_dep_json(dep)
{
name: dep[:name],
version: dep[:requirement],
ecosystem: dep[:ecosystem],
purl: dep[:purl],
integrity: dep[:integrity],
manifest: dep[:manifest_path]
}
end
def compute_dependencies_at_commit(target_commit, repo)
branch_name = @options[:branch] || repo.default_branch
branch = Models::Branch.first(name: branch_name)
return [] unless branch
snapshot_commit = branch.commits_dataset
.join(:dependency_snapshots, commit_id: :id)
.where { Sequel[:commits][:committed_at] <= target_commit.committed_at }
.order(Sequel.desc(Sequel[:commits][:committed_at]))
.distinct
.first
deps = {}
if snapshot_commit
snapshot_commit.dependency_snapshots.each do |s|
key = [s.manifest.path, s.name]
deps[key] = {
manifest_path: s.manifest.path,
name: s.name,
ecosystem: s.ecosystem,
kind: s.manifest.kind,
manifest_kind: s.manifest.kind,
purl: s.purl,
requirement: s.requirement,
dependency_type: s.dependency_type,
integrity: s.integrity
}
end
end
deps.values
end
def parse_options
options = {}
parser = OptionParser.new do |opts|
opts.banner = "Usage: git pkgs integrity [options]"
opts.separator ""
opts.separator "Show integrity hashes from lockfiles. Hashes come from lockfile checksums"
opts.separator "(Gemfile.lock CHECKSUMS, package-lock.json integrity fields, etc.)"
opts.on("-r", "--ref=REF", "Git ref to check (default: HEAD)") do |v|
options[:ref] = v
end
opts.on("-e", "--ecosystem=NAME", "Filter by ecosystem") do |v|
options[:ecosystem] = v
end
opts.on("-b", "--branch=NAME", "Branch context for database queries") do |v|
options[:branch] = v
end
opts.on("-f", "--format=FORMAT", "Output format (text, json)") do |v|
options[:format] = v
end
opts.on("--drift", "Detect packages with different hashes for same version") do
options[:drift] = true
end
opts.on("--stateless", "Parse manifests directly without database") do
options[:stateless] = true
end
opts.on("--no-pager", "Do not pipe output into a pager") do
options[:no_pager] = true
end
opts.on("-h", "--help", "Show this help") do
puts opts
exit
end
end
parser.parse!(@args)
options
end
end
end
end
end