oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package chatmarkdown
2
3import (
4 "html"
5 "regexp"
6 "strings"
7 "testing"
8)
9
10func TestSafeSubset(t *testing.T) {
11 got := string(labels.Render("**bold** *italic* `inline`\n\n- one\n- two\n\n```go\n<script>\n```\n\n[link](https://example.test)"))
12 for _, want := range []string{"<strong>bold</strong>", "<em>italic</em>", "<code>inline</code>", "<ul>", "<li>", "<pre><code>", "&lt;script&gt;", `rel="noopener"`} {
13 if !strings.Contains(got, want) {
14 t.Errorf("missing %s: %s", want, got)
15 }
16 }
17 for _, raw := range []string{`<script>alert(1)</script>`, `<img src=x onerror=alert(1)>`, `[x](javascript:alert%281%29)`, `[x](data:text/html,evil)`, `[x](//evil.test)`, `[x](jav&#x61;script:evil)`, `![x](https://evil.test/a)`} {
18 got := string(labels.Render(raw))
19 if strings.Contains(got, "<script") || strings.Contains(got, "<img") || strings.Contains(got, `href="javascript:`) || strings.Contains(got, `href="data:`) || strings.Contains(got, `href="//`) {
20 t.Fatal("unsafe", got)
21 }
22 }
23 if !strings.Contains(string(labels.Render(`<b>raw</b>`)), "&lt;b&gt;") {
24 t.Fatal("raw HTML not escaped")
25 }
26}
27
28// The coordinator links a task by its title; on this site only task and
29// project pages become links.
30func TestSiteLinksOnlyTasksAndProjects(t *testing.T) {
31 got := string(labels.Render("I've logged [Swipe back doesn't work](/tasks/tks6kwzzdngr) in [Woodstar](/projects/p_abc123)."))
32 for _, want := range []string{`<a href="/tasks/tks6kwzzdngr" rel="noopener">Swipe back doesn&#39;t work</a>`, `<a href="/projects/p_abc123" rel="noopener">Woodstar</a>`} {
33 if !strings.Contains(got, want) {
34 t.Fatalf("missing %s: %s", want, got)
35 }
36 }
37 for _, raw := range []string{`[x](/conversation/tasks/tk1/retry)`, `[x](/tasks/tk1/../../logout)`, `[x](/settings)`, `[x](tasks/tk1)`, `[x](/tasks/TK1)`, `[x](/tasks/tk1?a=b)`, `[x](/\\evil.test)`} {
38 if got := string(labels.Render(raw)); strings.Contains(got, "<a ") {
39 t.Fatalf("linked %s: %s", raw, got)
40 }
41 }
42}
43
44// Every fenced block is a paste box: its label, a Copy button and the
45// text in a selectable <pre><code>.
46func TestFenceIsAPasteBox(t *testing.T) {
47 got := string(labels.Render("Run this:\n\n```bash\ngo test ./...\n```\n"))
48 want := `<p>Run this:</p>
49<div equipo-paste="code"><div equipo-paste-bar><span equipo-paste-lang>Command</span>` + button("") + `</div><pre><code>go test ./...
50</code></pre></div>
51`
52 if got != want {
53 t.Fatalf("paste box\n got %q\nwant %q", got, want)
54 }
55 // No label: still a box, a code one. A prompt or text label is prose.
56 for _, c := range []struct{ fence, kind, lang string }{{"```", "code", ""}, {"~~~", "code", ""}, {"```prompt", "prose", "Prompt"}, {"```Text", "prose", "Text"}, {"```go linenos", "code", "go"}, {"```c++", "code", "c++"}, {"```.env", "code", ".env"}} {
57 got := string(labels.Render(c.fence + "\nx\n" + c.fence[:3]))
58 if !strings.HasPrefix(got, `<div equipo-paste="`+c.kind+`">`) {
59 t.Fatalf("%s: not a %s box: %s", c.fence, c.kind, got)
60 }
61 if has := strings.Contains(got, "<span equipo-paste-lang>"+c.lang+"</span>"); has != (c.lang != "") || c.lang == "" && strings.Contains(got, "equipo-paste-lang") {
62 t.Fatalf("%s: label: %s", c.fence, got)
63 }
64 }
65 // An indented code block stays a plain one.
66 if got := string(labels.Render(" indented\n")); got != "<pre><code>indented\n</code></pre>\n" {
67 t.Fatalf("indented code: %s", got)
68 }
69}
70
71// A box's label names what it holds: Prompt, Text, Command for a shell
72// (bash, sh, zsh, shell, console), whatever the case of the fence, or
73// the language as written. An unlabelled fence has none. Copy carries
74// its two icons, Copy's and the tick shown once it has copied, beside
75// its word.
76func TestPasteBoxLabels(t *testing.T) {
77 for _, c := range []struct{ fence, label string }{
78 {"prompt", "Prompt"}, {"PROMPT", "Prompt"}, {"text", "Text"}, {"Text", "Text"},
79 {"bash", "Command"}, {"sh", "Command"}, {"zsh", "Command"}, {"shell", "Command"}, {"console", "Command"}, {"Bash", "Command"},
80 {"go", "go"}, {"Python", "Python"}, {"c++", "c++"}, {"txt", "txt"}, {"", ""},
81 } {
82 got := string(labels.Render("```" + c.fence + "\nx\n```"))
83 want := `<div equipo-paste-bar>` + `<span equipo-paste-lang>` + c.label + `</span>` + button("") + `</div>`
84 if c.label == "" {
85 want = `<div equipo-paste-bar>` + button("") + `</div>`
86 }
87 if !strings.Contains(got, want) {
88 t.Fatalf("%q: want %s in\n%s", c.fence, want, got)
89 }
90 if strings.Contains(string(labels.RenderDraft("```"+c.fence+"\nx\n")), want[:len(want)-len(button("")+`</div>`)]) != true {
91 t.Fatalf("%q: the draft labels it otherwise", c.fence)
92 }
93 }
94}
95
96// A label shows only in a safe charset, escaped; anything else shows no
97// label and keeps the box. The button's words are escaped too.
98func TestPasteBoxLabelIsEscaped(t *testing.T) {
99 for _, info := range []string{"<script>", `a"onmouseover=x`, "x'y", "a&amp;b", "&lt;b&gt;", "a<b", "\"x", strings.Repeat("a", 25), "-x", "{.go}", "é"} {
100 got := string(labels.Render("```" + info + "\nbody\n```"))
101 if strings.Contains(got, "equipo-paste-lang") || strings.Contains(got, "<script") || strings.Contains(got, "onmouseover") || !strings.Contains(got, "<pre><code>body\n</code></pre>") {
102 t.Fatalf("label %q: %s", info, got)
103 }
104 }
105 odd := Labels{Copy: `<b>"Copy"</b>`, Copied: `"><x>`, Selected: "a&b", Command: "<i>"}
106 got := string(odd.Render("```sh\nls\n```"))
107 if strings.Contains(got, "<b>") || strings.Contains(got, "<x>") || strings.Contains(got, "<i>") || !strings.Contains(got, `data-copied="&#34;&gt;&lt;x&gt;"`) || !strings.Contains(got, `data-selected="a&amp;b"`) || !strings.Contains(got, `&lt;b&gt;&#34;Copy&#34;&lt;/b&gt;</span></button>`) {
108 t.Fatalf("labels not escaped: %s", got)
109 }
110}
111
112// The box's code is the fence's text exactly: what the page reads as the
113// code's text content (entities resolved) is the raw content, with its
114// <, &, tabs and trailing spaces; Copy drops only the one newline that
115// ends the last line.
116func TestPasteBoxKeepsTheText(t *testing.T) {
117 raw := "if a < b && c > d {\n\tfmt.Println(\"x &amp; y\") \n}\t\n two trailing \n<script>alert(1)</script>"
118 for _, render := range []func(string) string{func(s string) string { return string(labels.Render(s)) }, func(s string) string { return string(labels.RenderDraft(s)) }} {
119 got := render("```go\n" + raw + "\n```\n")
120 m := regexp.MustCompile(`(?s)<pre><code>(.*)</code></pre>`).FindStringSubmatch(got)
121 if m == nil || strings.Contains(m[1], "<script") {
122 t.Fatalf("code: %s", got)
123 }
124 if text := strings.TrimSuffix(html.UnescapeString(m[1]), "\n"); text != raw {
125 t.Fatalf("the box's text is not the fence's\n got %q\nwant %q", text, raw)
126 }
127 }
128}
129
130func TestLinkEntitiesRequireSemicolons(t *testing.T) {
131 for _, raw := range []string{"https://x/?from=1&timestamp=2&region=us", "https://x/?q=&amp;timestamp=2&amp;region=us"} {
132 got := string(labels.Render("[x](" + raw + ")"))
133 if strings.Contains(got, "×") || strings.Contains(got, "®") {
134 t.Fatalf("semicolonless query entity changed: %s", got)
135 }
136 }
137 if got := string(labels.Render("[x](https://x/?a=1&amp;b=2)")); !strings.Contains(got, `href="https://x/?a=1&amp;b=2"`) {
138 t.Fatal(got)
139 }
140}
141
142func TestLinkEntitiesAreDecodedOnceAndOnlyByWholeName(t *testing.T) {
143 for _, c := range []struct{ source, target string }{
144 {"&#38;amp;", "&amp;"}, {"&notarealentity;", "&notarealentity;"}, {"&#0000000000038;", "&"},
145 } {
146 got := string(labels.Render("[x](https://x/?q=" + c.source + ")"))
147 if !strings.Contains(got, `href="https://x/?q=`+html.EscapeString(c.target)+`"`) {
148 t.Fatal(got)
149 }
150 }
151}
152