render.gno
10.92 Kb · 306 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(Badge(pkgpath) + "\n\n")
89 b.WriteString("_" + Disclaimer + "_\n\n")
90 if successor := latestSuccessor(rec); successor != "" {
91 b.WriteString("The author points to a newer version: " + passportLink(successor) + "\n\n")
92 }
93
94 b.WriteString("## Findings\n\n")
95 if len(a.Findings) == 0 {
96 b.WriteString("No findings with the current rules.\n\n")
97 } else {
98 b.WriteString("| # | Rule | Severity | Label | Location |\n| --- | --- | --- | --- | --- |\n")
99 for i, f := range a.Findings {
100 b.WriteString("| " + strconv.Itoa(i+1) + " | " + f.Rule + " | " + f.Severity + " | **" + f.Label + "** | " + sanitize.InlineCode(location(f)) + " |\n")
101 }
102 b.WriteString("\n")
103 for i, f := range a.Findings {
104 renderFinding(&b, i+1, f)
105 }
106 }
107
108 b.WriteString("## Attestation\n\n| | |\n| --- | --- |\n")
109 row := func(k, v string) { b.WriteString("| " + k + " | " + v + " |\n") }
110 row("Attested", date(a.Time)+" (block "+strconv.FormatInt(a.Height, 10)+")")
111 row("Attester", sanitize.InlineCode(a.Attester.String()))
112 row("Source hash", sanitize.InlineCode(a.SourceHash))
113 row("Scanner", sanitize.InlineCode(a.ScannerVersion)+", rule set "+sanitize.InlineCode(a.RulesetHash[:12]))
114 row("Gno toolchain", sanitize.InlineCode(a.Toolchain))
115 if u := reportURL(a.ReportURI); u != "" {
116 row("Full report", "[report]("+u+"), hash "+sanitize.InlineCode(a.ReportHash[:12]))
117 }
118 row("Dependencies", strconv.Itoa(a.Deps.DirectAtRisk)+" direct at risk, "+strconv.Itoa(a.Deps.IndirectAtRisk)+" indirect at risk, "+strconv.Itoa(a.Deps.Unverified)+" unverified")
119 b.WriteString("\n")
120
121 if len(rec.replies) > 0 {
122 b.WriteString("## Author replies\n\n")
123 for _, r := range rec.replies {
124 b.WriteString("- " + sanitize.InlineText(r.Note))
125 if r.Successor != "" {
126 b.WriteString(" (fixed version: " + passportLink(r.Successor) + ")")
127 }
128 b.WriteString("\n")
129 }
130 b.WriteString("\n")
131 }
132
133 if ds := DisputesFor(pkgpath); len(ds) > 0 {
134 b.WriteString("## Disputes\n\n| Filer | Report | Note | Status |\n| --- | --- | --- | --- |\n")
135 for _, d := range ds {
136 status := "open"
137 if d.Resolved {
138 status = "resolved: " + sanitize.TableCell(d.Resolution)
139 }
140 b.WriteString("| " + sanitize.InlineCode(d.Filer.String()) + " | " + sanitize.InlineCode(d.ReportHash[:12]) + " | " + sanitize.TableCell(d.Note) + " | " + status + " |\n")
141 }
142 b.WriteString("\n")
143 }
144
145 renderVersions(&b, pkgpath)
146
147 b.WriteString("## Badge\n\nShow this live badge on your realm's page by adding one line to its `Render`:\n\n")
148 b.WriteString(sanitize.LanguageCodeBlock("go", "out += bouncer.Badge(\""+pkgpath+"\") // import \""+selfPath+"\"") + "\n\n")
149
150 if len(rec.history) > 1 {
151 b.WriteString("## Scans of this version\n\n| Date | Score | Grade | Findings | Change |\n| --- | --- | --- | --- | --- |\n")
152 for i := len(rec.history) - 1; i >= 0; i-- {
153 h := rec.history[i]
154 change := "first scan"
155 if i > 0 {
156 change = diffFindings(rec.history[i-1].Findings, h.Findings)
157 }
158 b.WriteString("| " + date(h.Time) + " | " + strconv.Itoa(h.Score) + " | " + h.Grade + " | " + strconv.Itoa(len(h.Findings)) + " | " + change + " |\n")
159 }
160 }
161 return b.String()
162}
163
164// location is "file:line in Func".
165func location(f Finding) string {
166 loc := f.File + ":" + strconv.Itoa(f.Line)
167 if f.Func != "" {
168 loc += " in " + f.Func + "()"
169 }
170 return loc
171}
172
173// renderFinding writes one finding: what it means, why it is dangerous, the
174// vulnerable code, the fix and why it works, and the exploit if PROVEN.
175func renderFinding(b *strings.Builder, n int, f Finding) {
176 g, known := RuleGuidance(f.Rule)
177 title := g.Title
178 if !known {
179 title = "see the full report"
180 }
181 b.WriteString("### " + strconv.Itoa(n) + ". " + f.Rule + ": " + title + "\n\n")
182 b.WriteString("**" + f.Label + "** · " + f.Severity + " · " + sanitize.InlineCode(location(f)))
183 if g.Guide != "" {
184 b.WriteString(" · security guide " + g.Guide)
185 }
186 b.WriteString("\n\n")
187 if known {
188 b.WriteString("**What it means.** " + g.Meaning + "\n\n")
189 b.WriteString("**Why it is dangerous.** " + g.Danger + "\n\n")
190 }
191 if f.Snippet != "" {
192 b.WriteString("**Vulnerable code** (" + sanitize.InlineCode(f.File) + "):\n\n" + sanitize.LanguageCodeBlock("go", f.Snippet) + "\n\n")
193 }
194 if known {
195 b.WriteString("**Fix.** " + g.Fix + "\n\n")
196 if g.Example != "" {
197 b.WriteString(sanitize.LanguageCodeBlock("go", g.Example) + "\n\n")
198 }
199 b.WriteString("**Why it works.** " + g.Why + "\n\n")
200 }
201 switch f.Label {
202 case LabelProven:
203 b.WriteString("**Proof.** Bouncer wrote an attacker realm and ran it against this exact source")
204 if f.Attack != "" {
205 b.WriteString(": " + sanitize.InlineText(f.Attack))
206 }
207 b.WriteString(".\n\n")
208 if f.Exploit != "" {
209 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")
210 }
211 if f.Before != "" || f.After != "" {
212 b.WriteString("<gno-columns>\n\n**Before the attack**\n\n" + sanitize.CodeBlock(f.Before) +
213 "\n\n<gno-columns-sep />\n\n**After the attack**\n\n" + sanitize.CodeBlock(f.After) + "\n\n</gno-columns>\n\n")
214 }
215 case LabelNotReproduced:
216 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")
217 default:
218 b.WriteString("**Static warning.** The pattern matched, and Bouncer has no exploit generator for this rule yet.\n\n")
219 }
220}
221
222// renderVersions lists every attested version of the package, newest first,
223// with what changed from the version before it.
224func renderVersions(b *strings.Builder, pkgpath string) {
225 vs := Versions(pkgpath)
226 if len(vs) < 2 {
227 return
228 }
229 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")
230 b.WriteString("| Version | Score | Grade | Verdict | Change from the version before |\n| --- | --- | --- | --- | --- |\n")
231 for i := len(vs) - 1; i >= 0; i-- {
232 cur := getRecord(vs[i]).history
233 a := cur[len(cur)-1]
234 change := "first version scanned"
235 if i > 0 {
236 prev := getRecord(vs[i-1]).history
237 change = diffFindings(prev[len(prev)-1].Findings, a.Findings)
238 }
239 name := passportLink(vs[i])
240 if vs[i] == pkgpath {
241 name = "**" + name + "** (this page)"
242 }
243 b.WriteString("| " + name + " | " + strconv.Itoa(a.Score) + " | " + a.Grade + " | " + verdictText(a.Verdict) + " | " + change + " |\n")
244 }
245 b.WriteString("\n")
246}
247
248// latestSuccessor returns the newest successor an author named.
249func latestSuccessor(rec *record) string {
250 for i := len(rec.replies) - 1; i >= 0; i-- {
251 if rec.replies[i].Successor != "" {
252 return rec.replies[i].Successor
253 }
254 }
255 return ""
256}
257
258// diffFindings describes which rules disappeared or appeared between two
259// scans. A rule that was PROVEN and is gone is "resolved"; a warning that is
260// gone is only "no longer detected", since a missing warning proves nothing.
261func diffFindings(prev, next []Finding) string {
262 had, has := map[string]bool{}, map[string]bool{}
263 proven := map[string]bool{}
264 for _, f := range prev {
265 had[f.Rule] = true
266 if f.Label == LabelProven {
267 proven[f.Rule] = true
268 }
269 }
270 for _, f := range next {
271 has[f.Rule] = true
272 }
273 var parts []string
274 done := map[string]bool{}
275 for _, f := range prev {
276 if has[f.Rule] || done[f.Rule] {
277 continue
278 }
279 done[f.Rule] = true
280 if proven[f.Rule] {
281 parts = append(parts, f.Rule+" resolved")
282 } else {
283 parts = append(parts, f.Rule+" no longer detected")
284 }
285 }
286 for _, f := range next {
287 if had[f.Rule] || done[f.Rule] {
288 continue
289 }
290 done[f.Rule] = true
291 parts = append(parts, f.Rule+" new")
292 }
293 if len(parts) == 0 {
294 return "no rule changes"
295 }
296 return strings.Join(parts, ", ")
297}
298
299// reportURL turns a stored report URI into a safe link target. IPFS URIs
300// go through the IPFS gateway gnoweb itself uses, since links need https.
301func reportURL(uri string) string {
302 if cid, ok := strings.CutPrefix(uri, "ipfs://"); ok {
303 uri = "https://ipfs.filebase.io/ipfs/" + cid
304 }
305 return sanitize.URL(uri)
306}