//! WASI contains oversized allocations and script introspection without a native host abort. use sausage::cred::{ quickjs::QuickJs, script::{ErrorKind, Limits, Script}, }; #[test] fn removes_bootstrap_capabilities_and_clocks() { let result = QuickJs::default().evaluate( "export default () => [typeof WebAssembly,typeof SharedArrayBuffer,typeof Atomics,typeof __infra,typeof Intl,typeof queueMicrotask]", "null", None, Limits { max_time_ms: Some(1000), ..Limits::default() }, ).unwrap(); assert_eq!( result, "[\"undefined\",\"undefined\",\"undefined\",\"undefined\",\"undefined\",\"undefined\"]" ); } #[test] fn huge_allocations_and_clock_access_return_errors() { let runtime = QuickJs::default(); for source in [ "export default () => new Uint8Array(2**30).fill(1)", "export default () => 'x'.repeat(2**27).indexOf('y')", "export default () => 'x'.repeat(2**28).indexOf('y')", "export default () => new WebAssembly.Memory({initial:16384})", "export default () => Intl.DateTimeFormat().format()", ] { assert!( runtime .evaluate( source, "null", None, Limits { max_time_ms: Some(1000), ..Limits::default() } ) .is_err() ); } assert_eq!( runtime .evaluate("export default () => true", "null", None, Limits::default()) .unwrap(), "true" ); } #[tokio::test(flavor = "current_thread")] async fn accepts_a_caller_inside_tokio() { let result = QuickJs::default() .evaluate( "export default () => true", "null", None, Limits { max_time_ms: Some(1000), ..Limits::default() }, ) .unwrap(); assert_eq!(result, "true"); } #[test] fn stack_trace_has_no_native_call_sites() { let runtime = QuickJs::default(); let error = runtime.evaluate( "export default () => { Error.prepareStackTrace=(error, sites)=>sites.map(site=>site.getThis()); return typeof new Error().stack; }", "null", None, Limits { max_time_ms: Some(1000), ..Limits::default() }, ).unwrap_err(); assert_eq!(error.kind, ErrorKind::Throw); assert_eq!( runtime .evaluate( "export default () => typeof new Error().stack", "null", None, Limits { max_time_ms: Some(1000), ..Limits::default() }, ) .unwrap(), "\"string\"" ); }