Пример #1
0
    def pre_visit(self, node):
        self.context = {}
        self.context["imports"] = self.imports
        self.context["import_aliases"] = self.import_aliases

        if self.debug:
            LOG.debug(ast.dump(node))
            self.metaast.add_node(node, "", self.depth)

        if hasattr(node, "lineno"):
            self.context["lineno"] = node.lineno

            # explicitly check for empty set to skip all tests for a line
            nosec_tests = self.nosec_lines.get(node.lineno)
            if nosec_tests is not None and not len(nosec_tests):
                LOG.debug("skipped, nosec without test number")
                self.metrics.note_nosec()
                return False

        if hasattr(node, "col_offset"):
            self.context["col_offset"] = node.col_offset

        self.context["node"] = node
        self.context["linerange"] = b_utils.linerange(node)
        self.context["filename"] = self.fname
        self.context["file_data"] = self.fdata

        self.seen += 1
        LOG.debug(
            "entering: %s %s [%s]", hex(id(node)), type(node), self.depth
        )
        self.depth += 1
        LOG.debug(self.context)
        return True
Пример #2
0
    def test_linerange(self):
        self.test_file = open("./examples/jinja2_templating.py")
        self.tree = ast.parse(self.test_file.read())
        # Check linerange returns corrent number of lines
        line = self.tree.body[8]
        lrange = b_utils.linerange(line)

        # line 9 should be three lines long
        self.assertEqual(3, len(lrange))

        # the range should be the correct line numbers
        self.assertEqual([11, 12, 13], list(lrange))
Пример #3
0
    def test_linerange(self):
        self.test_file = open("./examples/jinja2_templating.py")
        self.tree = ast.parse(self.test_file.read())
        # Check linerange returns corrent number of lines
        line = self.tree.body[8]
        lrange = b_utils.linerange(line)

        # line 9 should be three lines long
        self.assertEqual(3, len(lrange))

        # the range should be the correct line numbers
        self.assertEqual([11, 12, 13], list(lrange))
Пример #4
0
    def visit_Bytes(self, node):
        """Visitor for AST Bytes nodes

        add relevant information about node to
        the context for use in tests which inspect strings.
        :param node: The node that is being inspected
        :return: -
        """
        self.context["bytes"] = node.s
        if not isinstance(node._bandit_parent, ast.Expr):  # docstring
            self.context["linerange"] = b_utils.linerange(node._bandit_parent)
            self.update_scores(self.tester.run_tests(self.context, "Bytes"))