reads.gno
4.50 Kb · 150 lines
1package bouncer
2
3import "gno.land/p/nt/avl/v0"
4
5// Reads for other realms and for clients. All are plain (non-crossing)
6// functions that return values or fresh copies: no pointer into state ever
7// leaves this realm, and no caller-supplied function is ever invoked.
8
9// copyAttestation returns a deep copy (the Findings slice is copied too).
10func copyAttestation(a Attestation) Attestation {
11 fs := make([]Finding, len(a.Findings))
12 copy(fs, a.Findings)
13 a.Findings = fs
14 ds := make([]Dep, len(a.Deps.List))
15 copy(ds, a.Deps.List)
16 a.Deps.List = ds
17 return a
18}
19
20// Latest returns the newest attestation for pkgpath.
21func Latest(pkgpath string) (Attestation, bool) {
22 rec := getRecord(pkgpath)
23 if rec == nil {
24 return Attestation{}, false
25 }
26 return live(rec.history[len(rec.history)-1]), true
27}
28
29// History returns every attestation for pkgpath, oldest first, with scores
30// that use the current dependency statuses (like Latest).
31func History(pkgpath string) []Attestation {
32 rec := getRecord(pkgpath)
33 out := []Attestation{}
34 if rec == nil {
35 return out
36 }
37 for _, a := range rec.history {
38 out = append(out, live(a))
39 }
40 return out
41}
42
43// Replies returns the author's replies on pkgpath, oldest first.
44func Replies(pkgpath string) []AuthorReply {
45 rec := getRecord(pkgpath)
46 out := []AuthorReply{}
47 if rec != nil {
48 out = append(out, rec.replies...)
49 }
50 return out
51}
52
53// Score returns the latest score for pkgpath.
54func Score(pkgpath string) (int, bool) {
55 a, ok := Latest(pkgpath)
56 return a.Score, ok
57}
58
59// ByAuthor returns the attested packages in addr's namespace, sorted.
60func ByAuthor(addr address) []string {
61 out := []string{}
62 v := byAuthor.Get(string(addr))
63 if v == nil {
64 return out
65 }
66 v.(*avl.Tree).Iterate("", "", func(k string, _ any) bool {
67 out = append(out, k)
68 return false
69 })
70 return out
71}
72
73// Policy is an integration policy for Check. No policy accepts a red
74// verdict: a PROVEN Critical or High finding fails every policy, and there
75// is no option to allow it. The zero value is the most conservative: no
76// PROVEN findings at all, no open disputes, any score.
77type Policy struct {
78 MinScore int
79 // AllowProvenMedium permits PROVEN medium findings (which do not make
80 // the verdict red).
81 AllowProvenMedium bool
82 // RequireVerifiedDeps forbids at-risk and unverified dependencies.
83 RequireVerifiedDeps bool
84 AllowOpenDisputes bool
85}
86
87// Strict: score 90 or more, no PROVEN findings, every dependency verified,
88// no open disputes.
89func Strict() Policy {
90 return Policy{MinScore: 90, RequireVerifiedDeps: true}
91}
92
93// Standard: score 75 or more and no PROVEN Critical or High finding.
94// Open disputes are tolerated.
95func Standard() Policy {
96 return Policy{MinScore: 75, AllowProvenMedium: true, AllowOpenDisputes: true}
97}
98
99// Custom starts a policy at minScore that is otherwise as strict as the
100// zero value; loosen it with the With methods:
101//
102// bouncer.Custom(60).WithProvenMedium().WithOpenDisputes()
103//
104// Other realms must build policies this way: the VM refuses to allocate a
105// Bouncer type in a foreign realm, so a bouncer.Policy{...} literal written
106// in another realm panics.
107func Custom(minScore int) Policy { return Policy{MinScore: minScore} }
108
109// WithProvenMedium allows PROVEN medium findings.
110func (p Policy) WithProvenMedium() Policy { p.AllowProvenMedium = true; return p }
111
112// WithVerifiedDeps requires every dependency to be verified.
113func (p Policy) WithVerifiedDeps() Policy { p.RequireVerifiedDeps = true; return p }
114
115// WithOpenDisputes allows open disputes.
116func (p Policy) WithOpenDisputes() Policy { p.AllowOpenDisputes = true; return p }
117
118// Check reports whether the latest attestation of pkgpath satisfies p.
119// An unattested package never passes.
120func Check(pkgpath string, p Policy) bool {
121 a, ok := Latest(pkgpath)
122 if !ok || !allows(p, a.Score, a.Findings, a.Deps) {
123 return false
124 }
125 if !p.AllowOpenDisputes && hasOpenDispute(pkgpath, "") {
126 return false
127 }
128 return true
129}
130
131// allows is the policy decision on a score, findings and dependency counts
132// (disputes aside). bouncer gate's Go copy (internal/score) must decide the
133// same; TestMatchesRealm checks it.
134func allows(p Policy, score int, fs []Finding, d Deps) bool {
135 if verdictOf(fs) == VerdictNotRecommended {
136 return false // a red verdict never passes
137 }
138 if score < p.MinScore {
139 return false
140 }
141 for _, f := range fs {
142 if f.Label == LabelProven && !p.AllowProvenMedium {
143 return false // only medium can be PROVEN here
144 }
145 }
146 if p.RequireVerifiedDeps && (d.DirectAtRisk > 0 || d.IndirectAtRisk > 0 || d.Unverified > 0) {
147 return false
148 }
149 return true
150}