Skip to content

Commit 4ff0a31

Browse files
committed
updates for issue #24 and other minor fixes
1 parent c36e03f commit 4ff0a31

3 files changed

Lines changed: 20 additions & 22 deletions

File tree

src/codeaudit/api_reporting.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ def total_weaknesses(input_file):
3232
sast_result = file_info.get("sast_result", {})
3333
for (
3434
construct,
35-
occurence,
35+
occurrence,
3636
) in (
3737
sast_result.items()
38-
): # occurence is times the construct appears in a single file
39-
counter[construct] += len(occurence)
38+
): # occurrence is times the construct appears in a single file
39+
counter[construct] += len(occurrence)
4040

4141
result = dict(counter)
4242
df = pd.DataFrame(list(result.items()), columns=["call", "count"])

src/codeaudit/ci_workflowscan.py

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,33 @@
1414
"""
1515

1616
import sys
17-
1817
from codeaudit.api_interfaces import filescan
1918

20-
import sys
21-
2219

2320
def ci_scan(input_path, format="text", nosec=True):
2421
"""Basic SAST scan to be used in CI workflows
2522
The nosec is set to true for CI workflows by default, it can be changed
23+
Security weakness SHOULD be marked for an exit 0 status in your CI
24+
25+
Note: If you use JSON output you will have an exit status 0, since you have to determine yourself if there are weaknesses found in your code.
2626
"""
2727
try:
2828
scanresult = filescan(input_path, nosec=nosec)
2929
# collect and return info from scanned files
3030
if format == "text":
31-
output = report_result_txt(scanresult)
31+
output, security_status = report_result_txt(scanresult)
3232
elif format == "json":
33-
output = report_result_json(scanresult)
33+
output, security_status = report_result_json(scanresult)
3434
else:
3535
# Fallback handling for unsupported formats to prevent output crashes
3636
output = f"ERROR: Unsupported format '{format}'"
3737
print(output, file=sys.stderr)
3838
sys.exit(1)
3939
print(output)
40-
sys.exit(0) # correct finish
40+
if security_status == 0: # no files with weakness found - or properly marked!
41+
sys.exit(0) # correct finish
42+
else:
43+
sys.exit(20) # finish with detected weakness
4144

4245
except Exception as e:
4346
# Log the actual error 'e' for debugging CI failures
@@ -47,20 +50,15 @@ def ci_scan(input_path, format="text", nosec=True):
4750

4851
def report_result_json(scanresult):
4952
"""Returns scan result in json format.
50-
Note: not (yet) directly usuable since you still need to dive in the dict structure to retrieve results, if any for weaknesses found per file.
53+
Note: not (yet) directly usable since you still need to dive in the dict structure to retrieve results, if any for weaknesses found per file. The resulting json structure is outlined in the documentation. You can use e.g. the `jq` tool. Or join the Python Code Audit community to create CI json output that suites your needs!
54+
Note that it is hierarchical json structure. See the docs!
5155
"""
5256
if not isinstance(scanresult, dict):
5357
raise TypeError("Expected scanresult to be a dictionary")
54-
5558
file_security_info = scanresult.get("file_security_info")
59+
files_with_findings_count = 0
5660

57-
if not isinstance(file_security_info, dict) or len(file_security_info) == 0:
58-
# Raising an error forces the calling function's 'try/except' block to trigger
59-
raise ValueError("Critical Error: 'file_security_info' is missing or empty.")
60-
return file_security_info
61-
62-
63-
import sys
61+
return file_security_info, files_with_findings_count
6462

6563

6664
def report_result_txt(scanresult):
@@ -135,13 +133,13 @@ def safe_line(x):
135133
total_number_of_files = stats.get("Number_Of_Files", 1)
136134

137135
if files_with_findings_count == 0:
138-
summary = "✅ No security issues found in file(s) or Package.\n"
136+
summary = "✅ No security issue(s) found in file(s) or Package.\n"
139137
else:
140138
summary = ""
141139

142140
summary += f"\nTotal files with findings: {files_with_findings_count} of {total_number_of_files} Python files checked."
143141

144142
if files_with_findings_count == 0:
145-
return summary
143+
return summary, files_with_findings_count
146144
else:
147-
return output + summary
145+
return output + summary, files_with_findings_count

src/dashboard/dashboardapp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ async def get_package_source_wasm(url):
118118
with tarfile.open(tar_path, "r:gz") as tar:
119119
tar.extractall(
120120
path=temp_dir, filter="data"
121-
) # nosec Possible risks are mitigated and this happends in the WASM context.
121+
) # nosec Possible risks are mitigated and this happens in the WASM context.
122122

123123
return temp_dir, tmpdir_obj
124124

0 commit comments

Comments
 (0)