Skip to content

Commit

Permalink
Deleting an attribute has to be guarded, too.
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Howitz committed May 5, 2017
1 parent f843967 commit 10df6c5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/RestrictedPython/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -835,8 +835,9 @@ def visit_Attribute(self, node):
"""Checks and mutates attribute access/assignment.
'a.b' becomes '_getattr_(a, "b")'
'a.b = c' becomes '_write_(a).b = c'
'del a.b' becomes 'del _write_(a).b'
The _write_ function should return a security proxy.
"""
if node.attr.startswith('_') and node.attr != '_':
Expand All @@ -861,7 +862,7 @@ def visit_Attribute(self, node):
copy_locations(new_node, node)
return new_node

elif isinstance(node.ctx, ast.Store):
elif isinstance(node.ctx, (ast.Store, ast.Del)):
node = self.node_contents_visit(node)
new_value = ast.Call(
func=ast.Name('_write_', ast.Load()),
Expand Down
17 changes: 17 additions & 0 deletions tests/transformer/test_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,23 @@ def test_transformer__RestrictingNodeTransformer__visit_Attribute__5(
assert glb['a'].b == 'it works'


@pytest.mark.parametrize(*e_exec)
def test_transformer__RestrictingNodeTransformer__visit_Attribute__5_5(
e_exec, 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']

e_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()
Expand Down

0 comments on commit 10df6c5

Please sign in to comment.