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.

sanitize.gno

1.72 Kb · 68 lines
 1package hearthbounty
 2
 3import "strings"
 4
 5func asciiLowerByte(c byte) byte {
 6	if c >= 'A' && c <= 'Z' {
 7		return c + ('a' - 'A')
 8	}
 9	return c
10}
11
12func hasASCIIPrefixFold(s string, pos int, pattern string) bool {
13	if pos+len(pattern) > len(s) {
14		return false
15	}
16	for j := 0; j < len(pattern); j++ {
17		if asciiLowerByte(s[pos+j]) != pattern[j] {
18			return false
19		}
20	}
21	return true
22}
23
24// NeutralizeGnoTags escapes any <gno- or </gno- tag sequences using direct
25// byte-indexed ASCII case folding on the original string. Avoiding strings.ToLower
26// prevents multi-byte Unicode case expansions (such as U+0130 'İ' -> "i\u0307")
27// from shifting slice indices and letting raw <gno-*> tags pass through.
28func NeutralizeGnoTags(s string) string {
29	var b strings.Builder
30	changed := false
31	for i := 0; i < len(s); i++ {
32		if s[i] == '<' && (hasASCIIPrefixFold(s, i, "<gno-") || hasASCIIPrefixFold(s, i, "</gno-")) {
33			if !changed {
34				b.Grow(len(s) + 8)
35				b.WriteString(s[:i])
36				changed = true
37			}
38			b.WriteString("&lt;")
39			continue
40		}
41		if changed {
42			b.WriteByte(s[i])
43		}
44	}
45	if !changed {
46		return s
47	}
48	return b.String()
49}
50
51// ValidateProofURL enforces HTTPS-only deliverable links with no whitespace so
52// rendered proof links always survive p/nt/markdown/sanitize/v0 intact.
53func ValidateProofURL(raw string) string {
54	trimmed := strings.TrimSpace(raw)
55	if len(trimmed) < 12 {
56		panic("hearthbounty: proof URL is too short")
57	}
58	if len(trimmed) > 300 {
59		panic("hearthbounty: proof URL exceeds maximum length")
60	}
61	if !strings.HasPrefix(trimmed, "https://") {
62		panic("hearthbounty: proof URL must start with https://")
63	}
64	if strings.ContainsAny(trimmed, " \t\r\n") {
65		panic("hearthbounty: proof URL must not contain whitespace")
66	}
67	return trimmed
68}