2026-08-05 04:18:37 +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.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// Invoices: markdown, clearsigned with GPG.
|
|
|
|
|
//
|
|
|
|
|
// Markdown because an invoice's job is to be READ — by the buyer, by an
|
|
|
|
|
// accountant, by a tax office, in thirty years, with any text editor. A
|
|
|
|
|
// clearsigned document keeps the text human-readable with the signature
|
|
|
|
|
// inline (gpg --verify checks it), so authenticity does not depend on this
|
|
|
|
|
// server still existing — which is the point: the buyer downloads the file
|
|
|
|
|
// once and the shop makes no promise to host receipt pages forever.
|
|
|
|
|
//
|
|
|
|
|
// Signing shells out to the gpg binary rather than linking a PGP library:
|
|
|
|
|
// the key management story (GNUPGHOME, agent, key generation) is exactly the
|
|
|
|
|
// part a library reimplements badly, and the server signs a handful of
|
|
|
|
|
// documents per week. The subprocess writes to files under a private
|
|
|
|
|
// directory, never a shell-interpolated user string — the only variable in
|
|
|
|
|
// the command line is the key id, validated to a safe alphabet.
|
|
|
|
|
|
|
|
|
|
module;
|
|
|
|
|
#include <cstdio>
|
|
|
|
|
#include <cstdlib>
|
|
|
|
|
module Catcrafts.Server;
|
|
|
|
|
|
|
|
|
|
import std;
|
|
|
|
|
import Catcrafts.Shared;
|
|
|
|
|
|
|
|
|
|
namespace Catcrafts::Server {
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
std::string gGpgKeyId;
|
|
|
|
|
|
2026-08-09 00:14:09 +02:00
|
|
|
// The registered business identity lives in the module interface
|
|
|
|
|
// (kSeller...) — the order email's footer states the same facts, and two
|
|
|
|
|
// copies of a KVK number is one copy too many.
|
2026-08-05 04:18:37 +02:00
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
std::string BuildInvoiceMarkdown(const OrderRecord& o,
|
|
|
|
|
std::string_view productName,
|
|
|
|
|
std::string_view colorLabel) {
|
|
|
|
|
std::string md;
|
|
|
|
|
md.reserve(2048);
|
|
|
|
|
|
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
|
|
|
// The item goes into a cell of the amounts table. Its two halves come from
|
|
|
|
|
// the catalogue, except that a colour the catalogue no longer lists is
|
|
|
|
|
// passed through as the ledger has it, and that was once whatever was
|
|
|
|
|
// posted. So it is kept to one line and cannot close its cell: a '|' here
|
|
|
|
|
// would otherwise add columns to a row of a signed document.
|
|
|
|
|
std::string item = Form::SingleLine(
|
|
|
|
|
colorLabel.empty() ? std::string(productName)
|
|
|
|
|
: std::format("{} — {}", productName, colorLabel));
|
|
|
|
|
std::ranges::replace(item, '|', '/');
|
2026-08-05 04:18:37 +02:00
|
|
|
|
|
|
|
|
// The number scheme continues the pre-shop administration: the customer
|
|
|
|
|
// number is a UUID series, the invoice number counts within it.
|
|
|
|
|
const std::size_t dash = o.invoiceNumber.size() > 37 ? 36 : std::string::npos;
|
|
|
|
|
const std::string customer = dash != std::string::npos
|
|
|
|
|
? o.invoiceNumber.substr(0, 36) : o.invoiceNumber;
|
|
|
|
|
const std::string seq = dash != std::string::npos
|
|
|
|
|
? o.invoiceNumber.substr(37) : o.invoiceNumber;
|
|
|
|
|
|
|
|
|
|
md += std::format("# Invoice {}\n\n", o.invoiceNumber);
|
|
|
|
|
md += std::format("**{}** \n{} \n{} \nKVK {} · VAT {} · {}\n\n",
|
|
|
|
|
kSellerName, kSellerStreet, kSellerCity,
|
|
|
|
|
kSellerKvk, kSellerVat, kSellerSite);
|
|
|
|
|
// "*" bullets, never "-": clearsigning dash-escapes lines that start
|
|
|
|
|
// with a dash ("- - Invoice date"), and the raw file is meant to be read.
|
|
|
|
|
md += std::format("* Customer number: {}\n", customer);
|
|
|
|
|
md += std::format("* Invoice number: {}\n", seq);
|
|
|
|
|
md += std::format("* Invoice date: {}\n", o.invoicedAt);
|
|
|
|
|
md += std::format("* Order reference: {}\n", o.reference);
|
|
|
|
|
md += std::format("* Order placed: {}\n", o.createdAt);
|
|
|
|
|
if (!o.paidVia.empty()) {
|
|
|
|
|
md += std::format("* Paid via: {}\n", o.paidVia);
|
|
|
|
|
}
|
|
|
|
|
md += "\n## Billed and shipped to\n\n";
|
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
|
|
|
// The one block of this document the buyer wrote, and it is about to be
|
|
|
|
|
// signed, so it is printed as what it is: text, not markup. Two things
|
|
|
|
|
// make that so, and each needs the other.
|
|
|
|
|
//
|
|
|
|
|
// Every field is held to ONE LINE (Form::SingleLine). Checkout refuses a
|
|
|
|
|
// field that is not, but what is signed is decided here, by what is in
|
|
|
|
|
// the record, and a record can be older than that check or written by
|
|
|
|
|
// hand.
|
|
|
|
|
//
|
|
|
|
|
// And every line is indented four spaces, which is Markdown's literal
|
|
|
|
|
// block: nothing in it is read as a heading, a table, a link or HTML.
|
|
|
|
|
// One line alone was not enough. These fields sit at the start of
|
|
|
|
|
// consecutive lines, so a name of "# CREDIT NOTE" was a heading, and a
|
|
|
|
|
// name, a street and a city of "| a | b |", "|---|---|" and "| c | d |"
|
|
|
|
|
// were a table, with not a line break among them (found in review,
|
|
|
|
|
// 2026-11-26). Indented, they are the characters the buyer typed. It
|
|
|
|
|
// also reads right as plain text, which is how this file is mostly read,
|
|
|
|
|
// and a line that starts with spaces is never dash-escaped by the
|
|
|
|
|
// clearsigning.
|
|
|
|
|
md += std::format(" {}\n {}\n {} {}\n {}\n\n",
|
|
|
|
|
Form::SingleLine(o.buyer.name), Form::SingleLine(o.buyer.street),
|
|
|
|
|
Form::SingleLine(o.buyer.postal), Form::SingleLine(o.buyer.city),
|
|
|
|
|
Form::SingleLine(o.buyer.country));
|
2026-08-05 04:18:37 +02:00
|
|
|
|
|
|
|
|
md += "## Amounts\n\n";
|
|
|
|
|
md += "| Description | Qty | Amount |\n|---|---|---|\n";
|
|
|
|
|
if (o.vatIncluded) {
|
2026-08-17 11:04:03 +02:00
|
|
|
// EU supply: VAT once over the taxable total — the same rounding the
|
|
|
|
|
// checkout charged with — so the subtotal and VAT rows are fixed.
|
|
|
|
|
// Only the goods line rounds on its own; shipping is printed as the
|
|
|
|
|
// REMAINDER of the subtotal, never a third independent rounding.
|
|
|
|
|
// Three half-up roundings need not sum (about a quarter of the real
|
|
|
|
|
// price grid lands a cent apart), and a signed tax document whose
|
|
|
|
|
// columns disagree with themselves invites exactly the scrutiny it
|
|
|
|
|
// exists to settle. Shipping absorbs the cent rather than the device
|
|
|
|
|
// because it is the ancillary line: worst case it prints one cent
|
|
|
|
|
// off the carrier's ex-VAT rate, a number no buyer sees elsewhere.
|
2026-08-05 04:18:37 +02:00
|
|
|
const std::int64_t net = Money::NetFromGross(o.totalMinor);
|
|
|
|
|
const std::int64_t vat = o.totalMinor - net;
|
2026-08-17 11:04:03 +02:00
|
|
|
const std::int64_t goodsNet = Money::NetFromGross(o.goodsMinor);
|
2026-08-05 04:18:37 +02:00
|
|
|
md += std::format("| {} | {} | {} |\n", item, o.quantity,
|
2026-08-17 11:04:03 +02:00
|
|
|
Money::FormatEuro(goodsNet));
|
2026-08-05 04:18:37 +02:00
|
|
|
md += std::format("| Shipping | 1 | {} |\n",
|
2026-08-17 11:04:03 +02:00
|
|
|
Money::FormatEuro(net - goodsNet));
|
2026-08-05 04:18:37 +02:00
|
|
|
md += std::format("| Subtotal (ex VAT) | | {} |\n", Money::FormatEuro(net));
|
|
|
|
|
md += std::format("| VAT 21% (NL) | | {} |\n", Money::FormatEuro(vat));
|
|
|
|
|
md += std::format("| **Total (incl. VAT)** | | **{}** |\n",
|
|
|
|
|
Money::FormatEuro(o.totalMinor));
|
|
|
|
|
} else {
|
|
|
|
|
md += std::format("| {} | {} | {} |\n", item, o.quantity,
|
|
|
|
|
Money::FormatEuro(o.goodsMinor));
|
|
|
|
|
md += std::format("| Shipping | 1 | {} |\n",
|
|
|
|
|
Money::FormatEuro(o.shippingMinor));
|
|
|
|
|
md += std::format("| **Total** | | **{}** |\n",
|
|
|
|
|
Money::FormatEuro(o.totalMinor));
|
|
|
|
|
md += "\nVAT 0%: zero-rated export outside the EU "
|
|
|
|
|
"(art. 146 EU VAT Directive). Import duties and taxes are levied "
|
|
|
|
|
"by the destination country and are not part of this invoice.\n";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
md += "\nThis invoice was generated by catcrafts.net and signed with the "
|
|
|
|
|
"shop's GPG key. Verify with: gpg --verify <this file>\n";
|
|
|
|
|
return md;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConfigureInvoicing(std::string gpgKeyId) {
|
|
|
|
|
// The key id ends up on a command line — constrain it to the alphabet a
|
|
|
|
|
// fingerprint or uid email actually needs, and refuse anything else.
|
|
|
|
|
for (const char c : gpgKeyId) {
|
|
|
|
|
const bool ok = (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
|
|
|
|
|
|| (c >= '0' && c <= '9') || c == '@' || c == '.'
|
|
|
|
|
|| c == '_' || c == '-' || c == '+';
|
|
|
|
|
if (!ok) {
|
|
|
|
|
std::println(std::cerr,
|
|
|
|
|
"invoice: refusing GPG key id with unexpected characters");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
gGpgKeyId = std::move(gpgKeyId);
|
|
|
|
|
if (!gGpgKeyId.empty()) {
|
|
|
|
|
std::println(std::cerr, "invoice: signing with GPG key '{}'", gGpgKeyId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool InvoiceSigningConfigured() { return !gGpgKeyId.empty(); }
|
|
|
|
|
|
|
|
|
|
std::optional<std::string> ClearsignInvoice(const std::string& markdown) {
|
|
|
|
|
if (gGpgKeyId.empty()) return std::nullopt;
|
|
|
|
|
|
|
|
|
|
std::error_code ec;
|
|
|
|
|
const std::filesystem::path dir =
|
|
|
|
|
std::filesystem::temp_directory_path(ec) / "catcrafts-invoice";
|
|
|
|
|
if (ec) return std::nullopt;
|
|
|
|
|
std::filesystem::create_directories(dir, ec);
|
|
|
|
|
std::filesystem::permissions(dir, std::filesystem::perms::owner_all, ec);
|
|
|
|
|
|
|
|
|
|
// Distinct per call so concurrent downloads cannot collide.
|
|
|
|
|
static std::atomic<std::uint64_t> counter{1};
|
|
|
|
|
const std::uint64_t n = counter.fetch_add(1);
|
|
|
|
|
const std::filesystem::path in = dir / std::format("in-{}.md", n);
|
|
|
|
|
const std::filesystem::path out = dir / std::format("out-{}.md.asc", n);
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
std::ofstream f(in, std::ios::trunc | std::ios::binary);
|
|
|
|
|
if (!f) return std::nullopt;
|
|
|
|
|
f << markdown;
|
|
|
|
|
if (!f.flush()) return std::nullopt;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// --batch: never prompt (the service has no terminal). The key must be
|
|
|
|
|
// passphrase-free or preset in the agent — deploy/README.md covers it.
|
|
|
|
|
const std::string cmd = std::format(
|
|
|
|
|
"gpg --batch --yes --clearsign --local-user '{}' -o '{}' '{}' 2>/dev/null",
|
|
|
|
|
gGpgKeyId, out.string(), in.string());
|
|
|
|
|
const int rc = std::system(cmd.c_str());
|
|
|
|
|
|
|
|
|
|
std::string signedText;
|
|
|
|
|
if (rc == 0) {
|
|
|
|
|
std::ifstream f(out, std::ios::binary);
|
|
|
|
|
std::ostringstream buf;
|
|
|
|
|
buf << f.rdbuf();
|
|
|
|
|
signedText = buf.str();
|
|
|
|
|
} else {
|
|
|
|
|
std::println(std::cerr, "invoice: gpg clearsign failed (rc {})", rc);
|
|
|
|
|
}
|
|
|
|
|
std::filesystem::remove(in, ec);
|
|
|
|
|
std::filesystem::remove(out, ec);
|
|
|
|
|
|
|
|
|
|
if (signedText.empty()) return std::nullopt;
|
|
|
|
|
return signedText;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Catcrafts::Server
|