重写 ConfigParser get() 方法以包含 eval 有什么缺点吗?

发布于 2024-10-02 04:52:44 字数 502 浏览 0 评论 0原文

我正在创建一个 ConfigParser 的子类,它更容易在我的整个项目中使用:

class MyConfiguration(ConfigParser.ConfigParser):

    def __init__(self, filename):
        ConfigParser.ConfigParser.__init__(self)
        self.readfp(open(filename))

    def get(self, section, option):
        return eval(ConfigParser.ConfigParser.get(self, section, option))

问题:使用包含 eval 的方法覆盖 get() 方法是否有任何缺点(安全性、意外后果)?

我宁愿将 eval 烘焙到 MyConfiguration 类中,因为我想在我的配置文件中使用 Python 数据类型(元组等),但我不想在我的项目代码中处理 eval。

I'm creating a subclass of ConfigParser that is easier for me to use throughout my project:

class MyConfiguration(ConfigParser.ConfigParser):

    def __init__(self, filename):
        ConfigParser.ConfigParser.__init__(self)
        self.readfp(open(filename))

    def get(self, section, option):
        return eval(ConfigParser.ConfigParser.get(self, section, option))

Question: are there any downsides (security, unintended consequences) to overriding the get() method with one that includes eval?

I'd rather bake the eval into the MyConfiguration class because I want to use Python data types (tuples, etc.) in my config files but I don't want to deal with evals all over my project code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陪你到最终 2024-10-09 04:52:44

如果您对 eval 的唯一兴趣是您似乎指出的文字值,那么您可以使用 ast.literal_eval

这将读取元组文字、列表文字等,并且可以安全使用,因为它对接受的内容是有选择性的。

>>> import ast
>>> a = ast.literal_eval('(1, 2, 3)')
>>> a
(1, 2, 3)
>>> b = ast.literal_eval('__import__("evil")')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/ast.py", line 68, in literal_eval
    return _convert(node_or_string)
  File "/usr/lib/python2.6/ast.py", line 67, in _convert
    raise ValueError('malformed string')
ValueError: malformed string

像这样的用例正是该函数的用途。

If your only interest in eval is literal values as you seem to indicate, then you can use ast.literal_eval

This will read tuple literals, list literals and others and is safe to use because it is selective about what it will accept.

>>> import ast
>>> a = ast.literal_eval('(1, 2, 3)')
>>> a
(1, 2, 3)
>>> b = ast.literal_eval('__import__("evil")')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/ast.py", line 68, in literal_eval
    return _convert(node_or_string)
  File "/usr/lib/python2.6/ast.py", line 67, in _convert
    raise ValueError('malformed string')
ValueError: malformed string

Use cases like this are exactly what this function is intended for.

流年已逝 2024-10-09 04:52:44

我不确定评估配置文件中可能包含的任意文本是否是个好主意。对 eval 的正常调用通常被认为是不安全的。

请参阅:

  1. Python eval() 在不受信任的字符串上的安全性?
  2. 在Python中使用eval?

如果你想使用python数据类型,那么它会好得多将其存储为 python 模块并导入它。在这种情况下,这可能是更好的解决方案。

您可以将配置文件拆分为 python 模块中包含 python 数据类型的配置文件,并将其余部分保留为可以由 configparser 解析的配置文件。

I am not sure if it is a good idea to eval an arbitary text that may be contained in the config file. A normal call to eval is usually considered unsafe.

See:

  1. Security of Python's eval() on untrusted strings?
  2. Use of eval in Python?

If you want to use python data types, then it is much better to store it as a python module and import it. This might be a better solution in this case.

You can split up the config file as those containing python data types in a python module and keeping the rest as config file that can be parsed by configparser.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文