qhwa
Hi, all,
I just published Formular package. It is a tiny library that evaluates a piece of Elixir code.
On the shoulder of Elixir’s Code module
Given a piece of Elixir code (as a string, or AST), Formular runs it with Elixir’s Code module under some security limitations.
So far, the limitations are:
- No calling module functions;
- No calling exit;
- No sending messages.
Indeed, the whole library consists of only one thin module, thanks to the power Elixir has already shipped out of the box.
Motivation
Formular was developed to support some dynamic configuration scenarios. For example, in a scene of an online book store, the discount of a book can be dynamically configured as a piece of code, then evaluated by Formular:
iex> discount_formula = ~s"
...> case order do
...> # old books get a big promotion
...> %{book: %{year: year}} when year < 2000 ->
...> 0.5
...>
...> %{book: %{tags: tags}} ->
...> # Elixir books!
...> if ~s{elixir} in tags do
...> 0.9
...> else
...> 1.0
...> end
...>
...> _ ->
...> 1.0
...> end
...> "
...>
...> book_order = %{
...> book: %{
...> title: "Elixir in Action", year: 2019, tags: ["elixir"]
...> }
...> }
...>
...> Formular.eval(discount_formula, [order: book_order])
{:ok, 0.9}
In such a way, the discount calculation code, which changes frequently, is separated away from the stable business flow, and the primary code is probably more generic and flexible.
I’ve been using it in production for a while so I publish it today in case others may find it useful too.
Cheers!
Trending in Announcing
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #ai
- #elixirconf-us
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
mat-hek
Nice, though I’m curious if you tried using Sand?
hauleth
It is very unsafe implementation. Super simple example of how you can run arbitrary code with it:
And as soon as you have access to
apply/3(or any of thespawn_*/3family) then you can run any code you want. In general as soon as you have access toimportthen you can do anything, and you do not preventimportin any way (it is imported by default as it is part ofKernel.SpecialForms).If you want something like that, it is better to use any embedded language that is distinct from the Elixir and give it access only to needed primitives. You can take a look on Luerl or Erlog for example.
mat-hek
I suppose it wasn’t meant to be safe, meaning resistant to malicious input, but rather to impose some restrictions on the code that’s changing frequently to limit its impact on the system. Although I have doubts if that’s the correct approach, that’s why I mentioned Sand, that aims to be an actual sandbox.
qhwa
Nice! I forgot that
importis inKernel.SpecialForms. It could be prevented after parsing.qhwa
Yes, the purpose was to separate complex configurations from code. Thanks for sharing Sand. I’ll give it a try!
qhwa
Thanks for pointing out Luerl and Erlong which are very solid and good references. However, what I want to achieve is to compile the config into Elixir code which can be sent to and used in some Elixir applications.
Ideally, there can be a service with some UI to manage the configuration rules. On update of any rule, the change is synchronized to some services who are interested in the config. The configuration would be compiled into BEAM code so that it can be directly called in the code.
Lua, or Prolog also works in such scenario, but I prefer Elixir because:
a) Elixir has a more friendly syntax IMHO (personal taste?)
b) I think compiling to BEAM code instead of running in a sandbox is more performant. But I haven’t benchmarked it yet. Will try to see how different approaches work.
I built a configuration management system in Elixir years ago but the data format was a little lispy formatted JSON. It worked very well but I think compiling rules to Elixir code would be more fun!
qhwa
Forumlar 0.2.1 released
importandrequireare now disallowed in the code. Thank @hauleth for pointing it out.hauleth
DoS (atom exhaustion):
I needed to create range manually, as you do not export
../2operator.qhwa
I played with Sand as @mat-hek shared and it does what I wanted. Not implying by the name, under the scene, Sand runs the code with
Code.eval_quoted/3too. Only in a separated process which can be limited in reductions & memory usage. I think that is the right way to go.Also, I did some benchmarks, and the result was surprising at first glance.
Code:
(Sand doesn’t accept AST at this moment)
Result:
I figured out the reason after some research and it is very interesting. I’m excited about the work ahead.
Thank you @hauleth , really appreciate it. I know the next direction now.
collegeimprovements
We use it in production now
Thanks a lot for this package. We replaced Expreso with Formular and so far it’s been great.