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

Exit code 1 if unsanitised vulnerabilities found #156

Merged
merged 3 commits into from
Jul 27, 2018
Merged
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
5 changes: 5 additions & 0 deletions pyt/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
get_vulnerabilities_not_in_baseline,
UImode
)
from .vulnerabilities.vulnerability_helper import SanitisedVulnerability
from .web_frameworks import (
FrameworkAdaptor,
is_django_view_function,
Expand Down Expand Up @@ -137,6 +138,10 @@ def main(command_line_args=sys.argv[1:]): # noqa: C901
else:
text.report(vulnerabilities, args.output_file)

has_unsanitized_vulnerabilities = any(not isinstance(v, SanitisedVulnerability) for v in vulnerabilities)
if has_unsanitized_vulnerabilities:
sys.exit(1)


if __name__ == '__main__':
main()
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
install_requires=[],
entry_points={
'console_scripts': [
'pyt = pyt:main'
'pyt = pyt.__main__:main'
]
}
)
55 changes: 35 additions & 20 deletions tests/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,41 @@ def test_text_output(self, mock_text, mock_find_vulnerabilities, mock_parse_args

mock_discover_files.return_value = [example_file]
mock_parse_args.return_value = mock.Mock(
autospec=True,
project_root=None,
baseline=None,
json=None,
output_file=output_file
)
main([
'parse_args is mocked'
])
with self.assertRaises(SystemExit):
main(['parse_args is mocked'])
assert mock_text.report.call_count == 1
# This with: makes no sense
with self.assertRaises(AssertionError):
assert mock_text.report.assert_called_with(
mock_find_vulnerabilities.return_value,
mock_parse_args.return_value.output_file
)
mock_text.report.assert_called_with(
mock_find_vulnerabilities.return_value,
mock_parse_args.return_value.output_file
)

@mock.patch('pyt.__main__.discover_files')
@mock.patch('pyt.__main__.parse_args')
@mock.patch('pyt.__main__.find_vulnerabilities')
@mock.patch('pyt.__main__.text')
def test_no_vulns_found(self, mock_text, mock_find_vulnerabilities, mock_parse_args, mock_discover_files):
mock_find_vulnerabilities.return_value = []
example_file = 'examples/vulnerable_code/inter_command_injection.py'
output_file = 'mocked_outfile'

mock_discover_files.return_value = [example_file]
mock_parse_args.return_value = mock.Mock(
project_root=None,
baseline=None,
json=None,
output_file=output_file
)
main(['parse_args is mocked']) # No SystemExit
assert mock_text.report.call_count == 1
mock_text.report.assert_called_with(
mock_find_vulnerabilities.return_value,
mock_parse_args.return_value.output_file
)

@mock.patch('pyt.__main__.discover_files')
@mock.patch('pyt.__main__.parse_args')
Expand All @@ -44,22 +63,18 @@ def test_json_output(self, mock_json, mock_find_vulnerabilities, mock_parse_args

mock_discover_files.return_value = [example_file]
mock_parse_args.return_value = mock.Mock(
autospec=True,
project_root=None,
baseline=None,
json=True,
output_file=output_file
)
main([
'parse_args is mocked'
])
with self.assertRaises(SystemExit):
main(['parse_args is mocked'])
assert mock_json.report.call_count == 1
# This with: makes no sense
with self.assertRaises(AssertionError):
assert mock_json.report.assert_called_with(
mock_find_vulnerabilities.return_value,
mock_parse_args.return_value.output_file
)
mock_json.report.assert_called_with(
Copy link
Collaborator

@KevinHock KevinHock Jul 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're my hero! :D
What was the bug? re: This with: makes no sense

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@KevinHock no, the function mock_text.report.assert_called_with() does the assertion - it either raises AssertionError or returns None.

Therefore, if you do assert mock_text.report.assert_called_with() it's like assert None.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow 🤦‍♂️ I must have had tunnel vision at the time. Thanks so much for fixing that 😄

mock_find_vulnerabilities.return_value,
mock_parse_args.return_value.output_file
)


class DiscoverFilesTest(BaseTestCase):
Expand Down