reads.gno
4.10 Kb · 140 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 return a
15}
16
17// Latest returns the newest attestation for pkgpath.
18func Latest(pkgpath string) (Attestation, bool) {
19 rec := getRecord(pkgpath)
20 if rec == nil {
21 return Attestation{}, false
22 }
23 return copyAttestation(rec.history[len(rec.history)-1]), true
24}
25
26// History returns every attestation for pkgpath, oldest first.
27func History(pkgpath string) []Attestation {
28 rec := getRecord(pkgpath)
29 out := []Attestation{}
30 if rec == nil {
31 return out
32 }
33 for _, a := range rec.history {
34 out = append(out, copyAttestation(a))
35 }
36 return out
37}
38
39// Replies returns the author's replies on pkgpath, oldest first.
40func Replies(pkgpath string) []AuthorReply {
41 rec := getRecord(pkgpath)
42 out := []AuthorReply{}
43 if rec != nil {
44 out = append(out, rec.replies...)
45 }
46 return out
47}
48
49// Score returns the latest score for pkgpath.
50func Score(pkgpath string) (int, bool) {
51 a, ok := Latest(pkgpath)
52 return a.Score, ok
53}
54
55// ByAuthor returns the attested packages in addr's namespace, sorted.
56func ByAuthor(addr address) []string {
57 out := []string{}
58 v := byAuthor.Get(string(addr))
59 if v == nil {
60 return out
61 }
62 v.(*avl.Tree).Iterate("", "", func(k string, _ any) bool {
63 out = append(out, k)
64 return false
65 })
66 return out
67}
68
69// Policy is an integration policy for Check. The zero value is the most
70// conservative: no PROVEN findings, no open disputes, any score.
71type Policy struct {
72 MinScore int
73 // AllowProvenCritical and AllowProvenOther permit PROVEN findings of
74 // critical, or of high and medium, severity.
75 AllowProvenCritical bool
76 AllowProvenOther bool
77 // RequireVerifiedDeps forbids at-risk and unverified dependencies.
78 RequireVerifiedDeps bool
79 AllowOpenDisputes bool
80}
81
82// Strict: score 90 or more, no PROVEN findings, every dependency verified,
83// no open disputes.
84func Strict() Policy {
85 return Policy{MinScore: 90, RequireVerifiedDeps: true}
86}
87
88// Standard: score 75 or more and no PROVEN Critical.
89func Standard() Policy {
90 return Policy{MinScore: 75, AllowProvenOther: true, AllowOpenDisputes: true}
91}
92
93// Custom starts a policy at minScore that is otherwise as strict as the
94// zero value; loosen it with the With methods:
95//
96// bouncer.Custom(60).WithProvenOther().WithOpenDisputes()
97//
98// Other realms must build policies this way: the VM refuses to allocate a
99// Bouncer type in a foreign realm, so a bouncer.Policy{...} literal written
100// in another realm panics.
101func Custom(minScore int) Policy { return Policy{MinScore: minScore} }
102
103// WithProvenCritical allows PROVEN critical findings.
104func (p Policy) WithProvenCritical() Policy { p.AllowProvenCritical = true; return p }
105
106// WithProvenOther allows PROVEN high and medium findings.
107func (p Policy) WithProvenOther() Policy { p.AllowProvenOther = true; return p }
108
109// WithVerifiedDeps requires every dependency to be verified.
110func (p Policy) WithVerifiedDeps() Policy { p.RequireVerifiedDeps = true; return p }
111
112// WithOpenDisputes allows open disputes.
113func (p Policy) WithOpenDisputes() Policy { p.AllowOpenDisputes = true; return p }
114
115// Check reports whether the latest attestation of pkgpath satisfies p.
116// An unattested package never passes.
117func Check(pkgpath string, p Policy) bool {
118 a, ok := Latest(pkgpath)
119 if !ok || a.Score < p.MinScore {
120 return false
121 }
122 for _, f := range a.Findings {
123 if f.Label != LabelProven {
124 continue
125 }
126 if f.Severity == SeverityCritical && !p.AllowProvenCritical {
127 return false
128 }
129 if f.Severity != SeverityCritical && !p.AllowProvenOther {
130 return false
131 }
132 }
133 if p.RequireVerifiedDeps && (a.Deps.DirectAtRisk > 0 || a.Deps.IndirectAtRisk > 0 || a.Deps.Unverified > 0) {
134 return false
135 }
136 if !p.AllowOpenDisputes && hasOpenDispute(pkgpath, "") {
137 return false
138 }
139 return true
140}