Skip to content

Commit f22961e

Browse files
new formatting
1 parent 5094bff commit f22961e

File tree

4 files changed

+65
-13
lines changed

4 files changed

+65
-13
lines changed

exasol/toolbox/tools/security.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,16 @@ def from_maven(report: str) -> Iterable[Issue]:
100100
)
101101

102102

103-
def from_json(report_str: str, prefix: Path) -> Iterable[Issue]:
103+
@dataclass(frozen=True)
104+
class SecurityIssue:
105+
coordinates: str
106+
cwe: str
107+
test_id: str
108+
description: str
109+
references: tuple
110+
111+
112+
def from_json(report_str: str, prefix: Path) -> Iterable[SecurityIssue]:
104113
report = json.loads(report_str)
105114
issues = report.get("results", {})
106115
for issue in issues:
@@ -111,32 +120,32 @@ def from_json(report_str: str, prefix: Path) -> Iterable[Issue]:
111120
references.append(issue["issue_cve"]["link"])
112121
if issue.get("issue_cwe", {}).get("link", None):
113122
references.append(issue["issue_cwe"]["link"])
114-
yield Issue(
115-
cve=str(issue.get("issue_cve", {}).get("id", "")),
116-
cwe=str(issue.get("issue_cwe", {}).get("id", "")),
123+
yield SecurityIssue(
124+
cwe=str(issue["issue_cwe"].get("id", "")),
117125
description=issue["issue_text"],
126+
test_id=issue["test_id"],
118127
coordinates=issue["filename"].replace(
119128
str(prefix) + "/", ""
120129
) + ":" + str(issue["line_number"]) + ":" + str(issue["col_offset"]) + ":",
121130
references=tuple(references)
122131
)
123132

124133

125-
def issues_to_markdown(issues: Iterable[Issue]) -> str:
134+
def issues_to_markdown(issues: Iterable[SecurityIssue]) -> str:
126135
template = cleandoc("""
127136
{header}{rows}
128137
""")
129138

130139
def _header():
131140
header = "# Security\n\n"
132-
header += "|File|Cve|Cwe|Details|\n"
141+
header += "|File|Cwe|Test ID|Details|\n"
133142
header += "|---|:-:|:-:|---|\n"
134143
return header
135144

136145
def _row(issue):
137146
row = "|" + issue.coordinates + "|"
138-
row += issue.cve + "|"
139147
row += issue.cwe + "|"
148+
row += issue.test_id + "|"
140149
for element in issue.references:
141150
row += element + " ,<br>"
142151
row = row[:-5] + "|"
@@ -314,6 +323,7 @@ def json_issue_to_markdown(
314323
) -> None:
315324
content = json_file.read()
316325
issues = from_json(content, path.absolute())
326+
issues = sorted(issues, key=lambda i: (i.coordinates[0:i.coordinates.index(":")], i.cwe, i.test_id))
317327
print(issues_to_markdown(issues))
318328

319329

test/integration/cli/security-pprint-emty.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Run test case
1212
$ tbx security pretty-print .security.json
1313
# Security
1414

15-
|File|Cve|Cwe|Details|
15+
|File|Cwe|Test ID|Details|
1616
|---|:-:|:-:|---|
1717

1818

test/integration/cli/security-pprint.t

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,46 @@ Create test input
2424
> "more_info": "https://bandit.readthedocs.io/en/1.7.10/plugins/b602_subprocess_popen_with_shell_equals_true.html",
2525
> "test_id": "B602",
2626
> "test_name": "subprocess_popen_with_shell_equals_true"
27+
> },
28+
> {
29+
> "code": "156 )\n157 subprocess.check_call(cmd, cwd=gitroot, stdout=fp)\n158 fp.seek(0)\n",
30+
> "col_offset": 8,
31+
> "end_col_offset": 58,
32+
> "filename": "/home/jami/Git/python-toolbox/exasol/toolbox/sphinx/multiversion/git.py",
33+
> "issue_confidence": "HIGH",
34+
> "issue_cwe": {
35+
> "id": 78,
36+
> "link": "https://cwe.mitre.org/data/definitions/78.html"
37+
> },
38+
> "issue_severity": "LOW",
39+
> "issue_text": "subprocess call - check for execution of untrusted input.",
40+
> "line_number": 157,
41+
> "line_range": [
42+
> 157
43+
> ],
44+
> "more_info": "https://bandit.readthedocs.io/en/1.7.10/plugins/b603_subprocess_without_shell_equals_true.html",
45+
> "test_id": "B603",
46+
> "test_name": "subprocess_without_shell_equals_true"
47+
> },
48+
> {
49+
> "code": "159 with tarfile.TarFile(fileobj=fp) as tarfp:\n160 tarfp.extractall(dst)\n",
50+
> "col_offset": 12,
51+
> "end_col_offset": 33,
52+
> "filename": "exasol/toolbox/sphinx/multiversion/git.py",
53+
> "issue_confidence": "HIGH",
54+
> "issue_cwe": {
55+
> "id": 22,
56+
> "link": "https://cwe.mitre.org/data/definitions/22.html"
57+
> },
58+
> "issue_severity": "HIGH",
59+
> "issue_text": "tarfile.extractall used without any validation. Please check and discard dangerous members.",
60+
> "line_number": 160,
61+
> "line_range": [
62+
> 160
63+
> ],
64+
> "more_info": "https://bandit.readthedocs.io/en/1.7.10/plugins/b202_tarfile_unsafe_members.html",
65+
> "test_id": "B202",
66+
> "test_name": "tarfile_unsafe_members"
2767
> }
2868
> ]
2969
> }
@@ -34,6 +74,8 @@ Run test case
3474
$ tbx security pretty-print .security.json
3575
# Security
3676

