render.gno
12.68 Kb · 357 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
15// "hunters" link to the Training Grounds hunters board
16func Render(path string) string {
17 if pkgpath, ok := strings.CutPrefix(path, "realm/"); ok {
18 return renderPassport(pkgpath)
19 }
20 if path == "" {
21 return renderNoticeboard()
22 }
23 if path == "hunters" {
24 return "# Hunters\n\nThe hunters board lives with the Training Grounds, which this registry does not import: " +
25 "a deliberately vulnerable target must never count as one of Bouncer's own dependencies.\n\n" +
26 "[Open the hunters board](" + groundsWebPath() + ")\n"
27 }
28 return "# Not found\n\n[Back to the noticeboard](" + webPath() + ")\n"
29}
30
31// groundsWebPath is the Training Grounds' gnoweb path: "grounds" next to
32// this realm's /vN, in the same namespace.
33func groundsWebPath() string {
34 p := webPath()
35 return p[:strings.LastIndex(p, "/")] + "/grounds"
36}
37
38// webPath is this realm's gnoweb URL path ("/r/...").
39func webPath() string {
40 return strings.TrimPrefix(selfPath, "gno.land")
41}
42
43// passportLink links to pkgpath's Passport. pkgpath is a validated path
44// (lowercase letters, digits, underscores and slashes), so it is URL-safe.
45func passportLink(pkgpath string) string {
46 return "[" + sanitize.InlineCode(pkgpath) + "](" + webPath() + ":realm/" + pkgpath + ")"
47}
48
49func verdictText(v string) string {
50 switch v {
51 case VerdictNotRecommended:
52 return "🔴 Not recommended for integration"
53 case VerdictReview:
54 return "🟠 Review before integration"
55 }
56 return "🟢 No known Critical or High issues"
57}
58
59func date(unix int64) string {
60 return time.Unix(unix, 0).UTC().Format("2006-01-02")
61}
62
63func renderNoticeboard() string {
64 var b strings.Builder
65 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")
66 b.WriteString("| Passports | Findings | PROVEN | Open requests |\n| --- | --- | --- | --- |\n")
67 b.WriteString("| " + strconv.Itoa(records.Size()) + " | " + strconv.Itoa(totalFindings) + " | " + strconv.Itoa(totalProven) + " | " + strconv.Itoa(openByRequester.Size()) + " |\n\n")
68
69 b.WriteString("## Recent Passports\n\n")
70 if len(recent) == 0 {
71 b.WriteString("No packages attested yet.\n\n")
72 } else {
73 b.WriteString("| Package | Score | Grade | Verdict |\n| --- | --- | --- | --- |\n")
74 for _, p := range recent {
75 a, _ := Latest(p)
76 b.WriteString("| " + passportLink(p) + " | " + strconv.Itoa(a.Score) + " | " + a.Grade + " | " + verdictText(a.Verdict) + " |\n")
77 }
78 b.WriteString("\n")
79 }
80
81 b.WriteString("## Training Grounds\n\nPractice targets with a real bug each. Break one, claim it, and land on the [hunters board](" + groundsWebPath() + ").\n\n")
82 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")
83 b.WriteString("<gno-form exec=\"RequestScan\">\n<gno-input name=\"pkgpath\" placeholder=\"gno.land/r/<address>/myrealm\" />\n</gno-form>\n\n")
84 b.WriteString("_" + Disclaimer + "_\n")
85 return b.String()
86}
87
88func renderPassport(pkgpath string) string {
89 if !validPkgPath(pkgpath) {
90 return "# Not found\n\nThat is not a valid package path.\n"
91 }
92 rec := getRecord(pkgpath)
93 var b strings.Builder
94 b.WriteString("# Passport: " + sanitize.InlineCode(pkgpath) + "\n\n")
95 if rec == nil {
96 b.WriteString("Bouncer has not attested this package yet. [Request a scan](" + webPath() + ").\n")
97 return b.String()
98 }
99 a, _ := Latest(pkgpath)
100
101 b.WriteString("## " + strconv.Itoa(a.Score) + " / 100 · Grade " + a.Grade + " · " + verdictText(a.Verdict) + "\n\n")
102 b.WriteString(Badge(pkgpath) + "\n\n")
103 b.WriteString("_" + Disclaimer + "_\n\n")
104 if successor := latestSuccessor(rec); successor != "" {
105 b.WriteString("The author points to a newer version: " + passportLink(successor) + "\n\n")
106 }
107
108 b.WriteString("## Findings\n\n")
109 if len(a.Findings) == 0 {
110 b.WriteString("No findings with the current rules.\n\n")
111 } else {
112 b.WriteString("| # | Rule | Severity | Label | Location |\n| --- | --- | --- | --- | --- |\n")
113 for i, f := range a.Findings {
114 b.WriteString("| " + strconv.Itoa(i+1) + " | " + f.Rule + " | " + f.Severity + " | **" + f.Label + "** | " + sanitize.InlineCode(location(f)) + " |\n")
115 }
116 b.WriteString("\n")
117 for i, f := range a.Findings {
118 renderFinding(&b, i+1, f)
119 }
120 }
121
122 b.WriteString("## Attestation\n\n| | |\n| --- | --- |\n")
123 row := func(k, v string) { b.WriteString("| " + k + " | " + v + " |\n") }
124 row("Attested", date(a.Time)+" (block "+strconv.FormatInt(a.Height, 10)+")")
125 row("Attester", sanitize.InlineCode(a.Attester.String()))
126 row("Source hash", sanitize.InlineCode(a.SourceHash))
127 row("Scanner", sanitize.InlineCode(a.ScannerVersion)+", rule set "+sanitize.InlineCode(a.RulesetHash[:12]))
128 row("Gno toolchain", sanitize.InlineCode(a.Toolchain))
129 if u := reportURL(a.ReportURI); u != "" {
130 row("Full report", "[report]("+u+"), hash "+sanitize.InlineCode(a.ReportHash[:12]))
131 }
132 b.WriteString("\n")
133 renderDeps(&b, a.Deps)
134
135 if len(rec.replies) > 0 {
136 b.WriteString("## Author replies\n\n")
137 for _, r := range rec.replies {
138 b.WriteString("- " + sanitize.InlineText(r.Note))
139 if r.Successor != "" {
140 b.WriteString(" (fixed version: " + passportLink(r.Successor) + ")")
141 }
142 b.WriteString("\n")
143 }
144 b.WriteString("\n")
145 }
146
147 if ds := DisputesFor(pkgpath); len(ds) > 0 {
148 b.WriteString("## Disputes\n\n| Filer | Report | Note | Status |\n| --- | --- | --- | --- |\n")
149 for _, d := range ds {
150 status := "open"
151 if d.Resolved {
152 status = "resolved: " + sanitize.TableCell(d.Resolution)
153 }
154 b.WriteString("| " + sanitize.InlineCode(d.Filer.String()) + " | " + sanitize.InlineCode(d.ReportHash[:12]) + " | " + sanitize.TableCell(d.Note) + " | " + status + " |\n")
155 }
156 b.WriteString("\n")
157 }
158
159 renderVersions(&b, pkgpath)
160
161 b.WriteString("## Badge\n\nShow this live badge on your realm's page by adding one line to its `Render`:\n\n")
162 b.WriteString(sanitize.LanguageCodeBlock("go", "out += bouncer.Badge(\""+pkgpath+"\") // import \""+selfPath+"\"") + "\n\n")
163
164 if len(rec.history) > 1 {
165 b.WriteString("## Scans of this version\n\n| Date | Score | Grade | Findings | Change |\n| --- | --- | --- | --- | --- |\n")
166 for i := len(rec.history) - 1; i >= 0; i-- {
167 h := live(rec.history[i])
168 change := "first scan"
169 if i > 0 {
170 change = diffFindings(rec.history[i-1].Findings, h.Findings)
171 }
172 b.WriteString("| " + date(h.Time) + " | " + strconv.Itoa(h.Score) + " | " + h.Grade + " | " + strconv.Itoa(len(h.Findings)) + " | " + change + " |\n")
173 }
174 }
175 return b.String()
176}
177
178// location is "file:line in Func".
179func location(f Finding) string {
180 loc := f.File + ":" + strconv.Itoa(f.Line)
181 if f.Func != "" {
182 loc += " in " + f.Func + "()"
183 }
184 return loc
185}
186
187// renderFinding writes one finding: what it means, why it is dangerous, the
188// vulnerable code, the fix and why it works, and the exploit if PROVEN.
189func renderFinding(b *strings.Builder, n int, f Finding) {
190 g, known := RuleGuidance(f.Rule)
191 title := g.Title
192 if !known {
193 title = "see the full report"
194 }
195 b.WriteString("### " + strconv.Itoa(n) + ". " + f.Rule + ": " + title + "\n\n")
196 b.WriteString("**" + f.Label + "** · " + f.Severity + " · " + sanitize.InlineCode(location(f)))
197 if g.Guide != "" {
198 b.WriteString(" · security guide " + g.Guide)
199 }
200 b.WriteString("\n\n")
201 if known {
202 b.WriteString("**What it means.** " + g.Meaning + "\n\n")
203 b.WriteString("**Why it is dangerous.** " + g.Danger + "\n\n")
204 }
205 if f.Snippet != "" {
206 b.WriteString("**Vulnerable code** (" + sanitize.InlineCode(f.File) + "):\n\n" + sanitize.LanguageCodeBlock("go", f.Snippet) + "\n\n")
207 }
208 if known {
209 b.WriteString("**Fix.** " + g.Fix + "\n\n")
210 if g.Example != "" {
211 b.WriteString(sanitize.LanguageCodeBlock("go", g.Example) + "\n\n")
212 }
213 b.WriteString("**Why it works.** " + g.Why + "\n\n")
214 }
215 switch f.Label {
216 case LabelProven:
217 b.WriteString("**Proof.** Bouncer wrote an attacker realm and ran it against this exact source")
218 if f.Attack != "" {
219 b.WriteString(": " + sanitize.InlineText(f.Attack))
220 }
221 b.WriteString(".\n\n")
222 if f.Exploit != "" {
223 b.WriteString("Attacker realm (lines that print Bouncer's evidence markers are left out; the full report has the exact source):\n\n" + sanitize.LanguageCodeBlock("go", f.Exploit) + "\n\n")
224 }
225 if f.Before != "" || f.After != "" {
226 b.WriteString("Only the lines that changed are shown; `...` marks lines left out. The full report has the complete output.\n\n")
227 b.WriteString("<gno-columns>\n\n**Before the attack**\n\n" + sanitize.CodeBlock(f.Before) +
228 "\n\n<gno-columns-sep />\n\n**After the attack**\n\n" + sanitize.CodeBlock(f.After) + "\n\n</gno-columns>\n\n")
229 }
230 case LabelNotReproduced:
231 b.WriteString("**Not reproduced.** Bouncer's generated exploit did not land. This stays a warning: it is not evidence that the code is safe.\n\n")
232 default:
233 b.WriteString("**Static warning.** The pattern matched, and Bouncer has no exploit generator for this rule yet.\n\n")
234 }
235}
236
237// renderVersions lists every attested version of the package, newest first,
238// with what changed from the version before it.
239func renderVersions(b *strings.Builder, pkgpath string) {
240 vs := Versions(pkgpath)
241 if len(vs) < 2 {
242 return
243 }
244 b.WriteString("## Versions\n\nPackages on Gno cannot be overwritten, so each fix ships at a new path. Latest scan of each version:\n\n")
245 b.WriteString("| Version | Score | Grade | Verdict | Change from the version before |\n| --- | --- | --- | --- | --- |\n")
246 for i := len(vs) - 1; i >= 0; i-- {
247 cur := getRecord(vs[i]).history
248 a := live(cur[len(cur)-1])
249 change := "first version scanned"
250 if i > 0 {
251 prev := getRecord(vs[i-1]).history
252 change = diffFindings(prev[len(prev)-1].Findings, a.Findings)
253 }
254 name := passportLink(vs[i])
255 if vs[i] == pkgpath {
256 name = "**" + name + "** (this page)"
257 }
258 b.WriteString("| " + name + " | " + strconv.Itoa(a.Score) + " | " + a.Grade + " | " + verdictText(a.Verdict) + " | " + change + " |\n")
259 }
260 b.WriteString("\n")
261}
262
263// latestSuccessor returns the newest successor an author named.
264func latestSuccessor(rec *record) string {
265 for i := len(rec.replies) - 1; i >= 0; i-- {
266 if rec.replies[i].Successor != "" {
267 return rec.replies[i].Successor
268 }
269 }
270 return ""
271}
272
273// diffFindings describes which rules disappeared or appeared between two
274// scans. A rule that was PROVEN and is gone is "resolved"; a warning that is
275// gone is only "no longer detected", since a missing warning proves nothing.
276func diffFindings(prev, next []Finding) string {
277 had, has := map[string]bool{}, map[string]bool{}
278 proven := map[string]bool{}
279 for _, f := range prev {
280 had[f.Rule] = true
281 if f.Label == LabelProven {
282 proven[f.Rule] = true
283 }
284 }
285 for _, f := range next {
286 has[f.Rule] = true
287 }
288 var parts []string
289 done := map[string]bool{}
290 for _, f := range prev {
291 if has[f.Rule] || done[f.Rule] {
292 continue
293 }
294 done[f.Rule] = true
295 if proven[f.Rule] {
296 parts = append(parts, f.Rule+" resolved")
297 } else {
298 parts = append(parts, f.Rule+" no longer detected")
299 }
300 }
301 for _, f := range next {
302 if had[f.Rule] || done[f.Rule] {
303 continue
304 }
305 done[f.Rule] = true
306 parts = append(parts, f.Rule+" new")
307 }
308 if len(parts) == 0 {
309 return "no rule changes"
310 }
311 return strings.Join(parts, ", ")
312}
313
314// reportURL turns a stored report URI into a safe link target. IPFS URIs
315// go through the IPFS gateway gnoweb itself uses, since links need https.
316func reportURL(uri string) string {
317 if cid, ok := strings.CutPrefix(uri, "ipfs://"); ok {
318 uri = "https://ipfs.filebase.io/ipfs/" + cid
319 }
320 return sanitize.URL(uri)
321}
322
323// renderDeps shows dependency trust as it stands now, direct first.
324func renderDeps(b *strings.Builder, d Deps) {
325 b.WriteString("## Dependencies\n\n")
326 if len(d.List) == 0 {
327 b.WriteString("No `gno.land` imports.\n\n")
328 return
329 }
330 b.WriteString(strconv.Itoa(d.DirectAtRisk) + " direct at risk, " + strconv.Itoa(d.IndirectAtRisk) + " indirect at risk, " +
331 strconv.Itoa(d.Unverified) + " unverified. A dependency at risk does not mean this package is exploitable; " +
332 "an unverified one has not been scanned yet, which is not a finding.\n\n")
333 b.WriteString("| Dependency | Import | Status |\n| --- | --- | --- |\n")
334 for _, direct := range []bool{true, false} {
335 for _, dep := range d.List {
336 if dep.Direct != direct {
337 continue
338 }
339 kind := "indirect"
340 if direct {
341 kind = "direct"
342 }
343 b.WriteString("| " + passportLink(dep.Path) + " | " + kind + " | " + depStatusText(dep.Status) + " |\n")
344 }
345 }
346 b.WriteString("\n")
347}
348
349func depStatusText(s string) string {
350 switch s {
351 case DepVerified:
352 return "🟢 Verified"
353 case DepAtRisk:
354 return "🔴 At risk"
355 }
356 return "⚪ Unverified"
357}