@@ -3,6 +3,7 @@ import os from "node:os";
33import path from "node:path" ;
44// Covers message-action media hydration, sandbox path normalization,
55// attachments, and channel/plugin media source aliases.
6+ import { canonicalizeBase64 } from "@openclaw/media-core/base64" ;
67import { createRequireRecord } from "openclaw/plugin-sdk/test-fixtures" ;
78import { afterEach , beforeEach , describe , expect , it , vi } from "vitest" ;
89import { jsonResult } from "../../agents/tools/common.js" ;
@@ -17,12 +18,17 @@ import {
1718 setMessageActionTestPlugin as setTestPlugin ,
1819} from "./message-action-runner.test-helpers.js" ;
1920
21+ const { hydrateAttachmentParamsForAction, normalizeSandboxMediaParams } =
22+ await import ( "./message-action-params.js" ) ;
2023const loadWebMedia = messageActionRunnerMocks . loadWebMedia ;
2124
2225const onePixelPng = Buffer . from (
2326 "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO5m8gAAAABJRU5ErkJggg==" ,
2427 "base64" ,
2528) ;
29+ const onePixelPngBase64 = onePixelPng . toString ( "base64" ) ;
30+ const wrappedOnePixelPngBase64 = onePixelPngBase64 . match ( / .{ 1 , 24 } / g) ?. join ( "\r\n" ) ?? "" ;
31+ const parameterizedPngDataUrl = `data:image/png;charset=utf-8;name=../../ignored.svg;base64,${ wrappedOnePixelPngBase64 } ` ;
2632
2733function firstMockArg (
2834 mock : { mock : { calls : readonly unknown [ ] [ ] } } ,
@@ -99,6 +105,106 @@ describe("runMessageAction media behavior", () => {
99105 beforeEach ( async ( ) => {
100106 await resetMessageActionMediaMocks ( ) ;
101107 } ) ;
108+
109+ it . each ( [ "send" , "sendAttachment" , "reply" , "upload-file" , "setGroupIcon" ] as const ) (
110+ "normalizes parameterized, line-wrapped image data URLs for %s" ,
111+ async ( action ) => {
112+ const args : Record < string , unknown > = { buffer : parameterizedPngDataUrl } ;
113+
114+ await hydrateAttachmentParamsForAction ( {
115+ cfg : { } ,
116+ channel : "imessage" ,
117+ args,
118+ action,
119+ dryRun : true ,
120+ mediaPolicy : { mode : "host" } ,
121+ } ) ;
122+
123+ expect ( args . contentType ) . toBe ( "image/png" ) ;
124+ if ( action === "send" ) {
125+ expect ( args . media ) . toBe ( "buffer://message-send/attachment" ) ;
126+ expect ( args . filename ) . toBe ( "attachment.png" ) ;
127+ } else {
128+ expect ( canonicalizeBase64 ( String ( args . buffer ) ) ) . toBe ( onePixelPngBase64 ) ;
129+ }
130+ expect ( args . filename ) . not . toBe ( "../../ignored.svg" ) ;
131+ } ,
132+ ) ;
133+
134+ it . each ( [ "send" , "sendAttachment" , "reply" , "upload-file" , "setGroupIcon" ] as const ) (
135+ "keeps explicit content type authoritative for %s data URLs" ,
136+ async ( action ) => {
137+ const args : Record < string , unknown > = {
138+ buffer : parameterizedPngDataUrl ,
139+ contentType : "image/jpeg" ,
140+ } ;
141+
142+ await hydrateAttachmentParamsForAction ( {
143+ cfg : { } ,
144+ channel : "imessage" ,
145+ args,
146+ action,
147+ dryRun : true ,
148+ mediaPolicy : { mode : "host" } ,
149+ } ) ;
150+
151+ expect ( args . contentType ) . toBe ( "image/jpeg" ) ;
152+ expect ( args . filename ) . toBe ( "attachment.jpg" ) ;
153+ } ,
154+ ) ;
155+
156+ it . each ( [
157+ [ "duplicate marker" , "image/png;base64;base64" ] ,
158+ [ "marker before another parameter" , "image/png;base64;charset=utf-8" ] ,
159+ [ "missing MIME type" , ";base64" ] ,
160+ [ "metadata newline" , "image/png;name=bad\r\nvalue;base64" ] ,
161+ ] ) ( "rejects a data URL with a %s" , async ( _label , metadata ) => {
162+ await expect (
163+ hydrateAttachmentParamsForAction ( {
164+ cfg : { } ,
165+ channel : "imessage" ,
166+ args : { buffer : `data:${ metadata } ,${ onePixelPngBase64 } ` } ,
167+ action : "send" ,
168+ dryRun : true ,
169+ mediaPolicy : { mode : "host" } ,
170+ } ) ,
171+ ) . rejects . toThrow ( / i n v a l i d b a s e 6 4 / i) ;
172+ } ) ;
173+
174+ it ( "rejects an oversized parameterized data URL before decoding its payload" , async ( ) => {
175+ const fromSpy = vi . spyOn ( Buffer , "from" ) ;
176+ try {
177+ await expect (
178+ hydrateAttachmentParamsForAction ( {
179+ cfg : { agents : { defaults : { mediaMaxMb : 0.00001 } } } ,
180+ channel : "imessage" ,
181+ args : { buffer : parameterizedPngDataUrl } ,
182+ action : "send" ,
183+ dryRun : true ,
184+ mediaPolicy : { mode : "host" } ,
185+ } ) ,
186+ ) . rejects . toThrow ( / t o o l a r g e | l i m i t / i) ;
187+ const base64Calls = ( fromSpy . mock . calls as ReadonlyArray < readonly unknown [ ] > ) . filter (
188+ ( call ) => call [ 1 ] === "base64" ,
189+ ) ;
190+ expect ( base64Calls ) . toHaveLength ( 0 ) ;
191+ } finally {
192+ fromSpy . mockRestore ( ) ;
193+ }
194+ } ) ;
195+
196+ it . each ( [ "media" , "mediaUrl" , "path" , "filePath" ] ) (
197+ "keeps data URLs forbidden in the %s source field" ,
198+ async ( field ) => {
199+ await expect (
200+ normalizeSandboxMediaParams ( {
201+ args : { [ field ] : parameterizedPngDataUrl } ,
202+ mediaPolicy : { mode : "host" } ,
203+ } ) ,
204+ ) . rejects . toThrow ( / d a t a : U R L s a r e n o t s u p p o r t e d f o r m e d i a / i) ;
205+ } ,
206+ ) ;
207+
102208 describe ( "sendAttachment hydration" , ( ) => {
103209 const cfg = {
104210 channels : {
@@ -208,6 +314,25 @@ describe("runMessageAction media behavior", () => {
208314 expect ( options . sandboxValidated ) . not . toBe ( true ) ;
209315 } ) ;
210316
317+ it . each ( [ "sendAttachment" , "upload-file" ] as const ) (
318+ "delivers parameterized wrapped image data URLs through the %s handler" ,
319+ async ( action ) => {
320+ const result = await runMessageAction ( {
321+ cfg,
322+ action,
323+ params : {
324+ channel : "attachmentchat" ,
325+ target : "+15551234567" ,
326+ buffer : parameterizedPngDataUrl ,
327+ } ,
328+ } ) ;
329+
330+ const payload = requireActionPayload ( result ) ;
331+ expect ( payload . contentType ) . toBe ( "image/png" ) ;
332+ expect ( canonicalizeBase64 ( String ( payload . buffer ) ) ) . toBe ( onePixelPngBase64 ) ;
333+ } ,
334+ ) ;
335+
211336 it ( "allows host-local image attachment paths when fs root expansion is enabled" , async ( ) => {
212337 await restoreRealMediaLoader ( ) ;
213338
@@ -478,6 +603,23 @@ describe("runMessageAction media behavior", () => {
478603 expect ( handlerParams . contentType ) . toBe ( "image/png" ) ;
479604 } ) ;
480605
606+ it ( "delivers parameterized wrapped image data URLs through the reply handler" , async ( ) => {
607+ await runMessageAction ( {
608+ cfg,
609+ action : "reply" ,
610+ params : {
611+ channel : "replychat" ,
612+ target : "+15551234567" ,
613+ messageId : "parent-id" ,
614+ buffer : parameterizedPngDataUrl ,
615+ } ,
616+ } ) ;
617+
618+ const handlerParams = firstMockArg ( handleActionMock , "handleAction" ) ;
619+ expect ( handlerParams . contentType ) . toBe ( "image/png" ) ;
620+ expect ( canonicalizeBase64 ( String ( handlerParams . buffer ) ) ) . toBe ( onePixelPngBase64 ) ;
621+ } ) ;
622+
481623 it ( "hydrates buffer and metadata from attachments[] before the reply handler runs" , async ( ) => {
482624 const result = await runMessageAction ( {
483625 cfg,
0 commit comments