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.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// Request provenance: the forwarded-address parser the rate limiter keys on,
|
|
|
|
|
// and the Origin check that gates the order form.
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
|
// The rate limiter keys on this, so getting the WRONG end of the header
|
|
|
|
|
// is not a cosmetic bug: the leftmost entry is client-controlled, and
|
|
|
|
|
// trusting it would hand every attacker an endless supply of identities.
|
|
|
|
|
{
|
|
|
|
|
using Server::ClientAddressFromForwarded;
|
|
|
|
|
Check(ClientAddressFromForwarded("203.0.113.7") == "203.0.113.7",
|
|
|
|
|
"forwarded: single entry");
|
|
|
|
|
Check(ClientAddressFromForwarded("198.51.100.4, 203.0.113.7") == "203.0.113.7",
|
|
|
|
|
"forwarded: rightmost entry wins");
|
|
|
|
|
// The attack this exists to defeat: a client that sends its own header
|
|
|
|
|
// to look like a different peer. Caddy appends the truth on the right.
|
|
|
|
|
Check(ClientAddressFromForwarded("1.1.1.1, 2.2.2.2, 203.0.113.7") == "203.0.113.7",
|
|
|
|
|
"forwarded: spoofed prefix ignored");
|
|
|
|
|
Check(ClientAddressFromForwarded("198.51.100.4, 203.0.113.7") == "203.0.113.7",
|
|
|
|
|
"forwarded: padding trimmed");
|
|
|
|
|
Check(ClientAddressFromForwarded("2001:db8::1") == "2001:db8::1",
|
|
|
|
|
"forwarded: ipv6 passes through");
|
|
|
|
|
Check(ClientAddressFromForwarded("").empty(), "forwarded: empty stays empty");
|
|
|
|
|
// No header at all means nothing proxied this request; the caller must
|
|
|
|
|
// see an empty peer and fall back to the global budget.
|
|
|
|
|
Check(ClientAddressFromForwarded("198.51.100.4, ").empty(),
|
|
|
|
|
"forwarded: empty last entry is no peer");
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
// What the limiter actually counts under. An IPv4 address is one client.
|
|
|
|
|
// An IPv6 address is not: a subscriber is handed a whole prefix (a /56 or
|
|
|
|
|
// a /48) and can submit from a fresh address every time, so a budget kept
|
|
|
|
|
// per address is a budget nobody ever runs out of. The key is the /48,
|
|
|
|
|
// the largest block one subscriber is normally given.
|
|
|
|
|
{
|
|
|
|
|
using Server::RateLimitKeyFor;
|
|
|
|
|
Check(RateLimitKeyFor("203.0.113.7") == "203.0.113.7",
|
|
|
|
|
"key: an IPv4 address is its own key", RateLimitKeyFor("203.0.113.7"));
|
|
|
|
|
Check(RateLimitKeyFor("2001:db8:1234:5678:9abc:def0:1234:5678")
|
|
|
|
|
== "2001:db8:1234::/48",
|
|
|
|
|
"key: an IPv6 address is keyed by its /48",
|
|
|
|
|
RateLimitKeyFor("2001:db8:1234:5678:9abc:def0:1234:5678"));
|
|
|
|
|
|
|
|
|
|
// The property the limiter needs: whatever address a subscriber
|
|
|
|
|
// picks inside their prefix, it is the same peer. A different /64 of
|
|
|
|
|
// the same /56, the far end of the same /48, privacy addresses.
|
|
|
|
|
const std::string home = RateLimitKeyFor("2001:db8:1234:ab00::1");
|
|
|
|
|
Check(RateLimitKeyFor("2001:db8:1234:ab01:1c2d:3e4f:5a6b:7c8d") == home,
|
|
|
|
|
"key: another /64 of the same line is the same peer");
|
|
|
|
|
Check(RateLimitKeyFor("2001:db8:1234:ffff:ffff:ffff:ffff:ffff") == home,
|
|
|
|
|
"key: the far end of the same /48 is the same peer");
|
|
|
|
|
Check(RateLimitKeyFor("2001:db8:1235:ab00::1") != home,
|
|
|
|
|
"key: the next /48 is somebody else");
|
|
|
|
|
Check(RateLimitKeyFor("2001:db9:1234:ab00::1") != home,
|
|
|
|
|
"key: the same third group under another prefix is somebody else");
|
|
|
|
|
|
|
|
|
|
// One address, however it is spelled, is one key: otherwise the
|
|
|
|
|
// spelling would be a way to be several peers.
|
|
|
|
|
Check(RateLimitKeyFor("2001:0DB8:1234:AB00:0000:0000:0000:0001") == home,
|
|
|
|
|
"key: case and leading zeros do not make a new peer");
|
|
|
|
|
Check(RateLimitKeyFor("[2001:db8:1234:ab00::1]") == home,
|
|
|
|
|
"key: brackets do not make a new peer");
|
|
|
|
|
Check(RateLimitKeyFor(" 2001:db8:1234:ab00::1 ") == home,
|
|
|
|
|
"key: padding does not make a new peer");
|
|
|
|
|
Check(RateLimitKeyFor("2001:db8::1") == "2001:db8:0::/48",
|
|
|
|
|
"key: the shorthand is expanded before the prefix is taken",
|
|
|
|
|
RateLimitKeyFor("2001:db8::1"));
|
|
|
|
|
Check(RateLimitKeyFor("2001:db8:1234::") == "2001:db8:1234::/48",
|
|
|
|
|
"key: a trailing shorthand", RateLimitKeyFor("2001:db8:1234::"));
|
|
|
|
|
Check(RateLimitKeyFor("::1") == "0:0:0::/48", "key: loopback",
|
|
|
|
|
RateLimitKeyFor("::1"));
|
|
|
|
|
Check(RateLimitKeyFor("fe80::1%eth0") == "fe80:0:0::/48",
|
|
|
|
|
"key: a zone is not part of the address", RateLimitKeyFor("fe80::1%eth0"));
|
|
|
|
|
|
|
|
|
|
// An IPv4 client reported through an IPv6 socket is still that IPv4
|
|
|
|
|
// client, in either spelling of the mapped form.
|
|
|
|
|
Check(RateLimitKeyFor("::ffff:203.0.113.7") == "203.0.113.7",
|
|
|
|
|
"key: an IPv4-mapped address is the IPv4 client",
|
|
|
|
|
RateLimitKeyFor("::ffff:203.0.113.7"));
|
|
|
|
|
Check(RateLimitKeyFor("::ffff:cb00:7107") == "203.0.113.7",
|
|
|
|
|
"key: and in its all-hex spelling", RateLimitKeyFor("::ffff:cb00:7107"));
|
|
|
|
|
// A dotted tail anywhere else is an ordinary IPv6 address (NAT64).
|
|
|
|
|
Check(RateLimitKeyFor("64:ff9b::203.0.113.7") == "64:ff9b:0::/48",
|
|
|
|
|
"key: a dotted tail outside the mapped range stays IPv6",
|
|
|
|
|
RateLimitKeyFor("64:ff9b::203.0.113.7"));
|
|
|
|
|
|
|
|
|
|
// Not an address at all. It is Caddy's word and should never happen,
|
|
|
|
|
// but a peer must not fall through to "no peer" (the global budget
|
|
|
|
|
// only) because of how it was written: keyed as written instead.
|
|
|
|
|
Check(RateLimitKeyFor("").empty(), "key: no peer stays no peer");
|
|
|
|
|
Check(RateLimitKeyFor("not-an-address") == "not-an-address",
|
|
|
|
|
"key: something unreadable is keyed as written");
|
|
|
|
|
Check(RateLimitKeyFor("2001:db8:::1") == "2001:db8:::1",
|
|
|
|
|
"key: a malformed IPv6 address is keyed as written");
|
|
|
|
|
Check(RateLimitKeyFor("1:2:3:4:5:6:7:8:9") == "1:2:3:4:5:6:7:8:9",
|
|
|
|
|
"key: nine groups is not an address");
|
|
|
|
|
Check(RateLimitKeyFor("203.0.113.999") == "203.0.113.999",
|
|
|
|
|
"key: an out-of-range octet is not an address");
|
|
|
|
|
|
|
|
|
|
// The two together, as checkout uses them.
|
|
|
|
|
Check(RateLimitKeyFor(Server::ClientAddressFromForwarded(
|
|
|
|
|
"198.51.100.4, 2001:db8:1234:42::7")) == "2001:db8:1234::/48",
|
|
|
|
|
"key: the rightmost forwarded entry, reduced to its /48");
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-15 00:54:05 +02:00
|
|
|
{
|
|
|
|
|
using Server::OriginAllowed;
|
|
|
|
|
Check(OriginAllowed("https://catcrafts.net", "https://catcrafts.net"),
|
|
|
|
|
"origin: same origin allowed");
|
|
|
|
|
Check(OriginAllowed("https://catcrafts.net", "https://catcrafts.net/"),
|
|
|
|
|
"origin: trailing slash on the base normalised");
|
|
|
|
|
// A non-browser client (curl, the e2e suite) sends no Origin and
|
|
|
|
|
// cannot be a cross-site forgery — there is no session to ride on.
|
|
|
|
|
Check(OriginAllowed("", "https://catcrafts.net"), "origin: absent allowed");
|
|
|
|
|
Check(!OriginAllowed("https://evil.example", "https://catcrafts.net"),
|
|
|
|
|
"origin: foreign origin refused");
|
|
|
|
|
// Neither a subdomain nor a lookalike is us.
|
|
|
|
|
Check(!OriginAllowed("https://catcrafts.net.evil.example", "https://catcrafts.net"),
|
|
|
|
|
"origin: suffix lookalike refused");
|
|
|
|
|
Check(!OriginAllowed("https://shop.catcrafts.net", "https://catcrafts.net"),
|
|
|
|
|
"origin: subdomain refused");
|
|
|
|
|
// Scheme is part of an origin: http is not https.
|
|
|
|
|
Check(!OriginAllowed("http://catcrafts.net", "https://catcrafts.net"),
|
|
|
|
|
"origin: scheme mismatch refused");
|
|
|
|
|
// A sandboxed iframe posts Origin: null. Present, and not us.
|
|
|
|
|
Check(!OriginAllowed("null", "https://catcrafts.net"), "origin: null refused");
|
|
|
|
|
Check(!OriginAllowed("https://catcrafts.net", ""),
|
|
|
|
|
"origin: unconfigured base refuses rather than accepts all");
|
|
|
|
|
// dev.sh serves on localhost and sets --redirect-base to match.
|
|
|
|
|
Check(OriginAllowed("http://localhost:8080", "http://localhost:8080"),
|
|
|
|
|
"origin: dev localhost base matches");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (failures != 0) {
|
|
|
|
|
std::println(std::cerr, "{} check(s) failed", failures);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|