motorino/internal/build/fixture_test.go
tgiachi 1298a98f30 fix: escape the title attribute, refuse clashing addresses and unsafe paths, check Switch
A quote in a title broke the og:title attribute (a regression from build.py).
Two sources that give the same output address now stop the build and name
both, instead of one replacing the other. Asset names and sources must be
paths inside the site, language codes must look like codes, and the Switch key
is escaped, must point to a path and may name only listed languages.
2026-11-15 10:03:58 +01:00

194 lines
7.9 KiB
Go

// SPDX-License-Identifier: GPL-3.0-or-later
package build
import (
"os"
"path/filepath"
"strings"
"testing"
)
// writeFiles writes the files (path relative to a new directory, then content)
// and returns the directory.
func writeFiles(t *testing.T, files map[string]string) string {
t.Helper()
dir := t.TempDir()
for name, content := range files {
path := filepath.Join(dir, filepath.FromSlash(name))
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
t.Fatal(err)
}
}
return dir
}
// runBuild builds the site made of files into a fresh output folder and
// returns the folder and the build error.
func runBuild(t *testing.T, files map[string]string) (string, error) {
t.Helper()
out := filepath.Join(t.TempDir(), "public")
_, err := Build(Options{SiteDir: writeFiles(t, files), OutDir: out, Version: "1.2.3"})
return out, err
}
// readOut returns the content of a file of the output, failing the test when it
// is missing.
func readOut(t *testing.T, out, rel string) string {
t.Helper()
data, err := os.ReadFile(filepath.Join(out, filepath.FromSlash(rel)))
if err != nil {
t.Fatalf("output file %s: %v", rel, err)
}
return string(data)
}
// mustContain fails the test for each want that is not in got.
func mustContain(t *testing.T, name, got string, wants ...string) {
t.Helper()
for _, want := range wants {
if !strings.Contains(got, want) {
t.Errorf("%s does not contain %q\n%s", name, want, got)
}
}
}
// page returns a page file with the given ID, title, extra header lines and body.
func page(id, title, extra, body string) string {
return "Title: " + title + "\nID: " + id + "\nDescription: About " + title + ".\n" + extra + "-\n" + body
}
// baseFiles is a small complete site: English and Italian with every page, a
// Spanish manifesto, three news with categories, templates and one asset.
func baseFiles() map[string]string {
return map[string]string{
"site.json": `{
"base_url": "https://kmalloc.org",
"languages": ["en", "it", "es"],
"contact": "squid@kmalloc.org",
"mastodon_url": "https://social.kmalloc.org/@squid",
"motorino_url": "https://git.kmalloc.org/squid/motorino",
"manifesto_version": "1.2",
"manifesto_date": "2026-11-14",
"issues_url": "https://git.kmalloc.org/squid/kmalloc.org/issues",
"assets": {"logo.svg": "static/logo.svg"}
}`,
"lang/en.json": `{"lang_name": "English", "sections": "Sections", "home_link": "/", "nojs": "No JavaScript.",
"powered_by": "Powered by", "back": "kmalloc.org", "version": "Version", "toc": "Contents", "history_a": "Manifesto history",
"news_p": "What is new.", "news_all": "All news", "news_empty": "No news yet.", "category_h": "Category",
"nav": {"manifesto": "Manifesto", "people": "People", "services": "Services", "news": "News"}}`,
"lang/it.json": `{"lang_name": "Italiano", "sections": "Sezioni", "home_link": "/it/", "nojs": "Niente JavaScript.",
"powered_by": "Powered by", "back": "kmalloc.org", "version": "Versione", "toc": "Indice", "history_a": "Cronologia",
"news_p": "Le novita.", "news_all": "Tutte le news", "news_empty": "Nessuna news.", "category_h": "Categoria",
"nav": {"manifesto": "Manifesto", "people": "Persone", "services": "Servizi", "news": "News"}}`,
"lang/es.json": `{"lang_name": "Espanol", "header": "header-min", "sections": "Secciones", "home_link": "/?lang=en",
"nojs": "Sin JavaScript.", "powered_by": "Powered by", "back": "kmalloc.org", "version": "Version", "toc": "Indice",
"history_a": "Historial", "translation_note": "Traduccion automatica.", "translation_note_a": "avisar",
"news_p": "x", "news_all": "x", "news_empty": "x", "category_h": "x",
"nav": {"manifesto": "Manifiesto", "news": "News"}}`,
"content/en/index.md": page("home", "kmalloc.org", "Layout: bare\n", "<h1 class=\"hero\">Technology.</h1>\n\n{{latest_news}}\n"),
"content/en/manifesto.md": page("manifesto", "Manifesto", "Layout: manifesto\n", "## 01 — Antifascist\n\nText one.\n\n## Our commitments\n\nText two.\n"),
"content/en/manifesto-history.md": page("history", "Manifesto history", "", "Versions.\n"),
"content/en/people.md": page("people", "People", "", "## squid\n\nI run it.\n"),
"content/en/services.md": page("services", "Services", "Switch: it=/it/servizi.html\n", "Write to **{{contact_shown}}**.\n"),
"content/en/news/index.md": page("news", "News", "", "{{news_list}}\n\n{{categories_list}}\n"),
"content/it/index.md": page("home", "kmalloc.org", "Layout: bare\n", "{{latest_news}}\n"),
"content/it/manifesto.md": page("manifesto", "Manifesto", "Layout: manifesto\n", "## 01 — Antifascisti\n\nTesto.\n"),
"content/it/cronologia-manifesto.md": page("history", "Cronologia", "", "Versioni.\n"),
"content/it/persone.md": page("people", "Persone", "", "Io.\n"),
"content/it/servizi.md": page("services", "Servizi", "", "Scrivi a {{contact_shown}}.\n"),
"content/es/manifesto.md": page("manifesto", "Manifiesto", "Layout: manifesto\nNote: Traduccion de la version 1.0.\nManifestoVersion: 1.0\nManifestoDate: 2026-11-12\n", "## 01 — Antifascistas\n\nTexto.\n"),
"news/gnu-linux-day-2026.md": "Title: GNU/Linux Day 2026\nAuthor: Matteo Bini\nDate: 2026-09-18T17:00:45+0200\nCategories: Linux Day, FLUG\n-\nThe programme is out. See [people](/people.html).\n",
"news/shop.md": "Title: A shop\nAuthor: squid\nDate: 2026-11-14T12:00:00+0100\nCategories: FLUG\nDescription: About the shop.\n-\nThere is a shop.\n",
"news/status.md": "Title: Status page\nDate: 2026-11-12T12:00:00+0100\n-\nThe status page.\n",
"static/style.css": "body { margin: 0 }\n",
"static/logo.svg": "<svg/>\n",
"templates/base.html": `<!doctype html>
<html lang="{{lang}}">
<head>
<meta charset="utf-8">
<meta name="generator" content="motorino {{motorino_version}}">
<title>{{title_html}}</title>
<meta property="og:title" content="{{title_html}}">
<meta name="description" content="{{description_attr}}">
<link rel="me" href="{{mastodon_url}}">
{{alt_links}}
<style>
{{css}}
</style>
</head>
<body>
{{header}}
{{content}}
{{footer}}
</body>
</html>
`,
"templates/header.html": `<header>
<a href="{{home_link}}"{{cur_home}}>home</a>
<a href="{{url_manifesto}}"{{cur_manifesto}}>{{nav_manifesto}}</a>
<a href="{{url_news}}"{{cur_news}}>{{nav_news}}</a>
{{lang_links}}
</header>
`,
"templates/header-min.html": `<header>
<a href="{{url_manifesto}}"{{cur_manifesto}}>{{nav_manifesto}}</a>
{{lang_links}}
<p class="note">{{translation_note}} <a href="{{issues_url}}">{{translation_note_a}}</a></p>
</header>
`,
"templates/footer.html": `<footer>
{{lead}}
<p>{{nojs}}</p>
<p><a href="{{url_history}}"{{history_attr}}>{{history_a}}</a></p>
<p>{{powered_by}} <a href="{{motorino_url}}">motorino</a></p>
{{badges}}
</footer>
`,
"templates/lead-home.html": "<p>lead home</p>\n",
"templates/lead-back.html": "<p><a href=\"{{home_link}}\">{{back}}</a></p>\n",
"templates/lead-news.html": "<p><a href=\"{{url_news}}\">{{news_all}}</a> <a href=\"{{home_link}}\">{{back}}</a></p>\n",
"templates/badges.html": "<p class=\"badges\">{{feed}} {{doc_q}}</p>\n",
"templates/feed.html": "<a href=\"{{url_news}}feed.xml\">RSS</a>\n",
"templates/latest-news.html": `<h2 id="news">{{nav_news}}</h2>
<ul>
{{news_items}}
</ul>
`,
"templates/pages/page.html": "<h1>{{page_title}}</h1>\n\n{{body}}\n",
"templates/pages/bare.html": "{{body}}\n",
"templates/pages/manifesto.html": `<h1>{{page_title}}</h1>
<p><strong>{{version}} {{manifesto_version}}, <time datetime="{{manifesto_date}}">{{manifesto_date}}</time></strong></p>
{{note_block}}<h2>{{toc}}</h2>
<ul>
{{manifesto_toc}}
</ul>
{{body}}
`,
"templates/post.html": `<h1>{{post_title}}</h1>
<p>
<time datetime="{{date}}">{{date}}</time>{{post_byline}}
</p>
{{post_body}}
`,
"templates/category.html": `<h1>{{category_h}}: {{category_name}}</h1>
{{category_posts}}
`,
}
}