Simple javascript expressions sandbox implement via Function.
The implementation of pico-sandbox is not absolutely secure, as it cannot restrict the JavaScript prototype chain. For example, the following code can bypass the limitations of the sandbox.
''.constructor.__proto__.constructor("alert()")()
npm install pico-sandbox
import { Sandbox } from 'pico-sandbox';
const parser = Sandbox.compile('a + b');
const result = parser({ a: 1, b: 2 });
console.log(result); // 3
const parser = Sandbox.compile('a + b', { throwOnUndefined: true });
const result = parser({ a: 1 }); // Error: b is not defined
import { Sandbox } from 'pico-sandbox';
function compileTemplate(template: string, options: SandboxOptions = {}) {
return Sandbox.compile(`\`${ template }\``, options);
}
const parser = compileTemplate('Hello ${ name }!');
const result = parser({ name: 'Pico' });
console.log(result); // "Hello Pico!"
- throwOnUndefined: throw
ReferenceError
if variable is undefined in expression