Skip to content

Commit 286e9cd

Browse files
changed format of pprint
1 parent 4d89c60 commit 286e9cd

File tree

4 files changed

+29
-24
lines changed

4 files changed

+29
-24
lines changed

exasol/toolbox/tools/security.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@ def from_maven(report: str) -> Iterable[Issue]:
103103

104104
@dataclass(frozen=True)
105105
class SecurityIssue:
106-
coordinates: str
106+
file_name: str
107+
line: int
108+
column: int
107109
cwe: str
108110
test_id: str
109111
description: str
@@ -117,17 +119,15 @@ def from_json(report_str: str, prefix: Path) -> Iterable[SecurityIssue]:
117119
references = []
118120
if issue["more_info"]:
119121
references.append(issue["more_info"])
120-
if issue.get("issue_cve", {}).get("link", None):
121-
references.append(issue["issue_cve"]["link"])
122122
if issue.get("issue_cwe", {}).get("link", None):
123123
references.append(issue["issue_cwe"]["link"])
124124
yield SecurityIssue(
125+
file_name=issue["filename"].replace(str(prefix) + "/", ""),
126+
line=issue["line_number"],
127+
column=issue["col_offset"],
125128
cwe=str(issue["issue_cwe"].get("id", "")),
126-
description=issue["issue_text"],
127129
test_id=issue["test_id"],
128-
coordinates=issue["filename"].replace(
129-
str(prefix) + "/", ""
130-
) + ":" + str(issue["line_number"]) + ":" + str(issue["col_offset"]) + ":",
130+
description=issue["issue_text"],
131131
references=tuple(references)
132132
)
133133

@@ -139,12 +139,13 @@ def issues_to_markdown(issues: Iterable[SecurityIssue]) -> str:
139139

140140
def _header():
141141
header = "# Security\n\n"
142-
header += "|File|Cwe|Test ID|Details|\n"
143-
header += "|---|:-:|:-:|---|\n"
142+
header += "|File|line/<br>column|Cwe|Test ID|Details|\n"
143+
header += "|---|:-:|:-:|:-:|---|\n"
144144
return header
145145

146146
def _row(issue):
147-
row = "|" + issue.coordinates + "|"
147+
row = "|" + issue.file_name + "|"
148+
row += f"line: {issue.line}<br>column: {issue.column}|"
148149
row += issue.cwe + "|"
149150
row += issue.test_id + "|"
150151
for element in issue.references:
@@ -324,7 +325,7 @@ def json_issue_to_markdown(
324325
) -> None:
325326
content = json_file.read()
326327
issues = from_json(content, path.absolute())
327-
issues = sorted(issues, key=lambda i: (i.coordinates[0:i.coordinates.index(":")], i.cwe, i.test_id))
328+
issues = sorted(issues, key=lambda i: (i.file_name, i.cwe, i.test_id))
328329
print(issues_to_markdown(issues))
329330

330331

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

Lines changed: 2 additions & 2 deletions
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|Cwe|Test ID|Details|
16-
|---|:-:|:-:|---|
15+
|File|line/<br>column|Cwe|Test ID|Details|
16+
|---|:-:|:-:|:-:|---|
1717

1818

test/integration/cli/security-pprint.t

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ Run test case
7474
$ tbx security pretty-print .security.json
7575
# Security
7676

77-
|File|Cwe|Test ID|Details|
78-
|---|:-:|:-:|---|
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 |
77+
|File|line/<br>column|Cwe|Test ID|Details|
78+
|---|:-:|:-:|:-:|---|
79+
|exasol/toolbox/sphinx/multiversion/git.py|line: 160<br>column: 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|line: 157<br>column: 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|line: 556<br>column: 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: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -414,17 +414,17 @@ def test_format_jsonl_removes_newline():
414414
"results": [
415415
{
416416
"code": "1 import subprocess\\n2 from typing import Iterable\\n3 \\n",
417-
"col_offset": 0,
417+
"col_offset": 12,
418418
"end_col_offset": 17,
419-
"filename": "/home/test/Git/python-toolbox/exasol/toolbox/git.py",
419+
"filename": "/home/test/python-toolbox/exasol/toolbox/git.py",
420420
"issue_confidence": "HIGH",
421421
"issue_cwe": {
422422
"id": 78,
423423
"link": "https://cwe.mitre.org/data/definitions/78.html"
424424
},
425425
"issue_severity": "LOW",
426426
"issue_text": "Consider possible security implications associated with the subprocess module.",
427-
"line_number": 1,
427+
"line_number": 53,
428428
"line_range": [
429429
1
430430
],
@@ -436,10 +436,12 @@ def test_format_jsonl_removes_newline():
436436
}
437437
''',
438438
{
439+
"file_name": "exasol/toolbox/git.py",
440+
"line": 53,
441+
"column": 12,
439442
"cwe": "78",
440443
"test_id": "B404",
441444
"description": "Consider possible security implications associated with the subprocess module.",
442-
"coordinates": "exasol/toolbox/git.py:1:0:",
443445
"references": (
444446
"https://bandit.readthedocs.io/en/1.7.10/blacklists/blacklist_imports.html#b404-import-subprocess",
445447
"https://cwe.mitre.org/data/definitions/78.html"
@@ -449,12 +451,14 @@ def test_format_jsonl_removes_newline():
449451
]
450452
)
451453
def test_from_json(json_file, expected):
452-
actual = security.from_json(json_file, pathlib.Path("/home/test/Git/python-toolbox"))
454+
actual = security.from_json(json_file, pathlib.Path("/home/test/python-toolbox"))
453455
expected_issue = security.SecurityIssue(
456+
file_name=expected["file_name"],
457+
line=expected["line"],
458+
column=expected["column"],
454459
cwe=expected["cwe"],
455460
test_id=expected["test_id"],
456461
description=expected["description"],
457-
coordinates=expected["coordinates"],
458462
references=expected["references"]
459463
)
460464
assert list(actual) == [expected_issue]

0 commit comments

Comments
 (0)