1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
from RestrictedPython import compile_restricted_exec
from tests.helper import restricted_exec
BAD_ATTR_UNDERSCORE = """\
def bad_attr():
some_ob = object()
some_ob._some_attr = 15
"""
def test_RestrictingNodeTransformer__visit_Attribute__1():
"""It is an error if a bad attribute name is used."""
result = compile_restricted_exec(BAD_ATTR_UNDERSCORE)
assert result.errors == (
'Line 3: "_some_attr" is an invalid attribute name because it '
'starts with "_".',)
BAD_ATTR_ROLES = """\
def bad_attr():
some_ob = object()
some_ob.abc__roles__
"""
def test_RestrictingNodeTransformer__visit_Attribute__2():
"""It is an error if a bad attribute name is used."""
result = compile_restricted_exec(BAD_ATTR_ROLES)
assert result.errors == (
'Line 3: "abc__roles__" is an invalid attribute name because it '
'ends with "__roles__".',)
TRANSFORM_ATTRIBUTE_ACCESS = """\
def func():
return a.b
"""
def test_RestrictingNodeTransformer__visit_Attribute__3(mocker):
"""It transforms the attribute access to `_getattr_`."""
glb = {
'_getattr_': mocker.stub(),
'a': [],
'b': 'b'
}
restricted_exec(TRANSFORM_ATTRIBUTE_ACCESS, glb)
glb['func']()
glb['_getattr_'].assert_called_once_with([], 'b')
ALLOW_UNDERSCORE_ONLY = """\
def func():
some_ob = object()
some_ob._
"""
def test_RestrictingNodeTransformer__visit_Attribute__4():
"""It allows `_` as attribute name."""
result = compile_restricted_exec(ALLOW_UNDERSCORE_ONLY)
assert result.errors == ()
def test_RestrictingNodeTransformer__visit_Attribute__5(
mocker):
"""It transforms writing to an attribute to `_write_`."""
glb = {
'_write_': mocker.stub(),
'a': mocker.stub(),
}
glb['_write_'].return_value = glb['a']
restricted_exec("a.b = 'it works'", glb)
glb['_write_'].assert_called_once_with(glb['a'])
assert glb['a'].b == 'it works'
def test_RestrictingNodeTransformer__visit_Attribute__5_5(
mocker):
"""It transforms deleting of an attribute to `_write_`."""
glb = {
'_write_': mocker.stub(),
'a': mocker.stub(),
}
glb['a'].b = 'it exists'
glb['_write_'].return_value = glb['a']
restricted_exec("del a.b", glb)
glb['_write_'].assert_called_once_with(glb['a'])
assert not hasattr(glb['a'], 'b')
DISALLOW_TRACEBACK_ACCESS = """
try:
raise Exception()
except Exception as e:
tb = e.__traceback__
"""
def test_RestrictingNodeTransformer__visit_Attribute__6():
"""It denies access to the __traceback__ attribute."""
result = compile_restricted_exec(DISALLOW_TRACEBACK_ACCESS)
assert result.errors == (
'Line 5: "__traceback__" is an invalid attribute name because '
'it starts with "_".',)
TRANSFORM_ATTRIBUTE_ACCESS_FUNCTION_DEFAULT = """
def func_default(x=a.a):
return x
"""
def test_RestrictingNodeTransformer__visit_Attribute__7(
mocker):
"""It transforms attribute access in function default kw to `_write_`."""
_getattr_ = mocker.Mock()
_getattr_.side_effect = getattr
glb = {
'_getattr_': _getattr_,
'a': mocker.Mock(a=1),
}
restricted_exec(TRANSFORM_ATTRIBUTE_ACCESS_FUNCTION_DEFAULT, glb)
_getattr_.assert_has_calls([mocker.call(glb['a'], 'a')])
assert glb['func_default']() == 1
def test_RestrictingNodeTransformer__visit_Attribute__8(
mocker):
"""It transforms attribute access in lamda default kw to `_write_`."""
_getattr_ = mocker.Mock()
_getattr_.side_effect = getattr
glb = {
'_getattr_': _getattr_,
'b': mocker.Mock(b=2)
}
restricted_exec('lambda_default = lambda x=b.b: x', glb)
_getattr_.assert_has_calls([mocker.call(glb['b'], 'b')])
assert glb['lambda_default']() == 2
|