Playground
example_test.gno
package reaper
import (
"strings"
"gno.land/p/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/x/storagecost/v0"
"gno.land/p/nt/ufmt/v0"
)
// Every Example in this file is a *query*, and that is a constraint rather
// than a choice: an example function takes no arguments, so it never receives
// a `cur realm` and can never call Post, Reap or Compact. What is left is
// exactly the surface a reader of the live realm has without a key, so each
// one carries the `vm/qeval` line that produces the same answer on chain:
//
// gnokey query vm/qeval -remote https://rpc.gno.land \
// -data 'gno.land/r/moul/x/reaper/v0.<expr>'
//
// The board is empty by the time these run, because every Test in this
// package drains what it posted and examples run after all of them.
// ExampleRender pins the page's skeleton: the title, the two sections that are
// always present, and the footer crediting the two packages that own the parts
// this realm does not.
//
// vm/qrender gno.land/r/moul/x/reaper/v0:
//
// It deliberately prints only the invariant lines. Everything interesting on
// the page is priced from the current block height and the notes outstanding,
// and an Example runs after every Test in the package and sees whatever state
// they left. The variable half is pinned by TestRenderShowsTheBountyAndTheReapLink
// instead, which controls the board first.
func ExampleRender() {
for _, line := range strings.Split(Render(""), "\n") {
switch {
case strings.HasPrefix(line, "# "),
strings.HasPrefix(line, "## "),
strings.HasPrefix(line, "A noticeboard"),
strings.HasPrefix(line, "The arithmetic is"):
print(line + "\n")
}
}
// Output:
// # Reaper
// A noticeboard whose garbage is a standing bounty. Posting a note locks a storage deposit. Once the note expires, anyone can delete it and the chain refunds that deposit **to whoever signs the deleting transaction**.
// ## On the table right now
// ## Board
// The arithmetic is [p/moul/x/storagecost](/p/moul/x/storagecost/v0); the storage is [p/moul/ulist](/p/moul/ulist/v1), whose `Compact` reclaims dead nodes without moving a live index. Byte figures on this page are estimates from payload length: no stdlib call exposes a realm's real locked storage.
}
// ExampleReapable is the poll a reaping bot runs before it spends anything:
// three counters, three queries, no key and no gas.
//
// vm/qeval gno.land/r/moul/x/reaper/v0.Live()
// vm/qeval gno.land/r/moul/x/reaper/v0.Reapable()
// vm/qeval gno.land/r/moul/x/reaper/v0.Compactable()
//
// Reapable and Compactable are separate numbers because reaping and
// compacting are separate transactions, and compaction returns nothing at all
// while a live note still sits below the dead ones. A bot that sees
// `reapable 0, compactable 0` should not sign anything, which is the answer
// here.
func ExampleReapable() {
print(ufmt.Sprintf("live %d, reapable %d, compactable %d\n",
Live(), Reapable(), Compactable()))
// Output:
// live 0, reapable 0, compactable 0
}
// ExampleBounty is the single query that decides whether reaping is worth
// doing, and on an empty board it has to refuse to advertise a profit.
//
// vm/qeval gno.land/r/moul/x/reaper/v0.Bounty().String()
//
// Fee is what the reaping transaction costs at the floor gas price, and the
// chain charges it whether or not anything is freed. BreakEven turns that fee
// back into bytes: free fewer than 50 and the reaper is paying to tidy up.
// That threshold, not the size of the board, is what makes the mechanism a
// market rather than a chore.
func ExampleBounty() {
print(Bounty().String() + "\n")
// Output:
// 0 bytes, refunds 0 GNOT against 0.005 GNOT of gas, break-even 50 bytes: not worth it yet
}
// ExampleBounty_worth is the same query answered by a board worth draining:
// ten expired notes of a kilobyte each.
//
// It recomputes the quote instead of posting, because an example has no
// `cur realm` to post with. The arithmetic is the one Bounty applies, so the
// numbers are the ones the live query would return with that board on it.
//
// This is the whole concept in two lines. The poster locked 1.8944 GNOT to
// occupy that space; a stranger who never posted anything spends 0.005 GNOT of
// gas to delete it and the chain hands them the deposit. Nobody minted a
// token, and no pool was funded.
func ExampleBounty_worth() {
q := storagecost.EvaluateAtFloor(storagecost.EstimateBytes(10*1024), gasWantedReap)
print(q.String() + "\n")
print(ufmt.Sprintf("the reaper is paid %dx what the transaction costs\n", q.Refund/q.Fee))
// Output:
// 18944 bytes, refunds 1.8944 GNOT against 0.005 GNOT of gas, break-even 50 bytes: worth 1.8894 GNOT
// the reaper is paid 378x what the transaction costs
}
gnomod.toml
module = "gno.land/r/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/x/reaper/v1" gno = "0.9" private = true [addpkg] creator = "g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv" height = 914348 max_deposit = "17000000ugnot"
reaper.gno
// Package reaper is a noticeboard whose garbage is a standing bounty.
//
// Anyone can post a note with an expiry. Posting locks a storage deposit, paid
// by the poster. Once a note expires, anyone at all can delete it, and the
// chain refunds that deposit to whoever signed the deleting transaction. So
// the poster pays to occupy space and a stranger is paid to reclaim it.
//
// Nothing here is a token, a reward pool or an emission schedule. The incentive
// is the chain's own storage accounting, which already works this way for every
// realm on gno.land; this realm only makes it legible. Reap and Compact are
// permissionless because the expiry predicate is checked on chain, so the
// worst a malicious reaper can do is waste their own gas.
//
// Two collaborators own the parts this realm does not:
//
// - gno.land/p/moul/ulist/v1 stores the notes and owns compaction. Its
// Delete is a soft delete that leaves a dead tree node behind, and its
// Compact frees those nodes without moving any live index.
// - gno.land/p/moul/x/storagecost/v0 owns the arithmetic: what a byte
// refunds, and how many bytes a transaction must free to pay for itself.
// - gno.land/p/moul/kit/ui/v0 owns display: the escaping, the length cap
// and the address format. A note body is arbitrary bytes chosen by
// whoever paid to post it and is rendered back to every reader, so it is
// attacker-controlled input in a markdown slot. Render never emits a body
// unescaped; ui.Excerpt is what makes that true, and it delegates to
// p/nt/markdown/sanitize.
//
// The realm deliberately cannot see its own byte count. No stdlib call exposes
// a realm's locked storage, so every figure Render shows is an estimate from
// payload length. The authoritative numbers are the chain's, in the
// StorageDepositEvent and StorageUnlockEvent each transaction emits.
package reaper
import (
"strings"
"chain/runtime"
"gno.land/p/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/kit/ui/v0"
"gno.land/p/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/md/v0"
"gno.land/p/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/ulist/v1"
"gno.land/p/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/x/storagecost/v0"
"gno.land/p/nt/ufmt/v0"
)
// gasWantedReap is the gas ceiling a reaping transaction is assumed to ask
// for, used only to price the advertised bounty. It is the measured cost of a
// ten-note reap rounded up; a caller reaping more notes should re-price with
// storagecost.EvaluateAtFloor directly rather than trust this.
const gasWantedReap int64 = 5_000_000
// maxBody caps a note so one poster cannot lock an unbounded deposit in a
// single call, which would also make the gas cost of reaping it unpredictable.
const maxBody = 4096
// Note is one posting. Body is what costs storage; the rest is bookkeeping
// that makes the incentive visible in Render.
type Note struct {
Body string
Author address
Posted int64 // block height
Expires int64 // block height; reapable once ChainHeight passes it
}
// notes is append-addressed: an index is stable for the life of the realm,
// which is what lets Compact reclaim dead nodes without renumbering.
var notes = ulist.New()
// Post adds a note that becomes reapable ttl blocks from now, and locks the
// storage deposit for it against the caller.
//
// A ttl of zero is allowed and makes the note reapable immediately, which is
// the cheapest way to demonstrate the mechanism.
func Post(cur realm, body string, ttl int64) int {
if body == "" {
panic("reaper: empty note")
}
if len(body) > maxBody {
panic(ufmt.Sprintf("reaper: note too long, %d bytes against a %d cap", len(body), maxBody))
}
if ttl < 0 {
panic("reaper: negative ttl")
}
height := runtime.ChainHeight()
notes.Append(&Note{
Body: body,
Author: cur.Previous().Address(),
Posted: height,
Expires: height + ttl,
})
return notes.TotalSize() - 1
}
// Reap deletes up to limit expired notes and returns how many it deleted.
//
// Permissionless by design. The storage deposit freed goes to whoever signed
// this transaction, so a stranger keeping the board tidy is paid for it out of
// the deposits the posters locked. A note that has not expired is skipped, not
// refused, so a reaper never has to guess which indices are ripe.
//
// It walks from the highest index down, which is not cosmetic. In the backing
// list the oldest indices are the ancestors of the newest, so a node can only
// be freed once everything below it is dead. Reaping oldest-first with a
// binding limit therefore never creates a dead tail and leaves Compactable at
// zero, stranding the tree structure, which measures at roughly two thirds of
// what an entry costs. Reaping newest-first makes each batch immediately
// compactable. Every candidate is expired either way, so the order changes
// only who gets paid how much, and it is measured: see the ulist package doc.
func Reap(cur realm, limit int) int {
if limit <= 0 {
panic("reaper: limit must be positive")
}
height := runtime.ChainHeight()
reaped := 0
for i := notes.TotalSize() - 1; i >= 0 && reaped < limit; i-- {
n, ok := noteAt(i)
if !ok || n.Expires > height {
continue
}
notes.MustDelete(i)
reaped++
}
return reaped
}
// Compact frees the tree nodes that reaping left behind and returns how many.
//
// Also permissionless, and also paid the same way. It is a separate call
// because it is a separate economic decision, and a surprisingly large one: on
// chain, compacting a drained board returned about three times what deleting
// the notes themselves did, because a list node costs more than the note it
// carries. But it returns nothing at all while any live note sits below the
// dead ones, so the two calls are worth batching in that order: reap, then
// compact. Compactable says how much is actually there, for free.
func Compact(cur realm) int {
return notes.Compact()
}
// Reapable counts the notes that have expired and not yet been reaped.
func Reapable() int {
height := runtime.ChainHeight()
count := 0
total := notes.TotalSize()
for i := 0; i < total; i++ {
n, ok := noteAt(i)
if ok && n.Expires <= height {
count++
}
}
return count
}
// Compactable reports how many dead tree nodes a Compact would free.
func Compactable() int { return notes.Compactable() }
// Live counts the notes still holding storage.
func Live() int { return notes.Size() }
// Bounty prices what is currently on the table for a reaper, at the default
// storage price and the floor gas price.
//
// The byte figure is an estimate from payload length, never the chain's own
// accounting. Treat it as an advertisement, not a settlement.
func Bounty() storagecost.Quote {
height := runtime.ChainHeight()
var payload int64
total := notes.TotalSize()
for i := 0; i < total; i++ {
n, ok := noteAt(i)
if ok && n.Expires <= height {
payload += int64(len(n.Body))
}
}
return storagecost.EvaluateAtFloor(storagecost.EstimateBytes(payload), gasWantedReap)
}
// noteAt reads index i, reporting whether a live note is there. A reaped or
// compacted index reads as absent.
func noteAt(i int) (*Note, bool) {
v := notes.Get(i)
if v == nil {
return nil, false
}
n, ok := v.(*Note)
return n, ok
}
func Render(path string) string {
var b strings.Builder
b.WriteString(md.H1("Reaper"))
b.WriteString("\nA noticeboard whose garbage is a standing bounty. Posting a note locks a storage deposit. Once the note expires, anyone can delete it and the chain refunds that deposit **to whoever signs the deleting transaction**.\n\n")
quote := Bounty()
b.WriteString(md.H2("On the table right now"))
b.WriteString("\n")
reapable := Reapable()
if reapable == 0 {
b.WriteString("Nothing has expired. Every note here is still paid for.\n\n")
} else {
b.WriteString(md.BulletList([]string{
ufmt.Sprintf("**%d expired notes**, about %d bytes of state", reapable, quote.Bytes),
ufmt.Sprintf("refunds roughly **%s** to the reaper", storagecost.FormatGNOT(quote.Refund)),
ufmt.Sprintf("against **%s** of gas at the floor price, break-even at %d bytes", storagecost.FormatGNOT(quote.Fee), quote.BreakEven),
ufmt.Sprintf("verdict: **%s**", verdictWord(quote)),
}))
b.WriteString("\n")
b.WriteString(ui.Action("Reap them", "Reap", "limit", "100") + "\n\n")
}
if dead := Compactable(); dead > 0 {
b.WriteString(md.H2("Compaction available"))
b.WriteString(ufmt.Sprintf("\n%d dead tree nodes are reclaimable. Compacting frees fewer bytes per unit of gas than reaping does, so it is worth doing in batches.\n\n%s\n\n",
dead, ui.Action("Compact", "Compact")))
}
b.WriteString(md.H2("Board"))
b.WriteString("\n")
if notes.Size() == 0 {
b.WriteString("Empty. " + ui.Action("Post the first note", "Post", "body", "hello", "ttl", "0") + "\n\n")
} else {
height := runtime.ChainHeight()
rows := []string{}
total := notes.TotalSize()
for i := 0; i < total; i++ {
n, ok := noteAt(i)
if !ok {
continue
}
state := ufmt.Sprintf("expires at %d", n.Expires)
if n.Expires <= height {
state = "**reapable**"
}
rows = append(rows, ufmt.Sprintf("`#%d` %s | %d bytes by %s, %s",
i, summarize(n.Body), len(n.Body), ui.Addr(n.Author), state))
}
b.WriteString(md.BulletList(rows))
b.WriteString("\n")
}
b.WriteString(md.HorizontalRule())
b.WriteString("\nThe arithmetic is [p/moul/x/storagecost](/p/moul/x/storagecost/v0); the storage is [p/moul/ulist](/p/moul/ulist/v1), whose `Compact` reclaims dead nodes without moving a live index. Byte figures on this page are estimates from payload length: no stdlib call exposes a realm's real locked storage.\n")
return b.String()
}
func verdictWord(q storagecost.Quote) string {
if q.Worth() {
return "worth " + storagecost.FormatGNOT(q.Net)
}
return "not worth the gas yet"
}
// summarize is the board's one-line view of a note.
//
// A body is whatever bytes a poster paid to store, so on the board it is
// attacker-controlled input in an inline markdown slot. ui.Excerpt owns both
// halves of that: the rune-boundary cut and the escaping, in the order that is
// safe. This realm shipped a hand-rolled version that did neither, and the
// replacement for it is one call.
func summarize(body string) string {
const width = 48
// Cosmetic, not a defense: "|" is the column separator of the row this
// lands in. The escaper deliberately leaves pipes literal, which is right,
// since a lone pipe cannot open a GFM table with no delimiter row.
return ui.Excerpt(strings.ReplaceAll(body, "|", " "), width)
}
reaper_test.gno
package reaper
import (
"strings"
"testing"
"unicode/utf8"
"gno.land/p/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/kit/ui/v0"
"gno.land/p/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/x/storagecost/v0"
"gno.land/p/nt/testutils/v0"
"gno.land/p/nt/uassert/v0"
)
var (
poster = testutils.TestAddress("poster")
// stranger never posts, it only deletes. The whole point of the realm is
// that this is the address the chain pays.
stranger = testutils.TestAddress("stranger")
)
// ttlFuture is the ttl used for a note that must not be reapable yet. It is a
// named constant because clear has to outrun it.
const ttlFuture = 1000
// clear empties the board so a test starts from a known state, whatever ran
// before it.
//
// gno resets the block height for each test function but keeps realm state, so
// a test that left an unexpired note behind cannot be cleaned up by a later
// test skipping heights: the later test's clock starts over. Every test that
// posts a future-dated note therefore drains it before returning, and clear
// asserts an empty board rather than trusting that.
//
// clear cannot reset TotalSize, which is append-addressed for the life of the
// realm, so tests assert on Live and Reapable and never on absolute indices.
func clear(cur realm, t *testing.T) {
t.Helper()
drain(cur)
uassert.Equal(t, 0, Live())
uassert.Equal(t, 0, Reapable())
}
// drain expires everything outstanding and reaps it.
func drain(cur realm) {
testing.SkipHeights(ttlFuture + 1)
testing.SetRealm(testing.NewUserRealm(stranger))
for Reapable() > 0 {
Reap(cross(cur), 1000)
}
Compact(cross(cur))
}
func TestPostLocksAndReapFrees(cur realm, t *testing.T) {
clear(cur, t)
testing.SetRealm(testing.NewUserRealm(poster))
Post(cross(cur), "first", 0)
Post(cross(cur), "second", 0)
uassert.Equal(t, 2, Live())
uassert.Equal(t, 2, Reapable())
// A stranger who posted nothing may reap, and that is the point.
testing.SetRealm(testing.NewUserRealm(stranger))
uassert.Equal(t, 2, Reap(cross(cur), 100))
uassert.Equal(t, 0, Live())
uassert.Equal(t, 0, Reapable())
}
func TestReapSkipsUnexpiredAndHonoursLimit(cur realm, t *testing.T) {
clear(cur, t)
testing.SetRealm(testing.NewUserRealm(poster))
Post(cross(cur), "ripe one", 0)
Post(cross(cur), "ripe two", 0)
Post(cross(cur), "not yet", ttlFuture)
uassert.Equal(t, 3, Live())
uassert.Equal(t, 2, Reapable())
// The limit caps the work, so a reaper can size a transaction to its gas.
testing.SetRealm(testing.NewUserRealm(stranger))
uassert.Equal(t, 1, Reap(cross(cur), 1))
uassert.Equal(t, 1, Reapable())
// The unexpired note survives a reap that asks for everything.
uassert.Equal(t, 1, Reap(cross(cur), 100))
uassert.Equal(t, 0, Reapable())
uassert.Equal(t, 1, Live())
// Leave nothing behind: the next test's height starts over and could not
// expire this note.
drain(cur)
}
func TestReapOfNothingIsNotAnError(cur realm, t *testing.T) {
clear(cur, t)
// A bot that races another bot to the same notes must not revert, or it
// loses its gas to an abort instead of merely earning nothing.
testing.SetRealm(testing.NewUserRealm(stranger))
uassert.Equal(t, 0, Reap(cross(cur), 100))
}
func TestCompactFollowsReaping(cur realm, t *testing.T) {
clear(cur, t)
testing.SetRealm(testing.NewUserRealm(poster))
for i := 0; i < 8; i++ {
Post(cross(cur), "note", 0)
}
// Nothing is reclaimable until something has been reaped.
uassert.Equal(t, 0, Compactable())
testing.SetRealm(testing.NewUserRealm(stranger))
uassert.Equal(t, 8, Reap(cross(cur), 100))
uassert.True(t, Compactable() > 0)
freed := Compact(cross(cur))
uassert.True(t, freed > 0)
uassert.Equal(t, 0, Compactable())
// Compacting twice is not an error, it just frees nothing.
uassert.Equal(t, 0, Compact(cross(cur)))
}
func TestPostRejectsBadInput(cur realm, t *testing.T) {
clear(cur, t)
testing.SetRealm(testing.NewUserRealm(poster))
uassert.AbortsWithMessage(t, cur, "reaper: empty note", func() {
Post(cross(cur), "", 0)
})
uassert.AbortsWithMessage(t, cur, "reaper: negative ttl", func() {
Post(cross(cur), "fine", -1)
})
// The cap keeps one call from locking an unbounded deposit.
uassert.AbortsWithMessage(t, cur, "reaper: note too long, 4097 bytes against a 4096 cap", func() {
Post(cross(cur), strings.Repeat("x", maxBody+1), 0)
})
uassert.AbortsWithMessage(t, cur, "reaper: limit must be positive", func() {
Reap(cross(cur), 0)
})
}
func TestBountyPricesWhatIsOnTheTable(cur realm, t *testing.T) {
clear(cur, t)
// An empty board advertises nothing, and must not claim a profit.
empty := Bounty()
uassert.Equal(t, int64(0), empty.Bytes)
uassert.Equal(t, int64(0), empty.Refund)
uassert.False(t, empty.Worth())
testing.SetRealm(testing.NewUserRealm(poster))
body := strings.Repeat("x", 1024)
for i := 0; i < 10; i++ {
Post(cross(cur), body, 0)
}
// Ten 1 KB notes, priced through the same estimate the library documents.
q := Bounty()
uassert.Equal(t, storagecost.EstimateBytes(10*1024), q.Bytes)
uassert.Equal(t, storagecost.Refund(q.Bytes, storagecost.DefaultStoragePrice), q.Refund)
uassert.True(t, q.Worth())
// Once reaped, nothing is on the table.
testing.SetRealm(testing.NewUserRealm(stranger))
Reap(cross(cur), 100)
uassert.Equal(t, int64(0), Bounty().Bytes)
}
func TestRenderShowsTheBountyAndTheReapLink(cur realm, t *testing.T) {
clear(cur, t)
// Empty board: an invitation, and no bounty claimed.
out := Render("")
uassert.True(t, strings.Contains(out, "# Reaper"), out)
uassert.True(t, strings.Contains(out, "Nothing has expired"), out)
uassert.True(t, strings.Contains(out, "Post the first note"), out)
testing.SetRealm(testing.NewUserRealm(poster))
Post(cross(cur), strings.Repeat("y", 1024), 0)
out = Render("")
uassert.True(t, strings.Contains(out, "1 expired notes"), out)
uassert.True(t, strings.Contains(out, "refunds roughly **0.1894 GNOT**"), out)
// The reap link must be a help link for the right function with the right
// argument, or the button on the page does nothing useful. txlink builds
// it relative to the current realm, so there is no path to get wrong.
uassert.True(t, strings.Contains(out, "$help&func=Reap&limit=100"), out)
// The author is named, in the house address format, which is what makes
// "the poster paid" visible without 40 opaque characters per row.
uassert.True(t, strings.Contains(out, ui.Addr(poster)), out)
uassert.True(t, strings.Contains(out, "**reapable**"), out)
// A long note is summarized rather than dumped into the table.
uassert.True(t, strings.Contains(out, ui.Ellipsis), out)
uassert.False(t, strings.Contains(out, strings.Repeat("y", 200)), "body should be truncated")
}
func TestRenderNeverEmitsTwoBlankLines(cur realm, t *testing.T) {
// gno collapses two consecutive blank lines, so output containing them can
// never be pinned by an Example. This test is what lets ExampleRender stay
// meaningful.
clear(cur, t)
testing.SetRealm(testing.NewUserRealm(poster))
Post(cross(cur), "one", 0)
Post(cross(cur), "two", ttlFuture)
testing.SetRealm(testing.NewUserRealm(stranger))
Reap(cross(cur), 1)
for _, path := range []string{"", "anything"} {
out := Render(path)
uassert.False(t, strings.Contains(out, "\n\n\n"), "blank-line run in Render("+path+")")
}
drain(cur)
}
func TestReapWalksNewestFirstSoCompactionHasWork(cur realm, t *testing.T) {
// The ordering is economic, not cosmetic: in the backing list the oldest
// indices are the ancestors of the newest, so a partial reap that took the
// oldest first would leave nothing compactable and strand the tree
// structure, which costs more than the notes do.
clear(cur, t)
testing.SetRealm(testing.NewUserRealm(poster))
for i := 0; i < 16; i++ {
Post(cross(cur), "note", 0)
}
first := notes.TotalSize() - 16
last := notes.TotalSize() - 1
testing.SetRealm(testing.NewUserRealm(stranger))
uassert.Equal(t, 4, Reap(cross(cur), 4))
// The four highest indices went, and the four lowest survived.
uassert.Equal(t, nil, notes.Get(last))
uassert.True(t, notes.Get(first) != nil)
// Which is the whole point: there is something to compact after one batch.
uassert.True(t, Compactable() > 0)
drain(cur)
}
// TestSummarizeNeutralizesAnAttackerAuthoredBody is the regression test for a
// hole this realm shipped with: Render emitted a note body into the board
// after nothing but a ReplaceAll of "\n" and "|", so anyone who paid to post
// could put arbitrary markdown on a page every reader of the realm loads.
//
// The body is the one string here an attacker controls and the board is an
// INLINE slot, so sanitize.InlineText owns it. Each case asserts the dangerous
// SEQUENCE is dead and the readable text survived, rather than pinning the
// exact escaped bytes, which are the sanitizer's business and not this realm's.
func TestSummarizeNeutralizesAnAttackerAuthoredBody(t *testing.T) {
cases := []struct {
name string
body string
gone []string // sequences that must not survive into the page
kept string // the readable text, which must
}{
{
// The payload that matters: a phishing link rendered as prose,
// attributed by the page to the realm rather than to the poster.
name: "inline link",
body: "[Claim your 100 GNOT](https://evil.example/drain)",
gone: []string{"](", "](https"},
kept: "Claim your 100 GNOT",
},
{
// An image is a beacon: it fires on load and hands the attacker
// the IP of everyone who opened the board.
name: "image beacon",
body: "",
gone: []string{" stopped exactly this and nothing
// else. Keep it dead: one newline ends the bullet item, and every
// byte after it is top-level markdown.
name: "break out of the list item",
body: "ok\n# Reaper is deprecated, use /r/evil/reaper",
gone: []string{"\n"},
kept: "Reaper is deprecated",
},
{
// Folding only "\n" left the other three line terminators open.
name: "exotic line terminators",
body: "ok\r\n- fake row\u2028- another\u2029- third",
gone: []string{"\r", "\n", "\u2028", "\u2029"},
kept: "fake row",
},
{
// Invisible reordering: the bytes say one thing, the page shows
// another, and a reader diffing the two sees nothing.
name: "bidi and zero-width",
body: "safe\u202enote\u200b\u202c",
gone: []string{"\u202e", "\u200b", "\u202c"},
kept: "safe",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
out := summarize(tc.body)
for _, bad := range tc.gone {
uassert.False(t, strings.Contains(out, bad),
tc.name+": a dangerous sequence survived into "+out)
}
uassert.True(t, strings.Contains(out, tc.kept),
tc.name+": escaping ate the readable text: "+out)
uassert.True(t, utf8.ValidString(out), tc.name+": invalid UTF-8: "+out)
})
}
}
// TestSummarizeEscapesAnUnbalancedBracket covers the cross-row case: a "[" in
// one note and a "]" in another used to let two cheap notes bracket every row
// between them into one link.
func TestSummarizeEscapesAnUnbalancedBracket(t *testing.T) {
uassert.Equal(t, "\\[", summarize("["))
uassert.Equal(t, "\\]", summarize("]"))
uassert.Equal(t, "\\[", ui.Excerpt("[", 48))
}
// TestSummarizeCutsOnARuneBoundary pins the other half of the old bug: the
// truncation sliced at a byte offset, so a body of multi-byte characters put
// half a character on the page. One emoji in a note was enough.
func TestSummarizeCutsOnARuneBoundary(t *testing.T) {
// The leading "x" puts byte 48, the old cut point, in the middle of a
// 4-byte rune rather than on a boundary.
out := summarize("x" + strings.Repeat("\U0001F33E", 60))
uassert.True(t, utf8.ValidString(out), "cut mid-rune: "+out)
uassert.True(t, strings.HasSuffix(out, ui.Ellipsis), "long body should be elided: "+out)
// A short body comes back whole, with no elision and nothing to escape.
short := summarize("plain note")
uassert.Equal(t, "plain note", short)
}
// TestRenderEscapesTheBoard is the end-to-end half: the sanitizer is only
// worth anything if Render is what calls it, and the board is the one place a
// stranger's bytes reach the page.
func TestRenderEscapesTheBoard(cur realm, t *testing.T) {
clear(cur, t)
testing.SetRealm(testing.NewUserRealm(poster))
Post(cross(cur), "[Claim 100 GNOT](https://evil.example)\n# Official", 0)
out := Render("")
uassert.False(t, strings.Contains(out, "](https://evil.example)"), out)
uassert.False(t, strings.Contains(out, "\n# Official"), out)
// The note stays legible, just inert: the text survives, the chrome does not.
uassert.True(t, strings.Contains(out, "Claim 100 GNOT"), out)
uassert.True(t, utf8.ValidString(out), "Render emitted invalid UTF-8")
testing.SetRealm(testing.NewUserRealm(stranger))
Reap(cross(cur), 100)
Compact(cross(cur))
}