Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Bandit json parser #44

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ Now you will see a review with linting errors...
```
$ stylelint . | lintly --format=stylelint
```
- [bandit](https://github.com/PyCQA/bandit)
```
$ bandit -r . --format=json | lintly --format=bandit-json
```

- [cfn-lint](https://github.com/aws-cloudformation/cfn-python-lint)
```
Expand Down
74 changes: 74 additions & 0 deletions lintly/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,77 @@ def parse_violations(self, output):
return violations


class BanditJSONParser(BaseLintParser):
"""
Bandit JSON format:

[
{
"errors": [],
"generated_at": "2021-01-07T23:39:39Z",
"metrics": {
"./lintly/formatters.py": {
"CONFIDENCE.HIGH": 1.0,
"CONFIDENCE.LOW": 0.0,
"CONFIDENCE.MEDIUM": 0.0,
"CONFIDENCE.UNDEFINED": 0.0,
"SEVERITY.HIGH": 1.0,
"SEVERITY.LOW": 0.0,
"SEVERITY.MEDIUM": 0.0,
"SEVERITY.UNDEFINED": 0.0,
"loc": 31,
"nosec": 0
},
"results": [
{
"code": "13 \n14 env = Environment(\n15 loader=FileSystemLoader(TEMPLATES_PATH),
\n16 autoescape=False\n17 )\n",
"filename": "./lintly/formatters.py",
"issue_confidence": "HIGH",
"issue_severity": "HIGH",
"issue_text": "Using jinja2 templates with autoescape=False is dangerous and can lead to XSS."
"Use autoescape=True or use the select_autoescape function.",
"line_number": 14,
"line_range": [
14,
15,
16
],
"more_info": "https://bandit.readthedocs.io/en/latest/plugins/b701_jinja2_autoescape_false.html",
"test_id": "B701"
"test_name": "jinja2_autoescape_false"
}
]
}
]

"""

def parse_violations(self, output):

output = output.strip()
if not output:
return dict()

json_data = json.loads(output)

violations = collections.defaultdict(list)
for violation_json in json_data["results"]:
violation = Violation(
line=violation_json["line_number"],
column=0,
code="{} ({})".format(
violation_json["test_id"], violation_json["test_name"]
),
message=violation_json["issue_text"],
)

path = self._normalize_path(violation_json["filename"])
violations[path].append(violation)

return violations


class CfnNagParser(BaseLintParser):

def parse_violations(self, output):
Expand Down Expand Up @@ -294,6 +365,9 @@ def parse_violations(self, output):
# cfn-lint default formatter
'cfn-lint': CfnLintParser(),

# Bandit Parser
"bandit-json": BanditJSONParser(),

# cfn-nag JSON output
'cfn-nag': CfnNagParser(),
}
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ Jinja2<3.0
PyGithub<2.0
cached-property<2.0
ci-py
click<7.0
click<8.0
codecov
coverage
flake8
mock
pytest
python-gitlab<2.0
python-gitlab<3.0
tox
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
'click<8.0',
'Jinja2<3.0',
'PyGithub<2.0',
'python-gitlab<2.0',
'python-gitlab<3.0',
'six',
]

Expand Down
120 changes: 120 additions & 0 deletions tests/linters_output/bandit-json.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
{
"errors": [],
"generated_at": "2021-01-07T23:39:39Z",
"metrics": {
"./lintly/formatters.py": {
"CONFIDENCE.HIGH": 1.0,
"CONFIDENCE.LOW": 0.0,
"CONFIDENCE.MEDIUM": 0.0,
"CONFIDENCE.UNDEFINED": 0.0,
"SEVERITY.HIGH": 1.0,
"SEVERITY.LOW": 0.0,
"SEVERITY.MEDIUM": 0.0,
"SEVERITY.UNDEFINED": 0.0,
"loc": 31,
"nosec": 0
},
"_totals": {
"CONFIDENCE.HIGH": 6.0,
"CONFIDENCE.LOW": 0.0,
"CONFIDENCE.MEDIUM": 0.0,
"CONFIDENCE.UNDEFINED": 0.0,
"SEVERITY.HIGH": 2.0,
"SEVERITY.LOW": 4.0,
"SEVERITY.MEDIUM": 0.0,
"SEVERITY.UNDEFINED": 0.0,
"loc": 2596,
"nosec": 0
}
},
"results": [
{
"code": "13 \n14 env = Environment(\n15 loader=FileSystemLoader(TEMPLATES_PATH),\n16 autoescape=False\n17 )\n",
"filename": "./build/lib/lintly/formatters.py",
"issue_confidence": "HIGH",
"issue_severity": "HIGH",
"issue_text": "Using jinja2 templates with autoescape=False is dangerous and can lead to XSS. Use autoescape=True or use the select_autoescape function to mitigate XSS vulnerabilities.",
"line_number": 14,
"line_range": [
14,
15,
16
],
"more_info": "https://bandit.readthedocs.io/en/latest/plugins/b701_jinja2_autoescape_false.html",
"test_id": "B701",
"test_name": "jinja2_autoescape_false"
},
{
"code": "13 \n14 env = Environment(\n15 loader=FileSystemLoader(TEMPLATES_PATH),\n16 autoescape=False\n17 )\n",
"filename": "./lintly/formatters.py",
"issue_confidence": "HIGH",
"issue_severity": "HIGH",
"issue_text": "Using jinja2 templates with autoescape=False is dangerous and can lead to XSS. Use autoescape=True or use the select_autoescape function to mitigate XSS vulnerabilities.",
"line_number": 14,
"line_range": [
14,
15,
16
],
"more_info": "https://bandit.readthedocs.io/en/latest/plugins/b701_jinja2_autoescape_false.html",
"test_id": "B701",
"test_name": "jinja2_autoescape_false"
},
{
"code": "47 builds.LintlyBuild(config, \"Some linter output\")\n48 assert GitHubBackend.call_args[1][\"context\"] == format_and_context[2]\n",
"filename": "./tests/test_builds.py",
"issue_confidence": "HIGH",
"issue_severity": "LOW",
"issue_text": "Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.",
"line_number": 48,
"line_range": [
48
],
"more_info": "https://bandit.readthedocs.io/en/latest/plugins/b101_assert_used.html",
"test_id": "B101",
"test_name": "assert_used"
},
{
"code": "12 result = runner.invoke(cli.main, ['--help'])\n13 assert result.exit_code == 0\n14 assert not result.exception\n",
"filename": "./tests/test_cli.py",
"issue_confidence": "HIGH",
"issue_severity": "LOW",
"issue_text": "Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.",
"line_number": 13,
"line_range": [
13
],
"more_info": "https://bandit.readthedocs.io/en/latest/plugins/b101_assert_used.html",
"test_id": "B101",
"test_name": "assert_used"
},
{
"code": "13 assert result.exit_code == 0\n14 assert not result.exception\n15 assert 'Usage' in result.output\n",
"filename": "./tests/test_cli.py",
"issue_confidence": "HIGH",
"issue_severity": "LOW",
"issue_text": "Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.",
"line_number": 14,
"line_range": [
14
],
"more_info": "https://bandit.readthedocs.io/en/latest/plugins/b101_assert_used.html",
"test_id": "B101",
"test_name": "assert_used"
},
{
"code": "14 assert not result.exception\n15 assert 'Usage' in result.output\n16 \n",
"filename": "./tests/test_cli.py",
"issue_confidence": "HIGH",
"issue_severity": "LOW",
"issue_text": "Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.",
"line_number": 15,
"line_range": [
15
],
"more_info": "https://bandit.readthedocs.io/en/latest/plugins/b101_assert_used.html",
"test_id": "B101",
"test_name": "assert_used"
}
]
}
33 changes: 33 additions & 0 deletions tests/test_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,39 @@ class Flake8ParserTestCase(ParserTestCaseMixin, unittest.TestCase):
}


class BanditJSONParserTestCase(ParserTestCaseMixin, unittest.TestCase):
parser = PARSERS['bandit-json']
linter_output_file_name = 'bandit-json.txt'
expected_violations = {
'build/lib/lintly/formatters.py': [
{'line': 14, 'column': 0, 'code': 'B701 (jinja2_autoescape_false)',
'message': ('Using jinja2 templates with autoescape=False is dangerous and can lead to XSS. '
'Use autoescape=True or use the select_autoescape function to mitigate XSS vulnerabilities.')}
],
'lintly/formatters.py': [
{'line': 14, 'column': 0, 'code': 'B701 (jinja2_autoescape_false)',
'message': ('Using jinja2 templates with autoescape=False is dangerous and can lead to XSS. '
'Use autoescape=True or use the select_autoescape function to mitigate XSS vulnerabilities.')}
],
'tests/test_builds.py': [
{'line': 48, 'column': 0, 'code': 'B101 (assert_used)',
'message': ('Use of assert detected. '
'The enclosed code will be removed when compiling to optimised byte code.')}
],
'tests/test_cli.py': [
{'line': 13, 'column': 0, 'code': 'B101 (assert_used)',
'message': ('Use of assert detected. '
'The enclosed code will be removed when compiling to optimised byte code.')},
{'line': 14, 'column': 0, 'code': 'B101 (assert_used)',
'message': ('Use of assert detected. '
'The enclosed code will be removed when compiling to optimised byte code.')},
{'line': 15, 'column': 0, 'code': 'B101 (assert_used)',
'message': ('Use of assert detected. '
'The enclosed code will be removed when compiling to optimised byte code.')}
]
}


class PylintJSONParserTestCase(ParserTestCaseMixin, unittest.TestCase):
parser = PARSERS['pylint-json']
linter_output_file_name = 'pylint-json.txt'
Expand Down