label.gno
3.06 Kb · 109 lines
1// Package label is a Cellar challenge about Render as an attack surface.
2package label
3
4import (
5 "chain/runtime"
6 "strconv"
7 "strings"
8
9 "gno.land/p/g15c02s5hhdny9uvr9za3xsyn9sw8p0klptrh6kf/cellar/flag"
10 "gno.land/p/nt/avl/v0"
11)
12
13const (
14 maxNote = 280 // characters per note
15 maxNotes = 10 // the register keeps each guest's latest notes only
16)
17
18var (
19 master = address("g15c02s5hhdny9uvr9za3xsyn9sw8p0klptrh6kf")
20 sealed = avl.NewTree() // taster address -> block height of the seal
21 notes = avl.NewTree() // author address -> []note
22)
23
24type note struct {
25 text string
26 height int64
27}
28
29// Chalk writes a note on the register, on the caller's own page.
30func Chalk(cur realm, text string) {
31 if text == "" || len(text) > maxNote {
32 panic("label: a note is 1 to " + strconv.Itoa(maxNote) + " characters")
33 }
34 author := cur.Previous().Address().String()
35 list := notesOf(author)
36 list = append(list, note{text: text, height: runtime.ChainHeight()})
37 if len(list) > maxNotes {
38 list = list[len(list)-maxNotes:]
39 }
40 notes.Set(author, list)
41}
42
43// Seal marks taster as trusted. Only the cellar master seals.
44func Seal(cur realm, taster address) {
45 if cur.Previous().Address() != master {
46 panic("label: only the cellar master seals tasters")
47 }
48 sealed.Set(taster.String(), runtime.ChainHeight())
49}
50
51// Inspect asks the master to read your page of the register aloud. If she
52// reads a seal in your name that she never granted, the flag is yours.
53func Inspect(cur realm) {
54 player := flag.MustPlayer(cur.Previous())
55 if sealed.Has(player.String()) {
56 panic("label: you hold a real seal; there is nothing to forge")
57 }
58 if !strings.Contains(Render(player.String()), "\n"+sealLine(player)+"\n") {
59 panic("label: the master reads your page and finds no seal for you")
60 }
61 captures.Record(player)
62}
63
64func sealLine(taster address) string {
65 return "> **Sealed by the cellar master:** " + taster.String()
66}
67
68func notesOf(author string) []note {
69 if v := notes.Get(author); v != nil {
70 return v.([]note)
71 }
72 return nil
73}
74
75// Render prints the register. The front page lists the seals; a guest's
76// chalk marks are on their own page, at :<address>.
77func Render(path string) string {
78 var b strings.Builder
79 b.WriteString(flag.Card(info, captures))
80 b.WriteString("\n## Seals\n\n")
81 if sealed.Size() == 0 {
82 b.WriteString("*The master has sealed nobody yet.*\n")
83 }
84 sealed.Iterate("", "", func(addr string, _ any) bool {
85 b.WriteString(sealLine(address(addr)) + "\n\n")
86 return false
87 })
88
89 if path == "" {
90 b.WriteString("\n## Chalk marks\n\n" + strconv.Itoa(notes.Size()) +
91 " guests have chalked the register. Each guest's marks are on their own page: `:<address>`.\n")
92 b.WriteString("\n## Captured by\n\n" + flag.Captures(captures, 5))
93 return b.String()
94 }
95
96 if !address(path).IsValid() {
97 b.WriteString("\n*No such guest.*\n")
98 return b.String()
99 }
100 b.WriteString("\n## Chalk marks by `" + path + "`\n\n")
101 list := notesOf(path)
102 if len(list) == 0 {
103 b.WriteString("*No marks.*\n")
104 }
105 for _, n := range list {
106 b.WriteString("- (block " + strconv.FormatInt(n.height, 10) + ") " + n.text + "\n")
107 }
108 return b.String()
109}