deps.gno
2.47 Kb · 90 lines
1package bouncer
2
3import "strings"
4
5// Dependency trust (CLAUDE.md §4, §8). The attester sends the package's
6// import tree as a list of paths, each marked direct or indirect; the
7// realm never trusts a status from outside. Each dependency's status is
8// read from this registry whenever the attestation is read, so a
9// dependency attested later updates its importers' score, badge and
10// Check() without a re-scan:
11//
12// - verified: attested, and its latest verdict has no Critical or High issue
13// - at-risk: attested, and its latest verdict has a Critical or High issue
14// - unverified: not attested here yet (never shown as a vulnerability)
15
16// Dependency statuses.
17const (
18 DepVerified = "verified"
19 DepAtRisk = "at-risk"
20 DepUnverified = "unverified"
21)
22
23const maxDeps = 200
24
25// Dep is one dependency of an attested package. Status is filled in when
26// the attestation is read.
27type Dep struct {
28 Path string
29 Direct bool
30 Status string
31}
32
33// DepStatus returns the trust status of pkgpath as a dependency.
34func DepStatus(pkgpath string) string {
35 rec := getRecord(pkgpath)
36 if rec == nil {
37 return DepUnverified
38 }
39 if rec.history[len(rec.history)-1].Verdict == VerdictClear {
40 return DepVerified
41 }
42 return DepAtRisk
43}
44
45// parseDeps decodes Attest's deps argument: ";"-separated "d,<pkgpath>"
46// (direct) or "i,<pkgpath>" (indirect) entries, "" for none.
47func parseDeps(pkgpath, s string) []Dep {
48 out := []Dep{}
49 if s == "" {
50 return out
51 }
52 seen := map[string]bool{}
53 for _, e := range strings.Split(s, ";") {
54 kind, path, ok := strings.Cut(e, ",")
55 if !ok || (kind != "d" && kind != "i") || !validPkgPath(path) {
56 panic("invalid dependency entry")
57 }
58 if path == pkgpath || seen[path] {
59 panic("duplicate or self dependency")
60 }
61 seen[path] = true
62 out = append(out, Dep{Path: path, Direct: kind == "d"})
63 if len(out) > maxDeps {
64 panic("too many dependencies")
65 }
66 }
67 return out
68}
69
70// live returns a copy of a with dependency statuses, counts, score and
71// grade as they stand now. The verdict depends on the findings only.
72func live(a Attestation) Attestation {
73 a = copyAttestation(a)
74 d := Deps{List: make([]Dep, len(a.Deps.List))}
75 for i, dep := range a.Deps.List {
76 dep.Status = DepStatus(dep.Path)
77 d.List[i] = dep
78 switch {
79 case dep.Status == DepAtRisk && dep.Direct:
80 d.DirectAtRisk++
81 case dep.Status == DepAtRisk:
82 d.IndirectAtRisk++
83 case dep.Status == DepUnverified:
84 d.Unverified++
85 }
86 }
87 a.Deps = d
88 a.Score, a.Grade, _ = ScoreFor(a.Findings, d)
89 return a
90}