python ast.literal_eval和datetime

时间:2010-11-20 23:35:19

标签: python

我有一个字符串"{'datetime': datetime.datetime(2010, 11, 21, 0, 56, 58)}",我想将其转换为它所代表的对象。使用ast.literal_eval()给出

ValueError: malformed string; 

因为它不允许构造对象(即datetime调用)。无论如何要么让ast正确处理这个问题,要么保护eval以防止代码注入?

6 个答案:

答案 0 :(得分:14)

跟进Ignacio Vazquez-Abrams'想法:

import ast
import datetime

def parse_datetime_dict(astr,debug=False):
    try: tree=ast.parse(astr)
    except SyntaxError: raise ValueError(astr)
    for node in ast.walk(tree):
        if isinstance(node,(ast.Module,ast.Expr,ast.Dict,ast.Str,
                            ast.Attribute,ast.Num,ast.Name,ast.Load, ast.Tuple)): continue
        if (isinstance(node,ast.Call)
                and isinstance(node.func, ast.Attribute)
                and node.func.attr == 'datetime'): continue
        if debug:
            attrs=[attr for attr in dir(node) if not attr.startswith('__')]
            print(node)
            for attrname in attrs:
                print('    {k} ==> {v}'.format(k=attrname,v=getattr(node,attrname)))
        raise ValueError(astr)
    return eval(astr)

good_strings=["{'the_datetime': datetime.datetime(2010, 11, 21, 0, 56, 58)}"]
bad_strings=["__import__('os'); os.unlink",
             "import os; os.unlink",
             "import(os)", # SyntaxError
             ]

for astr in good_strings:
    result=parse_datetime_dict(astr)    
    print('{s} ... [PASSED]'.format(s=astr))

for astr in bad_strings:
    try:
        result=parse_datetime_dict(astr)
    except ValueError:
        print('{s} ... [REJECTED]'.format(s=astr))
    else:
        sys.exit('ERROR: failed to catch {s!r}'.format(s=astr))

产量

{'the_datetime': datetime.datetime(2010, 11, 21, 0, 56, 58)} ... [PASSED]
__import__('os'); os.unlink ... [REJECTED]
import os; os.unlink ... [REJECTED]
import(os) ... [REJECTED]

答案 1 :(得分:8)

您可以使用(2010, 11, 21, 0, 56, 58)从字符串中提取regex字符,将其传递给ast.literal_eval()以获取元组,然后将该元组传递给datetime.datetime(*a_tuple)以获取物体。听起来很多,但每个步骤都非常简单(而且安全)。 这就是我所说的:

import ast
import datetime
import re

s = "{'datetime': datetime.datetime(2010, 11, 21, 0, 56, 58)}"
m = re.search(r"""datetime(\((\d+)(,\s*\d+)*\))""", s)
if m:  # any matches?
    args = ast.literal_eval(m.group(1))
    print datetime.datetime(*args)
    # 2010-11-21 00:56:58

这将在字符串中搜索模式"datetime(<comma separated list of integers>)",并仅将文字整数值列表传递给ast.literal_eval(),以便转换为元组 - 这应该始终成功并且是代码注入抵抗的。我相信它被称为“上下文敏感字符串评估”或CSSE。

答案 2 :(得分:3)

不必编写大量代码,在必须解析datetime objs时不要使用ast。你可以运行eval()。请注意,如果字符串可能包含狡猾的python命令,则使用此函数可能会出现安全问题。

以下是它的工作原理:

>>> x="{'datetime': datetime.datetime(2010, 11, 21, 0, 56, 58)}"
>>> b=eval(x)
>>> b
{'datetime': datetime.datetime(2010, 11, 21, 0, 56, 58)}
>>> b["datetime"].year
2010

享受! :d

答案 3 :(得分:1)

使用language services将其编译成AST,走AST,确保它只包含列入白名单的节点集,然后执行它。

答案 4 :(得分:0)

我遇到了这个问题,并通过将datetime对象替换为这样的字符串来解决了这个问题:

            if mystrobj.__contains__('datetime.datetime('):
                mystrobj = re.sub(r"datetime.datetime([(),0-9 ]*),", r"'\1',", mystrobj)

答案 5 :(得分:-2)

使用repr(datetime.datetime.utcnow())保存在您的dict或文件中。看,

>>> import datetime
>>> oarg = datetime.datetime.utcnow()
>>> oarg
datetime.datetime(2013, 2, 6, 12, 39, 51, 709024)
>>> butiwantthis = repr(oarg)
>>> butiwantthis
'datetime.datetime(2013, 2, 6, 12, 39, 51, 709024)'
>>> eval(oarg)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: eval() arg 1 must be a string, bytes or code object
>>> eval(butiwantthis)
datetime.datetime(2013, 2, 6, 12, 39, 51, 709024)

酷! eval()有效!

注意导入

>>> from datetime import datetime, date, time
>>> oarg = datetime.datetime.utcnow()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: type object 'datetime.datetime' has no attribute 'datetime'
>>> oarg = datetime.utcnow()
>>> oarg
datetime.datetime(2013, 2, 6, 12, 41, 51, 870458)
>>> butiwantthis = repr(oarg)
>>> butiwantthis
'datetime.datetime(2013, 2, 6, 12, 41, 51, 870458)'
>>> eval(butiwantthis)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>
AttributeError: type object 'datetime.datetime' has no attribute 'datetime'
>>> # wrong import

完美!

Ps:Python3.3