Repository navigation
Expand file tree
/
Copy pathdiff.go
More file actions
134 lines (123 loc) · 3.89 KB
/
Copy pathdiff.go
File metadata and controls
134 lines (123 loc) · 3.89 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
package malware
import (
"context"
"fmt"
"slices"
"strings"
"github.com/git-pkgs/registries"
)
// Comparison describes changes in detected mechanisms between two releases.
type Comparison struct {
Introduced []Finding `json:"introduced"`
Changed []EvidenceChange `json:"changed"`
Removed []Finding `json:"removed"`
}
// EvidenceChange contains both sets of findings for one mechanism and phase.
type EvidenceChange struct {
Before []Finding `json:"before"`
After []Finding `json:"after"`
Added []Finding `json:"added"`
Removed []Finding `json:"removed"`
}
// Diff compares local artifacts of the same package, starting with the baseline.
func Diff(ctx context.Context, ecosystem string, before, after Artifact) (*Comparison, error) {
old, err := inspect(ctx, ecosystem, before)
if err != nil {
return nil, fmt.Errorf("baseline %s: %w", before.Filename, err)
}
current, err := inspect(ctx, ecosystem, after)
if err != nil {
return nil, fmt.Errorf("candidate %s: %w", after.Filename, err)
}
return compare(old, current)
}
// DiffPURLs compares two versioned PURLs of the same package, starting with the baseline.
func DiffPURLs(ctx context.Context, before, after string) (*Comparison, error) {
base, err := registries.ParsePURL(before)
if err != nil {
return nil, fmt.Errorf("baseline %s: %w", before, err)
}
next, err := registries.ParsePURL(after)
if err != nil {
return nil, fmt.Errorf("candidate %s: %w", after, err)
}
if base.Type != next.Type || base.ComparisonForm().FullName() != next.ComparisonForm().FullName() {
return nil, fmt.Errorf("PURLs must identify versions of the same package: %s and %s", before, after)
}
old, err := inspectPURL(ctx, base)
if err != nil {
return nil, fmt.Errorf("baseline %s: %w", before, err)
}
current, err := inspectPURL(ctx, next)
if err != nil {
return nil, fmt.Errorf("candidate %s: %w", after, err)
}
return compare(old, current)
}
type signal struct {
mechanism, phase string
}
func group(findings []Finding) map[signal][]Finding {
groups := make(map[signal][]Finding)
for _, f := range findings {
key := signal{f.Mechanism, f.Phase}
groups[key] = append(groups[key], f)
}
return groups
}
func relative(f Finding, root string) Finding {
f.Path = strings.TrimPrefix(strings.TrimPrefix(f.Path, "./"), root)
f.Target = strings.TrimPrefix(strings.TrimPrefix(f.Target, "./"), root)
return f
}
func evidenceOnly(findings []Finding, root string, other []Finding, otherRoot string) []Finding {
counts := make(map[Finding]int, len(other))
for _, f := range other {
counts[relative(f, otherRoot)]++
}
only := []Finding{}
for _, f := range findings {
key := relative(f, root)
if counts[key] == 0 {
only = append(only, f)
} else {
counts[key]--
}
}
return only
}
func compare(before, after scan) (*Comparison, error) {
if before.platform != after.platform {
return nil, fmt.Errorf("gem platform mismatch: baseline %q, candidate %q", before.platform, after.platform)
}
diff := &Comparison{Introduced: []Finding{}, Changed: []EvidenceChange{}, Removed: []Finding{}}
old, current := group(before.findings), group(after.findings)
for _, f := range before.findings {
if _, exists := current[signal{f.Mechanism, f.Phase}]; !exists {
diff.Removed = append(diff.Removed, f)
}
}
for _, f := range after.findings {
key := signal{f.Mechanism, f.Phase}
next := current[key]
if len(next) == 0 {
continue
}
delete(current, key)
prior := old[key]
if len(prior) == 0 {
diff.Introduced = append(diff.Introduced, next...)
continue
}
if !slices.EqualFunc(prior, next, func(a, b Finding) bool {
return relative(a, before.root) == relative(b, after.root)
}) {
diff.Changed = append(diff.Changed, EvidenceChange{
Before: prior, After: next,
Added: evidenceOnly(next, after.root, prior, before.root),
Removed: evidenceOnly(prior, before.root, next, after.root),
})
}
}
return diff, nil
}