
Research
Malicious npm Packages Impersonate Flashbots SDKs, Targeting Ethereum Wallet Credentials
Four npm packages disguised as cryptographic tools steal developer credentials and send them to attacker-controlled Telegram infrastructure.
@m93a/filtrex
Advanced tools
A simple, safe, JavaScript Filter Expression compiler for end-users
A simple, safe, JavaScript expression engine, allowing end-users to enter arbitrary expressions without p0wning you.
category == "meal" and (calories * weight > 2000.0 or subcategory in ("cake", "pie"))
There are many cases where you want a user to be able enter an arbitrary expression through a user interface. e.g.
Sure, you could do that with JavaScript and eval()
, but I'm sure I don't have to tell you how stupid that would be.
Filtrex defines a really simple expression language that should be familiar to anyone who's ever used a spreadsheet and compile it into a JavaScript function at runtime.
transactions <= 5 and abs(profit) > 20.5
function(item) { return item.transactions <=5 && Math.abs(item.profit) > 20.5; }
// Input from user (e.g. search filter)
var expression = 'transactions <= 5 and abs(profit) > 20.5';
// Compile expression to executable function
var myfilter = compileExpression(expression);
// Execute function
myfilter({transactions: 3, profit:-40.5}); // returns 1
myfilter({transactions: 3, profit:-14.5}); // returns 0
Under the hood, the above expression gets compiled to a clean and fast JavaScript function, looking something like this:
// Resulting function
function(item) {
return item.transactions <= 5 && Math.abs(item.profit) > 20.5;
}
There are only 3 types: numbers, strings and arrays of these. Numbers may be floating point or integers. Boolean logic is applied on the truthy value of values (e.g. any non-zero number is true, any non-empty string is true, otherwise false).
Okay, I lied to you, there are also objects whose properties can be accessed by the of
operator. And there's undefined. But everything else is just numbers, strings and arrays!
Values | Description |
---|---|
43, -1.234 | Numbers |
"hello" | String |
" \" \\ " | Escaping of double-quotes and blackslash in string |
foo, a.b.c, 'foo-bar' | External data variable defined by application (may be numbers or strings) |
Numeric arithmetic | Description |
---|---|
x + y | Add |
x - y | Subtract |
x * y | Multiply |
x / y | Divide |
x % y | Modulo |
x ^ y | Power |
Comparisons | Description |
---|---|
x == y | Equals |
x != y | Does not equal |
x < y | Less than |
x <= y | Less than or equal to |
x > y | Greater than |
x >= y | Greater than or equal to |
x ~= y | Regular expression match |
x in (a, b, c) | Equivalent to (x == a or x == b or x == c) |
x not in (a, b, c) | Equivalent to (x != a and x != b and x != c) |
Boolean logic | Description |
---|---|
x or y | Boolean or |
x and y | Boolean and |
not x | Boolean not |
x ? y : z | If boolean x, value y, else z |
( x ) | Explicity operator precedence |
Objects and arrays | Description |
---|---|
(a, b, c) | Array |
a in b | Array a is a subset of array b |
x of y | Property x of object y |
Built-in functions | Description |
---|---|
abs(x) | Absolute value |
ceil(x) | Round floating point up |
floor(x) | Round floating point down |
log(x) | Natural logarithm |
max(a, b, c...) | Max value (variable length of args) |
min(a, b, c...) | Min value (variable length of args) |
random() | Random floating point from 0.0 to 1.0 |
round(x) | Round floating point |
sqrt(x) | Square root |
Operator precedence follows that of any sane language.
When integrating in to your application, you can add your own custom functions.
// Custom function: Return string length.
function strlen(s) {
return s.length;
}
let options = {
extraFunctions: { strlen }
};
// Compile expression to executable function
let myfilter = compileExpression('strlen(firstname) > 5', options);
myfilter({firstname:'Joe'}); // returns 0
myfilter({firstname:'Joseph'}); // returns 1
If you want to do some more magic with your filtrex, you can supply a custom function that will resolve the identifiers used in expressions and assign them a value yourself. This is called a property function and has the following signature:
function propFunction(
propertyName: string, // name of the property being accessed
get: (name: string) => obj[name], // safe getter that retrieves the property from obj
obj: any // the object passed to compiled expression
)
For example, this can be useful when you're filtering based on whether a string contains some words or not:
function containsWord(string, word) {
// your optimized code
}
let options = {
customProp: (word, _, string) => containsWord(string, word)
};
let myfilter = compileExpression('Bob and Alice or Cecil', options);
myfilter("Bob is boring"); // returns 0
myfilter("Bob met Alice"); // returns 1
myfilter("Cecil is cool"); // returns 1
Safety note: The get
function returns undefined
for properties that are defined on the object's prototype, not on the object itself. This is important, because otherwise the user could access things like toString.constructor
and maybe do some nasty things with it. Bear this in mind if you decide not to use get
and access the properties yourself.
Why the name?
Because it was originally built for FILTeR EXpressions.
What's Jison?
Jison is bundled with Filtrex – it's a JavaScript parser generator that does the underlying hard work of understanding the expression. It's based on Flex and Bison.
License?
Unit tests?
What happens if the expression is malformed?
Calling compileExpression()
with a malformed expression will throw an exception. You can catch that and display feedback to the user. A good UI pattern is to attempt to compile on each keystroke and continuously indicate whether the expression is valid.
FAQs
A simple, safe, JavaScript Filter Expression compiler for end-users
The npm package @m93a/filtrex receives a total of 0 weekly downloads. As such, @m93a/filtrex popularity was classified as not popular.
We found that @m93a/filtrex 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.
Research
Four npm packages disguised as cryptographic tools steal developer credentials and send them to attacker-controlled Telegram infrastructure.
Security News
Ruby maintainers from Bundler and rbenv teams are building rv to bring Python uv's speed and unified tooling approach to Ruby development.
Security News
Following last week’s supply chain attack, Nx published findings on the GitHub Actions exploit and moved npm publishing to Trusted Publishers.