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

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.js
index 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.js
index 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('&#999999999999;')).not.toThrow();
+ expect(decodeXmlEntities('&#999999999999;')).toBe('&#999999999999;');
+ expect(decodeXmlEntities('&#x110000;')).toBe('&#x110000;'); // just past U+10FFFF
+ expect(decodeXmlEntities('&#x10FFFF;')).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="&#999999999999;" startDate="d"/></HealthData>';
+ const records = await parse(xml);
+ expect(records).toHaveLength(1);
+ expect(records[0].attributes.sourcename).toBe('&#999999999999;'); // 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');
+ });
});