Safe expression evaluation in Ruby for user submitted formulas

Ruby has a robust library of functions to evaluate mathematical expressions. However, this library is not as good as it could be when it comes to dealing with user-submitted mathematical formulas. This is how Keisan helps.

Arnaud Lachaume
Arnaud Lachaume LinkedIn
• 4 min read
Safe expression evaluation in Ruby for user submitted formulas

TL;DR; checkout Keisan for evaluation of user-submitted expressions and formulas. The gem has plenty of built-in functions and can be easily extended.

Our reporting API powers all the data driven results on Keypup, such as the Priority Inbox, Team Board and Data Explorer. Recently we decided to try out a small MVP allowing us to pass dynamic expressions as part our report API calls to perform post-processing (~ data mapping/formatting).

Our objective with that MVP was to allow report queries to become self-contained (e.g. chart queries) and reduce the amount of data massage we have to do in frontend.

The main problem with any kind of user input evaluation is security of course. You do not want to use Ruby eval or give expression evaluators access to the context of your app (i.e. give access to Rails.application.credentials for instance).

So we needed a purely interpreted expression processor with no direct connection to Ruby when evaluating expressions. A few Google searches later we ended up finding...

Keisan, expression parser and evaluator

This gem provides an expression evaluator which is close enough to Ruby but without running expressions in the Ruby context.

Instead expressions provided to Keisan are parsed as an Abstract Syntax Tree (AST) then each node in the tree gets evaluated. Each evaluation is done on primitive values without access to the Ruby context. Invoking a function which is not defined in the evaluator (e.g. Rails.application.credentials) will raise an error.

Because expressions are interpreted from scratch using a string parser and a whitelist of functions, this is considerably safer than any form of eval.

Thanks to Keisan we can safely allow our reporting API to accept dynamic processors:

Image

Getting started with Keisan

First install the gem

Then open an irb console and start playing with the evaluator. Keisan provides a whole lot of base functions to play with, so don't be shy.

Convenient isn't it? Now let's move onto Keisan's most powerful feature.

Defining your own functions

Keisan allows you to extend its functionalities with custom functions.

Custom functions can be defined within the evaluator or in Ruby directly.

Because Keisan can be extended with custom functions, you can create very powerful and flexible calculators.

Let's say you wish to give your app administrators a web UI allowing them to derive stats from your data. You could go as far as providing Keisan functions retrieving data from your database and let them play with them. Let's see what this would look like in Rails.

Creating your own calculator

Alright, you know almost everything there is to know about Keisan. So what about creating your own calculator?

Below is an example of custom calculator with predefined functions.

You may note two things about the calculator above which are related to security. I said earlier Keisan was safe...it is, mostly...except for Denial of Service (DoS) attacks using infinite loops. Fortunately there is way to disable them.

First we disable recursive functions. This prevents users from defining functions looping indefinitely.

Second we disable the while loop. This also prevents users from entering never ending expressions.

That's it. Now feel free to get creative!

I think Keisan can be particularly appealing for any app / admin UI where formulas must be maintained and saved in database (think tax calculations, premium calculation etc..). Keisan provides a safe and extensible way of handling these use cases.

About us

Keypup's SaaS solution allows engineering teams and all software development stakeholders to gain a better understanding of their engineering efforts by combining real-time insights from their development and project management platforms. The solution integrates multiple data sources into a unified database along with a user-friendly dashboard and insights builder interface. Keypup users can customize tried-and-true templates or create their own reports, insights and dashboards to get a full picture of their development operations at a glance, tailored to their specific needs.


Code snippets hosted with ❤ by GitHub‍ Thumbnail designed by Vexels.com Banner designed by pngtree.com

Ready to Transform Your Analytics?

Join teams already using AI to make data-driven decisions faster than ever.

Most Recent Articles

The Due Date Mirage: Why Half of Your Jira Deadlines Slip, and Where the Late Days Go

The Due Date Mirage: Why Half of Your Jira Deadlines Slip, and Where the Late Days Go

Engineering leaders on Reddit keep asking the same question: why do we miss so many dates when every team says the work is on track? In a typical enterprise organization, 46% of Jira issues with a due date close late, and only 41% of the misses come from code taking too long. Learn how Keypup MCP splits every missed due date into waiting, coding, review and closing time, using Jira due dates, workflow history, story points and linked pull requests.

Stephane Ibos
The Thin Ticket Tax: How Underspecified Jira Issues Double Your Cycle Time

The Thin Ticket Tax: How Underspecified Jira Issues Double Your Cycle Time

Engineers on Reddit keep saying the same thing: the ticket said "fix the export" and nothing else. In a typical enterprise organization, 38% of completed Jira issues have an empty or one-line description, and once started they take twice as long, bounce back to In Progress 3.5 times as often and get changes requested in review twice as often. Learn how Keypup MCP measures ticket quality from issue descriptions, workflow history, sprints, comments and linked pull requests.

Arnaud Lachaume
The Untraceable Merge: When a Third of Your Merged Code Has No Ticket Behind It

The Untraceable Merge: When a Third of Your Merged Code Has No Ticket Behind It

Enterprise change policies say every change traces back to a ticket, but in a typical organization 34% of merged pull requests resolve no Jira issue, carrying 29% of the code. Learn what engineering leaders on Reddit say about pull requests without linked issues, audit traceability and R&D capitalization, and how Keypup MCP measures the traceability gap from resolved issues, branch names, issue types, approvals and mergers.

Thomas Williams