Bypassing a restrictive JS sandbox
The static-eval library, used as a JavaScript sandbox, could be bypassed to execute arbitrary code because it failed to properly handle function parameter destructuring, allowing access to the Function constructor. An attacker could achieve remote code execution on the server.
Published on licenciaparahackear.github.io
While participating in a bug bounty program, I found a site with a very interesting functionality: it allowed me to filter some data based on a user-controlled expression. I could put something like book.price > 100 to make it only show the books that are more expensive than $100.
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely, the sandbox's check for function arguments only handled simple identifiers, not destructured parameters, so it used the original variable value instead of null, allowing access to the Function constructor.
What the attacker needed
Knowledge of a variable name with a non-function value; ability to inject expressions into static-eval
How it was found
- Identify that the site uses static-eval for filtering expressions.
- Discover that function parameter destructuring bypasses the argument nulling.
- Craft an expression that uses destructuring to access the Function constructor.
- Use the Function constructor to execute arbitrary code, such as reading environment variables or running system commands.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.
A sandbox is a security mechanism to run untrusted code safely. The static-eval library tried to evaluate JavaScript expressions without allowing dangerous operations, but a flaw let attackers escape and run system commands.
What is RCE?
Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.