@@ -13,7 +13,9 @@ import {
1313} from "../test-support/install-test-mocks.js" ;
1414import { createCanonicalFixtureSkill } from "../test-support/test-helpers.js" ;
1515import type { SkillEntry , SkillInstallSpec } from "../types.js" ;
16- import { installDownloadSpec } from "./install-download.js" ;
16+ import { installDownloadSpec , installDownloadTesting } from "./install-download.js" ;
17+
18+ const MAX_SKILL_DOWNLOAD_BYTES = 256 * 1024 * 1024 ;
1719
1820vi . mock ( "../../process/exec.js" , ( ) => ( {
1921 runCommandWithTimeout : ( ...args : unknown [ ] ) => runCommandWithTimeoutMock ( ...args ) ,
@@ -90,6 +92,7 @@ function mockArchiveResponse(buffer: Uint8Array): void {
9092 ok : true ,
9193 status : 200 ,
9294 statusText : "OK" ,
95+ headers : new Headers ( ) ,
9396 body : Readable . from ( [ Buffer . from ( buffer ) ] ) ,
9497 } ,
9598 release : async ( ) => undefined ,
@@ -212,6 +215,132 @@ describe("installDownloadSpec extraction safety", () => {
212215 ) . toBe ( "payload" ) ;
213216 } ) ;
214217
218+ it ( "rejects downloads whose advertised size exceeds the skill download limit" , async ( ) => {
219+ const response = new Response ( "payload" , {
220+ status : 200 ,
221+ headers : { "content-length" : String ( MAX_SKILL_DOWNLOAD_BYTES + 1 ) } ,
222+ } ) ;
223+ const cancel = vi . spyOn ( response . body ! , "cancel" ) . mockResolvedValue ( undefined ) ;
224+ fetchWithSsrFGuardMock . mockResolvedValue ( {
225+ response,
226+ release : async ( ) => undefined ,
227+ } ) ;
228+ const entry = buildEntry ( "oversized-download-header" ) ;
229+ const toolsRoot = resolveSkillToolsRootDir ( entry ) ;
230+
231+ const result = await installDownloadSpec ( {
232+ entry,
233+ spec : {
234+ kind : "download" ,
235+ id : "dl" ,
236+ url : "https://example.invalid/payload.bin" ,
237+ extract : false ,
238+ targetDir : "runtime" ,
239+ } ,
240+ timeoutMs : 30_000 ,
241+ } ) ;
242+
243+ expect ( result ) . toMatchObject ( {
244+ ok : false ,
245+ stderr : `download too large: ${ MAX_SKILL_DOWNLOAD_BYTES + 1 } bytes (limit: ${ MAX_SKILL_DOWNLOAD_BYTES } bytes)` ,
246+ } ) ;
247+ expect ( cancel ) . toHaveBeenCalledOnce ( ) ;
248+ await expect ( fileExists ( path . join ( toolsRoot , "runtime" , "payload.bin" ) ) ) . resolves . toBe ( false ) ;
249+ await expect ( fs . readdir ( path . join ( toolsRoot , ".openclaw-download-staging" ) ) ) . resolves . toEqual (
250+ [ ] ,
251+ ) ;
252+ } ) ;
253+
254+ it ( "rejects malformed advertised sizes before reading the response body" , async ( ) => {
255+ const response = new Response ( "payload" , {
256+ status : 200 ,
257+ headers : { "content-length" : "1e9" } ,
258+ } ) ;
259+ const cancel = vi . spyOn ( response . body ! , "cancel" ) . mockResolvedValue ( undefined ) ;
260+ fetchWithSsrFGuardMock . mockResolvedValue ( {
261+ response,
262+ release : async ( ) => undefined ,
263+ } ) ;
264+ const entry = buildEntry ( "malformed-download-header" ) ;
265+ const toolsRoot = resolveSkillToolsRootDir ( entry ) ;
266+
267+ const result = await installDownloadSpec ( {
268+ entry,
269+ spec : {
270+ kind : "download" ,
271+ id : "dl" ,
272+ url : "https://example.invalid/payload.bin" ,
273+ extract : false ,
274+ targetDir : "runtime" ,
275+ } ,
276+ timeoutMs : 30_000 ,
277+ } ) ;
278+
279+ expect ( result ) . toMatchObject ( {
280+ ok : false ,
281+ stderr : "invalid content-length header: 1e9" ,
282+ } ) ;
283+ expect ( cancel ) . toHaveBeenCalledOnce ( ) ;
284+ await expect ( fileExists ( path . join ( toolsRoot , "runtime" , "payload.bin" ) ) ) . resolves . toBe ( false ) ;
285+ await expect ( fs . readdir ( path . join ( toolsRoot , ".openclaw-download-staging" ) ) ) . resolves . toEqual (
286+ [ ] ,
287+ ) ;
288+ } ) ;
289+
290+ it ( "rejects headerless streamed overflow and removes the partial staging file" , async ( ) => {
291+ const maxBytes = 8 ;
292+ const partialChunk = Buffer . from ( "partial" ) ;
293+ const overflowChunk = Buffer . from ( "!!" ) ;
294+ const chunks = [ partialChunk , overflowChunk ] ;
295+ let chunkIndex = 0 ;
296+ const body = new Readable ( {
297+ read ( ) {
298+ if ( chunkIndex < chunks . length ) {
299+ this . push ( chunks [ chunkIndex ++ ] ) ;
300+ }
301+ } ,
302+ } ) ;
303+ const destroy = vi . spyOn ( body , "destroy" ) ;
304+ fetchWithSsrFGuardMock . mockResolvedValue ( {
305+ response : {
306+ ok : true ,
307+ status : 200 ,
308+ statusText : "OK" ,
309+ headers : new Headers ( ) ,
310+ body,
311+ } ,
312+ release : async ( ) => undefined ,
313+ } ) ;
314+ const entry = buildEntry ( "oversized-download-stream" ) ;
315+ const toolsRoot = resolveSkillToolsRootDir ( entry ) ;
316+
317+ const result = await installDownloadTesting . installDownloadSpecWithLimit (
318+ {
319+ entry,
320+ spec : {
321+ kind : "download" ,
322+ id : "dl" ,
323+ url : "https://example.invalid/payload.bin" ,
324+ extract : false ,
325+ targetDir : "runtime" ,
326+ } ,
327+ timeoutMs : 30_000 ,
328+ } ,
329+ maxBytes ,
330+ ) ;
331+
332+ expect ( result ) . toMatchObject ( {
333+ ok : false ,
334+ stderr : `download too large: ${ partialChunk . byteLength + overflowChunk . byteLength } bytes (limit: ${ maxBytes } bytes)` ,
335+ } ) ;
336+ expect ( destroy ) . toHaveBeenCalled ( ) ;
337+ expect ( body . destroyed ) . toBe ( true ) ;
338+ await expect ( fileExists ( path . join ( toolsRoot , "runtime" , "payload.bin" ) ) ) . resolves . toBe ( false ) ;
339+ await expect ( fs . readdir ( path . join ( toolsRoot , ".openclaw-download-staging" ) ) ) . resolves . toEqual (
340+ [ ] ,
341+ ) ;
342+ } ) ;
343+
215344 it ( "cancels failed download response bodies before returning the error" , async ( ) => {
216345 const { stream, wasCanceled } = createCancelableBody ( ) ;
217346 const release = vi . fn ( async ( ) => undefined ) ;
@@ -256,6 +385,7 @@ describe("installDownloadSpec extraction safety", () => {
256385 ok : true ,
257386 status : 200 ,
258387 statusText : "OK" ,
388+ headers : new Headers ( ) ,
259389 body : Readable . from (
260390 ( async function * ( ) {
261391 yield Buffer . from ( "payload" ) ;
0 commit comments