Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: strip WinAnsi-unencodable control bytes before PDF draw (#1431)

Adam Eivyb5edd217c231
diff --git a/server/services/legacyExport.js b/server/services/legacyExport.js
index 9250a15ecc..b77a16d43e 100644
--- a/server/services/legacyExport.js
+++ b/server/services/legacyExport.js
@@ -493,6 +493,13 @@ function stripInlineMarkdown(text) {
// an unencodable glyph throws at draw time. Map the few non-ASCII characters
// our Markdown builders actually emit (VO₂, ⚠️, smart quotes) to safe ASCII,
// then drop anything still outside the encodable range. Pure.
+//
+// pdf-lib's WinAnsi encoder also throws on control bytes that ARE ≤ 0xFF —
+// the C0 controls (0x00–0x1F), DEL (0x7F), and the undefined C1 range
+// (0x80–0x9F) — so a stray control char pasted into free-text identity content
+// (brain notes, journals, autobiography) must be stripped BEFORE it reaches
+// `widthOfTextAtSize`/`drawText`, or the whole PDF render throws. Tabs become a
+// space (lines are already newline-split before they reach here).
function toWinAnsi(text) {
return String(text)
.replace(/[‘’]/g, "'")
@@ -501,8 +508,11 @@ function toWinAnsi(text) {
.replace(/…/g, '...')
.replace(/₂/g, '2') // subscript 2 (VO₂)
.replace(/[✅⚠️⚡]/g, '') // ✅ ⚠ emoji-variation ⚡
+ .replace(/\t/g, ' ')
// eslint-disable-next-line no-control-regex
- .replace(/[^\x00-\xFF]/g, '');
+ .replace(/[\x00-\x1F\x7F-\x9F]/g, '') // control bytes WinAnsi can't encode
+ // eslint-disable-next-line no-control-regex
+ .replace(/[^\x00-\xFF]/g, ''); // anything still above the WinAnsi range
}
// Word-wrap a single logical line to fit `maxWidth` at `size`. A single token
diff --git a/server/services/legacyExport.test.js b/server/services/legacyExport.test.js
index 2cae4e58ca..2582dbad84 100644
--- a/server/services/legacyExport.test.js
+++ b/server/services/legacyExport.test.js
@@ -234,6 +234,17 @@ describe('buildLegacyPdf', () => {
const { bytes } = await buildLegacyPdf(contentFiles, {});
expect(bytes.length).toBeGreaterThan(0);
});
+
+ it('does not throw on control bytes WinAnsi cannot encode (NUL, VT, ESC, DEL, C1)', async () => {
+ // Stray control chars pasted into free-text identity content (a note, a
+ // journal) must not crash the whole PDF render. pdf-lib's WinAnsi encoder
+ // throws on 0x00–0x1F / 0x7F / 0x80–0x9F even though they're ≤ 0xFF.
+ const dirty = 'before\x00\x0B\x1B\x7F\x9Dafter and a\ttab';
+ const contentFiles = [{ name: 'brain/journals.md', data: Buffer.from(`# Journal\n\n${dirty}\n`) }];
+ const { bytes, pageCount } = await buildLegacyPdf(contentFiles, {});
+ expect(bytes.length).toBeGreaterThan(0);
+ expect(pageCount).toBeGreaterThanOrEqual(2);
+ });
});
describe('buildLegacyZip includePdf', () => {