fix(media): support base64 data URLs for uploads

Parse RFC 2397 base64 data URLs through the shared binary source classifier so inline media cannot fall through to local file handling.

Reuse the same parsing path for buffered and disk-staged uploads, reject malformed or oversized inputs explicitly, and add regression coverage for both paths.

Refs #236
This commit is contained in:
DrSmoothl committed 2026-07-16 10:05:56 +08:00
1 parent ebd02c3f3d
commit b4f5e9b71b
4 files changed
+89 -9

No files matched your search

+30
View File
@@ -25,6 +25,29 @@ describe('highway source paths', () => {
try { fs.unlinkSync(filePath); } catch { /* ignore */ }
}
});
it('loads base64 data URLs as inline bytes', async () => {
const loaded = await loadBinarySource('data:audio/webm;base64,AQID', 'record', 64);
expect({ bytes: Array.from(loaded.bytes), fileName: loaded.fileName })
.toEqual({ bytes: [1, 2, 3], fileName: '' });
});
it('never classifies data URLs as local file paths', () => {
expect(resolveLocalFilePath('data:audio/webm;base64,AQID')).toBeNull();
});
it('rejects non-base64 data URLs explicitly', async () => {
await expect(
loadBinarySource('data:text/plain,hello', 'record', 64),
).rejects.toThrow(/data URL source must use base64 encoding/);
});
it('requires the RFC 2397 base64 flag instead of a base64 media type', async () => {
await expect(
loadBinarySource('data:base64,AQID', 'record', 64),
).rejects.toThrow(/data URL source must use base64 encoding/);
});
});
describe('loadBinarySource maxBytes enforcement', () => {
@@ -98,6 +121,13 @@ describe('loadBinarySource maxBytes enforcement', () => {
loadBinarySource(`base64://${big}`, 'test', 64),
).rejects.toThrow(/too large: 128 > 64/);
});
it('rejects oversized base64 data URL payloads', async () => {
const big = Buffer.alloc(128).toString('base64');
await expect(
loadBinarySource(`data:audio/webm;base64,${big}`, 'test', 64),
).rejects.toThrow(/too large: 128 > 64/);
});
});
describe('loadBinarySource HTTP header hardening', () => {
+22
View File
@@ -38,12 +38,34 @@ describe('stageSourceToDisk — base64', () => {
} finally { await staged.cleanup(); }
});
it('stages base64 data URLs to disk', async () => {
const data = Buffer.from('hello data URL');
const staged = await stageSourceToDisk(
`data:audio/webm;codecs=opus;base64,${data.toString('base64')}`,
1024,
);
try {
expect(staged.fileSize).toBe(data.length);
expect(staged.fileName).toBe('');
expect((await readStaged(staged.filePath)).equals(data)).toBe(true);
} finally { await staged.cleanup(); }
});
it('rejects oversized base64 on decoded length (before allocation) and leaves no temp', async () => {
const big = Buffer.alloc(4096).toString('base64');
await expect(stageSourceToDisk(`base64://${big}`, 64)).rejects.toThrow(/too large/);
const parts = (await fsp.readdir(STAGE_DIR).catch(() => [] as string[])).filter((n) => n.endsWith('.part'));
expect(parts).toEqual([]);
});
it('rejects oversized base64 data URLs before allocation and leaves no temp', async () => {
const big = Buffer.alloc(4096).toString('base64');
await expect(
stageSourceToDisk(`data:audio/webm;base64,${big}`, 64),
).rejects.toThrow(/too large/);
const parts = (await fsp.readdir(STAGE_DIR).catch(() => [] as string[])).filter((n) => n.endsWith('.part'));
expect(parts).toEqual([]);
});
});
describe('stageSourceToDisk — http', () => {