Example #1
0
def test_RestrictingNodeTransformer__generic_visit__1():
    """It log an error if there is an unknown ast node visited."""
    class MyFancyNode(ast.AST):
        pass

    transformer = RestrictingNodeTransformer()
    transformer.visit(MyFancyNode())
    assert transformer.errors == [
        'Line None: MyFancyNode statements are not allowed.']
    assert transformer.warnings == [
        'Line None: MyFancyNode statement is not known to RestrictedPython']
Example #2
0
class UntrustedPythonExpr(expressions.PythonExpr):
    restricted_python_transformer = RestrictingNodeTransformer()
    page_templates_expression_transformer = RestrictionTransform()

    # Make copy of parent expression builtins
    builtins = expressions.PythonExpr.builtins.copy()

    # Update builtins with Restricted Python utility builtins
    builtins.update({
        name: static(builtin)
        for (name, builtin) in utility_builtins.items()
    })

    def rewrite(self, node):
        if node.id == 'repeat':
            node.id = 'wrapped_repeat'
        else:
            node = super().rewrite(node)

        return node

    def parse(self, string):
        encoded = string.encode('utf-8')
        node = parse(encoded, mode='eval')

        # Run Node Transformation from RestrictedPython:
        self.restricted_python_transformer.visit(node)

        # Run PageTemplate.expression RestrictedPython Transform:
        self.page_templates_expression_transformer.visit(node)

        return node
Example #3
0
class UntrustedPythonExpr(expressions.PythonExpr):
    restricted_python_transformer = RestrictingNodeTransformer()
    page_templates_expression_transformer = RestrictionTransform()

    # Make copy of parent expression builtins
    builtins = expressions.PythonExpr.builtins.copy()

    # Update builtins with Restricted Python utility builtins
    builtins.update(
        dict((name, static(builtin))
             for (name, builtin) in utility_builtins.items()))

    def parse(self, string):
        encoded = string.encode('utf-8')
        node = parse(encoded, mode='eval')

        # Run Node Transformation from RestrictedPython:
        self.restricted_python_transformer.visit(node)

        # Run PageTemplate.expression RestrictedPython Transform:
        self.page_templates_expression_transformer.visit(node)

        return node