/* catcrafts.net Copyright (C) 2026 Catcrafts The source code of this website is made available for viewing purposes only. No permission is granted to copy, modify, distribute, or create derivative works. */ // catcrafts-server — the native product. // // Serves the server-rendered pages (crawlers and no-JS clients get real HTML), // runs the shop — orders, the bunq payment rail, the reconciler — and doubles // as the test harness for Catcrafts.Shared. // // The harness half is not filler. Catcrafts.Shared is the security boundary // for every piece of markup the site emits, and it is target-neutral precisely // so it can be tested somewhere with a debugger, sanitizers and a normal test // loop instead of only inside a wasm module in a browser tab. `--selftest` // is how the shared code gets executed rather than merely compiled. // // crafter-build -- --product=server && ./bin/Catcrafts.Server-*/catcrafts-server --selftest import std; import Catcrafts.Shared; import Catcrafts.Server; using namespace Catcrafts; namespace { int failures = 0; void Check(bool ok, std::string_view what, std::string_view got = {}) { if (ok) return; ++failures; std::println(std::cerr, "FAIL: {}{}{}", what, got.empty() ? "" : " got: ", got); } void CheckEq(const Html::SafeHtml& actual, std::string_view expected, std::string_view what) { Check(actual.View() == expected, what, actual.View()); } void RunSelfTest() { using namespace Catcrafts::Html; // ── Escape ──────────────────────────────────────────────────────── CheckEq(Escape("plain"), "plain", "escape: passthrough"); CheckEq(Escape("ab"), "a>b", "escape: gt"); CheckEq(Escape("a&b"), "a&b", "escape: amp"); CheckEq(Escape("say \"hi\""), "say "hi"", "escape: dquote"); CheckEq(Escape("it's"), "it's", "escape: squote"); // Ampersand must be escaped first or the other replacements get // double-encoded; a single pass makes that ordering bug impossible. CheckEq(Escape("<"), "&lt;", "escape: no double-encode"); CheckEq(Escape(""), "<script>alert(1)</script>", "escape: script tag"); // Non-ASCII passes through untouched — the output is UTF-8, and // entity-encoding it would just bloat the page. CheckEq(Escape("café ✓ 日本"), "café ✓ 日本", "escape: utf-8 passthrough"); CheckEq(Escape(""), "", "escape: empty"); // ── Num ─────────────────────────────────────────────────────────── CheckEq(Num(0), "0", "num: zero"); CheckEq(Num(-42), "-42", "num: negative"); CheckEq(Num(9007199254740993LL), "9007199254740993", "num: beyond double precision"); // ── Attr ────────────────────────────────────────────────────────── CheckEq(Attr("class", "card"), " class=\"card\"", "attr: basic"); CheckEq(Attr("data-x", "a\"b"), " data-x=\"a"b\"", "attr: value escaped"); CheckEq(Attr("class", ""), "", "attr: empty value omits attribute"); // An invalid name is a programming error, not user data. Emitting // nothing is safer than emitting mangled markup. CheckEq(Attr("on error", "x"), "", "attr: invalid name rejected"); CheckEq(Attr("x>"), " href=\"#\"", "url: data: neutralised"); // Browsers strip control characters before resolving the scheme, so a // naive prefix check would pass this straight through. CheckEq(Url("href", "java\tscript:alert(1)"), " href=\"#\"", "url: embedded tab"); CheckEq(Url("href", " javascript:alert(1)"), " href=\"#\"", "url: leading space"); CheckEq(Url("href", "//evil.example/x"), " href=\"#\"", "url: protocol-relative blocked"); CheckEq(Url("href", "vbscript:x"), " href=\"#\"", "url: vbscript neutralised"); // ── Format ──────────────────────────────────────────────────────── // The compile-time half of this guarantee (raw std::string rejected) is // verified by the build itself — see the negative test in the notes. CheckEq(Format("

{}

", Escape("aa<b", "format: escapes flow through"); CheckEq(Format("{}", Url("href", "/x"), Escape("go")), "go", "format: attr + text"); CheckEq(Format("{}{}", Num(1), Num(2)), "12", "format: multiple args"); CheckEq(Format("literal"), "literal", "format: no args"); CheckEq(Format("{{literal braces}}"), "{literal braces}", "format: brace escaping"); // ── Join / concat ───────────────────────────────────────────────── const std::array parts{ Escape("a"), Escape("b"), Escape("c") }; CheckEq(Join(parts, Raw(", ")), "a, b, c", "join: separator"); CheckEq(Join(std::span{}), "", "join: empty"); CheckEq(Escape("a") + Escape("<"), "a<", "operator+: escapes preserved"); } // The format ladder. One /