Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
fix: support data URLs on mood boards and surface item add errors (#13520)
Stephen Hilderbranda071b2aaabfd
diff --git a/client/src/pages/MoodBoardDetail.jsx b/client/src/pages/MoodBoardDetail.jsxindex b7acdd47a1..a355c931b3 100644--- a/client/src/pages/MoodBoardDetail.jsx+++ b/client/src/pages/MoodBoardDetail.jsx@@ -81,6 +81,13 @@ export default function MoodBoardDetail() {return <MoodBoardEditor key={id} id={id} />;}+function moodBoardAddErrorMessage(err) {+ const details = Array.isArray(err?.context?.details)+ ? err.context.details.map((entry) => entry?.message).filter((message) => typeof message === 'string' && message)+ : [];+ return details.join('; ') || err?.message || 'Failed to add item';+}+function MoodBoardEditor({ id }) {const navigate = useNavigate();const [board, setBoard] = useState(null);@@ -306,10 +313,14 @@ function MoodBoardEditor({ id }) {payload.text = text.trim();}setAdding(true);- const item = await addMoodBoardItem(id, payload, { silent: true }).catch(() => null);+ let addError = null;+ const item = await addMoodBoardItem(id, payload, { silent: true }).catch((err) => {+ addError = err;+ return null;+ });if (!mountedRef.current) return;setAdding(false);- if (!item) { toast.error('Failed to add item'); return; }+ if (!item) { toast.error(moodBoardAddErrorMessage(addError)); return; }setBoard((prev) => (prev ? { ...prev, items: [...(prev.items || []).filter(existing => existing.id !== item.id), item] } : prev));resetAddForm();};@@ -651,8 +662,7 @@ function MoodBoardEditor({ id }) {id="item-image-url"type="text"value={imageUrl}- maxLength={2048}- placeholder="https://… or /data/images/…"+ placeholder="https://…, data:image/…, or /data/images/…"onChange={(e) => setImageUrl(e.target.value)}className="w-full bg-port-bg border border-port-border rounded px-2 py-1.5 text-white text-sm focus:border-port-accent outline-none"/>diff --git a/client/src/pages/MoodBoardDetail.test.jsx b/client/src/pages/MoodBoardDetail.test.jsxindex b72945cf89..e983a9ccbd 100644--- a/client/src/pages/MoodBoardDetail.test.jsx+++ b/client/src/pages/MoodBoardDetail.test.jsx@@ -850,3 +850,28 @@ it('keeps one visible item when the gallery returns an already pinned asset', asexpect(screen.getAllByRole('button', { name: 'Preview image' })).toHaveLength(1);expect(screen.getByLabelText('Item prompt')).toHaveAttribute('maxlength', '2000');});++it('accepts a >2048-char data:image URL without input truncation and surfaces server error messages when pinning fails', async () => {+ const longDataUrl = `data:image/webp;base64,${'A'.repeat(4000)}`;+ const validationError = new Error('Validation failed');+ validationError.context = { details: [{ path: 'imageUrl', message: 'Image data URL exceeds the 12MB limit' }] };+ mockGetMoodBoard.mockResolvedValueOnce({ id: 'a', name: 'Board A', items: [] });+ mockAddMoodBoardItem+ .mockRejectedValueOnce(validationError)+ .mockRejectedValueOnce(new Error('Image data URL is truncated — re-paste the full URL or upload the image'))+ .mockResolvedValueOnce({ id: 'mbi-data', type: 'image', imageUrl: '/data/images/board-1234.webp', caption: null });+ renderPage();+ const input = await screen.findByLabelText('Image URL');+ expect(input).not.toHaveAttribute('maxlength');+ fireEvent.change(input, { target: { value: longDataUrl } });+ await act(async () => { fireEvent.click(screen.getByRole('button', { name: 'Pin to board' })); });+ expect(mockAddMoodBoardItem).toHaveBeenCalledWith('a', { type: 'image', imageUrl: longDataUrl, caption: null, source: null }, { silent: true });+ expect(mockToastError).toHaveBeenCalledWith('Image data URL exceeds the 12MB limit');++ await act(async () => { fireEvent.click(screen.getByRole('button', { name: 'Pin to board' })); });+ expect(mockToastError).toHaveBeenCalledWith('Image data URL is truncated — re-paste the full URL or upload the image');++ await act(async () => { fireEvent.click(screen.getByRole('button', { name: 'Pin to board' })); });+ expect(screen.getByRole('button', { name: 'Preview image' })).toBeInTheDocument();+ expect(input).toHaveValue('');+});diff --git a/server/lib/moodBoardValidation.js b/server/lib/moodBoardValidation.jsindex e3656097d1..5268cbe7eb 100644--- a/server/lib/moodBoardValidation.js+++ b/server/lib/moodBoardValidation.js@@ -37,15 +37,36 @@ export function isVideoItemMediaKey(key) {return /\.[a-z0-9]{2,6}$/i.test(ref);}-// External/pinned image URL. http(s) or a same-origin app path (e.g. a served-// `/data/images/...` URL). Bounded; the UI renders it in an <img>, so no exotic-// schemes. A protocol-relative `//host/...` is rejected: it starts with `/` but-// resolves to an arbitrary external origin, which the leading-slash branch is-// not meant to allow.-const imageUrlSchema = z.string().trim().min(1).max(2048).refine(- (v) => /^https?:\/\//.test(v) || (v.startsWith('/') && !v.startsWith('//')),- 'imageUrl must be an http(s) URL or an absolute app path',-);+// External/pinned image URL: http(s), a same-origin app path (e.g. a served+// `/data/images/...` URL), or a `data:image/<fmt>;base64,...` URI (which+// `localizeImageUrl` decodes and saves into `/data/images/` before the board+// row commits). Standard URLs stay bounded to 2048 chars. Data URLs are bounded+// by the decoded 12MB ceiling (`MAX_IMAGE_DATA_URL_BYTES`), measured after+// stripping whitespace: MIME wrapping is insignificant to the image, and a+// character cap that counted it would reject a max-size paste before the+// decoder's own limit. A protocol-relative `//host/...` is rejected so the+// leading-slash branch never resolves off-origin.+const MAX_HTTP_OR_APP_IMAGE_URL_LEN = 2048;+export const MAX_IMAGE_DATA_URL_BYTES = 12 * 1024 * 1024;+const MAX_IMAGE_DATA_URL_PAYLOAD_LEN = Math.ceil((MAX_IMAGE_DATA_URL_BYTES * 4) / 3);+const DATA_IMAGE_URL_RE = /^data:image\/(?:png|jpe?g|gif|webp);base64,([A-Za-z0-9+/=\s]+)$/i;+const IMAGE_URL_MESSAGE = 'imageUrl must be an http(s) URL, a base64 PNG, JPEG, GIF, or WebP data URL, or an absolute app path';++function imageUrlIssue(value) {+ if (/^https?:\/\//.test(value) || (value.startsWith('/') && !value.startsWith('//'))) {+ return value.length <= MAX_HTTP_OR_APP_IMAGE_URL_LEN ? null : 'imageUrl must be at most 2048 characters';+ }+ const match = DATA_IMAGE_URL_RE.exec(value);+ if (!match) return IMAGE_URL_MESSAGE;+ const payload = match[1].replace(/\s+/g, '');+ if (payload.length > MAX_IMAGE_DATA_URL_PAYLOAD_LEN) return 'Image data URL exceeds the 12MB limit';+ return null;+}++const imageUrlSchema = z.string().trim().min(1).superRefine((value, ctx) => {+ const message = imageUrlIssue(value);+ if (message) ctx.addIssue({ code: z.ZodIssueCode.custom, message });+});const captionSchema = z.string().max(2000).nullable().optional();const sourceSchema = z.string().max(2048).nullable().optional();diff --git a/server/lib/moodBoardValidation.test.js b/server/lib/moodBoardValidation.test.jsindex 1dae74eefc..1cb1db1892 100644--- a/server/lib/moodBoardValidation.test.js+++ b/server/lib/moodBoardValidation.test.js@@ -74,6 +74,37 @@ describe('moodBoardItemCreateSchema', () => {it('accepts an image item with an app-path imageUrl', () => {expect(moodBoardItemCreateSchema.safeParse({ type: 'image', imageUrl: '/data/images/a.png' }).success).toBe(true);});+ it('accepts an image item with a >2048-char data:image/*;base64 imageUrl', () => {+ const dataUrl = `data:image/webp;base64,${'A'.repeat(4000)}`;+ expect(moodBoardItemCreateSchema.safeParse({ type: 'image', imageUrl: dataUrl }).success).toBe(true);+ });+ it('still rejects an http(s) imageUrl longer than 2048 characters', () => {+ const longHttpUrl = `https://example.com/${'a'.repeat(2040)}.png`;+ expect(moodBoardItemCreateSchema.safeParse({ type: 'image', imageUrl: longHttpUrl }).success).toBe(false);+ });+ it('rejects non-raster or non-base64 data: URLs', () => {+ expect(moodBoardItemCreateSchema.safeParse({ type: 'image', imageUrl: 'data:image/svg+xml;base64,PHN2Zz4=' }).success).toBe(false);+ expect(moodBoardItemCreateSchema.safeParse({ type: 'image', imageUrl: 'data:text/html;base64,PGh0bWw+' }).success).toBe(false);+ expect(moodBoardItemCreateSchema.safeParse({ type: 'image', imageUrl: 'data:image/png,iVBORw0KGgo=' }).success).toBe(false);+ expect(moodBoardItemCreateSchema.safeParse({ type: 'image', imageUrl: 'data:image/webp;charset=utf-8;base64,AAAA' }).success).toBe(false);+ });+ it('accepts whitespace inside a data URL and rejects a payload past the 12MB ceiling', () => {+ const payloadLen = Math.ceil((12 * 1024 * 1024 * 4) / 3);+ const payload = 'A'.repeat(payloadLen);+ let wrapped = '';+ let offset = 0;+ for (let breaks = 0; breaks < 21; breaks += 1) {+ wrapped += `${payload.slice(offset, offset + 76)}\r\n`;+ offset += 76;+ }+ wrapped += payload.slice(offset);+ const dataUrl = `data:image/webp;base64,${wrapped}`;+ expect(dataUrl.length).toBeGreaterThan(payloadLen + 64);+ expect(moodBoardItemCreateSchema.safeParse({ type: 'image', imageUrl: dataUrl }).success).toBe(true);+ const over = moodBoardItemCreateSchema.safeParse({ type: 'image', imageUrl: `data:image/webp;base64,${payload}AAAA` });+ expect(over.success).toBe(false);+ expect(over.error.issues[0].message).toBe('Image data URL exceeds the 12MB limit');+ });it('accepts an image item with a mediaKey', () => {expect(moodBoardItemCreateSchema.safeParse({ type: 'image', mediaKey: 'image:a.png' }).success).toBe(true);});diff --git a/server/services/moodBoard/localize.js b/server/services/moodBoard/localize.jsindex 95c6210e94..cf6d6ecac1 100644--- a/server/services/moodBoard/localize.js+++ b/server/services/moodBoard/localize.js@@ -16,6 +16,7 @@ import { createHash } from 'crypto';import { join } from 'path';import { ServerError } from '../../lib/errorHandler.js';import { PATHS, ensureDir, detectImageFormat, writeFileGuarded } from '../../lib/fileUtils.js';+import { MAX_IMAGE_DATA_URL_BYTES } from '../../lib/moodBoardValidation.js';import { fetchPublicBinary } from '../../lib/safeUrlFetch.js';import { withBackupAssetPublication } from '../../lib/backupSnapshotBoundary.js';import { emitRecordUpdated } from '../sharing/recordEvents.js';@@ -23,11 +24,58 @@ import { externalImageItems, isExternalImageUrl } from './logic.js';import * as store from './db.js';const IMAGE_TIMEOUT_MS = 20000;-const MAX_IMAGE_BYTES = 12 * 1024 * 1024;+const MAX_IMAGE_BYTES = MAX_IMAGE_DATA_URL_BYTES;const DOWNLOAD_CONCURRENCY = 3;const UA = 'Mozilla/5.0 (compatible; PortOS Mood Board/1.0; +https://github.com/atomantic/PortOS)';const IMAGE_HEADERS = { 'User-Agent': UA, Accept: 'image/*' };+/**+ * Decode a `data:image/<fmt>;base64,...` URI and persist it into `data/images/`+ * so the board stores a compact local `/data/images/board-<sha1>.<ext>` path+ * instead of inline base64 in JSONB. Throws a 400 ServerError when the payload+ * is malformed, truncated, oversized, or not a supported image format.+ */+async function saveDataImageUrl(dataUrl) {+ const match = /^data:image\/(?:png|jpe?g|gif|webp);base64,([A-Za-z0-9+/=\s]+)$/i.exec(dataUrl);+ if (!match) {+ throw new ServerError('Invalid image data URL (expected base64 PNG, JPEG, WebP, or GIF)', {+ status: 400,+ code: 'INVALID_IMAGE_DATA_URL',+ });+ }+ const base64 = match[1].replace(/\s+/g, '');+ if (!base64 || base64.length % 4 === 1) {+ throw new ServerError('Image data URL is truncated — re-paste the full URL or upload the image', {+ status: 400,+ code: 'INVALID_IMAGE_DATA_URL',+ });+ }+ const buffer = Buffer.from(base64, 'base64');+ if (buffer.length > MAX_IMAGE_BYTES) {+ throw new ServerError('Image data URL exceeds the 12MB limit', {+ status: 400,+ code: 'FILE_TOO_LARGE',+ });+ }+ const fmt = detectImageFormat(buffer);+ if (!fmt) {+ throw new ServerError('Invalid image data URL (expected PNG, JPEG, WebP, or GIF)', {+ status: 400,+ code: 'INVALID_IMAGE_DATA_URL',+ });+ }+ if (fmt.format === 'webp' && buffer.length < buffer.readUInt32LE(4) + 8) {+ throw new ServerError('Image data URL is truncated — re-paste the full URL or upload the image', {+ status: 400,+ code: 'INVALID_IMAGE_DATA_URL',+ });+ }+ const filename = `board-${createHash('sha1').update(buffer).digest('hex').slice(0, 16)}${fmt.ext}`;+ await ensureDir(PATHS.images);+ await withBackupAssetPublication(() => writeFileGuarded(join(PATHS.images, filename), buffer));+ return `/data/images/${filename}`;+}+/*** Download one remote image into the gallery. Returns its served path* (`/data/images/<file>`) or null on any failure (unreachable, blocked host,@@ -49,8 +97,11 @@ async function downloadExternalImage(url) {return `/data/images/${filename}`;}-/** Re-host a single item's image input for an add/update; falls back to the original URL. */+/** Re-host a single item's image input for an add/update; falls back to the original URL for http(s). */export async function localizeImageUrl(imageUrl) {+ if (typeof imageUrl === 'string' && /^data:image\//i.test(imageUrl.trim())) {+ return saveDataImageUrl(imageUrl.trim());+ }if (!isExternalImageUrl(imageUrl)) return imageUrl;return (await downloadExternalImage(imageUrl)) || imageUrl;}diff --git a/server/services/moodBoard/localize.test.js b/server/services/moodBoard/localize.test.jsindex 1f4299223c..89eb04e439 100644--- a/server/services/moodBoard/localize.test.js+++ b/server/services/moodBoard/localize.test.js@@ -1,15 +1,25 @@import { describe, it, expect, vi, beforeEach } from 'vitest';const net = { fetchPublicBinary: vi.fn() };+const writeFileGuarded = vi.fn(async () => {});vi.mock('../../lib/safeUrlFetch.js', () => net);vi.mock('./db.js', () => ({ getBoard: vi.fn() }));vi.mock('../sharing/recordEvents.js', () => ({ emitRecordUpdated: vi.fn() }));vi.mock('../../lib/backupSnapshotBoundary.js', () => ({ withBackupAssetPublication: (fn) => fn() }));+vi.mock('../../lib/fileUtils.js', async (importOriginal) => ({+ ...(await importOriginal()),+ PATHS: { images: '/mock/images' },+ ensureDir: vi.fn(async () => {}),+ writeFileGuarded: (...args) => writeFileGuarded(...args),+}));const { localizeImageUrl } = await import('./localize.js');describe('localizeImageUrl', () => {- beforeEach(() => net.fetchPublicBinary.mockReset());+ beforeEach(() => {+ net.fetchPublicBinary.mockReset();+ writeFileGuarded.mockClear();+ });it('refuses private and loopback hosts, since item URLs also arrive through peer sync', async () => {net.fetchPublicBinary.mockResolvedValue(null);@@ -17,4 +27,25 @@ describe('localizeImageUrl', () => {expect(net.fetchPublicBinary).toHaveBeenCalledTimes(1);expect(net.fetchPublicBinary.mock.calls[0][1]).toMatchObject({ blockPrivate: true, throwOnUnsafe: false });});++ it('decodes a valid data:image/*;base64 URL into a local /data/images/board-<sha1>.<ext> file', async () => {+ // Minimal 12-byte RIFF WEBP container (RIFF size = 4 bytes following byte 7).+ const webp = Buffer.from([0x52, 0x49, 0x46, 0x46, 0x04, 0x00, 0x00, 0x00, 0x57, 0x45, 0x42, 0x50]);+ const dataUrl = `data:image/webp;base64,${webp.toString('base64')}`;+ const result = await localizeImageUrl(dataUrl);+ expect(result).toMatch(/^\/data\/images\/board-[0-9a-f]{16}\.webp$/);+ expect(writeFileGuarded).toHaveBeenCalledTimes(1);+ expect(writeFileGuarded.mock.calls[0][1]).toEqual(webp);+ expect(net.fetchPublicBinary).not.toHaveBeenCalled();+ });++ it('rejects a truncated WebP or malformed data:image URL with a 400 ServerError instead of storing raw data:', async () => {+ // RIFF header declaring 0x2114 bytes (8468 bytes) but truncated after 12 bytes.+ const truncatedWebp = Buffer.from([0x52, 0x49, 0x46, 0x46, 0x14, 0x21, 0x00, 0x00, 0x57, 0x45, 0x42, 0x50]);+ await expect(localizeImageUrl(`data:image/webp;base64,${truncatedWebp.toString('base64')}`))+ .rejects.toMatchObject({ status: 400, code: 'INVALID_IMAGE_DATA_URL' });+ await expect(localizeImageUrl(`data:image/png;base64,${Buffer.from('not-an-image').toString('base64')}`))+ .rejects.toMatchObject({ status: 400, code: 'INVALID_IMAGE_DATA_URL' });+ expect(writeFileGuarded).not.toHaveBeenCalled();+ });});