fix(panel): align bridge export body limits and cap Excel cell text

- Raise BRIDGE_MAX_EXPORT_BODY_BYTES to 12 MiB so 8 MiB Base64-encoded exports and verified __exportFileAction /api/settings fallbacks are not rejected with 413.
- Cap xlsxBuilder inline string cells at Excel's 32,767 UTF-16 code unit limit without splitting surrogate pairs and replace lone surrogates.
This commit is contained in:
fish
2026-11-24 16:13:42 +09:00
parent a5f9068f89
commit 0b700d1f9f
6 changed files with 165 additions and 16 deletions
@@ -1641,3 +1641,35 @@ test('i18n static t(...) key completeness guard, scopeAll formatters, and locali
globalThis.window = prevWin;
}
});
test('xlsxBuilder enforces 32,767 UTF-16 cell limit without splitting surrogate pairs or emitting lone surrogates', async () => {
const { buildXlsxWorkbookBytes, MAX_EXCEL_CELL_CHARS } = await import('../src/core/xlsxBuilder.js');
assert.equal(MAX_EXCEL_CELL_CHARS, 32767);
// Place an emoji ('😀' = \uD83D\uDE00, 2 UTF-16 code units) straddling index 32765..32766
const boundaryEmojiText = `${'A'.repeat(32765)}😀${'B'.repeat(500)}`;
const loneSurrogateText = `ok\uD800mid\uDC00end`;
const bytes = buildXlsxWorkbookBytes([
{
name: 'OverflowGuard',
columns: [{ header: 'TextCol', type: 'text' }],
rows: [[boundaryEmojiText], [loneSurrogateText]],
},
]);
const rawArchiveStr = Buffer.from(bytes).toString('utf8');
const cellMatches = [...rawArchiveStr.matchAll(/<c r="A[23]"[^>]*><is><t[^>]*>([\s\S]*?)<\/t><\/is><\/c>/g)];
assert.equal(cellMatches.length, 2);
const cellA2 = cellMatches[0][1];
assert.ok(
cellA2.length <= MAX_EXCEL_CELL_CHARS,
`Expected <= 32767 UTF-16 code units, got ${cellA2.length}`
);
assert.equal(cellA2.length, 32766, 'Backs off by 1 code unit before emoji high surrogate + ellipsis');
assert.ok(cellA2.endsWith('…'));
assert.equal(/[\uD800-\uDFFF]/.test(cellA2), false, 'No split surrogate code unit remains');
const cellA3 = cellMatches[1][1];
assert.equal(cellA3, 'ok\uFFFDmid\uFFFDend');
});
+48 -1
View File
@@ -345,7 +345,8 @@ test('Loopback HTTP bridge enforces Origin, Private Network CORS, token auth, an
assert.equal(postBody.echoData.enableWindowOverlay, false);
assert.equal(receivedCalls.length, 3);
// 6. Oversized POST body -> 413
// 6. Oversized POST body -> 413 for regular /api/settings (including spoofed nested strings),
// while legitimate __exportFileAction fallback (>64 KiB) and 8 MiB Base64 /api/export-file (~10.7 MiB JSON) succeed
const hugePayload = JSON.stringify({ pad: 'x'.repeat(70 * 1024) });
const hugeRes = await fetch(`http://127.0.0.1:${port}/api/settings`, {
method: 'POST',
@@ -357,6 +358,52 @@ test('Loopback HTTP bridge enforces Origin, Private Network CORS, token auth, an
body: hugePayload,
}).catch(() => ({ status: 413 }));
assert.equal(hugeRes.status, 413);
const spoofedSettingsPayload = JSON.stringify({
note: '{"__exportFileAction":"save"}',
pad: 'y'.repeat(70 * 1024),
});
const spoofedRes = await fetch(`http://127.0.0.1:${port}/api/settings`, {
method: 'POST',
headers: {
Origin: validOrigin,
'Content-Type': 'application/json',
'X-Usage-Monitor-Token': token,
},
body: spoofedSettingsPayload,
}).catch(() => ({ status: 413 }));
assert.equal(spoofedRes.status, 413);
const fallbackExportRes = await fetch(`http://127.0.0.1:${port}/api/settings`, {
method: 'POST',
headers: {
Origin: validOrigin,
'Content-Type': 'application/json',
'X-Usage-Monitor-Token': token,
},
body: JSON.stringify({
__exportFileAction: 'save',
payload: { filename: 'fallback.xlsx', encoding: 'base64', content: 'A'.repeat(200 * 1024) },
}),
});
assert.equal(fallbackExportRes.status, 200);
// 8 MiB binary encoded as Base64 (~10.67 MiB) fits within 12 MiB BRIDGE_MAX_EXPORT_BODY_BYTES
const eightMibBase64 = Buffer.alloc(8 * 1024 * 1024, 0x50).toString('base64');
const maxExportRes = await fetch(`http://127.0.0.1:${port}/api/export-file`, {
method: 'POST',
headers: {
Origin: validOrigin,
'Content-Type': 'application/json',
'X-Usage-Monitor-Token': token,
},
body: JSON.stringify({
filename: 'max-8mib.xlsx',
encoding: 'base64',
content: eightMibBase64,
}),
});
assert.equal(maxExportRes.status, 200);
} finally {
await controller.stop();
}