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

+11 -6
View File
@@ -10,7 +10,7 @@
// http(s):// → stream the download to a .part file, fsync, atomic
// rename (shares the exact transport of loadBinarySource
// via downloadHttp — same UA/redirect/timeout/retry/caps).
// base64:// → enforce maxBytes on the DECODED length, then decode to
// base64:// / data: → enforce maxBytes on the DECODED length, then decode to
// a .part file and rename.
//
// Crash safety: staged temps are written `.part` → fsync → rename → fsync(dir),
@@ -23,7 +23,12 @@ import os from 'os';
import path from 'path';
import { randomUUID } from 'crypto';
import { createLogger } from '@snowluma/common/logger';
import { downloadHttp, resolveLocalFilePath, type DownloadSink } from './utils';
import {
downloadHttp,
inlineBase64Payload,
resolveLocalFilePath,
type DownloadSink,
} from './utils';
const log = createLogger('Highway.Stage');
@@ -155,9 +160,9 @@ export async function stageSourceToDisk(source: string, maxBytes: number): Promi
ensureReaper();
await fsp.mkdir(STAGE_DIR, { recursive: true });
// ── base64:// ──────────────────────────────────────────────────────────
if (/^base64:\/\//i.test(source)) {
const b64 = source.slice(9);
// ── inline Base64 (`base64://` or RFC 2397 `data:`) ────────────────────
const b64 = inlineBase64Payload(source);
if (b64 !== null) {
const decodedLen = base64DecodedLength(b64);
if (decodedLen > maxBytes) {
throw new Error(`stage source too large: ${decodedLen} > ${maxBytes}`);
@@ -178,7 +183,7 @@ export async function stageSourceToDisk(source: string, maxBytes: number): Promi
} finally {
active.delete(partPath);
}
log.debug('staged base64 source (%d bytes)', bytes.length);
log.debug('staged inline base64 source (%d bytes)', bytes.length);
return registerStaged(finalPath, bytes.length, '');
}
+26 -3
View File
@@ -50,9 +50,31 @@ export interface ImageFormat {
// --- Binary source loading ---
/**
* Return the encoded payload for an inline Base64 source.
*
* OneBot historically accepts the shorthand `base64://...`; media elements
* also accept RFC 2397 Data URLs such as `data:audio/webm;base64,...`. Keep
* their classification in one place so Data URLs can never fall through to
* the local-file path.
*/
export function inlineBase64Payload(source: string): string | null {
if (/^base64:\/\//i.test(source)) return source.slice(9);
if (!/^data:/i.test(source)) return null;
const comma = source.indexOf(',');
if (comma < 0) throw new Error('data URL source is missing its payload separator');
const metadata = source.slice(5, comma);
if (!/;base64$/i.test(metadata)) {
throw new Error('data URL source must use base64 encoding');
}
return source.slice(comma + 1);
}
export function resolveLocalFilePath(source: string): string | null {
if (!source) return null;
if (/^base64:\/\//i.test(source)) return null;
if (/^(?:base64:\/\/|data:)/i.test(source)) return null;
if (/^https?:\/\//i.test(source)) return null;
let filePath = source;
@@ -187,8 +209,9 @@ export async function loadBinarySource(
): Promise<LoadedBinary> {
if (!source) throw new Error(`${resourceName} source is empty`);
if (/^base64:\/\//i.test(source)) {
const bytes = Buffer.from(source.slice(9), 'base64');
const inlinePayload = inlineBase64Payload(source);
if (inlinePayload !== null) {
const bytes = Buffer.from(inlinePayload, 'base64');
if (bytes.length > maxBytes) {
throw new Error(`${resourceName} too large: ${bytes.length} > ${maxBytes}`);
}