
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
A language for writing safe expressions, in a tiny subset of JavaScript.
This library follows these design goals:
The following features of JavaScript are supported:
a.b.c, a[b][c].true, false, null, undefined, bigint, number, string.==, !=, ===, !==, >, >=, <, <=.+, -, *, /, %, **.&, |, ^, ~, <<, >>, >>>.!, &&, ||, ??.(...).The following features of JavaScript are instead not supported:
new operator: new can be used to execute unintentionally-exposed functions, so it's not supported.While the language by itself is safe to execute, it's important to note that in order for it to be useful it supports giving expressions explicit access to a set of variables you control. And in order to be an actual subset of JavaScript it must indirectly support some very dynamic parts of the language, like getters and Proxy instances.
If you want to make this library useless you can give your expressions access to a variable like this:
const footgun = new Proxy ( {}, {
get ( target, key ) {
eval ( key );
}
});
Which the no longer safe expressions could then use like this to execute arbitrary code:
footgun['alert(1)']
Additionally function calls to explicitly-provided functions are allowed, so providing this context object to your expressions is unsafe:
{ eval }
Note how a function must be explicitly listed to be callable by the expression:
// This will throw, "min" was not explicitly provided
safex.exec ( 'Math.min ( 1, 2 )', { Math } );
// This is allowed,"min" was explicitly provided
safex.exec ( 'min ( 1, 2 )', { min: Math.min } );
Basically executing a function in general is unsafe, and there are a lot of ways to execute a function in JavaScript, even with the allowed language being this restrictive, for example:
Symbol.toPrimitive, toString and valueOf on them.Proxy object.Unless you do weird stuff expressions executed via this library will be safe, but it's important to understand that you can shoot yourself in the foot by providing usafe variables to your expressions.
npm install --save safex
import safex from 'safex';
// Execute an expression without pre-compiling it, which is slower if you need to execute it multiple times
safex.exec ( '128 / 2' ); // => 64
safex.exec ( 'activeView === "search"', { activeView: 'search' } ); // => true
safex.exec ( 'isFoo && ( isBar || baz < 3 )', { isFoo: true, isBar: false, baz: 123 } ); // => false
// Compile an expression, parsing it once, which is faster if you need to execute it multiple times with different variables
const expression = safex.compile ( 'isFoo || isBar' );
expression ({ isFoo: 1, isBar: 2 }); // => 1
expression ({ isFoo: 0, isBar: 2 }); // => 2
// Validate that an expression is actually valid syntactically
safex.validate ( '( -1 ) ** 2' ); // => true
safex.validate ( '-1 ** 2' ); // => false
safex.validate ( 'eval ( "alert(1)" )' ); // => false
// Low-level function that parse an expression into an AST
const ast = safex.parse ( '1 + 2' ) // => { type: 'root', children: [{ type: 'addition', children: [{ type: 'number', value: 1 }, { type: 'number', value: 2 }] }] }
MIT © Fabio Spampinato
FAQs
A language for writing safe expressions, in a tiny subset of JavaScript.
The npm package safex receives a total of 9 weekly downloads. As such, safex popularity was classified as not popular.
We found that safex demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.