@@ -30,6 +30,7 @@ export interface WorkerJavaScriptBackendOptions {
3030 maxSourceBytes ?: number ;
3131 maxInputBytes ?: number ;
3232 maxStdinBytes ?: number ;
33+ maxEnvBytes ?: number ;
3334 maxResultBytes ?: number ;
3435 maxLogBytes ?: number ;
3536 maxLogEvents ?: number ;
@@ -69,6 +70,7 @@ type ResolvedWorkerJavaScriptBackendOptions = Required<
6970 | "maxSourceBytes"
7071 | "maxInputBytes"
7172 | "maxStdinBytes"
73+ | "maxEnvBytes"
7274 | "maxResultBytes"
7375 | "maxLogBytes"
7476 | "maxLogEvents"
@@ -149,6 +151,7 @@ export class WorkerJavaScriptBackend implements WorkspaceModuleBackend {
149151 assertPositiveFinite ( options . maxSourceBytes ?? 256 * 1024 , "maxSourceBytes" ) ;
150152 assertPositiveFinite ( options . maxInputBytes ?? 256 * 1024 , "maxInputBytes" ) ;
151153 assertPositiveFinite ( options . maxStdinBytes ?? 256 * 1024 , "maxStdinBytes" ) ;
154+ assertPositiveFinite ( options . maxEnvBytes ?? 1024 * 1024 , "maxEnvBytes" ) ;
152155 assertPositiveFinite ( options . maxResultBytes ?? 1024 * 1024 , "maxResultBytes" ) ;
153156 assertPositiveFinite ( options . maxLogBytes ?? 256 * 1024 , "maxLogBytes" ) ;
154157 assertPositiveInteger ( options . maxLogEvents ?? 1024 , "maxLogEvents" ) ;
@@ -191,6 +194,7 @@ export class WorkerJavaScriptBackend implements WorkspaceModuleBackend {
191194 maxSourceBytes : options . maxSourceBytes ?? 256 * 1024 ,
192195 maxInputBytes : options . maxInputBytes ?? 256 * 1024 ,
193196 maxStdinBytes : options . maxStdinBytes ?? 256 * 1024 ,
197+ maxEnvBytes : options . maxEnvBytes ?? 1024 * 1024 ,
194198 maxResultBytes : options . maxResultBytes ?? 1024 * 1024 ,
195199 maxLogBytes : options . maxLogBytes ?? 256 * 1024 ,
196200 maxLogEvents : options . maxLogEvents ?? 1024 ,
@@ -318,6 +322,7 @@ class JavaScriptBackendHandle implements WorkspaceModuleBackendHandle {
318322 if ( stdinBytes . byteLength > this . #options. maxStdinBytes ) {
319323 throw new Error ( `Workspace runtime stdin exceeds ${ this . #options. maxStdinBytes } bytes.` ) ;
320324 }
325+ assertEnv ( input . env , this . #options. maxEnvBytes ) ;
321326 if ( new TextEncoder ( ) . encode ( input . source ) . byteLength > this . #options. maxSourceBytes ) {
322327 throw new Error ( `Workspace runtime source exceeds ${ this . #options. maxSourceBytes } bytes.` ) ;
323328 }
@@ -1170,7 +1175,26 @@ function assertEncodedSize(value: WorkspaceRuntimeValue, maxBytes: number, name:
11701175function normalizeStdin ( stdin : Uint8Array | string | undefined ) : Uint8Array {
11711176 if ( stdin === undefined ) return new Uint8Array ( 0 ) ;
11721177 if ( typeof stdin === "string" ) return new TextEncoder ( ) . encode ( stdin ) ;
1173- return stdin ;
1178+ if ( stdin instanceof Uint8Array ) return stdin ;
1179+ throw new Error ( "Workspace runtime stdin must be a string or Uint8Array." ) ;
1180+ }
1181+
1182+ function assertEnv ( env : Record < string , string > | undefined , maxBytes : number ) : void {
1183+ if ( env === undefined ) return ;
1184+ if ( typeof env !== "object" || env === null || Array . isArray ( env ) ) {
1185+ throw new Error ( "Workspace runtime env must be a string-to-string record." ) ;
1186+ }
1187+ let bytes = 0 ;
1188+ const encoder = new TextEncoder ( ) ;
1189+ for ( const [ key , value ] of Object . entries ( env ) ) {
1190+ if ( typeof value !== "string" ) {
1191+ throw new Error ( `Workspace runtime env value for ${ JSON . stringify ( key ) } must be a string.` ) ;
1192+ }
1193+ bytes += encoder . encode ( key ) . byteLength + encoder . encode ( value ) . byteLength ;
1194+ }
1195+ if ( bytes > maxBytes ) {
1196+ throw new Error ( `Workspace runtime env exceeds ${ maxBytes } bytes.` ) ;
1197+ }
11741198}
11751199
11761200function assertPositiveFinite ( value : number , name : string ) {
0 commit comments