catcrafts.net/tests/ShouldValidateForms/main.cpp

542 lines
32 KiB
C++
Raw Normal View History

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.
*/
// The form layer: urlencoded parsing, the email/country shape checks, and
// checkout validation — the gate every order submission passes through.
import std;
import Catcrafts.Shared;
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() {
using namespace Catcrafts::Form;
// ── urlencoded parsing ────────────────────────────────────────────
auto parse = [](std::string_view b) { return ParseUrlEncoded(b); };
auto f = parse("email=a%40b.example&country=NL");
Check(f.has_value(), "form: basic body parses");
if (f) {
Check(f->Get("email") == "a@b.example", "form: %40 decodes to @");
Check(f->Get("country") == "NL", "form: second field");
Check(f->Get("missing").empty(), "form: absent field is empty");
Check(!f->Has("missing"), "form: Has() distinguishes absent");
}
Check(parse("a=1&&b=2")->Size() == 2, "form: empty segment tolerated");
Check(parse("a=1&")->Size() == 1, "form: trailing & tolerated");
Check(parse("flag")->Has("flag"), "form: valueless key present");
Check(parse("")->Size() == 0, "form: empty body");
Check(parse("q=hello+world")->Get("q") == "hello world", "form: + is space");
Check(parse("q=a%2Bb")->Get("q") == "a+b", "form: %2B is a literal plus");
Check(parse("n=caf%C3%A9")->Get("n") == "café", "form: utf-8 percent-decoding");
Check(parse("n=100%")->Get("n") == "100%", "form: malformed escape passes through");
Check(parse("n=%zz")->Get("n") == "%zz", "form: non-hex escape passes through");
// A repeated field keeps BOTH pairs and Get answers with the first. Most
// urlencoded parsers in the wild take the last, so this is pinned rather
// than left to the header comment: every refusal in ValidateCheckout reads
// its field through Get, and flipping this to "last wins" would silently
// hand a second `country=` the final say over where a parcel may go.
{
auto dup = parse("country=NL&country=RU");
Check(dup->Size() == 2, "form: a repeated field keeps both pairs");
Check(dup->Get("country") == "NL", "form: duplicates resolve to the first");
}
// Field NAMES are percent-decoded, not only values — %73 is 's', so
// `web%73ite` is the field `website`. The honeypot below is found by its
// decoded name and nothing else, so this is what makes the trap closed
// against a bot that encodes the key it is trying to avoid.
{
auto encodedName = parse("web%73ite=spam");
Check(encodedName->Has("website"), "form: a percent-encoded field name decodes");
Check(encodedName->Get("website") == "spam",
"form: the value still attaches to the decoded name");
}
2026-08-15 00:54:05 +02:00
// A field name is not allowed to be empty — "=x" is malformed, not a field.
Check(!parse("=x").has_value(), "form: empty field name rejected");
// Oversized input must be refused outright rather than truncated: acting on
// half a form is worse than refusing it.
Check(!parse(std::string(kMaxBodyBytes + 1, 'a')).has_value(), "form: oversized body rejected");
Check(!parse("a=" + std::string(kMaxFieldBytes + 1, 'x')).has_value(), "form: oversized field rejected");
// ── email shape ───────────────────────────────────────────────────
Check(LooksLikeEmail("a@b.example"), "email: minimal");
Check(LooksLikeEmail("first.last+tag@sub.domain.example"), "email: tagged, subdomain");
Check(!LooksLikeEmail("no-at-sign"), "email: no @");
Check(!LooksLikeEmail("@domain.example"), "email: empty local part");
Check(!LooksLikeEmail("user@"), "email: empty domain");
Check(!LooksLikeEmail("a@b@c.example"), "email: two @");
Check(!LooksLikeEmail("user@dotless"), "email: dotless domain");
Check(!LooksLikeEmail("user@.example"), "email: domain starts with dot");
Check(!LooksLikeEmail("a b@c.example"), "email: embedded space");
// Header-injection characters must never survive into anything that later
// builds an email envelope.
Check(!LooksLikeEmail("a@b.example\nBcc: x@y.example"), "email: newline rejected");
Check(!LooksLikeEmail("a@b.example\r\nSubject: x"), "email: CRLF rejected");
Check(!LooksLikeEmail("a,b@c.example"), "email: comma rejected");
Check(!LooksLikeEmail("<a@b.example>"), "email: angle brackets rejected");
Check(!LooksLikeEmail(std::string(250, 'a') + "@b.example"), "email: over 254 chars rejected");
// ── country code ──────────────────────────────────────────────────
Check(LooksLikeCountryCode("NL"), "country: uppercase");
Check(LooksLikeCountryCode("ca"), "country: lowercase accepted");
Check(!LooksLikeCountryCode("NLD"), "country: three letters rejected");
Check(!LooksLikeCountryCode("N"), "country: one letter rejected");
Check(!LooksLikeCountryCode("N1"), "country: digit rejected");
Check(!LooksLikeCountryCode(""), "country: empty rejected");
Check(Upper("nl") == "NL", "country: normalised to upper");
// ── trimming ──────────────────────────────────────────────────────
Check(Trim(" x ") == "x", "trim: spaces");
Check(Trim("\t\r\nx\n") == "x", "trim: tabs and newlines");
Check(Trim(" ").empty(), "trim: all whitespace");
// ── checkout validation ───────────────────────────────────────────
constexpr std::string_view kGoodOrder =
"email=a%40b.example&name=Ada&street=Main%20St%201&postal=1234AB&city=Delft&country=nl";
auto validate = [](std::string_view body) {
return ValidateCheckout(*ParseUrlEncoded(body));
};
auto good = validate(kGoodOrder);
Check(good.Ok(), "checkout: valid submission accepted");
Check(good.value.country == "NL", "checkout: country uppercased");
Check(good.value.street == "Main St 1", "checkout: street decoded and kept");
Check(!validate("name=Ada&street=x&postal=1&city=y&country=NL").Ok(),
"checkout: missing email rejected");
Check(!validate("email=a%40b.example&street=x&postal=1&city=y&country=NL").Ok(),
"checkout: missing name rejected");
Check(!validate("email=a%40b.example&name=Ada&postal=1&city=y&country=NL").Ok(),
"checkout: missing street rejected");
Check(!validate("email=a%40b.example&name=Ada&street=x&city=y&country=NL").Ok(),
"checkout: missing postal rejected");
Check(!validate("email=a%40b.example&name=Ada&street=x&postal=1&country=NL").Ok(),
"checkout: missing city rejected");
Check(!validate("email=a%40b.example&name=Ada&street=x&postal=1&city=y").Ok(),
"checkout: missing country rejected");
Check(!validate("email=nonsense&name=Ada&street=x&postal=1&city=y&country=NL").Ok(),
"checkout: bad email rejected");
// Every problem is reported at once — a form that surfaces one error per
// submission makes people resubmit to discover the rest.
Check(validate("email=&name=&street=&postal=&city=&country=").errors.size() == 6,
"checkout: errors accumulate");
// Honeypot: a filled hidden field means a bot. The message must not name
// the trap, or it teaches the next one how to pass.
auto pot = validate(std::string(kGoodOrder) + "&website=http%3A%2F%2Fspam");
Check(!pot.Ok(), "checkout: honeypot rejects");
Check(pot.errors.size() == 1 && pot.errors[0].message.find("honeypot") == std::string::npos
&& pot.errors[0].message.find("website") == std::string::npos,
"checkout: honeypot failure does not name the trap");
// The same trap with the trigger field's NAME percent-encoded, which is the
// obvious way to try to slip past it. It still fails closed only because
// ParseUrlEncoded decodes the key before Get looks it up, and the refusal
// has to stay identical — a different answer for the encoded spelling would
// itself tell a bot which spelling worked.
{
auto sneaky = validate(std::string(kGoodOrder) + "&web%73ite=http%3A%2F%2Fspam");
Check(!sneaky.Ok(), "checkout: honeypot catches a percent-encoded field name");
Check(sneaky.errors.size() == 1 && sneaky.errors[0].field.empty()
&& sneaky.errors[0].message == "Submission rejected.",
"checkout: the encoded-name trap gives the same single generic refusal");
}
2026-08-15 00:54:05 +02:00
Check(!validate("email=a%40b.example&name=" + std::string(200, 'x')
+ "&street=x&postal=1&city=y&country=NL").Ok(),
"checkout: overlong name rejected");
checkout: an address field is one line, and the invoice prints it as text The invoice is clearsigned, and its "Billed and shipped to" block is the buyer's own words: name, street, postal code and city went into it as typed. A name with a line break in it could write lines of its own into a document the shop signs. One with no line break could still open a heading ("# CREDIT NOTE") or, together with the street and the city, a table, because each of those fields starts a line. Checkout now refuses those four fields when they hold a line break or a control character: the C0 and C1 controls, U+2028 and U+2029, and the bidirectional embedding, override and isolate controls, which make a line display in another order than it is stored. A tab is the exception. A single-line input drops line breaks from a paste and keeps tabs, so "Hoofdstraat<TAB>12" is a real buyer's address, and it is stored with the space the tab stood for. Form::SingleLine is the one definition of what counts. The invoice builder applies the same function to whatever the record holds, since a record can be older than the check or written by hand, and prints the block indented four spaces. That is Markdown's literal block: nothing in it is read as markup, and it reads the same as plain text. The item in the amounts table is held to its cell too, because a colour the catalogue no longer lists is printed as the ledger has it, and checkout no longer stores a posted colour for a product that has no variants. ShouldBuildInvoices checks the property on the document (every line of the block is blank or indented) rather than on particular strings, and ShouldValidateForms pins the edges of each character range. Found in the security review of 2026-11-26; the Markdown half by the adversarial review of the first fix. No priced product is listed, so none of it was reachable yet. Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com>
2026-11-26 11:05:35 +01:00
// One line each. The address fields are printed one to a line on the
// invoice the shop signs, so a line break inside one is a way to write
// lines of one's own into that document: a heading, a row of the amounts
// table, a sentence about a refund. A browser's single-line input cannot
// send one; %0A in a hand-made post can.
{
auto broken = validate("email=a%40b.example"
"&name=Ada%0A%0A%23%23%20Amounts%0A%0ARefund%20due"
"&street=x&postal=1&city=y&country=NL");
Check(!broken.Ok(), "checkout: a line break inside the name is rejected");
Check(broken.errors.size() == 1 && broken.errors[0].field == "name",
"checkout: and the refusal hangs off the name field");
}
Check(!validate("email=a%40b.example&name=Ada&street=Main%20St%201%0D%0AMore"
"&postal=1&city=y&country=NL").Ok(),
"checkout: CRLF inside the street is rejected");
// A tab is the one of these a real buyer sends: a single-line input drops
// the line breaks from a paste and keeps the tabs, and an address copied
// out of two spreadsheet cells arrives with one in the middle. It is
// whitespace someone meant as a space, so it is stored as one. Refusing
// it told a buyer who pasted their own address that it "must be a single
// line", which it was.
{
auto pasted = validate("email=a%40b.example&name=Ada&street=Hoofdstraat%0912"
"&postal=1234%09AB&city=y&country=NL");
Check(pasted.Ok(), "checkout: a pasted tab does not refuse the address");
Check(pasted.value.street == "Hoofdstraat 12",
"checkout: a tab inside a field is stored as the space it stood for",
pasted.value.street);
Check(pasted.value.postal == "1234 AB",
"checkout: in every address field", pasted.value.postal);
}
// What a refusal says. It names the cause, because the buyer who meets it
// pasted something they cannot see.
{
auto broken = validate("email=a%40b.example&name=Ada%0AMore&street=x&postal=1"
"&city=y&country=NL");
Check(broken.errors.size() == 1
&& broken.errors[0].message.find("Line breaks") != std::string::npos,
"checkout: the refusal says line breaks are the problem",
broken.errors.empty() ? "no error" : broken.errors[0].message);
}
// Two more kinds of character that are not text on a line. U+009B is CSI,
// which starts an escape sequence in a terminal showing the ledger or the
// invoice. U+202E turns the rest of the line around on screen, so that a
// line reads one thing and holds another.
Check(!validate("email=a%40b.example&name=Ada%C2%9B31mRed&street=x&postal=1"
"&city=y&country=NL").Ok(),
"checkout: a C1 control character inside the name is rejected");
Check(!validate("email=a%40b.example&name=Ada&street=x&postal=1"
"&city=y%E2%80%AEdiap&country=NL").Ok(),
"checkout: a direction override inside the city is rejected");
// U+2028 and U+0085: line breaks to an editor, and not control characters
// to a check that only looks below 0x20.
Check(!validate("email=a%40b.example&name=Ada&street=x&postal=1"
"&city=Delft%E2%80%A8More&country=NL").Ok(),
"checkout: a Unicode line separator inside the city is rejected");
Check(!validate("email=a%40b.example&name=Ada%C2%85More&street=x&postal=1"
"&city=y&country=NL").Ok(),
"checkout: a Unicode next-line inside the name is rejected");
// What must NOT be caught: a line break at the very end is trimmed like
// any other stray whitespace, and a name outside ASCII is a name. Zoë and
// Łukasz are here for their bytes — C3 AB and C5 81 sit right next to the
// C2 85 the rule looks for.
Check(validate("email=a%40b.example&name=Ada%0D%0A&street=x&postal=1"
"&city=y&country=NL").Ok(),
"checkout: a trailing line break is trimmed, not refused");
Check(validate("email=a%40b.example&name=Zo%C3%AB%20%C5%81ukasz&street=x"
"&postal=1&city=%C2%A3%E2%80%99s-Hertogenbosch&country=NL").Ok(),
"checkout: names and cities outside ASCII are still accepted");
Check(SingleLine("a\nb\r\nc\td") == "a b c d",
"single line: each control character becomes one space");
Check(SingleLine("x\xC2\x85y\xE2\x80\xA8z\xE2\x80\xA9!") == "x y z !",
"single line: NEL, LS and PS each become one space");
Check(SingleLine("Zo\xC3\xAB \xC2\xA3 \xE2\x80\x99s") == "Zo\xC3\xAB \xC2\xA3 \xE2\x80\x99s",
"single line: other multi-byte characters pass through untouched");
// The edges of each range, so a neighbour is not swept up with it:
// U+007F..U+00A0 around the C1 block, U+2027..U+202F around the line
// breaks and overrides, U+2065..U+206A around the isolates.
Check(SingleLine("a\xC2\x80" "b\xC2\x9F" "c") == "a b c",
"single line: the first and last C1 controls become spaces");
Check(SingleLine("a\xC2\xA0" "b") == "a\xC2\xA0" "b",
"single line: a no-break space, one past the C1 block, is text");
Check(SingleLine("a\xE2\x80\xAA" "b\xE2\x80\xAE" "c\xE2\x81\xA6" "d\xE2\x81\xA9" "e")
== "a b c d e",
"single line: the direction controls become spaces");
Check(SingleLine("a\xE2\x80\xA7" "b\xE2\x80\xAF" "c\xE2\x81\xA5" "d\xE2\x81\xAA" "e")
== "a\xE2\x80\xA7" "b\xE2\x80\xAF" "c\xE2\x81\xA5" "d\xE2\x81\xAA" "e",
"single line: their neighbours on either side are left alone");
// Arabic and Hebrew text needs none of the explicit controls: its
// direction comes from the letters. A name in either must pass whole.
Check(SingleLine("\xD7\xA9\xD7\x9C\xD7\x95\xD7\x9D \xD9\x85\xD8\xB1\xD8\xAD\xD8\xA8\xD8\xA7")
== "\xD7\xA9\xD7\x9C\xD7\x95\xD7\x9D \xD9\x85\xD8\xB1\xD8\xAD\xD8\xA8\xD8\xA7",
"single line: right-to-left text itself is untouched");
2026-08-15 00:54:05 +02:00
// Colour and quantity: shape checks here, catalogue checks in the handler.
Check(validate(std::string(kGoodOrder) + "&color=green&quantity=2").Ok(),
"checkout: colour and quantity accepted");
Check(validate(std::string(kGoodOrder) + "&quantity=2").value.quantity == 2,
"checkout: quantity parsed");
Check(validate(kGoodOrder).value.quantity == 1, "checkout: quantity defaults to 1");
Check(!validate(std::string(kGoodOrder) + "&quantity=0").Ok(),
"checkout: zero quantity rejected");
Check(validate(std::string(kGoodOrder) + "&quantity=9").Ok(),
"checkout: bulk quantity welcome");
Check(validate(std::string(kGoodOrder) + "&quantity=99").Ok(),
"checkout: the technical ceiling itself is fine");
Check(!validate(std::string(kGoodOrder) + "&quantity=100").Ok(),
"checkout: past the technical ceiling rejected");
Check(!validate(std::string(kGoodOrder) + "&quantity=two").Ok(),
"checkout: non-numeric quantity rejected");
// "two" fails at the first character, which is the easy half. The hard half
// is a valid numeric PREFIX: from_chars consumes what it can, reports
// success, and leaves the leftovers to the caller — so the only thing
// standing between "2x" and a two-unit charge is the check that parsing
// reached the end of the field. Quantity multiplies the unit price into
// what the buyer actually pays, so a partial parse is a billing bug.
{
auto trailing = validate(std::string(kGoodOrder) + "&quantity=2x");
Check(!trailing.Ok(), "checkout: a numeric prefix with trailing junk rejected");
Check(trailing.errors.size() == 1 && trailing.errors[0].field == "quantity",
"checkout: the quantity refusal hangs off the quantity field");
// Rejected means rejected, not "keep what we managed to read": the
// parsed 2 must not survive into value, because value is what the
// handler prices if anything upstream ever ignores Ok().
Check(trailing.value.quantity == 1,
"checkout: a rejected quantity resets to 1, not the parsed prefix");
}
// from_chars for an integer stops at 'e' and at '.', so left unchecked each
// of these would be read as a bare 1 rather than refused — and "1e3" is a
// spelling of 1000 that no form control produces.
Check(!validate(std::string(kGoodOrder) + "&quantity=1e3").Ok(),
"checkout: exponent notation rejected rather than partly read");
Check(!validate(std::string(kGoodOrder) + "&quantity=1.5").Ok(),
"checkout: a fractional quantity rejected rather than truncated");
// "-1" parses cleanly all the way to the end, so it survives the prefix
// check and is caught by the range floor instead.
Check(!validate(std::string(kGoodOrder) + "&quantity=-1").Ok(),
"checkout: a negative quantity rejected");
Check(validate(std::string(kGoodOrder) + "&quantity=-1").value.quantity == 1,
"checkout: a negative quantity never reaches the record");
// Trim runs before from_chars, so surrounding whitespace is not junk:
// "%20" decodes to a space and " 2" trims back to "2". A buyer who pastes
// a padded number is not making a hostile submission.
Check(validate(std::string(kGoodOrder) + "&quantity=%202").Ok(),
"checkout: a leading space on the quantity is trimmed, not rejected");
Check(validate(std::string(kGoodOrder) + "&quantity=%202").value.quantity == 2,
"checkout: the trimmed quantity is the one that counts");
2026-08-15 00:54:05 +02:00
Check(!validate(std::string(kGoodOrder) + "&color=" + std::string(40, 'x')).Ok(),
"checkout: oversized colour rejected");
// The payment choice. Absent is a form that offered none (one rail
// configured, or the no-JS fallback page) and the handler resolves it to
// bank — the validator's job is only to refuse a word it does not know
// rather than let it fall through to a default the buyer never picked.
Check(validate(kGoodOrder).value.payChoice.empty(),
"checkout: absent payment choice stays empty");
Check(validate(std::string(kGoodOrder) + "&pay=bank").value.payChoice
== Catcrafts::Form::kPayBank,
"checkout: bank choice parsed");
Check(validate(std::string(kGoodOrder) + "&pay=crypto").value.payChoice
== Catcrafts::Form::kPayCrypto,
"checkout: crypto choice parsed");
{
auto bogus = validate(std::string(kGoodOrder) + "&pay=free");
Check(!bogus.Ok(), "checkout: unknown payment choice rejected");
Check(bogus.errors.size() == 1 && bogus.errors[0].field == "pay",
"checkout: the payment refusal hangs off the payment field");
}
// Destinations the shop refuses. Well-formed, real country codes — the
// refusal is policy, so it has to survive every spelling the form accepts,
// and it must not spill onto other non-EU destinations.
auto withCountry = [&](std::string_view cc) {
return validate("email=a%40b.example&name=Ada&street=x&postal=1&city=y&country="
+ std::string(cc));
};
2026-08-18 11:12:24 +02:00
Check(withCountry("NL").Ok(), "checkout: the home market sells");
Check(withCountry("CH").Ok() && withCountry("AU").Ok()
&& withCountry("HK").Ok() && withCountry("SG").Ok()
&& withCountry("RS").Ok() && withCountry("ME").Ok()
&& withCountry("AL").Ok() && withCountry("XK").Ok()
&& withCountry("GE").Ok(),
"checkout: every cleared destination sells");
Check(!withCountry("JP").Ok() && !withCountry("NZ").Ok(),
"checkout: destinations refuted on verification stay refused");
// The default is the point of an allow-list: uncleared, unknown and
// never-considered codes all refuse without anyone listing them.
2026-08-15 00:54:05 +02:00
Check(!withCountry("US").Ok(), "checkout: US refused");
Check(!withCountry("CA").Ok(), "checkout: CA refused");
Check(!withCountry("us").Ok(), "checkout: lowercase US refused too");
2026-08-18 11:12:24 +02:00
Check(!withCountry("DE").Ok() && !withCountry("GB").Ok()
&& !withCountry("NO").Ok() && !withCountry("TR").Ok(),
"checkout: uncleared destinations refuse");
Check(!withCountry("XX").Ok(),
"checkout: a syntactically valid code nobody has cleared refuses");
2026-08-15 00:54:05 +02:00
{
auto us = withCountry("US");
Check(us.errors.size() == 1 && us.errors[0].field == "country",
"checkout: refusal is a country error, nothing else");
2026-08-18 11:12:24 +02:00
Check(us.errors[0].message == Catcrafts::Form::kRegulatoryMessage,
"checkout: refusal gives the regulatory reason");
2026-08-15 00:54:05 +02:00
Check(us.value.country == "US", "checkout: refused country echoed back");
}
2026-08-18 11:12:24 +02:00
{
// One sentence for every non-sanctions refusal, whatever the underlying
// reason was — the buyer cannot act on the difference.
auto de = withCountry("DE");
auto tr = withCountry("TR");
Check(de.errors.size() == 1 && tr.errors.size() == 1
&& de.errors[0].message == Catcrafts::Form::kRegulatoryMessage
&& tr.errors[0].message == Catcrafts::Form::kRegulatoryMessage,
"checkout: every uncleared destination gets the same sentence");
}
2026-08-15 00:54:05 +02:00
2026-08-15 03:22:25 +02:00
// Sanctioned destinations: same gate, different sentence. The message has
// to name the law rather than shop policy — a buyer told "Catcrafts does
// not sell to Russia" would reasonably email to ask; one told the EU
// forbids it knows nothing can be arranged.
Check(!withCountry("RU").Ok(), "checkout: RU refused");
Check(!withCountry("BY").Ok(), "checkout: BY refused");
Check(!withCountry("KP").Ok(), "checkout: KP refused");
Check(!withCountry("ru").Ok(), "checkout: lowercase RU refused too");
{
auto ru = withCountry("RU");
Check(ru.errors.size() == 1 && ru.errors[0].field == "country",
"checkout: sanctions refusal is a country error, nothing else");
Check(ru.errors[0].message == Catcrafts::Form::kSanctionsMessage,
"checkout: sanctions refusal names the law, not shop policy");
Check(ru.value.country == "RU", "checkout: sanctioned country echoed back");
}
2026-08-18 11:12:24 +02:00
// Sanctions keep their own sentence: a buyer told the generic "email us and
// ask" would waste their time on something no amount of paperwork can lift.
Check(withCountry("RU").errors[0].message != Catcrafts::Form::kRegulatoryMessage,
"checkout: sanctions do not collapse into the regulatory sentence");
// Parameter pollution against the order gate. Every refusal above reads its
// field through Fields::Get, which takes the FIRST of a repeated pair, so
// appending a second value cannot reopen a destination the first one closed
// — kGoodOrder already carries country=nl, and the trailing RU is inert.
// The same property protects the number the buyer is charged for.
{
auto polluted = validate(std::string(kGoodOrder) + "&country=RU");
Check(polluted.Ok(),
"checkout: a trailing second country cannot displace the first");
Check(polluted.value.country == "NL",
"checkout: the first country is the one validated and stored");
Check(validate(std::string(kGoodOrder) + "&quantity=2&quantity=99").value.quantity == 2,
"checkout: a second quantity cannot raise what is charged");
}
2026-08-15 00:54:05 +02:00
// The shipping refusals. These are templates rather than plain strings
// because the buy page fills the same ones client-side, so the substitution
// has to work on both {cc} and {n} — a template that silently kept its
// placeholder would ship "up to {n} per order" to a real buyer.
{
const std::string none = NoShippingMessage("BR");
Check(none.find("BR") != std::string::npos
&& none.find("{cc}") == std::string::npos,
"shipping copy: the uncovered-country message names the country");
const std::string heavy = TooHeavyMessage("JP", 3);
Check(heavy.find("JP") != std::string::npos && heavy.find("3") != std::string::npos
&& heavy.find("{n}") == std::string::npos,
"shipping copy: the too-heavy message names the country and the limit");
const std::string nofit = TooHeavyMessage("JP", 0);
Check(nofit.find("JP") != std::string::npos
&& nofit.find("up to") == std::string::npos,
"shipping copy: with nothing fitting it does not promise a quantity");
Check(FillShipMessage("{cc} {n} {cc}", "NL", 2) == "NL 2 NL",
"shipping copy: every placeholder is filled, not just the first");
2026-08-15 03:22:25 +02:00
// Only the splittable refusal offers a way to order anyway — that
// buyer's order works as several smaller ones. The other two are
// final: what the carrier can't take, the shop doesn't ship, and a
// refusal that invites hand-arranging would promise exactly the
// ad-hoc export the shop decided against.
Check(heavy.find("orders@catcrafts.net") != std::string::npos,
"shipping copy: the splittable refusal names a human to email");
Check(none.find("orders@catcrafts.net") == std::string::npos
&& nofit.find("orders@catcrafts.net") == std::string::npos,
"shipping copy: unshippable refusals are final, no workaround offered");
2026-08-15 00:54:05 +02:00
}
// A rejected field must still come back, or the visitor has to retype the
// one thing they got wrong — the fastest way to lose a submission.
auto rejected = validate("email=notanemail&name=Ada&street=Main%201&postal=1&city=y&country=NLD");
Check(!rejected.Ok(), "checkout: invalid pair rejected");
Check(rejected.value.email == "notanemail", "checkout: invalid email echoed back");
Check(rejected.value.country == "NLD", "checkout: invalid country echoed back as typed");
Check(rejected.value.name == "Ada", "checkout: valid sibling field preserved");
// ── the euro-amount parser ────────────────────────────────────────
// Exact integer parsing for the one amount that ever arrives from a
// client (the donation). Same no-floats rule as every money path.
Check(ParseEuroAmountToMinor("25") == 2500, "amount: whole euros");
Check(ParseEuroAmountToMinor("12.50") == 1250, "amount: euros and cents");
Check(ParseEuroAmountToMinor("12,50") == 1250, "amount: comma decimal mark");
Check(ParseEuroAmountToMinor("2.5") == 250, "amount: one decimal is tenths, not cents");
Check(ParseEuroAmountToMinor("0.01") == 1, "amount: a single cent parses");
Check(ParseEuroAmountToMinor("10000") == 1000000, "amount: the ceiling parses");
Check(!ParseEuroAmountToMinor("").has_value(), "amount: empty rejected");
Check(!ParseEuroAmountToMinor("-5").has_value(), "amount: negative rejected");
Check(!ParseEuroAmountToMinor("1e3").has_value(), "amount: exponent rejected");
Check(!ParseEuroAmountToMinor("1.234").has_value(), "amount: third decimal rejected");
Check(!ParseEuroAmountToMinor("1.2.3").has_value(), "amount: two marks rejected");
Check(!ParseEuroAmountToMinor(".50").has_value(), "amount: bare fraction rejected");
Check(!ParseEuroAmountToMinor("25 EUR").has_value(), "amount: trailing text rejected");
Check(!ParseEuroAmountToMinor("12345678901").has_value(), "amount: oversized rejected");
// ── donation validation ───────────────────────────────────────────
// Its own validator, not checkout with fields waived: nothing ships, so
// no address is even asked for, and email is optional — the order page's
// capability URL is already the receipt.
auto donate = [](std::string_view body) {
return ValidateDonation(*ParseUrlEncoded(body));
};
{
auto ok = donate("amount=25");
Check(ok.Ok(), "donation: an amount alone is a complete submission");
Check(ok.value.amountMinor == 2500, "donation: the amount lands in cents");
Check(ok.value.quantity == 1, "donation: quantity is always one");
Check(ok.value.email.empty(), "donation: no email means no email");
}
Check(donate("amount=12.50&email=a%40b.example").Ok(),
"donation: an email may ride along for the confirmation");
Check(donate("amount=12.50&email=a%40b.example").value.amountMinor == 1250,
"donation: cents survive alongside the email");
Check(!donate("amount=25&email=nonsense").Ok(),
"donation: a present-but-bad email is still refused");
Check(!donate("email=a%40b.example").Ok(), "donation: no amount, no donation");
Check(!donate("amount=nonsense").Ok(), "donation: an unparseable amount is refused");
Check(!donate("amount=0.99").Ok(), "donation: below the €1 floor refused");
Check(donate("amount=1").Ok(), "donation: the €1 floor itself is welcome");
Check(donate("amount=10000").Ok(), "donation: the €10,000 ceiling itself is welcome");
Check(!donate("amount=10000.01").Ok(), "donation: past the ceiling refused");
{
// Rejected means rejected: the out-of-range figure must not survive
// into value, because value is what the handler charges if anything
// upstream ever ignores Ok().
auto big = donate("amount=99999");
Check(big.value.amountMinor == 0,
"donation: a refused amount never reaches the record");
Check(big.errors.size() == 1 && big.errors[0].field == "amount",
"donation: the refusal hangs off the amount field");
}
// The same honeypot as checkout, reported just as namelessly.
{
auto pot2 = donate("amount=25&website=spam");
Check(!pot2.Ok(), "donation: honeypot rejects");
Check(pot2.errors.size() == 1 && pot2.errors[0].field.empty()
&& pot2.errors[0].message == "Submission rejected.",
"donation: honeypot failure does not name the trap");
}
// The payment choice, same rules as checkout.
Check(donate("amount=25&pay=crypto").value.payChoice == Catcrafts::Form::kPayCrypto,
"donation: crypto choice parsed");
Check(!donate("amount=25&pay=free").Ok(), "donation: unknown payment choice rejected");
// First-wins duplicates protect the amount exactly as they protect
// checkout's quantity: a trailing second value cannot raise the charge.
Check(donate("amount=25&amount=9999").value.amountMinor == 2500,
"donation: a second amount cannot displace the first");
2026-08-15 00:54:05 +02:00
if (failures != 0) {
std::println(std::cerr, "{} check(s) failed", failures);
return 1;
}
return 0;
}