-
-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added SARIF support * Semgrep version bump * Rule QA
- Loading branch information
1 parent
55cb900
commit a17982f
Showing
12 changed files
with
337 additions
and
143 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Empty file.
This file contains 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,64 @@ | ||
# -*- coding: utf_8 -*- | ||
"""CLI njsscan output format.""" | ||
from njsscan.logger import init_logger | ||
|
||
logger = init_logger(__name__) | ||
|
||
|
||
def format_cli(rule_id, details): | ||
"""Get CLI friendly format.""" | ||
items = [] | ||
items.append('\n===================================================' | ||
'===================================================') | ||
items.append(f'RULE ID: {rule_id}') | ||
for meta, value in details['metadata'].items(): | ||
if meta == 'id': | ||
continue | ||
meta_format = meta.upper().replace('_', '') | ||
items.append(f'{meta_format}: {value}') | ||
items.append('===================================================' | ||
'===================================================') | ||
files = details.get('files') | ||
if not files: | ||
return '\n'.join(items) | ||
items.append('\n__________________FILES___________________________') | ||
for match in files: | ||
items.append('\n') | ||
file_path = match['file_path'] | ||
items.append(f'File: {file_path}') | ||
position = match['match_position'] | ||
items.append(f'Match Position: {position[0]} - {position[1]}') | ||
lines = match.get('match_lines') | ||
line = (lines[0] if lines[0] == lines[1] | ||
else f'{lines[0]}: {lines[1]}') | ||
items.append(f'Line Number(s): {line}') | ||
match_string = match['match_string'] | ||
if isinstance(match_string, list): | ||
match_string = '\n'.join(ln.strip() for ln in match_string) | ||
items.append(f'Match String: {match_string}') | ||
return '\n'.join(items) | ||
|
||
|
||
def cli_output(outfile, scan_results): | ||
"""Format output printing.""" | ||
if not scan_results: | ||
return | ||
scan_results.pop('errors', None) | ||
buffer = [] | ||
for out in scan_results: | ||
for rule_id, details in scan_results[out].items(): | ||
formatted = format_cli(rule_id, details) | ||
buffer.append(formatted) | ||
severity = details['metadata']['severity'].lower() | ||
if not outfile: | ||
if severity == 'error': | ||
logger.error(formatted) | ||
elif severity == 'warning': | ||
logger.warning(formatted) | ||
else: | ||
logger.info(formatted) | ||
if outfile and buffer: | ||
outdata = '\n'.join(buffer) | ||
with open(outfile, 'w') as of: | ||
of.write(outdata) | ||
return buffer |
This file contains 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,16 @@ | ||
# -*- coding: utf_8 -*- | ||
"""JSON output format.""" | ||
import json | ||
|
||
|
||
def json_output(outfile, scan_results): | ||
"""JSON Output.""" | ||
if outfile: | ||
with open(outfile, 'w') as of: | ||
json.dump(scan_results, of, sort_keys=True, | ||
indent=2, separators=(',', ': ')) | ||
else: | ||
json_output = (json.dumps(scan_results, sort_keys=True, | ||
indent=2, separators=(',', ': '))) | ||
print(json_output) | ||
return json_output |
Oops, something went wrong.