render.gno
8.05 Kb · 226 lines
1package bouncer
2
3import (
4 "strconv"
5 "strings"
6 "time"
7
8 "gno.land/p/nt/markdown/sanitize/v0"
9)
10
11// Render serves the Tavern pages:
12//
13// "" Noticeboard
14// "realm/<pkgpath>" Passport
15func Render(path string) string {
16 if pkgpath, ok := strings.CutPrefix(path, "realm/"); ok {
17 return renderPassport(pkgpath)
18 }
19 if path == "" {
20 return renderNoticeboard()
21 }
22 return "# Not found\n\n[Back to the noticeboard](" + webPath() + ")\n"
23}
24
25// webPath is this realm's gnoweb URL path ("/r/...").
26func webPath() string {
27 return strings.TrimPrefix(selfPath, "gno.land")
28}
29
30// passportLink links to pkgpath's Passport. pkgpath is a validated path
31// (lowercase letters, digits, underscores and slashes), so it is URL-safe.
32func passportLink(pkgpath string) string {
33 return "[" + sanitize.InlineCode(pkgpath) + "](" + webPath() + ":realm/" + pkgpath + ")"
34}
35
36func verdictText(v string) string {
37 switch v {
38 case VerdictNotRecommended:
39 return "🔴 Not recommended for integration"
40 case VerdictReview:
41 return "🟠 Review before integration"
42 }
43 return "🟢 No known Critical or High issues"
44}
45
46func date(unix int64) string {
47 return time.Unix(unix, 0).UTC().Format("2006-01-02")
48}
49
50func renderNoticeboard() string {
51 var b strings.Builder
52 b.WriteString("# Bouncer\n\n**No exploit, no claim.** Bouncer checks Gno realms against the Gno security guide, proves serious bugs with a working attacker realm, and records the verdict here.\n\n")
53 b.WriteString("| Passports | Findings | PROVEN | Open requests |\n| --- | --- | --- | --- |\n")
54 b.WriteString("| " + strconv.Itoa(records.Size()) + " | " + strconv.Itoa(totalFindings) + " | " + strconv.Itoa(totalProven) + " | " + strconv.Itoa(openByRequester.Size()) + " |\n\n")
55
56 b.WriteString("## Recent Passports\n\n")
57 if len(recent) == 0 {
58 b.WriteString("No packages attested yet.\n\n")
59 } else {
60 b.WriteString("| Package | Score | Grade | Verdict |\n| --- | --- | --- | --- |\n")
61 for _, p := range recent {
62 a := getRecord(p).history[len(getRecord(p).history)-1]
63 b.WriteString("| " + passportLink(p) + " | " + strconv.Itoa(a.Score) + " | " + a.Grade + " | " + verdictText(a.Verdict) + " |\n")
64 }
65 b.WriteString("\n")
66 }
67
68 b.WriteString("## Request a scan\n\nOne open request per address. The scanner bot picks it up, scans and proves, then posts the result.\n\n")
69 b.WriteString("<gno-form exec=\"RequestScan\">\n<gno-input name=\"pkgpath\" placeholder=\"gno.land/r/<address>/myrealm\" />\n</gno-form>\n\n")
70 b.WriteString("_" + Disclaimer + "_\n")
71 return b.String()
72}
73
74func renderPassport(pkgpath string) string {
75 if !validPkgPath(pkgpath) {
76 return "# Not found\n\nThat is not a valid package path.\n"
77 }
78 rec := getRecord(pkgpath)
79 var b strings.Builder
80 b.WriteString("# Passport: " + sanitize.InlineCode(pkgpath) + "\n\n")
81 if rec == nil {
82 b.WriteString("Bouncer has not attested this package yet. [Request a scan](" + webPath() + ").\n")
83 return b.String()
84 }
85 a := rec.history[len(rec.history)-1]
86
87 b.WriteString("## " + strconv.Itoa(a.Score) + " / 100 · Grade " + a.Grade + " · " + verdictText(a.Verdict) + "\n\n")
88 b.WriteString("_" + Disclaimer + "_\n\n")
89 if successor := latestSuccessor(rec); successor != "" {
90 b.WriteString("The author points to a newer version: " + passportLink(successor) + "\n\n")
91 }
92
93 b.WriteString("## Findings\n\n")
94 if len(a.Findings) == 0 {
95 b.WriteString("No findings with the current rules.\n\n")
96 } else {
97 b.WriteString("| Rule | Severity | Label | Location |\n| --- | --- | --- | --- |\n")
98 for _, f := range a.Findings {
99 b.WriteString("| " + f.Rule + " | " + f.Severity + " | **" + f.Label + "** | " + sanitize.InlineCode(f.File+":"+strconv.Itoa(f.Line)) + " |\n")
100 }
101 b.WriteString("\n")
102 seen := map[string]bool{}
103 for _, f := range a.Findings {
104 if seen[f.Rule] {
105 continue
106 }
107 seen[f.Rule] = true
108 title, guide, fix := ruleGuidance(f.Rule)
109 b.WriteString("### " + f.Rule + ": " + title + "\n\nGuide " + guide + ". **Fix:** " + fix + "\n\n")
110 }
111 }
112
113 b.WriteString("## Attestation\n\n| | |\n| --- | --- |\n")
114 row := func(k, v string) { b.WriteString("| " + k + " | " + v + " |\n") }
115 row("Attested", date(a.Time)+" (block "+strconv.FormatInt(a.Height, 10)+")")
116 row("Attester", sanitize.InlineCode(a.Attester.String()))
117 row("Source hash", sanitize.InlineCode(a.SourceHash))
118 row("Scanner", sanitize.InlineCode(a.ScannerVersion)+", rule set "+sanitize.InlineCode(a.RulesetHash[:12]))
119 row("Gno toolchain", sanitize.InlineCode(a.Toolchain))
120 if u := reportURL(a.ReportURI); u != "" {
121 row("Full report", "[report]("+u+"), hash "+sanitize.InlineCode(a.ReportHash[:12]))
122 }
123 row("Dependencies", strconv.Itoa(a.Deps.DirectAtRisk)+" direct at risk, "+strconv.Itoa(a.Deps.IndirectAtRisk)+" indirect at risk, "+strconv.Itoa(a.Deps.Unverified)+" unverified")
124 b.WriteString("\n")
125
126 if len(rec.replies) > 0 {
127 b.WriteString("## Author replies\n\n")
128 for _, r := range rec.replies {
129 b.WriteString("- " + sanitize.InlineText(r.Note))
130 if r.Successor != "" {
131 b.WriteString(" (fixed version: " + passportLink(r.Successor) + ")")
132 }
133 b.WriteString("\n")
134 }
135 b.WriteString("\n")
136 }
137
138 if ds := DisputesFor(pkgpath); len(ds) > 0 {
139 b.WriteString("## Disputes\n\n| Filer | Report | Note | Status |\n| --- | --- | --- | --- |\n")
140 for _, d := range ds {
141 status := "open"
142 if d.Resolved {
143 status = "resolved: " + sanitize.TableCell(d.Resolution)
144 }
145 b.WriteString("| " + sanitize.InlineCode(d.Filer.String()) + " | " + sanitize.InlineCode(d.ReportHash[:12]) + " | " + sanitize.TableCell(d.Note) + " | " + status + " |\n")
146 }
147 b.WriteString("\n")
148 }
149
150 b.WriteString("## History\n\n| Date | Score | Grade | Findings | Change |\n| --- | --- | --- | --- | --- |\n")
151 for i := len(rec.history) - 1; i >= 0; i-- {
152 h := rec.history[i]
153 change := "first scan"
154 if i > 0 {
155 change = diffRules(rec.history[i-1].Findings, h.Findings)
156 }
157 b.WriteString("| " + date(h.Time) + " | " + strconv.Itoa(h.Score) + " | " + h.Grade + " | " + strconv.Itoa(len(h.Findings)) + " | " + change + " |\n")
158 }
159 return b.String()
160}
161
162// latestSuccessor returns the newest successor an author named.
163func latestSuccessor(rec *record) string {
164 for i := len(rec.replies) - 1; i >= 0; i-- {
165 if rec.replies[i].Successor != "" {
166 return rec.replies[i].Successor
167 }
168 }
169 return ""
170}
171
172// diffRules describes which rules appeared or were resolved between scans.
173func diffRules(prev, next []Finding) string {
174 had, has := map[string]bool{}, map[string]bool{}
175 for _, f := range prev {
176 had[f.Rule] = true
177 }
178 for _, f := range next {
179 has[f.Rule] = true
180 }
181 var parts []string
182 for _, f := range prev {
183 if !has[f.Rule] {
184 parts = append(parts, f.Rule+" resolved")
185 has[f.Rule] = true // report once
186 }
187 }
188 for _, f := range next {
189 if !had[f.Rule] {
190 parts = append(parts, f.Rule+" new")
191 had[f.Rule] = true
192 }
193 }
194 if len(parts) == 0 {
195 return "no rule changes"
196 }
197 return strings.Join(parts, ", ")
198}
199
200// reportURL turns a stored report URI into a safe link target. IPFS URIs
201// go through the IPFS gateway gnoweb itself uses, since links need https.
202func reportURL(uri string) string {
203 if cid, ok := strings.CutPrefix(uri, "ipfs://"); ok {
204 uri = "https://ipfs.filebase.io/ipfs/" + cid
205 }
206 return sanitize.URL(uri)
207}
208
209// ruleGuidance is the fix guidance, written once per rule.
210func ruleGuidance(rule string) (title, guide, fix string) {
211 switch rule {
212 case "B01":
213 return "getter returns a pointer into state", "§5.1, §5.1a",
214 "return copies or plain values, never a pointer into package state, and keep mutating methods unexported."
215 case "B05":
216 return "IsUser() guards an OriginSend payment", "§5.5",
217 "use `cur.Previous().IsUserCall()` with the `OriginSend()` amount check; `IsUser()` also accepts `maketx run` callers that keep the coins."
218 case "B03":
219 return "caller-supplied callback under the realm's authority", "§3C, §5.3",
220 "never invoke a caller-supplied function or interface while holding this realm's authority."
221 case "B09":
222 return "external Teller used without IsCanonicalTeller", "§5.4",
223 "check `grc20.IsCanonicalTeller(t)` before calling a Teller passed in by a caller."
224 }
225 return "see the full report", "", "see the full report."
226}