Restricting Python's syntax to execute user code safely. Is this a safe approach?

后端 未结 3 701
鱼传尺愫
鱼传尺愫 2020-12-14 04:00

Original question:

Executing mathematical user code on a python web server, what is the simplest secure way?

  • I want to be able to run user submitted co
相关标签:
3条回答
  • 2020-12-14 04:35

    The Openerp's source code contains a safe_eval.py that do a similar thing. But Instead of checking the ast of the source, it restrict the byte code that is allowed to execute. I think you may also have a look on it :)

    0 讨论(0)
  • 2020-12-14 04:42

    Two points I noticed that you could still improve:

    You should always escape any output that can be generated from some form of user input. In your example, the unallowed identifiers get mirrored unmodified back to the output. This could potentially be exploited, one example being Cross Site Scripting. Therefore I would additionally escape any such error message to prevent this.

    Another thing you need to be aware of is Denial-of-Service attacks. Imagine someone whips up an Ackermann function and a script to submit it a couple of thousand times to your server... To prevent this, you should timebox the execution time of any code being submitted. This is essential, because this type of "attack" often happens unintentionally - someone managed to produce an infinite loop.

    Edit:

    Finally, I would also recommend to update your Python version to prevent a "hashDoS" attack.

    0 讨论(0)
  • 2020-12-14 04:57

    Have you looked at pypy's sandboxing features? It is reputedly much safer than any CPython sandboxing efforts. You can even limit the heap size and cpu execution time to prevent denial of service.

    0 讨论(0)
提交回复
热议问题