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.
This commit is contained in:
parent
0148306a64
commit
1298a98f30
9 changed files with 223 additions and 17 deletions
|
|
@ -5,7 +5,9 @@ package build
|
|||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"git.kmalloc.org/squid/motorino/internal/content"
|
||||
|
|
@ -51,6 +53,8 @@ type builder struct {
|
|||
byKey map[pageKey]content.Page
|
||||
// files holds the rendered output: relative path to content.
|
||||
files map[string]string
|
||||
// owners names, for each output file, what produces it.
|
||||
owners map[string]string
|
||||
// indexable holds the addresses listed in robots.txt.
|
||||
indexable []string
|
||||
// rendered holds the posts with their HTML, for the feed.
|
||||
|
|
@ -87,8 +91,12 @@ func Build(opts Options) (Result, error) {
|
|||
if err := b.renderCategories(); err != nil {
|
||||
return Result{}, err
|
||||
}
|
||||
b.renderFeed()
|
||||
b.files["robots.txt"] = robots(b.indexable)
|
||||
if err := b.renderFeed(); err != nil {
|
||||
return Result{}, err
|
||||
}
|
||||
if err := b.publish("robots.txt", "robots.txt", robots(b.indexable)); err != nil {
|
||||
return Result{}, err
|
||||
}
|
||||
assets, err := b.readAssets()
|
||||
if err != nil {
|
||||
return Result{}, err
|
||||
|
|
@ -107,7 +115,8 @@ func load(opts Options) (*builder, error) {
|
|||
}
|
||||
b := &builder{
|
||||
opts: opts, cfg: cfg, langs: langs, defaultLang: cfg.Languages[0],
|
||||
byKey: map[pageKey]content.Page{}, files: map[string]string{}, langCtx: map[string]map[string]string{},
|
||||
byKey: map[pageKey]content.Page{}, files: map[string]string{}, owners: map[string]string{},
|
||||
langCtx: map[string]map[string]string{},
|
||||
}
|
||||
if b.pages, err = content.LoadPages(opts.SiteDir, cfg.Languages); err != nil {
|
||||
return nil, err
|
||||
|
|
@ -117,6 +126,11 @@ func load(opts Options) (*builder, error) {
|
|||
}
|
||||
for _, p := range b.pages {
|
||||
b.byKey[pageKey{p.ID, p.Lang}] = p
|
||||
for lang := range p.Switch {
|
||||
if _, known := langs[lang]; !known {
|
||||
return nil, fmt.Errorf("%s: Switch names the language %q, which is not in site.json", p.Source, lang)
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, id := range requiredPages {
|
||||
if _, ok := b.byKey[pageKey{id, b.defaultLang}]; !ok {
|
||||
|
|
@ -181,15 +195,45 @@ func (b *builder) template(key string) (string, error) {
|
|||
return t, nil
|
||||
}
|
||||
|
||||
// readAssets reads the files listed in site.json; a missing source is an error.
|
||||
// claim records that source produces the output file rel, and returns an error
|
||||
// naming both sources when another one already does.
|
||||
func (b *builder) claim(rel, source string) error {
|
||||
if other, taken := b.owners[rel]; taken {
|
||||
return fmt.Errorf("%s and %s are both published as %s", other, source, rel)
|
||||
}
|
||||
b.owners[rel] = source
|
||||
return nil
|
||||
}
|
||||
|
||||
// publish stores a rendered file; source names what produced it.
|
||||
func (b *builder) publish(rel, source, text string) error {
|
||||
if err := b.claim(rel, source); err != nil {
|
||||
return err
|
||||
}
|
||||
b.files[rel] = text
|
||||
return nil
|
||||
}
|
||||
|
||||
// readAssets reads the files listed in site.json; a missing source is an
|
||||
// error, and so is an asset that has the address of a page or of another asset.
|
||||
func (b *builder) readAssets() (map[string][]byte, error) {
|
||||
names := make([]string, 0, len(b.cfg.Assets))
|
||||
for name := range b.cfg.Assets {
|
||||
names = append(names, name)
|
||||
}
|
||||
sort.Strings(names)
|
||||
out := map[string][]byte{}
|
||||
for name, source := range b.cfg.Assets {
|
||||
for _, name := range names {
|
||||
source := b.cfg.Assets[name]
|
||||
data, err := os.ReadFile(filepath.Join(b.opts.SiteDir, filepath.FromSlash(source)))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("asset %q: %w", name, err)
|
||||
}
|
||||
out[name] = data
|
||||
rel := path.Clean(name)
|
||||
if err := b.claim(rel, fmt.Sprintf("the asset %q (%s)", name, source)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out[rel] = data
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -273,6 +273,91 @@ func TestBuildManifestoVersionOverride(t *testing.T) {
|
|||
mustContain(t, "en manifesto", readOut(t, out, "manifesto.html"), `Version 1.2, <time datetime="2026-11-14">2026-11-14</time>`)
|
||||
}
|
||||
|
||||
// TestBuildEscapesQuotesInTitle checks that a title with quotes cannot break
|
||||
// out of an attribute such as og:title.
|
||||
func TestBuildEscapesQuotesInTitle(t *testing.T) {
|
||||
files := baseFiles()
|
||||
files["news/quoted.md"] = "Title: A \"shop\" with <mugs>\nDate: 2026-11-15T12:00:00+0100\n-\nText.\n"
|
||||
out, err := runBuild(t, files)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
post := readOut(t, out, "news/2026/11/quoted.html")
|
||||
mustContain(t, "post", post, `content="kmalloc.org: A "shop" with <mugs>"`)
|
||||
if strings.Contains(post, `content="kmalloc.org: A "shop"`) {
|
||||
t.Error("the title broke out of the attribute")
|
||||
}
|
||||
}
|
||||
|
||||
// TestBuildRejectsSameAddress checks that two outputs with one address are an
|
||||
// error naming both sources, instead of one silently replacing the other.
|
||||
func TestBuildRejectsSameAddress(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
extra map[string]string
|
||||
want []string
|
||||
}{
|
||||
{"two pages", map[string]string{"content/en/it/index.md": page("other", "Other", "", "x\n")},
|
||||
[]string{"content/en/it/index.md", "content/it/index.md"}},
|
||||
{"page and category", map[string]string{"content/en/news/category/flug.md": page("flugpage", "Flug", "", "x\n")},
|
||||
[]string{"content/en/news/category/flug.md", "flug"}},
|
||||
{"page and post", map[string]string{"content/en/news/2026/11/shop.md": page("shoppage", "Shop", "", "x\n")},
|
||||
[]string{"content/en/news/2026/11/shop.md", "news/shop.md"}},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
files := baseFiles()
|
||||
for k, v := range tt.extra {
|
||||
files[k] = v
|
||||
}
|
||||
_, err := runBuild(t, files)
|
||||
if err == nil {
|
||||
t.Fatal("Build() error = nil")
|
||||
}
|
||||
for _, want := range tt.want {
|
||||
if !strings.Contains(err.Error(), want) {
|
||||
t.Errorf("error %q does not contain %q", err, want)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestBuildRejectsAssetOverPage checks that an asset cannot take the address of a page.
|
||||
func TestBuildRejectsAssetOverPage(t *testing.T) {
|
||||
files := baseFiles()
|
||||
files["site.json"] = strings.Replace(files["site.json"], `"logo.svg": "static/logo.svg"`, `"index.html": "static/logo.svg"`, 1)
|
||||
_, err := runBuild(t, files)
|
||||
if err == nil || !strings.Contains(err.Error(), "index.html") {
|
||||
t.Errorf("Build() error = %v, want it to name index.html", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestBuildEscapesSwitchHref checks that a language switch cannot inject an attribute.
|
||||
func TestBuildEscapesSwitchHref(t *testing.T) {
|
||||
files := baseFiles()
|
||||
files["content/en/services.md"] = page("services", "Services", "Switch: it=/x\" onmouseover=\"y\n", "Body.\n")
|
||||
out, err := runBuild(t, files)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got := readOut(t, out, "services.html")
|
||||
mustContain(t, "services", got, `href="/x" onmouseover="y"`)
|
||||
if strings.Contains(got, `onmouseover="y"`) {
|
||||
t.Error("the switch value injected an attribute")
|
||||
}
|
||||
}
|
||||
|
||||
// TestBuildRejectsSwitchToUnknownLanguage checks that a typo in Switch is not ignored.
|
||||
func TestBuildRejectsSwitchToUnknownLanguage(t *testing.T) {
|
||||
files := baseFiles()
|
||||
files["content/en/services.md"] = page("services", "Services", "Switch: xx=/x\n", "Body.\n")
|
||||
_, err := runBuild(t, files)
|
||||
if err == nil || !strings.Contains(err.Error(), "content/en/services.md") || !strings.Contains(err.Error(), "xx") {
|
||||
t.Errorf("Build() error = %v, want it to name content/en/services.md and xx", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestBuildAssetsCopied checks the assets listed in site.json.
|
||||
func TestBuildAssetsCopied(t *testing.T) {
|
||||
out, err := runBuild(t, baseFiles())
|
||||
|
|
|
|||
|
|
@ -40,6 +40,8 @@ type document struct {
|
|||
description string
|
||||
// switches overrides the address of the language links, by language code.
|
||||
switches map[string]string
|
||||
// source names what produces the page, for error messages.
|
||||
source string
|
||||
}
|
||||
|
||||
// copyContext returns a copy of ctx.
|
||||
|
|
@ -105,7 +107,7 @@ func (b *builder) documentContext(d document) map[string]string {
|
|||
if d.prefixed {
|
||||
title = "kmalloc.org: " + title
|
||||
}
|
||||
ctx["title_html"] = render.Escape(title)
|
||||
ctx["title_html"] = render.EscapeAttr(title)
|
||||
ctx["page_title"] = render.Escape(d.title)
|
||||
ctx["description_attr"] = render.EscapeAttr(d.description)
|
||||
for _, k := range menuPages {
|
||||
|
|
@ -145,7 +147,7 @@ func (b *builder) languageLinks(d document) (links, alternates string) {
|
|||
if override, ok := d.switches[t.lang]; ok {
|
||||
href = override
|
||||
}
|
||||
anchors[i] = fmt.Sprintf(`<a href="%s" hreflang="%s" lang="%s">%s</a>`, href, t.lang, t.lang, render.Escape(b.langs[t.lang].Strings["lang_name"]))
|
||||
anchors[i] = fmt.Sprintf(`<a href="%s" hreflang="%s" lang="%s">%s</a>`, render.EscapeAttr(href), t.lang, t.lang, render.Escape(b.langs[t.lang].Strings["lang_name"]))
|
||||
}
|
||||
links = strings.Join(anchors, "\n·\n")
|
||||
|
||||
|
|
@ -195,7 +197,9 @@ func (b *builder) frame(d document, ctx map[string]string, body string) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
b.files[outputPath(d.path)] = html
|
||||
if err := b.publish(outputPath(d.path), d.source, html); err != nil {
|
||||
return err
|
||||
}
|
||||
b.indexable = append(b.indexable, d.path)
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -116,6 +116,7 @@ func baseFiles() map[string]string {
|
|||
<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}}
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ func (b *builder) renderPages() error {
|
|||
func (b *builder) renderPage(p content.Page) error {
|
||||
d := document{
|
||||
lang: p.Lang, id: p.ID, lead: "back", path: p.Path, title: p.Title,
|
||||
description: p.Description, switches: p.Switch,
|
||||
description: p.Description, switches: p.Switch, source: p.Source,
|
||||
}
|
||||
if contains(menuPages, p.ID) {
|
||||
d.nav = p.ID
|
||||
|
|
@ -145,7 +145,7 @@ func (b *builder) renderPosts() error {
|
|||
for _, p := range b.posts {
|
||||
d := document{
|
||||
lang: b.defaultLang, nav: "news", lead: "news", path: p.Path, title: p.Title,
|
||||
prefixed: true, description: p.Description,
|
||||
prefixed: true, description: p.Description, source: p.Source,
|
||||
}
|
||||
ctx := b.documentContext(d)
|
||||
html, _, err := b.markdown(p.Source, p.Body, b.defaultLang, ctx)
|
||||
|
|
@ -199,6 +199,7 @@ func (b *builder) renderCategories() error {
|
|||
d := document{
|
||||
lang: b.defaultLang, nav: "news", lead: "news", path: categoryPath(c.Slug), title: c.Name, prefixed: true,
|
||||
description: b.langs[b.defaultLang].Strings["category_h"] + ": " + c.Name,
|
||||
source: fmt.Sprintf("the page of the category %q", c.Name),
|
||||
}
|
||||
ctx := b.documentContext(d)
|
||||
ctx["category_name"] = render.Escape(c.Name)
|
||||
|
|
@ -215,10 +216,10 @@ func (b *builder) renderCategories() error {
|
|||
}
|
||||
|
||||
// renderFeed writes the feed next to the news page.
|
||||
func (b *builder) renderFeed() {
|
||||
func (b *builder) renderFeed() error {
|
||||
newsPath := b.byKey[pageKey{"news", b.defaultLang}].Path
|
||||
text := feed(b.cfg.Strings["base_url"], b.langs[b.defaultLang], newsPath, b.rendered, feedLimit)
|
||||
b.files[outputPath(newsPath+"feed.xml")] = text
|
||||
return b.publish(outputPath(newsPath+"feed.xml"), "the feed", text)
|
||||
}
|
||||
|
||||
// contains reports whether list has s.
|
||||
|
|
|
|||
|
|
@ -164,10 +164,14 @@ func parseSwitch(source, value string) (map[string]string, error) {
|
|||
}
|
||||
for _, entry := range strings.Split(value, ",") {
|
||||
lang, target, ok := strings.Cut(strings.TrimSpace(entry), "=")
|
||||
if !ok || lang == "" || target == "" {
|
||||
lang, target = strings.TrimSpace(lang), strings.TrimSpace(target)
|
||||
if !ok || lang == "" || !strings.HasPrefix(target, "/") {
|
||||
return nil, fmt.Errorf("%s: Switch entry %q is not of the form lang=/path", source, strings.TrimSpace(entry))
|
||||
}
|
||||
out[strings.TrimSpace(lang)] = strings.TrimSpace(target)
|
||||
if _, dup := out[lang]; dup {
|
||||
return nil, fmt.Errorf("%s: Switch lists the language %q twice", source, lang)
|
||||
}
|
||||
out[lang] = target
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -128,6 +128,8 @@ func TestLoadPagesHeaderErrors(t *testing.T) {
|
|||
{"missing id", "Title: T\nDescription: d\n-\nB\n", "ID"},
|
||||
{"bad layout", "Title: T\nID: x\nDescription: d\nLayout: fancy\n-\nB\n", "fancy"},
|
||||
{"bad switch", "Title: T\nID: x\nDescription: d\nSwitch: nonsense\n-\nB\n", "Switch"},
|
||||
{"switch target without a slash", "Title: T\nID: x\nDescription: d\nSwitch: en=elsewhere\n-\nB\n", "Switch"},
|
||||
{"switch language twice", "Title: T\nID: x\nDescription: d\nSwitch: en=/a, en=/b\n-\nB\n", "Switch"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ import (
|
|||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
|
|
@ -49,12 +50,15 @@ func Load(dir string) (Config, map[string]Lang, error) {
|
|||
if err := decodeField(raw, "site.json", "languages", &cfg.Languages); err != nil {
|
||||
return Config{}, nil, err
|
||||
}
|
||||
if len(cfg.Languages) == 0 {
|
||||
return Config{}, nil, fmt.Errorf("site.json: languages is empty")
|
||||
if err := checkLanguages(cfg.Languages); err != nil {
|
||||
return Config{}, nil, err
|
||||
}
|
||||
if err := decodeField(raw, "site.json", "assets", &cfg.Assets); err != nil {
|
||||
return Config{}, nil, err
|
||||
}
|
||||
if err := checkAssets(cfg.Assets); err != nil {
|
||||
return Config{}, nil, err
|
||||
}
|
||||
for _, key := range shownKeys {
|
||||
if value, ok := cfg.Strings[key]; ok {
|
||||
cfg.Strings[key+"_shown"] = strings.ReplaceAll(value, "@", " (at) ")
|
||||
|
|
@ -72,6 +76,40 @@ func Load(dir string) (Config, map[string]Lang, error) {
|
|||
return cfg, langs, nil
|
||||
}
|
||||
|
||||
// languageCode is the shape of a language code: "en", "pt-BR".
|
||||
var languageCode = regexp.MustCompile(`^[a-z]{2,3}(-[A-Za-z0-9]+)?$`)
|
||||
|
||||
// checkLanguages returns an error when the list is empty, has a code of an
|
||||
// unexpected shape (the code becomes part of addresses and folders) or has a
|
||||
// code twice.
|
||||
func checkLanguages(codes []string) error {
|
||||
if len(codes) == 0 {
|
||||
return fmt.Errorf("site.json: languages is empty")
|
||||
}
|
||||
seen := map[string]bool{}
|
||||
for _, code := range codes {
|
||||
if !languageCode.MatchString(code) {
|
||||
return fmt.Errorf("site.json: the language code %q is not like \"en\" or \"pt-BR\"", code)
|
||||
}
|
||||
if seen[code] {
|
||||
return fmt.Errorf("site.json: the language %q is listed twice", code)
|
||||
}
|
||||
seen[code] = true
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// checkAssets returns an error for an asset whose published name or source is
|
||||
// not a path inside the output folder and the site folder.
|
||||
func checkAssets(assets map[string]string) error {
|
||||
for name, source := range assets {
|
||||
if !filepath.IsLocal(name) || !filepath.IsLocal(source) {
|
||||
return fmt.Errorf("site.json: asset %q (%s): the name and the source must be paths inside the site", name, source)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// loadLang reads lang/<code>.json and checks the keys every language needs.
|
||||
func loadLang(dir, code string) (Lang, error) {
|
||||
name := filepath.ToSlash(filepath.Join("lang", code+".json"))
|
||||
|
|
|
|||
|
|
@ -125,6 +125,33 @@ func TestLoadLanguageNeedsNameAndNav(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
// TestLoadRejectsEscapingAssets checks that assets stay inside the site and the output.
|
||||
func TestLoadRejectsEscapingAssets(t *testing.T) {
|
||||
for _, assets := range []string{
|
||||
`{"../escaped.txt": "static/a"}`, `{"/abs.txt": "static/a"}`,
|
||||
`{"a.txt": "../../etc/hostname"}`, `{"a.txt": "/etc/hostname"}`,
|
||||
} {
|
||||
files := validFiles()
|
||||
files["site.json"] = `{"languages": ["en", "it"], "assets": ` + assets + `}`
|
||||
_, _, err := Load(writeSite(t, files))
|
||||
if err == nil || !strings.Contains(err.Error(), "asset") {
|
||||
t.Errorf("Load(%s) error = %v, want an error about the asset", assets, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestLoadRejectsBadLanguageCodes checks the shape of the language codes.
|
||||
func TestLoadRejectsBadLanguageCodes(t *testing.T) {
|
||||
for _, codes := range []string{`["en", "../x"]`, `["EN"]`, `["en", "en"]`, `["en", "it/x"]`} {
|
||||
files := validFiles()
|
||||
files["site.json"] = `{"languages": ` + codes + `}`
|
||||
_, _, err := Load(writeSite(t, files))
|
||||
if err == nil || !strings.Contains(err.Error(), "language") {
|
||||
t.Errorf("Load(%s) error = %v, want an error about the language code", codes, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestLoadNeedsLanguages checks that an empty language list is an error.
|
||||
func TestLoadNeedsLanguages(t *testing.T) {
|
||||
files := validFiles()
|
||||
|
|
|
|||
Loading…
Reference in a new issue