fix(pdf): RFC 6266-encode Content-Disposition on quote/invoice PDFs (#1024) (#1062)

Stable backport of #1055 (main: 3a11e6eb). Change content is byte-identical
to the main twin; cherry-picked clean, no resolutions needed.

The six quote/invoice PDF endpoints interpolated buildPdfFilename()'s result
straight into `inline; filename="${filename}"`. That result deliberately
preserves non-ASCII (it doubles as the PDF's internal Title metadata), and
HTTP header values are latin1, so a customer label reaching the header
directly failed in one of two ways:

  - U+0080-U+00FF (ä ö ü ß — every German umlaut): no throw. The raw byte
    goes out and the client reads back a mangled name. Silent corruption.
  - above U+00FF (Polish ł, Czech ř, Turkish ş, €, Cyrillic, CJK, emoji):
    Node's setHeader rejects it with ERR_INVALID_CHAR. The throw lands
    after the PDF buffer is already rendered, so the request 500s.

This corrects the issue's diagnosis: it reported umlauts as the 500 case,
but umlauts are inside latin1 and mangle rather than throw.

Route all six through buildContentDisposition(), which emits an ASCII
fallback plus the RFC 5987 filename*=UTF-8'' form. Also stops sanitiseSegment
splitting surrogate pairs at its 80-unit cap — a dangling high surrogate makes
encodeURIComponent throw URIError inside the helper, reaching the same 500 a
different way (found by external review on the main twin).

Verified on this branch: 14/14 in the new suite, 151/151 across the nine
surrounding pdf/filename/quote/invoice suites, lint clean.

Co-authored-by: Paul Nothaft <[email protected]>
This commit is contained in:
Paul Nothaft
2026-08-16 19:52:39 +02:00
committed by GitHub
co-authored by Paul Nothaft
parent 88fa3c5297
commit 376311cb90
5 changed files with 180 additions and 7 deletions
+25 -1
View File
@@ -20,6 +20,19 @@
* - The PDF's internal `Title` metadata (Chrome's PDF viewer
* uses this as the default name when saving from a blob URL,
* where Content-Disposition can't reach)
*
* IMPORTANT (#1024): the preserved non-ASCII is exactly what a raw
* `filename="${...}"` header cannot carry. HTTP header values are
* latin1, so a customer label reaching a header directly either
* mangles (U+0080-U+00FF — every German umlaut: `Müller` is sent as
* the byte 0xFC and read back as garbage) or throws ERR_INVALID_CHAR
* and 500s the request (anything above U+00FF — Polish ł, Czech ř,
* Turkish ş, €, Cyrillic, CJK, emoji).
*
* Never interpolate this result into a header. Pass it through
* `buildContentDisposition()` in utils/filenameSanitizer, which emits
* an ASCII fallback plus the RFC 5987 `filename*=UTF-8''…` form so the
* unicode name survives in browsers and the header stays legal.
*/
function sanitiseSegment(input, maxLen = 80) {
@@ -33,7 +46,18 @@ function sanitiseSegment(input, maxLen = 80) {
s = s.replace(/-+/g, '-');
// Trim leading/trailing dashes + dots.
s = s.replace(/^[-.]+|[-.]+$/g, '');
if (s.length > maxLen) s = s.slice(0, maxLen);
if (s.length > maxLen) {
s = s.slice(0, maxLen);
// slice() cuts UTF-16 code units, so a boundary landing inside an astral
// character (emoji, rarer CJK) leaves a dangling high surrogate. That is
// not merely cosmetic: the lone surrogate makes encodeURIComponent throw
// `URIError: URI malformed` inside buildContentDisposition, which 500s
// the PDF endpoint — the exact failure #1024 set out to remove, just via
// a different route. Drop the orphan rather than widening the cap, so the
// byte budget this limit exists to protect is unchanged.
const lastUnit = s.charCodeAt(s.length - 1);
if (lastUnit >= 0xD800 && lastUnit <= 0xDBFF) s = s.slice(0, -1);
}
return s;
}