E
E
ekapro2015-10-14 22:32:46
PHP
ekapro, 2015-10-14 22:32:46

How to check the security of user code before executing in eval?

It is necessary to provide users of the online service with the ability to create formulas (arithmetic and logical operations, branching, variables are allowed).
To solve the problem, I suppose to run user code through eval, after checking for prohibited functions and expressions. We wrote the following regular expression to search for forbidden constructs in the code:
'/[^$\w]([a-zA-Z][\w_]+)|(\$this)/i'
Is such a check sufficient for the safety of running user code ? Maybe there are better solutions to the problem?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
Cat Anton, 2015-10-14
@ekapro

https://github.com/ziadoz/awesome-php#code-analysis

E
evnuh, 2015-10-14
@evnuh

There is an option, of course. It's easier, but less correct. It is correct to write a parser for the syntax of your formulas, it should be primitive for you, judging by the conditions of the problem.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question