2026-08-15 00:54:05 +02:00
|
|
|
/*
|
|
|
|
|
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.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// Status codes, redirects, response headers, and the sitemap/feed documents —
|
|
|
|
|
// the things only a real request can show. A real 404 is the whole reason the
|
|
|
|
|
// backend exists: a client-side router cannot produce one.
|
|
|
|
|
|
|
|
|
|
import std;
|
http: bodies end at 64 KiB, and the checkout limit counts per IPv6 /48
Two limits that were not limiting.
The listener's default body limit is 512 MiB and nothing set another,
with no cap on connections: one 64 MiB POST took the server from 7 MB
to 72 MB before the handler's own 16 KiB check answered 413. The limit
is now 64 KiB. A request that announces more in Content-Length is
answered 400 from its headers, with none of its body read: a 64 MiB
upload is refused after 786 KB and the server's peak memory does not
move.
That is a limit on the decoded body, and the comment says so. A chunked
request sent straight to the port can pad its chunk framing and still
make one connection hold hundreds of megabytes; the parser that allows
it is Crafter.Network's. Behind Caddy an oversized body is refused by
request_body before it gets here, so this is one more thing that rests
on port 8081 being private.
The per-peer checkout limit was kept per address. Over IPv6 one
subscriber holds a whole prefix, a /56 or a /48, and can submit from a
fresh address every time, so the per-peer cap did nothing and the
global cap was the only limit an IPv6 client met. The key is now the
IPv4 address or the IPv6 /48 (RateLimitKeyFor). An IPv4-mapped address
is keyed as the IPv4 client it is, and something that does not read as
an address is keyed as written, so a peer is never dropped to "no peer"
by its spelling. On a /56 ISP 256 neighbouring households share one
budget of six submissions per ten minutes; that was weighed and taken.
ShouldGuardRequestProvenance pins the key. ShouldServeRoutes drives it
over HTTP: six addresses of one /48 are accepted, a seventh is refused
with 429, the next /48 is not; and a body past 64 KiB is refused with
the server still up afterwards.
Found in the security review of 2026-11-26.
Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com>
2026-11-26 11:05:35 +01:00
|
|
|
import Crafter.Network;
|
2026-09-05 18:38:39 +02:00
|
|
|
import Catcrafts.Shared;
|
2026-08-15 00:54:05 +02:00
|
|
|
import Catcrafts.E2eHarness;
|
|
|
|
|
|
2026-09-05 18:38:39 +02:00
|
|
|
using namespace Catcrafts;
|
2026-08-15 00:54:05 +02:00
|
|
|
using namespace Catcrafts::E2e;
|
|
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
|
TestServer srv(argv[1], 8210);
|
|
|
|
|
|
|
|
|
|
// ── status codes ──────────────────────────────────────────────────
|
2026-09-05 18:38:39 +02:00
|
|
|
for (std::string_view p : { "/", "/about", "/shop", "/shop/donation", "/projects",
|
2026-08-15 00:54:05 +02:00
|
|
|
"/posts", "/demos", "/demos/raytracer", "/financials",
|
|
|
|
|
"/legal/privacy", "/legal/terms", "/legal/imprint",
|
|
|
|
|
"/feed.xml", "/sitemap.xml", "/api/healthz" }) {
|
|
|
|
|
srv.CheckStatus(std::string(p), "200");
|
|
|
|
|
}
|
2026-09-05 18:38:39 +02:00
|
|
|
// Every catalogue entry has a page, whatever its status: a coming-soon or
|
|
|
|
|
// withdrawn listing stays up with its form closed, and only a slug the
|
|
|
|
|
// catalogue never held is a 404.
|
|
|
|
|
for (const Product& pr : Content::Products()) {
|
|
|
|
|
srv.CheckStatus("/shop/" + pr.slug, "200");
|
|
|
|
|
}
|
2026-08-15 00:54:05 +02:00
|
|
|
// Trailing slashes must normalise, not 404 or duplicate the canonical URL.
|
|
|
|
|
srv.CheckStatus("/projects/", "200");
|
|
|
|
|
srv.CheckStatus("/shop/", "200");
|
|
|
|
|
// A real 404, which a client-side router cannot produce — this is the
|
|
|
|
|
// whole reason the backend exists.
|
|
|
|
|
srv.CheckStatus("/nope", "404");
|
|
|
|
|
srv.CheckStatus("/shop/nope", "404");
|
|
|
|
|
srv.CheckStatus("/legal/nope", "404");
|
|
|
|
|
// A slug that cannot be one of ours is rejected before any lookup.
|
|
|
|
|
srv.CheckStatus("/shop/BAD--slug", "404");
|
|
|
|
|
srv.CheckStatus("/demos/nope", "404");
|
|
|
|
|
// A post slug that parsed but names nothing must be a real 404, or every
|
|
|
|
|
// typo and every retired post becomes an indexable empty page.
|
|
|
|
|
srv.CheckStatus("/posts/nope", "404");
|
|
|
|
|
srv.CheckStatus("/posts/BAD--slug", "404");
|
|
|
|
|
// The retired blog URLs are still in the wild; they must redirect, not 404.
|
|
|
|
|
srv.CheckStatus("/blog", "301");
|
|
|
|
|
srv.CheckStatus("/blog/hello-world", "301");
|
|
|
|
|
// /demo was the single-demo URL before there was a list; it must redirect,
|
|
|
|
|
// not 404, because it was linked from the home page.
|
|
|
|
|
srv.CheckStatus("/demo", "301");
|
|
|
|
|
|
|
|
|
|
// ── redirects ─────────────────────────────────────────────────────
|
|
|
|
|
{
|
|
|
|
|
const auto blog = srv.Get("/blog/hello-world");
|
|
|
|
|
const auto it = blog.headers.find("location");
|
|
|
|
|
Check(it != blog.headers.end() && it->second.starts_with("/posts"),
|
|
|
|
|
"/blog/* sends Location: /posts",
|
|
|
|
|
it == blog.headers.end() ? "no location header" : it->second);
|
|
|
|
|
}
|
|
|
|
|
{
|
|
|
|
|
const auto demo = srv.Get("/demo");
|
|
|
|
|
const auto it = demo.headers.find("location");
|
|
|
|
|
Check(it != demo.headers.end() && it->second.starts_with("/demos"),
|
|
|
|
|
"/demo sends Location: /demos",
|
|
|
|
|
it == demo.headers.end() ? "no location header" : it->second);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ── headers ───────────────────────────────────────────────────────
|
|
|
|
|
srv.HeaderHas("/", "x-content-type-options", "nosniff", "nosniff on pages");
|
|
|
|
|
srv.HeaderHas("/", "cache-control", "public", "pages are cacheable");
|
|
|
|
|
srv.HeaderHas("/nope", "x-robots-tag", "noindex", "404 is noindex");
|
|
|
|
|
srv.HeaderHas("/feed.xml", "content-type", "application/atom", "feed content-type");
|
|
|
|
|
srv.HeaderHas("/sitemap.xml", "content-type", "application/xml", "sitemap content-type");
|
|
|
|
|
|
|
|
|
|
// ── sitemap and feed content ──────────────────────────────────────
|
2026-09-05 18:38:39 +02:00
|
|
|
// Product URLs come off the catalogue, not a second list, so every entry
|
|
|
|
|
// is advertised and nothing that is not listed can be.
|
|
|
|
|
for (const Product& pr : Content::Products()) {
|
|
|
|
|
srv.BodyHas("/sitemap.xml", "/shop/" + pr.slug, "sitemap lists " + pr.slug);
|
|
|
|
|
}
|
2026-08-15 00:54:05 +02:00
|
|
|
srv.BodyHas("/sitemap.xml", "/about", "sitemap lists the about page");
|
|
|
|
|
srv.BodyHas("/sitemap.xml", "/legal/privacy", "sitemap lists the privacy page");
|
|
|
|
|
srv.BodyHas("/sitemap.xml", "/demos", "sitemap lists the demos page");
|
|
|
|
|
srv.BodyHas("/sitemap.xml", "/financials", "sitemap lists the financials page");
|
|
|
|
|
srv.BodyLacks("/sitemap.xml", "/blog", "sitemap does not advertise the redirect");
|
|
|
|
|
srv.BodyLacks("/sitemap.xml", "/order", "sitemap does not advertise order pages");
|
|
|
|
|
srv.BodyHas("/feed.xml", "<feed xmlns=\"http://www.w3.org/2005/Atom\">", "feed is Atom");
|
|
|
|
|
|
|
|
|
|
// ── abuse ─────────────────────────────────────────────────────────
|
2026-09-05 18:38:39 +02:00
|
|
|
// The body guards run for any listed product before its form is read, so
|
|
|
|
|
// the donation item (the one entry every catalogue has) is the target.
|
|
|
|
|
srv.CheckStatus("/shop/donation", "413", "POST",
|
2026-08-15 00:54:05 +02:00
|
|
|
"email=a%40b.example&name=" + std::string(20000, 'x')
|
|
|
|
|
+ "&street=x&postal=1&city=y&country=NL");
|
http: bodies end at 64 KiB, and the checkout limit counts per IPv6 /48
Two limits that were not limiting.
The listener's default body limit is 512 MiB and nothing set another,
with no cap on connections: one 64 MiB POST took the server from 7 MB
to 72 MB before the handler's own 16 KiB check answered 413. The limit
is now 64 KiB. A request that announces more in Content-Length is
answered 400 from its headers, with none of its body read: a 64 MiB
upload is refused after 786 KB and the server's peak memory does not
move.
That is a limit on the decoded body, and the comment says so. A chunked
request sent straight to the port can pad its chunk framing and still
make one connection hold hundreds of megabytes; the parser that allows
it is Crafter.Network's. Behind Caddy an oversized body is refused by
request_body before it gets here, so this is one more thing that rests
on port 8081 being private.
The per-peer checkout limit was kept per address. Over IPv6 one
subscriber holds a whole prefix, a /56 or a /48, and can submit from a
fresh address every time, so the per-peer cap did nothing and the
global cap was the only limit an IPv6 client met. The key is now the
IPv4 address or the IPv6 /48 (RateLimitKeyFor). An IPv4-mapped address
is keyed as the IPv4 client it is, and something that does not read as
an address is keyed as written, so a peer is never dropped to "no peer"
by its spelling. On a /56 ISP 256 neighbouring households share one
budget of six submissions per ten minutes; that was weighed and taken.
ShouldGuardRequestProvenance pins the key. ShouldServeRoutes drives it
over HTTP: six addresses of one /48 are accepted, a seventh is refused
with 429, the next /48 is not; and a body past 64 KiB is refused with
the server still up afterwards.
Found in the security review of 2026-11-26.
Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com>
2026-11-26 11:05:35 +01:00
|
|
|
// Past 64 KiB the listener refuses the request from its headers, so the
|
|
|
|
|
// body is never buffered and no handler runs. The answer is a 400 and a
|
|
|
|
|
// closed connection; a client still writing its body when the connection
|
|
|
|
|
// closes sees the close instead, and either is the refusal. What must not
|
|
|
|
|
// come back is the handler's own 413 above, which would mean the whole
|
|
|
|
|
// body was read into memory first.
|
|
|
|
|
{
|
|
|
|
|
std::string status = "connection closed";
|
|
|
|
|
try {
|
|
|
|
|
status = srv.Post("/shop/donation", std::string(70000, 'x')).status;
|
|
|
|
|
} catch (...) {
|
|
|
|
|
// Closed mid-write: refused before the body was taken.
|
|
|
|
|
}
|
|
|
|
|
Check(status == "400" || status == "connection closed",
|
|
|
|
|
"a body past 64 KiB is refused before it is read", status);
|
|
|
|
|
// A closed connection is only a refusal if the server is still
|
|
|
|
|
// there: one that had died would close every connection too.
|
|
|
|
|
srv.CheckStatus("/api/healthz", "200");
|
|
|
|
|
}
|
|
|
|
|
// The per-peer checkout budget, over IPv6. A subscriber holds a whole
|
|
|
|
|
// prefix and can submit from a different address every time, so the
|
|
|
|
|
// budget is kept per /48 (Server::RateLimitKeyFor, pinned in
|
|
|
|
|
// ShouldGuardRequestProvenance). What only a request can show is that
|
|
|
|
|
// checkout really counts under that key: six submissions from six
|
|
|
|
|
// addresses of one /48 use the budget up, a seventh address in it is
|
|
|
|
|
// refused, and the next /48 over is not.
|
|
|
|
|
{
|
|
|
|
|
auto submit = [&](std::string from) {
|
|
|
|
|
return srv.Request(Crafter::CreateRequestHTTP(
|
|
|
|
|
"POST", "/shop/donation", "127.0.0.1",
|
|
|
|
|
{{ "content-type", "application/x-www-form-urlencoded" },
|
|
|
|
|
{ "x-forwarded-for", std::move(from) }},
|
|
|
|
|
"amount=5")).status;
|
|
|
|
|
};
|
|
|
|
|
int accepted = 0;
|
|
|
|
|
for (int i = 0; i < 6; ++i) {
|
|
|
|
|
if (submit(std::format("2001:db8:1234:{:x}::{:x}", i, i + 1)) == "303") {
|
|
|
|
|
++accepted;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Check(accepted == 6, "six submissions from one /48 are within its budget",
|
|
|
|
|
std::format("{} of 6 accepted", accepted));
|
|
|
|
|
const std::string seventh = submit("2001:db8:1234:ffff:1c2d:3e4f:5a6b:7c8d");
|
|
|
|
|
Check(seventh == "429",
|
|
|
|
|
"a seventh from a fresh address in the same /48 is refused", seventh);
|
|
|
|
|
const std::string neighbour = submit("2001:db8:1235::1");
|
|
|
|
|
Check(neighbour == "303", "the next /48 has a budget of its own", neighbour);
|
|
|
|
|
}
|
2026-08-15 00:54:05 +02:00
|
|
|
{
|
2026-09-05 18:38:39 +02:00
|
|
|
const auto json = srv.Post("/shop/donation", "{}", "application/json");
|
2026-08-15 00:54:05 +02:00
|
|
|
Check(json.status == "415", "POST with a JSON content-type -> 415", json.status);
|
|
|
|
|
}
|
|
|
|
|
srv.CheckStatus("/shop/nope", "404", "POST",
|
|
|
|
|
"email=a%40b.example&name=Ada&street=x&postal=1&city=y&country=NL");
|
|
|
|
|
srv.CheckStatus("/projects", "405", "POST", "x=1");
|
|
|
|
|
// HEAD must not be a 500 or a body — some crawlers use it exclusively.
|
|
|
|
|
srv.CheckStatus("/", "200", "HEAD");
|
|
|
|
|
{
|
|
|
|
|
const auto head = srv.Head("/");
|
|
|
|
|
Check(head.body.empty(), "HEAD carries no body");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Finish();
|
|
|
|
|
}
|