37-
|File|Cve|Cwe|Details|
77+
|File|Cwe|Test ID|Details|
3878
|---|:-:|:-:|---|
39-
|exasol/toolbox/sphinx/multiversion/main.py:556:16:||78|https://bandit.readthedocs.io/en/1.7.10/plugins/b602_subprocess_popen_with_shell_equals_true.html ,<br>https://cwe.mitre.org/data/definitions/78.html |
79+
|exasol/toolbox/sphinx/multiversion/git.py:160:12:|22|B202|https://bandit.readthedocs.io/en/1.7.10/plugins/b202_tarfile_unsafe_members.html ,<br>https://cwe.mitre.org/data/definitions/22.html |
80+
|exasol/toolbox/sphinx/multiversion/git.py:157:8:|78|B603|https://bandit.readthedocs.io/en/1.7.10/plugins/b603_subprocess_without_shell_equals_true.html ,<br>https://cwe.mitre.org/data/definitions/78.html |
81+
|exasol/toolbox/sphinx/multiversion/main.py:556:16:|78|B602|https://bandit.readthedocs.io/en/1.7.10/plugins/b602_subprocess_popen_with_shell_equals_true.html ,<br>https://cwe.mitre.org/data/definitions/78.html |

test/unit/security_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -436,8 +436,8 @@ def test_format_jsonl_removes_newline():
436436
}
437437
''',
438438
{
439-
"cve": "",
440439
"cwe": "78",
440+
"test_id": "B404",
441441
"description": "Consider possible security implications associated with the subprocess module.",
442442
"coordinates": "exasol/toolbox/git.py:1:0:",
443443
"references": (
@@ -450,9 +450,9 @@ def test_format_jsonl_removes_newline():
450450
)
451451
def test_from_json(json_file, expected):
452452
actual = security.from_json(json_file, pathlib.Path("/home/test/Git/python-toolbox"))
453-
expected_issue = security.Issue(
454-
cve=expected["cve"],
453+
expected_issue = security.SecurityIssue(
455454
cwe=expected["cwe"],
455+
test_id=expected["test_id"],
456456
description=expected["description"],
457457
coordinates=expected["coordinates"],
458458
references=expected["references"]

0 commit comments

Comments
 (0)