details.gno
2.80 Kb · 103 lines
1package bouncer
2
3import (
4 "chain"
5 "strconv"
6 "strings"
7 "unicode/utf8"
8
9 "gno.land/p/nt/seqid/v0"
10)
11
12// Size limits for finding details. The full report (ReportURI) has
13// everything; the Passport shows what fits here.
14const (
15 maxSnippetLen = 1500
16 maxAttackLen = 200
17 maxExploitLen = 6000
18 maxEvidenceLen = 1500
19 // maxDetailBytes caps the detail text of one attestation.
20 maxDetailBytes = 32000
21)
22
23// AddDetail attaches the vulnerable snippet and, for a PROVEN finding, the
24// exploit and its evidence to finding index of attestation id. Only the
25// attester who posted the attestation may call it, once per finding: the
26// details are as permanent as the attestation itself.
27func AddDetail(cur realm, pkgpath string, id uint64, index int, snippet, attack, exploit, before, after string) {
28 caller := assertAttester(cur)
29 rec := getRecord(pkgpath)
30 if rec == nil {
31 panic("no attestation for this package")
32 }
33 ai := -1
34 for i := range rec.history {
35 if rec.history[i].ID == id {
36 ai = i
37 break
38 }
39 }
40 if ai < 0 {
41 panic("no such attestation for this package")
42 }
43 a := &rec.history[ai]
44 if a.Attester != caller {
45 panic("only the attestation's own attester can add details")
46 }
47 if index < 0 || index >= len(a.Findings) {
48 panic("finding index out of range")
49 }
50 f := &a.Findings[index]
51 if f.Detailed {
52 panic("details already added for this finding")
53 }
54 checkText("snippet", snippet, maxSnippetLen)
55 checkText("attack", attack, maxAttackLen)
56 checkText("exploit", exploit, maxExploitLen)
57 checkText("before", before, maxEvidenceLen)
58 checkText("after", after, maxEvidenceLen)
59 if strings.ContainsAny(attack, "\n\r") {
60 panic("attack must be one line")
61 }
62 proofLen := len(attack) + len(exploit) + len(before) + len(after)
63 if f.Label != LabelProven && proofLen > 0 {
64 panic("only PROVEN findings carry an exploit and evidence")
65 }
66 if detailBytes(a.Findings)+len(snippet)+proofLen > maxDetailBytes {
67 panic("detail limit reached for this attestation; see the full report")
68 }
69 f.Detailed = true
70 f.Snippet = snippet
71 f.Attack = attack
72 f.Exploit = exploit
73 f.Before = before
74 f.After = after
75 chain.Emit("DetailAdded",
76 "id", seqid.ID(id).String(),
77 "pkgpath", pkgpath,
78 "index", strconv.Itoa(index))
79}
80
81func detailBytes(fs []Finding) int {
82 n := 0
83 for _, f := range fs {
84 n += len(f.Snippet) + len(f.Attack) + len(f.Exploit) + len(f.Before) + len(f.After)
85 }
86 return n
87}
88
89// checkText rejects text that is too long, not UTF-8, or carries NUL or
90// other control characters besides tab and newline.
91func checkText(name, s string, max int) {
92 if len(s) > max {
93 panic(name + " is too long (max " + strconv.Itoa(max) + " bytes)")
94 }
95 if !utf8.ValidString(s) {
96 panic(name + " must be valid UTF-8")
97 }
98 for _, c := range s {
99 if c < 0x20 && c != '\n' && c != '\t' || c == 0x7f {
100 panic(name + " contains a control character")
101 }
102 }
103}