@@ -7,6 +7,7 @@ import officialExternalPluginCatalog from "../../scripts/lib/official-external-p
77import officialExternalProviderCatalog from "../../scripts/lib/official-external-provider-catalog.json" with { type : "json" } ;
88import { MANIFEST_KEY } from "../compat/legacy-names.js" ;
99import { normalizeClawHubSha256Integrity } from "../infra/clawhub.js" ;
10+ import { readResponseWithLimit } from "../infra/http-body.js" ;
1011import { isRecord } from "../utils.js" ;
1112import type {
1213 PluginManifestChannelConfig ,
@@ -508,91 +509,25 @@ function parseHostedCatalogContentLength(raw: string | null, maxBytes: number):
508509 }
509510}
510511
511- function hasStreamingResponseBody (
512- response : Response ,
513- ) : response is Response & { body : ReadableStream < Uint8Array > } {
514- return Boolean (
515- response . body && typeof ( response . body as { getReader ?: unknown } ) . getReader === "function" ,
516- ) ;
517- }
518-
519- async function readHostedCatalogChunkWithTimeout (
520- reader : ReadableStreamDefaultReader < Uint8Array > ,
521- chunkTimeoutMs : number ,
522- ) : Promise < Awaited < ReturnType < typeof reader . read > > > {
523- let timeoutId : ReturnType < typeof setTimeout > | undefined ;
524- let timedOut = false ;
525- return await new Promise ( ( resolve , reject ) => {
526- const clear = ( ) => {
527- if ( timeoutId !== undefined ) {
528- clearTimeout ( timeoutId ) ;
529- timeoutId = undefined ;
530- }
531- } ;
532- timeoutId = setTimeout ( ( ) => {
533- timedOut = true ;
534- clear ( ) ;
535- void reader . cancel ( ) . catch ( ( ) => undefined ) ;
536- reject ( new Error ( `hosted catalog feed read timed out after ${ chunkTimeoutMs } ms` ) ) ;
537- } , chunkTimeoutMs ) ;
538- void reader . read ( ) . then (
539- ( result ) => {
540- clear ( ) ;
541- if ( ! timedOut ) {
542- resolve ( result ) ;
543- }
544- } ,
545- ( err : unknown ) => {
546- clear ( ) ;
547- if ( ! timedOut ) {
548- reject ( err instanceof Error ? err : new Error ( String ( err ) ) ) ;
549- }
550- } ,
551- ) ;
552- } ) ;
553- }
554-
555512async function readHostedCatalogResponseText ( params : {
556513 response : Response ;
557514 maxBytes : number ;
558515 chunkTimeoutMs : number ;
559516} ) : Promise < string > {
560517 parseHostedCatalogContentLength ( params . response . headers . get ( "content-length" ) , params . maxBytes ) ;
561- if ( ! hasStreamingResponseBody ( params . response ) ) {
562- const text = await params . response . text ( ) ;
563- if ( new TextEncoder ( ) . encode ( text ) . byteLength > params . maxBytes ) {
564- throw new Error ( `hosted catalog feed exceeds ${ params . maxBytes } bytes` ) ;
565- }
566- return text ;
567- }
568- const reader = params . response . body . getReader ( ) ;
569- const chunks : Uint8Array [ ] = [ ] ;
570- let totalBytes = 0 ;
571- try {
572- while ( true ) {
573- const chunk = await readHostedCatalogChunkWithTimeout ( reader , params . chunkTimeoutMs ) ;
574- if ( chunk . done ) {
575- break ;
576- }
577- totalBytes += chunk . value . byteLength ;
578- if ( totalBytes > params . maxBytes ) {
579- throw new Error ( `hosted catalog feed exceeds ${ params . maxBytes } bytes` ) ;
580- }
581- chunks . push ( chunk . value ) ;
582- }
583- } catch ( err ) {
584- await reader . cancel ( ) . catch ( ( ) => undefined ) ;
585- throw err ;
586- } finally {
587- reader . releaseLock ( ) ;
588- }
589- const body = new Uint8Array ( totalBytes ) ;
590- let offset = 0 ;
591- for ( const chunk of chunks ) {
592- body . set ( chunk , offset ) ;
593- offset += chunk . byteLength ;
594- }
595- return new TextDecoder ( ) . decode ( body ) ;
518+ const streamless = ! params . response . body || typeof params . response . body . getReader !== "function" ;
519+ // Hosted remote feeds are untrusted input, so fail closed when Fetch cannot
520+ // provide a streaming body instead of trusting Content-Length before read.
521+ if ( streamless ) {
522+ throw new Error ( "hosted catalog feed streaming response body unavailable" ) ;
523+ }
524+ const buffer = await readResponseWithLimit ( params . response , params . maxBytes , {
525+ chunkTimeoutMs : params . chunkTimeoutMs ,
526+ onOverflow : ( { maxBytes } ) => new Error ( `hosted catalog feed exceeds ${ maxBytes } bytes` ) ,
527+ onIdleTimeout : ( { chunkTimeoutMs } ) =>
528+ new Error ( `hosted catalog feed read timed out after ${ chunkTimeoutMs } ms` ) ,
529+ } ) ;
530+ return new TextDecoder ( ) . decode ( buffer ) ;
596531}
597532
598533function bundledOfficialExternalPluginCatalogEntries ( ) : OfficialExternalPluginCatalogEntry [ ] {
0 commit comments