@ -3,6 +3,9 @@
// SPDX-License-Identifier: Apache-2.0 AND BUSL-1.1
import { describe , test } from 'node:test' ;
import assert from 'node:assert/strict' ;
import { execFile } from 'node:child_process' ;
import path from 'node:path' ;
import { promisify } from 'node:util' ;
import { JsExecutor } from '../api/jsExecutor' ;
import { oauthBearerProvider } from '../queue/oAuthBearerProvider' ;
@ -39,6 +42,121 @@ test('sandbox passes string args through unchanged', async () => {
assert . equal ( out . first , 'hello' ) ;
} ) ;
const sandboxRealmProbes : [ string , string ] [ ] = [
[ 'btoa' , 'btoa.constructor' ] ,
[ 'atob' , 'atob.constructor' ] ,
[ 'TextDecoder' , 'TextDecoder.constructor' ] ,
[ 'TextDecoder instance' , 'new TextDecoder().constructor.constructor' ] ,
[ 'TextDecoder.prototype.decode' , 'TextDecoder.prototype.decode.constructor' ] ,
[ 'TextDecoder getter' , 'Object.getOwnPropertyDescriptor(TextDecoder.prototype, "encoding").get.constructor' ] ,
[ 'TextDecoder constructor error' , '(function(){ try { new TextDecoder("no-such-encoding"); } catch (e) { return e.constructor.constructor; } })()' ] ,
[ 'TextDecoder decode error' , '(function(){ try { new TextDecoder().decode("not a buffer"); } catch (e) { return e.constructor.constructor; } })()' ] ,
[ 'TextDecoder fatal decode error' , '(function(){ try { new TextDecoder("utf-8", {fatal: true}).decode(new Uint8Array([0xff])); } catch (e) { return e.constructor.constructor; } })()' ] ,
[ 'getter on foreign receiver' , '(function(){ try { Object.getOwnPropertyDescriptor(TextDecoder.prototype, "fatal").get.call({}); } catch (e) { return e.constructor.constructor; } })()' ] ,
[ 'patched WeakMap/String' , '(function(){ var leak; WeakMap.prototype.get = function(k){ leak = this; }; String = function(x){ return x; }; var d = new TextDecoder(); d.decode(new Uint8Array([104])); try { btoa("\\u0100"); } catch (e) {} return leak ? leak.constructor.constructor : Function; })()' ] ,
[ 'Error.prepareStackTrace' , '(function(){ var fr; Error.prepareStackTrace = function(e, s){ fr = s; return ""; }; try { new TextDecoder("nope"); } catch (e) { e.stack; } for (var i = 0; fr && i < fr.length; i++) { var f = fr[i].getFunction && fr[i].getFunction(); if (f) return f.constructor; var t = fr[i].getThis && fr[i].getThis(); if (t && t.constructor) return t.constructor.constructor; } return Function; })()' ] ,
// Node formats some ERR_INVALID_ARG_TYPE messages with util.inspect, which calls a custom inspect hook.
. . . [ 'utf-8' , 'latin1' , 'utf-16le' ] . map ( ( encoding ) : [ string , string ] = > [ ` TextDecoder.decode inspect hook ( ${ encoding } ) ` ,
` (function(){ var F; var o = Object.create(null); o[Symbol.for("nodejs.util.inspect.custom")] = function(d, opts, insp){ F = insp && insp.constructor; return "x"; };
try { new TextDecoder ( "${encoding}" ) . decode ( o ) ; } catch ( e ) { }
var p = { } ; Object . setPrototypeOf ( p , ArrayBuffer . prototype ) ; Object . defineProperty ( p , "constructor" , { value : ArrayBuffer } ) ; p [ Symbol . for ( "nodejs.util.inspect.custom" ) ] = o [ Symbol . for ( "nodejs.util.inspect.custom" ) ] ;
try { new TextDecoder ( "${encoding}" ) . decode ( p ) ; } catch ( e ) { }
return F || Function ; } ) ( ) ` ]),
. . . [ 'btoa' , 'atob' ] . map ( ( fn ) : [ string , string ] = > [ ` ${ fn } with patched String returning an inspectable object ` ,
` (function(){ var F; String = function(x){ var o = Object.create(null); o[Symbol.for("nodejs.util.inspect.custom")] = function(d, opts, insp){ F = insp && insp.constructor; return "x"; }; return o; };
try { $ { fn } ( "a" ) ; } catch ( e ) { }
return F || Function ; } ) ( ) ` ]),
] ;
for ( const [ name , functionCtor ] of sandboxRealmProbes ) {
test ( ` sandbox globals belong to the sandbox realm: ${ name } ` , async ( ) = > {
const exec = new JsExecutor ( true ) ;
const script = await exec . compileScript ( ` function(msg, metadata, msgType){
var F = $ { functionCtor } ;
return F ( 'return process' ) ( ) ;
} ` );
await assert . rejects (
exec . executeScript ( script , [ '{}' , '{}' , 'X' ] , 5000 ) ,
/process is not defined/ ,
'host process must not be reachable from inside the sandbox' ,
) ;
} ) ;
}
test ( 'sandbox globals belong to the sandbox realm: resolving functions of a returned thenable' , async ( ) = > {
const exec = new JsExecutor ( true ) ;
const script = await exec . compileScript ( ` function(msg, metadata, msgType){
return { then : function ( resolve ) { resolve ( resolve . constructor . constructor ( 'return process' ) ( ) ) ; } } ;
} ` );
await assert . rejects (
exec . executeScript ( script , [ '{}' , '{}' , 'X' ] , 5000 ) ,
/process is not defined/ ,
) ;
} ) ;
// A stack overflow inside a helper reproduces only while its host code path is cold,
// so each probe runs as the first sandbox execution of a fresh process.
const coldPathCalls = [
"btoa('a')" ,
"atob('YQ==')" ,
"new TextDecoder('nope')" ,
"new TextDecoder().decode(new Uint8Array([104]))" ,
"new TextDecoder().encoding" ,
"Object.getOwnPropertyDescriptor(TextDecoder.prototype, 'fatal').get.call({})" ,
] ;
test ( 'sandbox helpers rethrow only sandbox-realm errors on stack overflow in cold host code' , async ( ) = > {
const run = promisify ( execFile ) ;
const results = await Promise . all ( coldPathCalls . map ( async ( call ) = > {
const { stdout } = await run ( process . execPath , [ '--require' , 'ts-node/register' , path . join ( __dirname , 'coldPathProbe.ts' ) , call ] ,
{ cwd : path.join ( __dirname , '..' ) , env : { . . . process . env , TS_NODE_TRANSPILE_ONLY : 'true' } , timeout : 60000 } ) ;
return [ call , stdout ] ;
} ) ) ;
assert . deepEqual ( results , coldPathCalls . map ( ( call ) = > [ call , 'isolated' ] ) ) ;
} ) ;
test ( 'sandbox btoa/atob encode, decode and coerce like the browser API' , async ( ) = > {
const exec = new JsExecutor ( true ) ;
const script = await exec . compileScript ( ` function(msg, metadata, msgType){
return JSON . stringify ( [ btoa ( 'hello' ) , atob ( 'aGVsbG8=' ) , atob ( btoa ( '\\u00ff\\u0000x' ) ) === '\\u00ff\\u0000x' , btoa ( 123 ) ,
btoa . name , atob . name , Object . getPrototypeOf ( btoa ) === Function . prototype ] ) ;
} ` );
const out = await exec . executeScript ( script , [ '{}' , '{}' , 'X' ] , 5000 ) ;
assert . equal ( out , JSON . stringify ( [ 'aGVsbG8=' , 'hello' , true , 'MTIz' , 'btoa' , 'atob' , true ] ) ) ;
} ) ;
test ( 'sandbox TextDecoder decodes like the host TextDecoder' , async ( ) = > {
const exec = new JsExecutor ( true ) ;
const script = await exec . compileScript ( ` function(msg, metadata, msgType){
var bytes = new Uint8Array ( [ 0xEF , 0xBB , 0xBF , 0x68 , 0xC3 , 0xA9 , 0xE2 , 0x82 , 0xAC ] ) ;
var d = new TextDecoder ( ) ;
var stream = new TextDecoder ( ) ;
var streamed = stream . decode ( new Uint8Array ( [ 0xE2 , 0x82 ] ) , { stream : true } ) + stream . decode ( new Uint8Array ( [ 0xAC ] ) ) ;
var latin = new TextDecoder ( 'latin1' ) ;
function errorType ( fn ) { try { fn ( ) ; return 'none' ; } catch ( e ) { return e instanceof RangeError ? 'RangeError' : e instanceof TypeError ? 'TypeError' : 'other' ; } }
return JSON . stringify ( [
d . decode ( bytes ) , d . decode ( bytes . buffer ) , d . decode ( new DataView ( bytes . buffer , 3 ) ) , d . decode ( ) ,
d . decode ( bytes . subarray ( 3 ) ) , d . decode ( bytes . subarray ( 4 , 6 ) ) , d . decode ( new Uint16Array ( bytes . buffer , 2 , 2 ) ) ,
new TextDecoder ( 'utf-8' , { ignoreBOM : true } ) . decode ( bytes ) . length , streamed ,
d . encoding , d . fatal , d . ignoreBOM , new TextDecoder ( 'UTF8' , { fatal : true } ) . fatal ,
latin . encoding , latin . decode ( new Uint8Array ( [ 0xE9 ] ) ) ,
errorType ( function ( ) { new TextDecoder ( 'utf-8' , { fatal : true } ) . decode ( new Uint8Array ( [ 0xFF ] ) ) ; } ) ,
errorType ( function ( ) { new TextDecoder ( 'no-such-encoding' ) ; } ) ,
errorType ( function ( ) { TextDecoder . prototype . decode . call ( { } , bytes ) ; } ) ,
errorType ( function ( ) { TextDecoder ( ) ; } ) ,
d instanceof TextDecoder , Object . prototype . toString . call ( d ) ] ) ;
} ` );
const out = await exec . executeScript ( script , [ '{}' , '{}' , 'X' ] , 5000 ) ;
assert . equal ( out , JSON . stringify ( [
'hé€' , 'hé€' , 'hé€' , '' ,
'hé€' , 'é' , '\uFFFDhé' ,
4 , '€' ,
'utf-8' , false , false , true ,
'windows-1252' , 'é' ,
'TypeError' , 'RangeError' , 'TypeError' , 'TypeError' ,
true , '[object TextDecoder]' ] ) ) ;
} ) ;
// The use_sandbox=false path is intentionally non-isolating: scripts compile
// and run in the host realm via vm.compileFunction. The two tests below codify
// that documented contract so any future behavior change shows up as a test