diff --git a/packages/onebot/src/event-converter/element-codecs.ts b/packages/onebot/src/event-converter/element-codecs.ts index df52d57a..7d8a954f 100644 --- a/packages/onebot/src/event-converter/element-codecs.ts +++ b/packages/onebot/src/event-converter/element-codecs.ts @@ -118,6 +118,10 @@ export function marketFaceElement(emojiId: string, data: Record * bytes, a remote url, or a filesystem path with a separator); otherwise, if a * real http(s) `url` accompanies it, prefer that. (issue #155) */ +export function isInlineMediaSource(value: string): boolean { + return /^(base64:\/\/|data:)/i.test(value); +} + export function pickMediaSource(data: Record): string { const file = String(data.file ?? '').trim(); const url = String(data.url ?? '').trim(); @@ -125,7 +129,7 @@ export function pickMediaSource(data: Record): string { if (!file) return fallback; // `file` is itself loadable: inline bytes, a remote url, or a path (anything // carrying a `/` or `\` separator, incl. file:// and absolute/relative paths). - if (/^(base64:\/\/|data:|https?:\/\/|file:\/\/)/i.test(file) || /[\\/]/.test(file)) return file; + if (isInlineMediaSource(file) || /^(https?:\/\/|file:\/\/)/i.test(file) || /[\\/]/.test(file)) return file; // `file` is a bare token (QQ-internal id) — fall back to a real url if present. if (/^https?:\/\//i.test(url)) return url; return fallback; @@ -260,17 +264,20 @@ export const ELEMENT_CODECS = { // pictures use `imageUrl`. get_forward_msg must accept either — // otherwise a just-sent merge comes back with empty file/url (#441). const source = (element.imageUrl || element.url || '').trim(); - const resolved = source && !element.imageUrl + const inline = isInlineMediaSource(source); + const resolved = !inline && source && !element.imageUrl ? { ...element, imageUrl: source } : element; - const url = ctx.imageUrlResolver - ? await ctx.imageUrlResolver(resolved, ctx.isGroup) - : source; + const url = inline + ? '' + : ctx.imageUrlResolver + ? await ctx.imageUrlResolver(resolved, ctx.isGroup) + : source; const data: JsonObject = { url, file: element.fileId || (element.md5Hex ? `${element.md5Hex.toLowerCase()}.png` : '') - || source, + || (inline ? '' : source), sub_type: element.subType ?? 0, summary: element.summary ?? '', }; @@ -296,7 +303,10 @@ export const ELEMENT_CODECS = { record: { async toSegment(element, ctx) { - const url = ctx.mediaUrlResolver ? await ctx.mediaUrlResolver(element, ctx.isGroup, ctx.sessionId) : (element.url ?? ''); + const rawUrl = ctx.mediaUrlResolver + ? await ctx.mediaUrlResolver(element, ctx.isGroup, ctx.sessionId) + : (element.url ?? ''); + const url = isInlineMediaSource(rawUrl) ? '' : rawUrl; const data: JsonObject = { file: element.fileName ?? element.fileId ?? '', url, @@ -316,7 +326,10 @@ export const ELEMENT_CODECS = { video: { async toSegment(element, ctx) { - const url = ctx.mediaUrlResolver ? await ctx.mediaUrlResolver(element, ctx.isGroup, ctx.sessionId) : (element.url ?? ''); + const rawUrl = ctx.mediaUrlResolver + ? await ctx.mediaUrlResolver(element, ctx.isGroup, ctx.sessionId) + : (element.url ?? ''); + const url = isInlineMediaSource(rawUrl) ? '' : rawUrl; const data: JsonObject = { file: element.fileName ?? element.fileId ?? '', url, diff --git a/packages/onebot/src/media-store.ts b/packages/onebot/src/media-store.ts index 79ccd5aa..e46d91c1 100644 --- a/packages/onebot/src/media-store.ts +++ b/packages/onebot/src/media-store.ts @@ -1,4 +1,5 @@ import type { MessageElement } from '@snowluma/protocol/events'; +import { createHash } from 'node:crypto'; import { DatabaseSync, type StatementSync } from 'node:sqlite'; import { openSqliteDb } from './sqlite-open'; @@ -59,6 +60,11 @@ const TYPE_IMAGE = 'image'; const TYPE_RECORD = 'record'; const TYPE_VIDEO = 'video'; const DEFAULT_KEEP_ENTRIES = 4096; +/** SQLite keys / aliases longer than this are folded to a short digest. */ +export const MEDIA_KEY_MAX_CHARS = 2048; +/** Persisted JSON must stay well under a page-overflow storm. */ +export const MEDIA_DATA_MAX_CHARS = 64 * 1024; +const INLINE_MEDIA_SOURCE = /^(base64:\/\/|data:)/i; export class MediaStore { private readonly db: DatabaseSync; @@ -78,6 +84,7 @@ export class MediaStore { this.db = openSqliteDb(dbPath); this.initSchema(); + this.purgeOversizedRows(); this.upsertEntry = this.db.prepare( `INSERT INTO media_entries (type, primary_key, data, last_seen) @@ -157,21 +164,21 @@ export class MediaStore { } updateImageUrl(key: string, url: string): void { - if (!url) return; + if (!url || mustFoldMediaKey(url)) return; const cached = this.findImage(key); if (!cached || cached.url === url) return; this.rememberImage({ ...cached, url }); } updateRecordUrl(key: string, url: string): void { - if (!url) return; + if (!url || mustFoldMediaKey(url)) return; const cached = this.findRecord(key); if (!cached || cached.url === url) return; this.rememberRecord({ ...cached, url }); } updateVideoUrl(key: string, url: string): void { - if (!url) return; + if (!url || mustFoldMediaKey(url)) return; const cached = this.findVideo(key); if (!cached || cached.url === url) return; this.rememberVideo({ ...cached, url }); @@ -209,13 +216,29 @@ export class MediaStore { `); } - private upsertWithAliases( + private purgeOversizedRows(): void { + // Older builds could persist a whole inline payload as the key / JSON. + // Drop those rows on open so later eviction does not walk overflow pages. + this.db.exec(` + DELETE FROM media_keys + WHERE length(key) > ${MEDIA_KEY_MAX_CHARS} + OR length(primary_key) > ${MEDIA_KEY_MAX_CHARS}; + DELETE FROM media_entries + WHERE length(primary_key) > ${MEDIA_KEY_MAX_CHARS} + OR length(data) > ${MEDIA_DATA_MAX_CHARS}; + DELETE FROM media_keys + WHERE primary_key NOT IN (SELECT primary_key FROM media_entries); + `); + } + + private upsertWithAliases( type: string, primaryKey: string, info: T, aliases: (string | undefined)[], ): void { - const data = JSON.stringify(info); + const data = JSON.stringify(persistableMediaInfo(info)); + if (data.length > MEDIA_DATA_MAX_CHARS) return; const lastSeen = Math.floor(Date.now() / 1000); // Run the writes in a transaction so concurrent readers always see a @@ -226,9 +249,10 @@ export class MediaStore { const seen = new Set(); for (const raw of aliases) { if (!raw) continue; - if (seen.has(raw)) continue; - seen.add(raw); - this.upsertKey.run(type, raw, primaryKey); + const key = foldMediaKey(raw); + if (!key || seen.has(key)) continue; + seen.add(key); + this.upsertKey.run(type, key, primaryKey); } this.db.exec('COMMIT'); } catch (err) { @@ -236,24 +260,26 @@ export class MediaStore { throw err; } - // Eager eviction. The DELETE is a no-op when row count <= cap so - // there is no measurable cost while we are below the limit. + // Eager eviction. Skip the DELETE entirely while we are under the cap. this.evictOldEntries(type); } private findByAnyKey(type: string, key: string): T | null { - if (!key) return null; - const row = this.findEntryByKey.get(type, key) as { data: string } | undefined; + const lookup = foldMediaKey(key); + if (!lookup) return null; + const row = this.findEntryByKey.get(type, lookup) as { data: string } | undefined; if (row?.data) return safeParse(row.data); // Fall back to looking up by primary_key directly so callers that pass // the canonical identifier still hit (e.g. when the alias index is yet // to be built for that key in this session). - const fallback = this.findEntryByPrimary.get(type, key) as { data: string } | undefined; + const fallback = this.findEntryByPrimary.get(type, lookup) as { data: string } | undefined; return fallback?.data ? safeParse(fallback.data) : null; } private evictOldEntries(type: string): void { try { + const row = this.countByType.get(type) as { n: number } | undefined; + if ((row?.n ?? 0) <= this.maxEntriesPerType) return; this.evictByType.run(type, type, this.maxEntriesPerType); this.purgeOrphanKeys.run(type, type); } catch { @@ -262,9 +288,32 @@ export class MediaStore { } } +function mustFoldMediaKey(value: string): boolean { + return value.length > MEDIA_KEY_MAX_CHARS || INLINE_MEDIA_SOURCE.test(value); +} + +function foldMediaKey(value: string): string { + if (!value) return ''; + if (!mustFoldMediaKey(value)) return value; + return `inline:${createHash('sha256').update(value).digest('hex')}`; +} + +function persistableMediaInfo(info: T): T { + const next = { ...info } as T & Record; + for (const field of ['file', 'fileId', 'fileName'] as const) { + const value = next[field]; + if (typeof value === 'string') next[field] = foldMediaKey(value); + } + for (const field of ['url', 'imageUrl'] as const) { + const value = next[field]; + if (typeof value === 'string' && mustFoldMediaKey(value)) next[field] = ''; + } + return next; +} + function pickPrimary(candidates: (string | undefined)[]): string { for (const c of candidates) { - if (c && c.length > 0) return c; + if (c && c.length > 0) return foldMediaKey(c); } return ''; } diff --git a/packages/onebot/tests/event-converter.test.ts b/packages/onebot/tests/event-converter.test.ts index 2da2819b..2ce1d929 100644 --- a/packages/onebot/tests/event-converter.test.ts +++ b/packages/onebot/tests/event-converter.test.ts @@ -436,6 +436,30 @@ describe('convertEvent — message elements', () => { }); }); + it('image: does not emit inline base64 as file/url (#463)', async () => { + const source = `base64://${'A'.repeat(32)}`; + const seg = await segment({ type: 'image', url: source }); + expect(seg).toEqual({ + type: 'image', + data: { + url: '', + file: '', + sub_type: 0, + summary: '', + }, + }); + }); + + it('image: keeps fileId when the only source is inline (#463)', async () => { + const seg = await segment({ + type: 'image', + fileId: 'fid', + url: `base64://${'A'.repeat(32)}`, + }); + expect((seg.data as Record).file).toBe('fid'); + expect((seg.data as Record).url).toBe(''); + }); + it('image: resolver sees the upload source when only url is present (#441)', async () => { const source = 'https://cdn.example/bot-built.png'; const seen: string[] = []; diff --git a/packages/onebot/tests/media-store.test.ts b/packages/onebot/tests/media-store.test.ts index 8cd6180f..7865432d 100644 --- a/packages/onebot/tests/media-store.test.ts +++ b/packages/onebot/tests/media-store.test.ts @@ -1,7 +1,8 @@ import { afterEach, beforeEach, describe, expect, it } from 'vitest'; +import { DatabaseSync } from 'node:sqlite'; import fs from 'fs'; import path from 'path'; -import { MediaStore } from '../src/media-store'; +import { MEDIA_DATA_MAX_CHARS, MEDIA_KEY_MAX_CHARS, MediaStore } from '../src/media-store'; import { convertEvent, type ConverterContext } from '../src/event-converter'; import type { GroupMessage, FriendMessage, MessageElement } from '@snowluma/protocol/events'; @@ -205,8 +206,157 @@ describe('MediaStore basic semantics', () => { expect(store.findImage('file-199.png')?.fileSize).toBe(199); store.close(); }); + + it('does not persist inline base64 as a key or JSON payload (#463)', () => { + const store = open('inline-b64'); + const payload = `base64://${'A'.repeat(8192)}`; + store.rememberImage({ + file: payload, + url: payload, + fileSize: 99, + fileName: '', + subType: 0, + summary: '', + imageUrl: payload, + isGroup: true, + sessionId: GROUP_ID, + md5Hex: 'aa'.repeat(16), + width: 800, + height: 600, + }); + + const found = store.findImage(payload); + expect(found).not.toBeNull(); + expect(found!.fileSize).toBe(99); + expect(found!.md5Hex).toBe('aa'.repeat(16)); + expect(found!.url).toBe(''); + expect(found!.imageUrl).toBe(''); + expect(found!.file.startsWith('inline:')).toBe(true); + expect(found!.file.includes('base64://')).toBe(false); + + store.close(); + const rows = inspectMediaDb(dbs[dbs.length - 1]!); + expect(rows.longestKey).toBeLessThanOrEqual(MEDIA_KEY_MAX_CHARS); + expect(rows.longestData).toBeLessThanOrEqual(MEDIA_DATA_MAX_CHARS); + expect(rows.longestKey).toBeLessThan(100); + expect(rows.concatenated).not.toContain('base64://'); + expect(rows.concatenated).not.toContain('A'.repeat(64)); + }); + + it('folds oversized non-inline keys the same way', () => { + const store = open('long-url'); + const url = `https://cdn.example/${'a'.repeat(3000)}.png`; + store.rememberImage({ + file: 'short.png', + url, + fileSize: 12, + fileName: 'short.png', + subType: 0, + summary: '', + imageUrl: url, + isGroup: true, + sessionId: GROUP_ID, + }); + + expect(store.findImage('short.png')?.fileSize).toBe(12); + expect(store.findImage(url)?.fileSize).toBe(12); + expect(store.findImage('short.png')?.url).toBe(''); + store.close(); + const rows = inspectMediaDb(dbs[dbs.length - 1]!); + expect(rows.longestKey).toBeLessThanOrEqual(MEDIA_KEY_MAX_CHARS); + expect(rows.concatenated).not.toContain('a'.repeat(64)); + }); + + it('ignores an inline URL refresh so eviction cannot be re-poisoned', () => { + const store = open('no-inline-refresh'); + store.rememberImage({ + file: 'abc.png', + url: 'https://example.com/abc.png', + fileSize: 7, + fileName: 'abc.png', + subType: 0, + summary: '', + imageUrl: '', + isGroup: true, + sessionId: GROUP_ID, + }); + store.updateImageUrl('abc.png', `base64://${'B'.repeat(100)}`); + expect(store.findImage('abc.png')?.url).toBe('https://example.com/abc.png'); + store.close(); + }); + + it('drops oversized rows left by older builds on open', () => { + const dbPath = tempDbPath('legacy-overflow'); + dbs.push(dbPath); + fs.mkdirSync(path.dirname(dbPath), { recursive: true }); + const raw = new DatabaseSync(dbPath); + raw.exec(` + CREATE TABLE media_entries ( + type TEXT NOT NULL, + primary_key TEXT NOT NULL, + data TEXT NOT NULL, + last_seen INTEGER NOT NULL, + PRIMARY KEY (type, primary_key) + ); + CREATE TABLE media_keys ( + type TEXT NOT NULL, + key TEXT NOT NULL, + primary_key TEXT NOT NULL, + PRIMARY KEY (type, key) + ); + `); + const poisonKey = 'k'.repeat(MEDIA_KEY_MAX_CHARS + 8); + raw.prepare( + `INSERT INTO media_entries (type, primary_key, data, last_seen) VALUES ('image', ?, ?, 1)`, + ).run(poisonKey, '{"file":"poison"}'); + raw.prepare( + `INSERT INTO media_keys (type, key, primary_key) VALUES ('image', ?, ?)`, + ).run(poisonKey, poisonKey); + raw.close(); + + const store = new MediaStore(dbPath); + expect(store.findImage(poisonKey)).toBeNull(); + expect(store.size().images).toBe(0); + store.close(); + const rows = inspectMediaDb(dbPath); + expect(rows.entryCount).toBe(0); + expect(rows.keyCount).toBe(0); + }); }); +function inspectMediaDb(dbPath: string): { + longestKey: number; + longestData: number; + concatenated: string; + entryCount: number; + keyCount: number; +} { + const db = new DatabaseSync(dbPath); + const entries = db.prepare( + `SELECT primary_key, data FROM media_entries`, + ).all() as Array<{ primary_key: string; data: string }>; + const keys = db.prepare( + `SELECT key, primary_key FROM media_keys`, + ).all() as Array<{ key: string; primary_key: string }>; + db.close(); + const keyLens = [ + ...entries.map((row) => row.primary_key.length), + ...keys.map((row) => row.key.length), + ...keys.map((row) => row.primary_key.length), + 0, + ]; + return { + longestKey: Math.max(...keyLens), + longestData: Math.max(0, ...entries.map((row) => row.data.length)), + concatenated: [ + ...entries.map((row) => row.primary_key + row.data), + ...keys.map((row) => row.key + row.primary_key), + ].join('\n'), + entryCount: entries.length, + keyCount: keys.length, + }; +} + describe('convertEvent media segment sink → MediaStore wiring', () => { const dbs: string[] = [];