eval()==邪恶? ---如何安全使用? [英] eval() == evil? --- How to use it safely?
问题描述
我正在创建一个程序,需要一些必须保持的数据
到目前为止。我的计划是将这些数据放在一个网站上,然后这个程序定期从网站上提取数据。
我的问题是当我从网站上提取数据(当前存储为网站上的
字典)时,它是一个字符串,我可以使用eval()
来制作那个字符串变成了字典,一切都很棒。
但是,这意味着我在web-
网站上的某个字符串上使用eval(),这看起来很漂亮不安全。
我通过使用eval(代码,{" __ builtins __":None},{})读取它我可以阻止
来自使用几乎任何东西,我的嵌套字典
字符串仍然是允许的。我想知道的是:
eval的危险是什么?
- 我最初使用的是exec()但切换到eval()因为我
不想让一些黑客能够从我的
客户端计算机上删除/窃取文件。我认为这不是eval()的问题,因为
eval不会执行命令。
- 通过在命令中修改我的代码字符串,可以做些什么呢? />
喜欢:thing = eval(代码{" __ builtins __":None},{}),除了
之外的任何东西都将自己的值分配给对象的东西?
I am creating a program that requires some data that must be kept up
to date. What I plan is to put this data up on a web-site then have
the program periodically pull the data off the web-site.
My problem is that when I pull the data (currently stored as a
dictionary on the site) off the site, it is a string, I can use eval()
to make that string into a dictionary, and everything is great.
However, this means that I am using eval() on some string on a web-
site, which seems pretty un-safe.
I read that by using eval(code,{"__builtins__":None},{}) I can prevent
them from using pretty much anything, and my nested dictionary of
strings is still allowable. What I want to know is:
What are the dangers of eval?
- I originally was using exec() but switched to eval() because I
didn''t want some hacker to be able to delete/steal files off my
clients computers. I assume this is not an issue with eval(), since
eval wont execute commands.
- What exactly can someone do by modifying my code string in a command
like: thing = eval(code{"__builtins__":None},{}), anything other than
assign their own values to the object thing?
推荐答案
2008年8月28日星期四下午6:51,Fett< Fe ******** @ gmail.comwrote:
On Thu, Aug 28, 2008 at 6:51 PM, Fett <Fe********@gmail.comwrote:
我正在创建一个程序,需要一些必须保持的数据
到目前为止。我的计划是将这些数据放在一个网站上,然后这个程序定期从网站上提取数据。
我的问题是当我从网站上提取数据(当前存储为网站上的
字典)时,它是一个字符串,我可以使用eval()
来制作那个字符串变成了字典,一切都很棒。
但是,这意味着我在web-
网站上的某个字符串上使用eval(),这看起来很漂亮不安全。
我通过使用eval(代码,{" __ builtins __":None},{})读取它我可以阻止
来自使用几乎任何东西,我的嵌套字典
字符串仍然是允许的。我想知道的是:
eval的危险是什么?
- 我最初使用的是exec()但切换到eval()因为我
不想让一些黑客能够从我的
客户端计算机上删除/窃取文件。我认为这不是eval()的问题,因为
eval不会执行命令。
- 通过在命令中修改我的代码字符串,可以做些什么呢? />
喜欢:thing = eval(代码{" __ builtins __":None},{}),除了
以外的任何东西都将自己的值分配给对象的东西?
I am creating a program that requires some data that must be kept up
to date. What I plan is to put this data up on a web-site then have
the program periodically pull the data off the web-site.
My problem is that when I pull the data (currently stored as a
dictionary on the site) off the site, it is a string, I can use eval()
to make that string into a dictionary, and everything is great.
However, this means that I am using eval() on some string on a web-
site, which seems pretty un-safe.
I read that by using eval(code,{"__builtins__":None},{}) I can prevent
them from using pretty much anything, and my nested dictionary of
strings is still allowable. What I want to know is:
What are the dangers of eval?
- I originally was using exec() but switched to eval() because I
didn''t want some hacker to be able to delete/steal files off my
clients computers. I assume this is not an issue with eval(), since
eval wont execute commands.
- What exactly can someone do by modifying my code string in a command
like: thing = eval(code{"__builtins__":None},{}), anything other than
assign their own values to the object thing?
通过禁用 __builtins__你确实削减了一些明显的技巧,但
有人仍然可以发给你一个像10 ** 10 ** 10的字符串。
By "disabling" __builtins__ you indeed cut some obvious tricks, but
someone still could send you a string like "10 ** 10 ** 10".
-
http://mail.python。 org / mailman / listinfo / python-list
-
- Guilherme H. Polo Goncalves
--
-- Guilherme H. Polo Goncalves
如果你不能使用简单的数据结构/格式
,如JSON,或CSV,或类似,_don''t_
使用eval或exec,但使用pickle
库代替。这更加安全。
欢呼
詹姆斯
周五,2008年8月29日7:51 AM,Fett< Fe ******** @ gmail.comwrote:
Hi,
If you cannot use a simple data structure/format
like JSON, or CSV, or similar, _don''t_
use eval or exec, but use the pickle
libraries instead. This is much safer.
cheers
James
On Fri, Aug 29, 2008 at 7:51 AM, Fett <Fe********@gmail.comwrote:
我正在创建一个程序,需要一些必须保持的数据
到目前为止。我的计划是将这些数据放在一个网站上,然后这个程序定期从网站上提取数据。
我的问题是当我从网站上提取数据(当前存储为网站上的
字典)时,它是一个字符串,我可以使用eval()
来制作那个字符串变成了字典,一切都很棒。
但是,这意味着我在web-
网站上的某个字符串上使用eval(),这看起来很漂亮不安全。
我通过使用eval(代码,{" __ builtins __":None},{})读取它我可以阻止
来自使用几乎任何东西,我的嵌套字典
字符串仍然是允许的。我想知道的是:
eval的危险是什么?
- 我最初使用的是exec()但切换到eval()因为我
不想让一些黑客能够从我的
客户端计算机上删除/窃取文件。我认为这不是eval()的问题,因为
eval不会执行命令。
- 通过在命令中修改我的代码字符串,可以做些什么呢? />
喜欢:thing = eval(代码{" __ builtins __":None},{}),除了
之外的任何东西都将自己的值分配给对象的东西?
-
http:// mail。 python.org/mailman/listinfo/python-list
-
-
- 问题通过方法解决
--
--
-- "Problems are solved by method"
8月28日,4:51 * pm,Fett< FettMan ... @ gmail。 comwrote:
On Aug 28, 4:51*pm, Fett <FettMan...@gmail.comwrote:
我正在创建一个程序,需要一些必须保持的数据
到目前为止。我的计划是将这些数据放在一个网站上,然后这个程序定期从网站上提取数据。
我的问题是当我从网站上提取数据(当前存储为网站上的
字典)时,它是一个字符串,我可以使用eval()
来制作那个字符串变成了字典,一切都很棒。
但是,这意味着我在web-
网站上的某个字符串上使用eval(),这看起来很漂亮联合国安全。
I am creating a program that requires some data that must be kept up
to date. What I plan is to put this data up on a web-site then have
the program periodically pull the data off the web-site.
My problem is that when I pull the data (currently stored as a
dictionary on the site) off the site, it is a string, I can use eval()
to make that string into a dictionary, and everything is great.
However, this means that I am using eval() on some string on a web-
site, which seems pretty un-safe.
我可以建议PyYAML吗?
May I suggest PyYAML?
这篇关于eval()==邪恶? ---如何安全使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!