Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

guidance.gno

4.28 Kb · 61 lines
 1package bouncer
 2
 3// Guidance is the fix guidance for one rule. It is written once per rule,
 4// so every Passport says the same thing and it costs nothing per finding.
 5type Guidance struct {
 6	Title   string // short name of the bug class
 7	Guide   string // security guide sections
 8	Meaning string // what the finding means
 9	Danger  string // why it is dangerous
10	Fix     string // what to change
11	Example string // the fixed code, Gno
12	Why     string // why the fix works
13}
14
15// RuleGuidance returns the guidance for rule, and false for a rule this
16// realm has no guidance for yet.
17func RuleGuidance(rule string) (Guidance, bool) {
18	switch rule {
19	case "B01":
20		return Guidance{
21			Title:   "Getter returns a pointer into state",
22			Guide:   "§5.1, §5.1a",
23			Meaning: "An exported function hands out a pointer to the realm's own state, and the pointed-to type has an exported method that changes it.",
24			Danger:  "Readonly taint stops another realm writing fields through the pointer, but not calling methods on it. Any realm can call the mutating method, and the write commits with this realm's authority: balances, owners and counters change with no check at all.",
25			Fix:     "Return copies or plain values, never a pointer into package state. Keep mutating methods unexported and reach them only through crossing functions that check the caller.",
26			Example: "type Entry struct {\n\tAddr    address\n\tBalance int64\n}\n\n// Balances returns a copy of the ledger.\nfunc Balances() []Entry {\n\tout := []Entry{}\n\tledger.Iterate(\"\", \"\", func(k string, v any) bool {\n\t\tout = append(out, Entry{address(k), v.(int64)})\n\t\treturn false\n\t})\n\treturn out\n}",
27			Why:     "A copy has no path back into the realm's storage, so nothing a caller does to it can reach the real state.",
28		}, true
29	case "B05":
30		return Guidance{
31			Title:   "IsUser() guards an OriginSend payment",
32			Guide:   "§5.5",
33			Meaning: "A payment is credited from `OriginSend()` behind an `IsUser()` check.",
34			Danger:  "`IsUser()` is also true for a `maketx run` script. In that case the coins in `OriginSend()` go to the script's own realm, not to this one, so the caller is credited for money this realm never received and can withdraw other users' funds.",
35			Fix:     "Accept payments only from a direct call: check `cur.Previous().IsUserCall()` before reading `OriginSend()`.",
36			Example: "func Deposit(cur realm) {\n\tif !cur.IsCurrent() {\n\t\tpanic(\"not current\")\n\t}\n\tif !cur.Previous().IsUserCall() {\n\t\tpanic(\"deposit with maketx call\")\n\t}\n\tamount := unsafe.OriginSend().AmountOf(\"ugnot\")\n\t// credit amount\n}",
37			Why:     "`IsUserCall()` is true only for a direct `maketx call`, where the sent coins are guaranteed to land at this realm before it runs.",
38		}, true
39	case "B03":
40		return Guidance{
41			Title:   "Caller-supplied callback runs with the realm's authority",
42			Guide:   "§3C, §5.3",
43			Meaning: "The realm calls a function value its caller passed in, and hands it a map, slice, pointer or other reference.",
44			Danger:  "A top-level function from a /p/ package does not switch realms when called, so inside this call it runs as this realm and can write whatever it is handed, such as the live balances map.",
45			Fix:     "Do not call caller-supplied functions. If you must, type the callback with a type declared in this realm, which /p/ code cannot name, and hand it a copy.",
46			Example: "type Entry struct {\n\tPlayer string\n\tPoints int64\n}\n\nfunc Report(cur realm, format func(rows []Entry) string) string {\n\tif !cur.IsCurrent() {\n\t\tpanic(\"not current\")\n\t}\n\treturn format(copyRows())\n}",
47			Why:     "A /p/ package cannot import a realm, so it cannot supply a func([]Entry); a function from another realm, or a closure, runs under its own realm and cannot write this realm's data.",
48		}, true
49	case "B09":
50		return Guidance{
51			Title:   "External Teller used without IsCanonicalTeller",
52			Guide:   "§5.4",
53			Meaning: "The realm calls a GRC20 Teller that a caller passed in, without checking it.",
54			Danger:  "A fake Teller can report transfers that never happened.",
55			Fix:     "Check `grc20.IsCanonicalTeller(t)` before using a Teller from a caller, or look the token up yourself.",
56			Example: "",
57			Why:     "A canonical Teller is the token's own, so its transfers are real.",
58		}, true
59	}
60	return Guidance{}, false
61}