-
Couldn't load subscription status.
- Fork 249
Output tweaks #172
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
Merged
Merged
Output tweaks #172
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Add colourful formatter "screen"
Prints vulnerabilities with ANSI colour codes for the terminal. Not crazily colourful: just tries to highlight the important stuff. Repeated filenames aren't printed. Colour scheme might not be to everyone's taste.
- Loading branch information
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,4 +15,5 @@ source = | |
| ./tests | ||
| omit = | ||
| pyt/formatters/json.py | ||
| pyt/formatters/screen.py | ||
| pyt/formatters/text.py | ||
104 changes: 104 additions & 0 deletions
pyt/formatters/screen.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| """This formatter outputs the issues as color-coded text.""" | ||
| from ..vulnerabilities.vulnerability_helper import SanitisedVulnerability, UnknownVulnerability | ||
|
|
||
| RESET = '033円[0m' | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❤️ |
||
| BOLD = '033円[1m' | ||
| UNDERLINE = '033円[4m' | ||
| DANGER = '033円[31m' | ||
| GOOD = '033円[32m' | ||
| HIGHLIGHT = '033円[45;1m' | ||
| RED_ON_WHITE = '033円[31m033円[107m' | ||
|
|
||
|
|
||
| def color(string, color_string): | ||
| return color_string + str(string) + RESET | ||
|
|
||
|
|
||
| def report( | ||
| vulnerabilities, | ||
| fileobj, | ||
| print_sanitised, | ||
| ): | ||
| """ | ||
| Prints issues in color-coded text format. | ||
|
|
||
| Args: | ||
| vulnerabilities: list of vulnerabilities to report | ||
| fileobj: The output file object, which may be sys.stdout | ||
| """ | ||
| n_vulnerabilities = len(vulnerabilities) | ||
| unsanitised_vulnerabilities = [v for v in vulnerabilities if not isinstance(v, SanitisedVulnerability)] | ||
| n_unsanitised = len(unsanitised_vulnerabilities) | ||
| n_sanitised = n_vulnerabilities - n_unsanitised | ||
| heading = "{} vulnerabilit{} found{}.\n".format( | ||
| 'No' if n_unsanitised == 0 else n_unsanitised, | ||
| 'y' if n_unsanitised == 1 else 'ies', | ||
| " (plus {} sanitised)".format(n_sanitised) if n_sanitised else "", | ||
| ) | ||
| vulnerabilities_to_print = vulnerabilities if print_sanitised else unsanitised_vulnerabilities | ||
| with fileobj: | ||
| for i, vulnerability in enumerate(vulnerabilities_to_print, start=1): | ||
| fileobj.write(vulnerability_to_str(i, vulnerability)) | ||
|
|
||
| if n_unsanitised == 0: | ||
| fileobj.write(color(heading, GOOD)) | ||
| else: | ||
| fileobj.write(color(heading, DANGER)) | ||
|
|
||
|
|
||
| def vulnerability_to_str(i, vulnerability): | ||
| lines = [] | ||
| lines.append(color('Vulnerability {}'.format(i), UNDERLINE)) | ||
| lines.append('File: {}'.format(color(vulnerability.source.path, BOLD))) | ||
| lines.append( | ||
| 'User input at line {}, source "{}":'.format( | ||
| vulnerability.source.line_number, | ||
| color(vulnerability.source_trigger_word, HIGHLIGHT), | ||
| ) | ||
| ) | ||
| lines.append('\t{}'.format(color(vulnerability.source.label, RED_ON_WHITE))) | ||
| if vulnerability.reassignment_nodes: | ||
| previous_path = None | ||
| lines.append('Reassigned in:') | ||
| for node in vulnerability.reassignment_nodes: | ||
| if node.path != previous_path: | ||
| lines.append('\tFile: {}'.format(node.path)) | ||
| previous_path = node.path | ||
| label = node.label | ||
| if ( | ||
| isinstance(vulnerability, SanitisedVulnerability) and | ||
| node.label == vulnerability.sanitiser.label | ||
| ): | ||
| label = color(label, GOOD) | ||
| lines.append( | ||
| '\t Line {}:\t{}'.format( | ||
| node.line_number, | ||
| label, | ||
| ) | ||
| ) | ||
| if vulnerability.source.path != vulnerability.sink.path: | ||
| lines.append('File: {}'.format(color(vulnerability.sink.path, BOLD))) | ||
| lines.append( | ||
| 'Reaches line {}, sink "{}"'.format( | ||
| vulnerability.sink.line_number, | ||
| color(vulnerability.sink_trigger_word, HIGHLIGHT), | ||
| ) | ||
| ) | ||
| lines.append('\t{}'.format( | ||
| color(vulnerability.sink.label, RED_ON_WHITE) | ||
| )) | ||
| if isinstance(vulnerability, SanitisedVulnerability): | ||
| lines.append( | ||
| 'This vulnerability is {}{} by {}'.format( | ||
| color('potentially ', BOLD) if not vulnerability.confident else '', | ||
| color('sanitised', GOOD), | ||
| color(vulnerability.sanitiser.label, BOLD), | ||
| ) | ||
| ) | ||
| elif isinstance(vulnerability, UnknownVulnerability): | ||
| lines.append( | ||
| 'This vulnerability is unknown due to "{}"'.format( | ||
| color(vulnerability.unknown_assignment.label, BOLD), | ||
| ) | ||
| ) | ||
| return '\n'.join(lines) + '\n\n' | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.