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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
address review (codex): tolerate malformed entities + resync past unterminated Record tags (#1824)
[._.]/ Adam Eivyb6a49738a859
diff --git a/server/services/appleHealthXmlParser.js b/server/services/appleHealthXmlParser.jsindex 7d2bc31699..7e099557f2 100644--- a/server/services/appleHealthXmlParser.js+++ b/server/services/appleHealthXmlParser.js@@ -21,6 +21,13 @@ import { StringDecoder } from 'string_decoder';const RECORD_TAG = '<Record';+// A real Apple Health `<Record>` opening tag is at most a few hundred bytes. If+// an apparent record's opening tag grows past this without closing (a malformed+// / unterminated-quote tag that would otherwise swallow the rest of the file and+// stall the import), treat it as garbage and resync to the next `<Record` —+// preserving the "skip the bad record, keep importing" contract sax provided.+const MAX_OPEN_TAG = 64 * 1024;+const NAMED_ENTITIES = {amp: '&',lt: '<',@@ -43,7 +50,11 @@ export function decodeXmlEntities(str) {const cp = code[1] === 'x' || code[1] === 'X'? parseInt(code.slice(2), 16): parseInt(code.slice(1), 10);- return Number.isFinite(cp) ? String.fromCodePoint(cp) : match;+ // Leave out-of-range / non-Unicode code points untouched — String+ // .fromCodePoint throws a RangeError for cp < 0 or > 0x10FFFF, which,+ // running inside the stream's synchronous write, would reject the entire+ // import. A malformed entity in one record must not fail the whole upload.+ return cp >= 0 && cp <= 0x10ffff ? String.fromCodePoint(cp) : match;}return NAMED_ENTITIES[code] ?? match;});@@ -132,7 +143,16 @@ export function createAppleHealthRecordStream({ onRecord }) {continue;}const end = findTagEnd(buffer, idx + RECORD_TAG.length);- if (end === -1) { buffer = buffer.slice(idx); return; } // opening tag not fully buffered — wait+ if (end === -1) {+ // Opening tag not closed in the buffer. Normally just a chunk boundary+ // mid-tag — retain and finish it next write. But a tag that never closes+ // (unterminated quote / truncation) would buffer the rest of the file+ // and stall; once it exceeds MAX_OPEN_TAG it can't be a real record, so+ // skip it and resync to the next `<Record`.+ if (buffer.length - idx > MAX_OPEN_TAG) { i = idx + RECORD_TAG.length; continue; }+ buffer = buffer.slice(idx);+ return;+ }onRecord({ name: 'record', attributes: parseAttributes(buffer.slice(idx, end + 1)) });i = end + 1;}diff --git a/server/services/appleHealthXmlParser.test.js b/server/services/appleHealthXmlParser.test.jsindex a2f8cfe76a..4081b51474 100644--- a/server/services/appleHealthXmlParser.test.js+++ b/server/services/appleHealthXmlParser.test.js@@ -44,6 +44,14 @@ describe('decodeXmlEntities', () => {it('leaves unknown entities untouched', () => {expect(decodeXmlEntities('&bogus;')).toBe('&bogus;');});++ it('leaves out-of-range numeric references untouched without throwing', () => {+ // String.fromCodePoint(999999999999) throws RangeError — must not propagate.+ expect(() => decodeXmlEntities('�')).not.toThrow();+ expect(decodeXmlEntities('�')).toBe('�');+ expect(decodeXmlEntities('�')).toBe('�'); // just past U+10FFFF+ expect(decodeXmlEntities('')).toBe(String.fromCodePoint(0x10ffff)); // max valid+ });});describe('parseAttributes', () => {@@ -143,4 +151,25 @@ describe('createAppleHealthRecordStream', () => {it('handles an empty stream without emitting anything', async () => {expect(await parse('')).toEqual([]);});++ it('does not throw on a malformed numeric entity inside a Record attribute', async () => {+ const xml = '<HealthData><Record type="t" sourceName="�" startDate="d"/></HealthData>';+ const records = await parse(xml);+ expect(records).toHaveLength(1);+ expect(records[0].attributes.sourcename).toBe('�'); // left as-is, not decoded+ });++ it('resyncs past an unterminated Record tag instead of stalling the stream', async () => {+ // An unclosed opening tag (no `>`) larger than MAX_OPEN_TAG must not buffer+ // the rest of the file forever — it should be skipped so later valid records+ // still import. Feed it as two writes so the malformed tag is unclosed at+ // the first drain.+ const records = [];+ const parser = createAppleHealthRecordStream({ onRecord: (n) => records.push(n) });+ parser.write(Buffer.from('<HealthData><Record ' + 'a'.repeat(70000), 'utf8'));+ parser.write(Buffer.from(' /><Record type="ok" value="1" startDate="d"/></HealthData>', 'utf8'));+ await new Promise((resolve, reject) => parser.end((err) => (err ? reject(err) : resolve())));+ expect(records).toHaveLength(1);+ expect(records[0].attributes.type).toBe('ok');+ });});