How safe is expression evaluation using eval?

前端 未结 3 1639
迷失自我
迷失自我 2020-12-10 08:33

I am building a website where I have a need that user should be able to evaluate some expression based from the value in DB tables, instead of using tools like pyparsing etc

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-10 08:51

    It's completely unsafe to use eval, even with built-ins emptied and blocked -- the attacker can start with a literal, get its __class__, etc, etc, up to object, its __subclasses__, and so forth... basically, Python introspection is just too strong to stand up to a skilled, determined attacker.

    ast.literal_eval is safe, if you can live by its limitations...

提交回复
热议问题