|
| 1 | +import io |
| 2 | +import logging |
| 3 | +import tokenize |
| 4 | + |
| 5 | +from bandit.core import config |
| 6 | +from bandit.core import docs_utils |
| 7 | +from bandit.core import manager |
| 8 | +from bandit.core import meta_ast |
| 9 | +from bandit.core import metrics |
| 10 | +from bandit.core import node_visitor |
| 11 | +from bandit.core import test_set |
| 12 | +from pyscript import document |
| 13 | + |
| 14 | + |
| 15 | +# Disable noisy output from Bandit getting rendered to page |
| 16 | +logging.basicConfig(level=logging.ERROR) |
| 17 | + |
| 18 | +ISSUE_BLOCK = """ |
| 19 | + <div id="issue-{issue_no}"> |
| 20 | + <div class="issue-block {issue_class}"> |
| 21 | + <b>[{test_id}:{test_name}]</b> {test_text}<br> |
| 22 | + <b>Severity: </b>{severity}<br> |
| 23 | + <b>Confidence: </b>{confidence}<br> |
| 24 | + <b>CWE: </b><a href="{cwe_link}" target="_blank">{cwe}</a><br> |
| 25 | + <b>More info: </b><a href="{url}" target="_blank">{url}</a><br> |
| 26 | + <b>Location: </b><stdin>:{line_number}:{col_offset}<br> |
| 27 | + </div> |
| 28 | + </div> |
| 29 | +""" |
| 30 | + |
| 31 | +MESSAGE_BLOCK = """ |
| 32 | + <div id="no-issues"> |
| 33 | + <div class="issue-block"> |
| 34 | + <b>{message}</b><br> |
| 35 | + </div> |
| 36 | + </div> |
| 37 | +""" |
| 38 | + |
| 39 | +output_element = document.getElementById("output") |
| 40 | + |
| 41 | + |
| 42 | +def run_analysis(code): |
| 43 | + issue_metrics = metrics.Metrics() |
| 44 | + scores = [] |
| 45 | + skipped = [] |
| 46 | + filename = "<stdin>" |
| 47 | + |
| 48 | + # Clear previous output |
| 49 | + output_element.innerHTML = "" |
| 50 | + |
| 51 | + try: |
| 52 | + fobj = io.BytesIO(code) |
| 53 | + issue_metrics.begin(filename) |
| 54 | + data = fobj.read() |
| 55 | + lines = data.splitlines() |
| 56 | + issue_metrics.count_locs(lines) |
| 57 | + nosec_lines = {} |
| 58 | + |
| 59 | + try: |
| 60 | + fobj.seek(0) |
| 61 | + tokens = tokenize.tokenize(fobj.readline) |
| 62 | + for toktype, tokval, (lineno, _), _, _ in tokens: |
| 63 | + if toktype == tokenize.COMMENT: |
| 64 | + nosec_lines[lineno] = manager._parse_nosec_comment(tokval) |
| 65 | + except tokenize.TokenError: |
| 66 | + pass |
| 67 | + |
| 68 | + visitor = node_visitor.BanditNodeVisitor( |
| 69 | + filename, |
| 70 | + fobj, |
| 71 | + metaast=meta_ast.BanditMetaAst(), |
| 72 | + testset=test_set.BanditTestSet( |
| 73 | + config.BanditConfig(), |
| 74 | + profile={ |
| 75 | + "include": [], |
| 76 | + "exclude": ["B613"], # FIXME: issue #1182 |
| 77 | + }, |
| 78 | + ), |
| 79 | + debug=False, |
| 80 | + nosec_lines=nosec_lines, |
| 81 | + metrics=issue_metrics, |
| 82 | + ) |
| 83 | + score = visitor.process(code) |
| 84 | + scores.append(score) |
| 85 | + issue_metrics.count_issues([score]) |
| 86 | + |
| 87 | + for index, issue in enumerate(visitor.tester.results): |
| 88 | + url = docs_utils.get_url(issue.test_id) |
| 89 | + output_element.innerHTML += ISSUE_BLOCK.format( |
| 90 | + issue_no=index, |
| 91 | + issue_class=f"issue-sev-{issue.severity.lower()}", |
| 92 | + test_name=issue.test, |
| 93 | + test_id=issue.test_id, |
| 94 | + test_text=issue.text, |
| 95 | + severity=issue.severity.capitalize(), |
| 96 | + confidence=issue.confidence.capitalize(), |
| 97 | + cwe=str(issue.cwe), |
| 98 | + cwe_link=issue.cwe.link(), |
| 99 | + url=url, |
| 100 | + line_number=issue.lineno, |
| 101 | + col_offset=issue.col_offset, |
| 102 | + ) |
| 103 | + |
| 104 | + if not visitor.tester.results: |
| 105 | + output_element.innerHTML += MESSAGE_BLOCK.format( |
| 106 | + message="No issues identified." |
| 107 | + ) |
| 108 | + except SyntaxError: |
| 109 | + output_element.innerHTML += MESSAGE_BLOCK.format( |
| 110 | + message="Syntax error parsing code." |
| 111 | + ) |
| 112 | + except Exception: |
| 113 | + output_element.innerHTML += MESSAGE_BLOCK.format( |
| 114 | + message="Exception scanning code." |
| 115 | + ) |
| 116 | + |
| 117 | + issue_metrics.aggregate() |
| 118 | + |
| 119 | + |
| 120 | +def handle_event(event): |
| 121 | + run_analysis(event.code.encode()) |
| 122 | + |
| 123 | + # prevent default execution |
| 124 | + return False |
| 125 | + |
| 126 | + |
| 127 | +editor = document.getElementById("editor") |
| 128 | +editor.handleEvent = handle_event |
0 commit comments