fix(apple-health): hold only the members a pass reads to the per-member limits

The ECG, route and Health Records passes applied the per-member byte,
ratio and count limits to every member of their kind, wanted or not,
so one large Health Records file nobody referenced, or more than 50,000
of them, ended the whole lab pass. A wanted member that broke a limit
also took every other member of the pass with it, and the compressed
bytes were read into memory before any size check.

The limits now apply to the members a pass will read; a breach fails
that member alone, and the compressed size is held to the member limit
before it is allocated. Running totals still end the pass. The ceiling
on directory entries is one shared value of 200,000 for all three
passes, so an export with more than 20,000 files keeps its ECGs.
This commit is contained in:
Marc-André Bombeck committed 2026-11-17 21:21:54 +01:00
1 parent f3662a8dc8
commit 3572645544
8 files changed
+506 -41

No files matched your search

+1 -1
View File
@@ -42,7 +42,7 @@
- A lab marker created from a text result, such as "negative" on a scanned report or a CSV row without a unit, can be edited again. It opens with "No unit" ticked, a rename saves without touching the unit, and the unit can be set while the marker holds only text results. The unit stays locked once numbers are stored under it, and writing the same unit another way (`mg/dl` for `mg/dL`) no longer counts as a change. (#1393)
- On an account with a watch sending heart rate every few seconds, weight, blood pressure and other occasional readings went missing from the MCP metric series, the Coach and the insights, because the heart rate filled every read before they were reached. Each kind of reading now gets its own share, so they all show up again. (#1398)
- Right after a large first import, before the database had refreshed its statistics, the dashboard's latest readings and the insight reads could walk through every reading of the account once for each kind of measurement, which on a long history takes seconds or runs into the timeout. Each kind is now looked up on its own whatever the statistics say.
- Large Apple Health exports, which the Health app writes as ZIP64 archives, no longer lose their ECGs and workout routes: the reader for those files refused ZIP64 and counted every one as failed.
- Large Apple Health exports, which the Health app writes as ZIP64 archives, no longer lose their ECGs and workout routes: the reader for those files refused ZIP64 and counted every one as failed. An export with more than 20,000 files no longer loses its ECGs either, a large file the import does not read no longer stops the routes or lab results from being read, and a single file over a size limit now fails on its own instead of taking every file of its kind with it.
### Security
@@ -0,0 +1,297 @@
/**
* Which members the per-member and per-kind limits apply to, and what a
* breach costs.
*
* A pass reads one kind of member, and of that kind only the ones it wants
* (the routes and Health Records files the export's XML references). A
* member it is not going to read cannot make the archive unsafe for it, so
* its size, its compression ratio and how many of its kind there are do not
* count; only the archive-wide ceiling on directory entries does. A member
* the pass does want and that breaks a per-member limit fails on its own, the
* other members are still read. A breach of a running total still ends the
* pass. The ceiling on directory entries is the same for every pass, so an
* export with many files reads the same in all three.
*/
import { afterEach, describe, expect, it } from "vitest";
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { randomBytes } from "node:crypto";
import { strToU8, zipSync, type Zippable } from "fflate";
import {
APPLE_HEALTH_ARCHIVE_MAX_ENTRIES,
streamAppleHealthClinicalMembers,
streamAppleHealthEcgMembers,
streamAppleHealthRouteMembers,
type AppleHealthArchiveMember,
} from "../archive-stream";
import { APPLE_HEALTH_CLINICAL_ARCHIVE_LIMITS } from "../clinical-labs-import";
import { APPLE_HEALTH_ECG_ARCHIVE_LIMITS } from "../ecg-import";
import { APPLE_HEALTH_ROUTE_ARCHIVE_LIMITS } from "../route-import";
const dirs: string[] = [];
afterEach(() => {
for (const dir of dirs.splice(0))
rmSync(dir, { recursive: true, force: true });
});
function archive(files: Zippable): string {
const dir = mkdtempSync(join(tmpdir(), "healthlog-archive-limits-"));
dirs.push(dir);
const path = join(dir, "export.zip");
writeFileSync(path, zipSync(files));
return path;
}
const CLINICAL = "apple_health_export/clinical-records/";
const ROUTES = "apple_health_export/workout-routes/";
const ECG = "apple_health_export/electrocardiograms/";
async function read(
members: AsyncIterable<AppleHealthArchiveMember>,
): Promise<Record<string, string>> {
const out: Record<string, string> = {};
for await (const member of members) {
try {
let text = "";
for await (const chunk of member.stream) text += String(chunk);
out[member.name] = text;
} catch {
out[member.name] = `refused:${member.refused ?? "?"}`;
}
}
return out;
}
const SMALL = {
maxMembers: 100,
maxMemberBytes: 1024,
maxCompressionRatio: 20,
};
describe("members the pass does not want", () => {
it("skips an oversized or bomb-like clinical file nobody references", async () => {
const path = archive({
[`${CLINICAL}Observation-a.json`]: strToU8('{"id":"a"}'),
// Over the per-member byte limit and far over the ratio.
[`${CLINICAL}DocumentReference-big.json`]: strToU8("x".repeat(64_000)),
});
const got = await read(
streamAppleHealthClinicalMembers({
archivePath: path,
limits: {
...SMALL,
maxClinicalMembers: 5,
maxTotalClinicalBytes: 1024,
},
wanted: (name) => name.endsWith("Observation-a.json"),
}),
);
expect(got).toEqual({ [`${CLINICAL}Observation-a.json`]: '{"id":"a"}' });
});
it("does not count unwanted files of the kind against the kind's ceiling", async () => {
const files: Zippable = {};
for (let i = 0; i < 12; i += 1) {
files[`${CLINICAL}Condition-${i}.json`] = strToU8(`{"id":"c${i}"}`);
}
files[`${CLINICAL}Observation-a.json`] = strToU8('{"id":"a"}');
const got = await read(
streamAppleHealthClinicalMembers({
archivePath: archive(files),
limits: {
...SMALL,
maxClinicalMembers: 5,
maxTotalClinicalBytes: 1024,
},
wanted: (name) => name.endsWith("Observation-a.json"),
}),
);
expect(Object.keys(got)).toEqual([`${CLINICAL}Observation-a.json`]);
});
it("skips an unwanted route that breaks a per-member limit", async () => {
const path = archive({
[`${ROUTES}route_a.gpx`]: strToU8("<gpx/>"),
[`${ROUTES}route_big.gpx`]: strToU8("<gpx>".repeat(20_000)),
});
const got = await read(
streamAppleHealthRouteMembers({
archivePath: path,
limits: { ...SMALL, maxRouteMembers: 1, maxTotalRouteBytes: 1024 },
wanted: (name) => name.endsWith("route_a.gpx"),
}),
);
expect(got).toEqual({ [`${ROUTES}route_a.gpx`]: "<gpx/>" });
});
});
describe("a wanted member that breaks a per-member limit", () => {
it("fails on its own; the members around it are still read", async () => {
const path = archive({
[`${CLINICAL}Observation-a.json`]: strToU8('{"id":"a"}'),
[`${CLINICAL}Observation-bomb.json`]: strToU8("x".repeat(64_000)),
[`${CLINICAL}Observation-b.json`]: strToU8('{"id":"b"}'),
});
const got = await read(
streamAppleHealthClinicalMembers({
archivePath: path,
limits: {
...SMALL,
maxClinicalMembers: 5,
maxTotalClinicalBytes: 1024,
},
wanted: () => true,
}),
);
expect(got).toEqual({
[`${CLINICAL}Observation-a.json`]: '{"id":"a"}',
[`${CLINICAL}Observation-bomb.json`]: "refused:too_large",
[`${CLINICAL}Observation-b.json`]: '{"id":"b"}',
});
});
it("is refused before its compressed bytes are read when they alone exceed the member limit", async () => {
// Random bytes do not compress: the stored size is over the limit, so
// the reader must not allocate it.
const path = archive({
[`${ECG}ecg_a.csv`]: strToU8("Name,a\n"),
[`${ECG}ecg_noise.csv`]: [randomBytes(4096), { level: 0 }],
});
const got = await read(
streamAppleHealthEcgMembers({
archivePath: path,
limits: {
...SMALL,
maxCompressionRatio: 200,
maxEcgMembers: 5,
maxTotalEcgBytes: 64 * 1024,
},
}),
);
expect(got).toEqual({
[`${ECG}ecg_a.csv`]: "Name,a\n",
[`${ECG}ecg_noise.csv`]: "refused:too_large",
});
});
it("is refused by its compressed size even when its declared size lies", async () => {
const path = archive({
[`${ECG}ecg_a.csv`]: strToU8("Name,a\n"),
[`${ECG}ecg_noise.csv`]: [randomBytes(4096), { level: 0 }],
});
// Declare the member one byte long in the central directory: neither
// the declared size nor the ratio then says anything is wrong.
const bytes = readFileSync(path);
const name = Buffer.from(`${ECG}ecg_noise.csv`);
for (let at = 0; at + 46 < bytes.length; at += 1) {
if (
bytes.readUInt32LE(at) === 0x02014b50 &&
bytes.subarray(at + 46, at + 46 + name.length).equals(name)
) {
bytes.writeUInt32LE(1, at + 24);
}
}
writeFileSync(path, bytes);
const got = await read(
streamAppleHealthEcgMembers({
archivePath: path,
limits: {
...SMALL,
maxCompressionRatio: 200,
maxEcgMembers: 5,
maxTotalEcgBytes: 64 * 1024,
},
}),
);
expect(got).toEqual({
[`${ECG}ecg_a.csv`]: "Name,a\n",
[`${ECG}ecg_noise.csv`]: "refused:too_large",
});
});
it("still ends the pass when a running total is exceeded", async () => {
const path = archive({
[`${ECG}ecg_a.csv`]: strToU8("a".repeat(600)),
[`${ECG}ecg_b.csv`]: strToU8("b".repeat(600)),
});
const run = () =>
read(
streamAppleHealthEcgMembers({
archivePath: path,
limits: {
...SMALL,
maxCompressionRatio: 200,
maxEcgMembers: 5,
maxTotalEcgBytes: 1000,
},
}),
);
await expect(run()).rejects.toThrow(/total ECG byte limit/);
});
});
describe("the ceiling on directory entries", () => {
it("is one value for the ECG, route and Health Records passes", () => {
expect(APPLE_HEALTH_ARCHIVE_MAX_ENTRIES).toBe(200_000);
expect(APPLE_HEALTH_ECG_ARCHIVE_LIMITS.maxMembers).toBe(
APPLE_HEALTH_ARCHIVE_MAX_ENTRIES,
);
expect(APPLE_HEALTH_ROUTE_ARCHIVE_LIMITS.maxMembers).toBe(
APPLE_HEALTH_ARCHIVE_MAX_ENTRIES,
);
expect(APPLE_HEALTH_CLINICAL_ARCHIVE_LIMITS.maxMembers).toBe(
APPLE_HEALTH_ARCHIVE_MAX_ENTRIES,
);
});
it("lets every pass read an export with more files than the old ECG ceiling", async () => {
// 25,000 entries: past the 20,000 the ECG pass used to stop at, and
// the shape a Health Records account with years of routes produces.
const files: Zippable = {
[`${ECG}ecg_a.csv`]: strToU8("Name,a\n"),
[`${ROUTES}route_a.gpx`]: strToU8("<gpx/>"),
[`${CLINICAL}Observation-a.json`]: strToU8('{"id":"a"}'),
};
const empty = new Uint8Array(0);
for (let i = 0; i < 25_000; i += 1) {
files[`${CLINICAL}Condition-${i}.json`] = empty;
}
const path = archive(files);
expect(
Object.keys(
await read(
streamAppleHealthEcgMembers({
archivePath: path,
limits: APPLE_HEALTH_ECG_ARCHIVE_LIMITS,
}),
),
),
).toEqual([`${ECG}ecg_a.csv`]);
expect(
Object.keys(
await read(
streamAppleHealthRouteMembers({
archivePath: path,
limits: APPLE_HEALTH_ROUTE_ARCHIVE_LIMITS,
wanted: () => true,
}),
),
),
).toEqual([`${ROUTES}route_a.gpx`]);
expect(
Object.keys(
await read(
streamAppleHealthClinicalMembers({
archivePath: path,
limits: APPLE_HEALTH_CLINICAL_ARCHIVE_LIMITS,
wanted: (name) => name.endsWith("Observation-a.json"),
}),
),
),
).toEqual([`${CLINICAL}Observation-a.json`]);
}, 60_000);
});
@@ -195,6 +195,29 @@ describe("importAppleHealthClinicalLabs", () => {
expect(counts).toMatchObject({ discovered: 1, imported: 0, failed: 1 });
});
it("fails only the file over a per-member limit and reads the rest", async () => {
const path = archive({
[`${ROOT}Observation-a.json`]: observation("a"),
[`${ROOT}Observation-big.json`]: observation("big", {
note: [{ text: "x".repeat(4_000) }],
}),
[`${ROOT}Observation-c.json`]: observation("c"),
});
const counts = await run(
path,
[
ref("/clinical-records/Observation-a.json"),
ref("/clinical-records/Observation-big.json"),
ref("/clinical-records/Observation-c.json"),
],
{ maxMemberBytes: 1_024 },
);
expect(counts).toMatchObject({ discovered: 3, imported: 2, failed: 1 });
expect(JSON.stringify(mocks.annotate.mock.calls)).toContain(
"member_too_large",
);
});
it("annotates counts and closed reasons only", async () => {
const path = archive({
[`${ROOT}Observation-a.json`]: observation("a", {
+139 -26
View File
@@ -20,9 +20,19 @@ export interface AppleHealthArchiveLimits {
maxCompressionRatio: number;
}
/**
* Why a wanted member was not read: it breaks a per-member limit (`too_large`
* for its size or compression ratio, `unsupported` for encryption or a
* compression method the reader does not inflate).
*/
export type AppleHealthMemberRefusal = "too_large" | "unsupported";
export interface AppleHealthArchiveMember {
name: string;
/** Errors on the first read when the member was refused. */
stream: Readable;
/** Set when the member broke a per-member limit and was not inflated. */
refused?: AppleHealthMemberRefusal;
}
interface ZipEntry {
@@ -32,8 +42,20 @@ interface ZipEntry {
compressedSize: number;
uncompressedSize: number;
localHeaderOffset: number;
/** Set on a wanted member of the kind that breaks a per-member limit. */
refused?: AppleHealthMemberRefusal;
}
/**
* The most directory entries an archive may hold, for every pass alike. It
* bounds the walk of the central directory, which every pass makes over every
* entry whatever its kind; a pass's own members are bounded by its own count.
* A Health app export holds one file per ECG, per workout route and per Health
* Records resource beside `export.xml`, so years of routes and a clinic's
* history reach tens of thousands.
*/
export const APPLE_HEALTH_ARCHIVE_MAX_ENTRIES = 200_000;
const EOCD_SIGNATURE = 0x06054b50;
const CENTRAL_SIGNATURE = 0x02014b50;
const LOCAL_SIGNATURE = 0x04034b50;
@@ -202,11 +224,20 @@ export function clinicalMemberNameFromReference(path: string): string | null {
return isClinicalRecordJson(name) ? name : null;
}
/**
* The central directory, checked as a whole: entry count, bounds, names. Of
* the entries, only the ones the pass will read (`wanted` members of its
* kind) are held to the per-member limits and counted against the kind's
* ceiling; one that breaks a per-member limit is marked refused rather than
* failing the archive. The others are not read, so neither their size nor how
* many there are can make reading the wanted ones unsafe.
*/
async function readCentralDirectory(
handle: FileHandle,
fileSize: number,
limits: AppleHealthArchiveLimits,
kind: MemberKind,
wanted: (name: string) => boolean,
): Promise<ZipEntry[]> {
if (fileSize < 22) throw archiveError("missing ZIP directory");
@@ -321,24 +352,16 @@ async function readCentralDirectory(
if (names.has(name)) throw archiveError("duplicate member name");
names.add(name);
if (kind.matches(name)) {
let refused: AppleHealthMemberRefusal | undefined;
if (kind.matches(name) && wanted(name)) {
kindCount += 1;
if (kindCount > kind.maxMembers) {
throw archiveError(`${kind.label} member count limit exceeded`);
}
if ((flags & 0x1) !== 0) {
throw archiveError(`encrypted ${kind.label} member is unsupported`);
}
if (method !== 0 && method !== 8) {
throw archiveError(`unsupported ${kind.label} compression method`);
}
if (uncompressedSize > limits.maxMemberBytes) {
throw archiveError(`${kind.label} member byte limit exceeded`);
}
const ratio = uncompressedSize / Math.max(1, compressedSize);
if (ratio > limits.maxCompressionRatio) {
throw archiveError(`${kind.label} compression ratio limit exceeded`);
}
refused = memberRefusal(
{ flags, method, compressedSize, uncompressedSize },
limits,
);
}
entries.push({
@@ -348,6 +371,7 @@ async function readCentralDirectory(
compressedSize,
uncompressedSize,
localHeaderOffset,
...(refused ? { refused } : {}),
});
cursor += 46 + nameLength + extraLength + commentLength;
if (cursor > centralOffset + centralSize) {
@@ -357,6 +381,60 @@ async function readCentralDirectory(
return entries;
}
/**
* Why a member breaks a per-member limit by what its directory entry
* declares, or undefined. The compressed size is held to the member limit
* too: it is what the reader allocates before inflating, and a member that
* does not fit compressed cannot fit inflated.
*/
function memberRefusal(
entry: Pick<
ZipEntry,
"flags" | "method" | "compressedSize" | "uncompressedSize"
>,
limits: AppleHealthArchiveLimits,
): AppleHealthMemberRefusal | undefined {
if ((entry.flags & 0x1) !== 0) return "unsupported";
if (entry.method !== 0 && entry.method !== 8) return "unsupported";
if (
entry.uncompressedSize > limits.maxMemberBytes ||
entry.compressedSize > limits.maxMemberBytes
) {
return "too_large";
}
const ratio = entry.uncompressedSize / Math.max(1, entry.compressedSize);
if (ratio > limits.maxCompressionRatio) return "too_large";
return undefined;
}
/** A member breaking a per-member limit; the pass goes on without it. */
class MemberRefused extends Error {
constructor(
readonly reason: AppleHealthMemberRefusal,
message: string,
) {
super(message);
}
}
/** A member that was not read: its stream errors on the first read. */
function refusedMember(
name: string,
reason: AppleHealthMemberRefusal,
label: string,
): AppleHealthArchiveMember {
const error = archiveError(`${label} member refused (${reason})`);
return {
name,
refused: reason,
stream: new Readable({
read() {
this.destroy(error);
},
}),
};
}
async function decompressEntry(
handle: FileHandle,
entry: ZipEntry,
@@ -387,6 +465,13 @@ async function decompressEntry(
if (dataOffset + entry.compressedSize > fileSize) {
throw archiveError("invalid member data bounds");
}
// Checked again here, beside the allocation it guards.
if (entry.compressedSize > limits.maxMemberBytes) {
throw new MemberRefused(
"too_large",
`${kind.label} member byte limit exceeded`,
);
}
const compressed = await readExactly(
handle,
@@ -401,22 +486,30 @@ async function decompressEntry(
: Readable.from([compressed]).pipe(createInflateRaw());
const chunks: Buffer[] = [];
let memberBytes = 0;
// Which limit stopped the inflate: the member's own fails the member, the
// running total ends the pass.
let stop: Error | null = null;
try {
for await (const value of source) {
const chunk = Buffer.isBuffer(value) ? value : Buffer.from(value);
memberBytes += chunk.byteLength;
if (memberBytes > limits.maxMemberBytes) {
throw archiveError(`actual ${kind.label} member byte limit exceeded`);
stop = new MemberRefused(
"too_large",
`actual ${kind.label} member byte limit exceeded`,
);
throw stop;
}
if (total.bytes + memberBytes > kind.maxTotalBytes) {
throw archiveError(`total ${kind.label} byte limit exceeded`);
stop = archiveError(`total ${kind.label} byte limit exceeded`);
throw stop;
}
chunks.push(chunk);
}
} catch {
for (const chunk of chunks) chunk.fill(0);
throw archiveError(`${kind.label} decompression or byte limit failure`);
throw stop ?? archiveError(`${kind.label} decompression failure`);
} finally {
compressed.fill(0);
}
@@ -430,6 +523,9 @@ async function decompressEntry(
* The complete central directory is validated before any health data is
* yielded, so a structurally unsafe archive is never partly processed.
* `wanted` narrows further (a route the export's XML actually references).
* A wanted member that breaks a per-member limit is yielded refused (its
* stream errors) and the pass goes on; a running total, a malformed archive
* or an inflate failure ends it.
*/
async function* streamMembers(
archivePath: string,
@@ -445,18 +541,35 @@ async function* streamMembers(
if (!Number.isSafeInteger(fileSize)) {
throw archiveError("archive is too large to address safely");
}
const entries = await readCentralDirectory(handle, fileSize, limits, kind);
const entries = await readCentralDirectory(
handle,
fileSize,
limits,
kind,
wanted,
);
const total = { bytes: 0 };
for (const entry of entries) {
if (!kind.matches(entry.name) || !wanted(entry.name)) continue;
const chunks = await decompressEntry(
handle,
entry,
fileSize,
limits,
kind,
total,
);
if (entry.refused) {
yield refusedMember(entry.name, entry.refused, kind.label);
continue;
}
let chunks: Buffer[];
try {
chunks = await decompressEntry(
handle,
entry,
fileSize,
limits,
kind,
total,
);
} catch (err) {
if (!(err instanceof MemberRefused)) throw err;
yield refusedMember(entry.name, err.reason, kind.label);
continue;
}
yield { name: entry.name, stream: Readable.from(chunks) };
}
} finally {
+11 -1
View File
@@ -24,6 +24,7 @@
*/
import type { PrismaClient } from "@/generated/prisma/client";
import {
APPLE_HEALTH_ARCHIVE_MAX_ENTRIES,
clinicalMemberNameFromReference,
isClinicalRecordJson,
streamAppleHealthClinicalMembers,
@@ -52,7 +53,7 @@ export type AppleHealthImportJobResult = ImportJobResult & {
/** Per-archive ceilings for the Health Records members. */
export const APPLE_HEALTH_CLINICAL_ARCHIVE_LIMITS: AppleHealthClinicalArchiveLimits =
{
maxMembers: 200_000,
maxMembers: APPLE_HEALTH_ARCHIVE_MAX_ENTRIES,
maxClinicalMembers: 50_000,
// A single Observation is a few KB; 2 MiB leaves room for a long note.
maxMemberBytes: 2 * 1024 * 1024,
@@ -129,6 +130,15 @@ export async function importAppleHealthClinicalLabs(input: {
byMember.has(name) || (byId.size > 0 && isClinicalRecordJson(name)),
})) {
const byPath = byMember.get(member.name);
if (member.refused) {
// Over a per-member limit: this file fails, the rest are read.
if (byPath) {
handled += 1;
counts.failed += 1;
note(`member_${member.refused}`);
}
continue;
}
let resource: unknown;
try {
resource = await readJson(member.stream);
+7 -3
View File
@@ -17,7 +17,10 @@
*/
import { createHash } from "node:crypto";
import type { PrismaClient } from "@/generated/prisma/client";
import { streamAppleHealthEcgMembers } from "@/lib/apple-health/archive-stream";
import {
APPLE_HEALTH_ARCHIVE_MAX_ENTRIES,
streamAppleHealthEcgMembers,
} from "@/lib/apple-health/archive-stream";
import {
parseAppleHealthEcgCsv,
type NormalizedAppleHealthEcg,
@@ -73,8 +76,9 @@ export async function importAppleHealthEcg(input: {
return "updated";
}
const APPLE_HEALTH_ECG_ARCHIVE_LIMITS = {
maxMembers: 20_000,
/** Per-archive ceilings for the ECG members. */
export const APPLE_HEALTH_ECG_ARCHIVE_LIMITS = {
maxMembers: APPLE_HEALTH_ARCHIVE_MAX_ENTRIES,
maxEcgMembers: 2_000,
maxMemberBytes: 16 * 1024 * 1024,
maxTotalEcgBytes: 512 * 1024 * 1024,
+2 -1
View File
@@ -22,6 +22,7 @@ import sax from "sax";
import type { PrismaClient } from "@/generated/prisma/client";
import {
APPLE_HEALTH_ARCHIVE_MAX_ENTRIES,
routeMemberNameFromReference,
streamAppleHealthRouteMembers,
type AppleHealthRouteArchiveLimits,
@@ -52,7 +53,7 @@ export interface WorkoutRouteImportCounts {
/** Per-archive ceilings for the route members. */
export const APPLE_HEALTH_ROUTE_ARCHIVE_LIMITS: AppleHealthRouteArchiveLimits =
{
maxMembers: 100_000,
maxMembers: APPLE_HEALTH_ARCHIVE_MAX_ENTRIES,
maxRouteMembers: 20_000,
// A three-hour ride at 1 Hz is a few MB of GPX; 32 MB covers a 24-hour
// ultra with room to spare and bounds the one buffer held at a time.
@@ -19,6 +19,7 @@ type ArchiveLimits = {
type ArchiveMember = {
name: string;
stream: Readable;
refused?: "too_large" | "unsupported";
};
type NormalizedEcg = {
@@ -171,6 +172,21 @@ async function collectMembers(path: string, limits = LIMITS) {
return members;
}
/** One member came back refused, and reading it fails. */
async function expectRefused(
members: Promise<ArchiveMember[]>,
reason: "too_large" | "unsupported",
) {
const [member, ...rest] = await members;
expect(rest).toEqual([]);
expect(member.refused).toBe(reason);
await expect(
(async () => {
for await (const chunk of member.stream) void chunk;
})(),
).rejects.toThrow(/refused/);
}
afterEach(() => {
for (const dir of tempDirs.splice(0)) {
rmSync(dir, { recursive: true, force: true });
@@ -239,7 +255,7 @@ describe("Apple Health ECG archive stream — path and resource bounds", () => {
(header: Buffer, central: boolean) =>
header.writeUInt16LE(99, central ? 10 : 8),
],
] as const)("rejects %s ECG entries", async (_label, mutate) => {
] as const)("refuses %s ECG entries on their own", async (_label, mutate) => {
const member = "apple_health_export/electrocardiograms/ecg_2026-07-18.csv";
const path = archivePath({
"apple_health_export/export.xml": "<HealthData/>",
@@ -251,9 +267,8 @@ describe("Apple Health ECG archive stream — path and resource bounds", () => {
mutateZipHeaders(fs.readFileSync(path), member, mutate),
);
await expect(collectMembers(path)).rejects.toThrow(
/encrypted|compression|unsupported/i,
);
// The member fails; the pass, and any other strip in it, goes on.
await expectRefused(collectMembers(path), "unsupported");
});
it("enforces member-count and ECG-count caps before parsing", async () => {
@@ -275,9 +290,9 @@ describe("Apple Health ECG archive stream — path and resource bounds", () => {
).rejects.toThrow(/member|count|limit/i);
});
it("rejects declared compression-ratio bombs", async () => {
it("refuses declared compression-ratio bombs", async () => {
const repetitive = `${"0,".repeat(2_000)}\n`;
await expect(
await expectRefused(
collectMembers(
archivePath({
"apple_health_export/export.xml": "<HealthData/>",
@@ -285,7 +300,8 @@ describe("Apple Health ECG archive stream — path and resource bounds", () => {
}),
{ ...LIMITS, maxMemberBytes: 10_000, maxCompressionRatio: 2 },
),
).rejects.toThrow(/compression|ratio|bomb/i);
"too_large",
);
});
it("enforces actual decompressed member bytes even when metadata lies", async () => {
@@ -305,9 +321,10 @@ describe("Apple Health ECG archive stream — path and resource bounds", () => {
);
writeFileSync(path, lied);
await expect(
await expectRefused(
collectMembers(path, { ...LIMITS, maxMemberBytes: 64 }),
).rejects.toThrow(/byte|size|limit/i);
"too_large",
);
});
it("enforces the running total across otherwise valid ECG members", async () => {