codeaudit 0.9.3__tar.gz → 1.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- codeaudit-1.1.0/CHANGELOG.md +44 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/PKG-INFO +27 -29
- {codeaudit-0.9.3 → codeaudit-1.1.0}/README.md +26 -28
- {codeaudit-0.9.3 → codeaudit-1.1.0}/SECURITY.md +0 -1
- {codeaudit-0.9.3 → codeaudit-1.1.0}/docs/CLIcommands.ipynb +15 -16
- {codeaudit-0.9.3 → codeaudit-1.1.0}/docs/CONTRIBUTE.md +2 -1
- {codeaudit-0.9.3 → codeaudit-1.1.0}/docs/_toc.yml +9 -7
- {codeaudit-0.9.3 → codeaudit-1.1.0}/docs/astlines.md +12 -3
- codeaudit-1.1.0/docs/changelog.md +4 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/docs/checks/builtinfunctions_check.md +15 -0
- codeaudit-1.1.0/docs/checks/dynamicimport_check.md +42 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/docs/checks/shutil_check.md +2 -1
- codeaudit-1.1.0/docs/checks/zipfile_check.md +56 -0
- codeaudit-1.1.0/docs/codeaudit.rst +12 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/docs/codeauditcommands.md +24 -55
- {codeaudit-0.9.3 → codeaudit-1.1.0}/docs/complexitycheck.md +1 -1
- {codeaudit-0.9.3 → codeaudit-1.1.0}/docs/features.md +6 -6
- {codeaudit-0.9.3 → codeaudit-1.1.0}/docs/filescan.md +25 -10
- codeaudit-1.1.0/docs/howtoscan.md +113 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/docs/intro.md +14 -5
- {codeaudit-0.9.3 → codeaudit-1.1.0}/docs/issues.md +22 -2
- codeaudit-1.1.0/docs/makeitbetter.md +109 -0
- codeaudit-1.1.0/docs/modules.rst +7 -0
- codeaudit-1.1.0/docs/pca_overview.png +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/docs/sponsors.md +2 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/docs/userguide.md +10 -3
- {codeaudit-0.9.3 → codeaudit-1.1.0}/docs/warnings.md +2 -2
- {codeaudit-0.9.3 → codeaudit-1.1.0}/src/codeaudit/__about__.py +1 -1
- codeaudit-1.1.0/src/codeaudit/api_interfaces.py +209 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/src/codeaudit/checkmodules.py +28 -10
- {codeaudit-0.9.3 → codeaudit-1.1.0}/src/codeaudit/codeaudit.py +13 -16
- codeaudit-1.1.0/src/codeaudit/data/sastchecks.csv +71 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/src/codeaudit/filehelpfunctions.py +25 -3
- {codeaudit-0.9.3 → codeaudit-1.1.0}/src/codeaudit/issuevalidations.py +10 -4
- {codeaudit-0.9.3 → codeaudit-1.1.0}/src/codeaudit/reporting.py +54 -33
- {codeaudit-0.9.3 → codeaudit-1.1.0}/src/codeaudit/simple.css +10 -6
- {codeaudit-0.9.3 → codeaudit-1.1.0}/src/codeaudit/totals.py +1 -1
- {codeaudit-0.9.3 → codeaudit-1.1.0}/tests/test_constructspart2.py +19 -14
- codeaudit-1.1.0/tests/test_modulecheck.py +40 -0
- codeaudit-1.1.0/tests/test_obfuscatingbuiltins.py +32 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/tests/validationfiles/allshit.py +52 -1
- codeaudit-1.1.0/tests/validationfiles/gzip.py +50 -0
- codeaudit-1.1.0/tests/validationfiles/obfuscating.py +54 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/tests/validationfiles/shutil.py +5 -0
- codeaudit-0.9.3/docs/checks/zipfile_check.md +0 -28
- codeaudit-0.9.3/docs/directoryscan.md +0 -75
- codeaudit-0.9.3/src/codeaudit/data/sastchecks.csv +0 -64
- codeaudit-0.9.3/tests/test_modulecheck.py +0 -21
- codeaudit-0.9.3/tests/validationfiles/gzip.py +0 -5
- {codeaudit-0.9.3 → codeaudit-1.1.0}/.gitignore +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/CONTRIBUTE.md +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/LICENSE.txt +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/docs/_config.yml +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/docs/_static/nocxstyle.css +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/docs/about.md +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/docs/astlines2.md +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/docs/checks/assert_check.md +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/docs/checks/base64_check.md +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/docs/checks/binding_check.md +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/docs/checks/chmod_check.md +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/docs/checks/directorycreation_check.md +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/docs/checks/exception_check.md +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/docs/checks/hash_check.md +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/docs/checks/httpserver_check.md +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/docs/checks/input_check.md +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/docs/checks/loggingconf_check.md +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/docs/checks/marshal_check.md +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/docs/checks/mktemp_check.md +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/docs/checks/multiprocessing_check.md +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/docs/checks/pickle_check.md +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/docs/checks/random_check.md +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/docs/checks/shelve_check.md +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/docs/checks/subprocess_check.md +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/docs/checks/syscalls_check.md +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/docs/checks/systemcalls_check.md +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/docs/checks/tarfile_extract_check.md +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/docs/checks/xml_check.md +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/docs/checksinformation.md +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/docs/codeauditchecks.md +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/docs/codeauditoverview.md +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/docs/examples/checks_example.html +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/docs/examples/directoryscan.html +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/docs/examples/filescan.html +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/docs/examples/modulescan.html +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/docs/examples/overview.html +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/docs/filescan.png +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/docs/help.md +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/docs/images/OO.png +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/docs/images/ROI_logo.png +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/docs/images/YourLogoHere.png +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/docs/images/codeauditlogo.png +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/docs/images/nocxbanner.png +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/docs/images/overview_linkaudit.png +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/docs/license.md +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/docs/modulescan.md +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/docs/overviewplot.png +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/docs/whysast.md +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/filescan.png +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/pyproject.toml +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/src/codeaudit/__init__.py +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/src/codeaudit/altairplots.py +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/src/codeaudit/complexitycheck.py +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/src/codeaudit/htmlhelpfunctions.py +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/src/codeaudit/security_checks.py +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/tests/__init__.py +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/tests/count_lines_file1.py +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/tests/test_basicpatterns.py +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/tests/test_chmod.py +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/tests/test_correctexceptionuse.py +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/tests/test_count_commentlines.py +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/tests/test_directorycreation.py +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/tests/test_directorycreation2.py +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/tests/test_hashstrenght.py +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/tests/test_oschecks.py +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/tests/test_random.py +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/tests/test_standardlibconstructs.py +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/tests/test_totalscheck.py +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/tests/validationfiles/assert.py +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/tests/validationfiles/base64.py +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/tests/validationfiles/chmod_things.py +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/tests/validationfiles/complexitycheck.py +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/tests/validationfiles/correctcounts.py +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/tests/validationfiles/directorycreation.py +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/tests/validationfiles/directorycreation2.py +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/tests/validationfiles/exception.py +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/tests/validationfiles/file3.py +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/tests/validationfiles/file_with_warnings.py +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/tests/validationfiles/hashcheck.py +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/tests/validationfiles/httpserver.py +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/tests/validationfiles/inputstatement.py +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/tests/validationfiles/marshal.py +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/tests/validationfiles/modulecheck.py +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/tests/validationfiles/multiprocessing.py +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/tests/validationfiles/oschecks.py +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/tests/validationfiles/pickle.py +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/tests/validationfiles/python2_file_willnotwork.py +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/tests/validationfiles/random.py +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/tests/validationfiles/shelve.py +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/tests/validationfiles/subprocess.py +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/tests/validationfiles/syslibrary.py +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/tests/validationfiles/tarfilevalidation.py +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/tests/validationfiles/tempcheck.py +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/tests/validationfiles/validation1.py +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/tests/validationfiles/validation2.py +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/tests/validationfiles/xml.py +0 -0
- {codeaudit-0.9.3 → codeaudit-1.1.0}/tests/validationfiles/zipfile.py +0 -0
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# Change Log
|
|
2
|
+
|
|
3
|
+
## Version 1.1:What's New
|
|
4
|
+
|
|
5
|
+
We've released a new version with several key improvements focused on making your security workflow smoother and providing more detailed security information.
|
|
6
|
+
|
|
7
|
+
* Streamlined Scanning:
|
|
8
|
+
|
|
9
|
+
The separate `directoryscan` command has been removed. You can now use the versatile `filescan` command to scan both individual files and entire directories. This simplifies the command-line interface and makes the process more intuitive.
|
|
10
|
+
|
|
11
|
+
* Enhanced Reporting:
|
|
12
|
+
|
|
13
|
+
We've made minor corrections to the documentation and static HTML reports to improve clarity. Additionally, warning messages are now more descriptive, helping you quickly understand potential issues.
|
|
14
|
+
|
|
15
|
+
* Improved Vulnerability Data:
|
|
16
|
+
|
|
17
|
+
You'll now get more detailed information about module vulnerabilities. The tool now includes CVSS scores, a standard metric for rating vulnerability severity, giving you a clearer picture of the risks.
|
|
18
|
+
|
|
19
|
+
* Behind-the-Scenes Fixes:
|
|
20
|
+
|
|
21
|
+
We've made a more robust and reliable adjustment to how the tool retrieves file names. This ensures consistency and accuracy during scans. We've also added beta-level API functions, opening up new possibilities for integration.
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
## Version 1.0
|
|
26
|
+
|
|
27
|
+
This release represents a stabilisation of Python Code Audit!
|
|
28
|
+
Main changes in relation to the pre-1.0 versions are:
|
|
29
|
+
* More validations added: Python Code Audit now counts 70 security validations!
|
|
30
|
+
* Documentation updates
|
|
31
|
+
* Improved validation for `builtins`, like `compile`, `exec`,, `eval` that can be obfuscated in code.
|
|
32
|
+
* Various UI/UX updates. CLI text improved and HTML report text made consistent.
|
|
33
|
+
* Added test to validate correct working for now and in the future. Also validated working with other SAST tools to make sure core functionality is rock solid or better! Spoiler Python Code Audit is better than most used OSS and commercial SAST tools available today!
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
## Beta Versions (Before 1.0)
|
|
37
|
+
|
|
38
|
+
All published beta version are stable and verified!
|
|
39
|
+
During the public beta phase input of users and experts is retrieved.
|
|
40
|
+
This resulted is mainly:
|
|
41
|
+
* More validation
|
|
42
|
+
* Better documentation and
|
|
43
|
+
* UI/UX improvements to make sure Python Code Audit is dead simple to use for non-programmers to validate a Python package.
|
|
44
|
+
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: codeaudit
|
|
3
|
-
Version:
|
|
3
|
+
Version: 1.1.0
|
|
4
4
|
Summary: Simplified static security checks for Python
|
|
5
5
|
Project-URL: Documentation, https://github.com/nocomplexity/codeaudit#readme
|
|
6
6
|
Project-URL: Issues, https://github.com/nocomplexity/codeaudit/issues
|
|
@@ -33,28 +33,28 @@ Description-Content-Type: text/markdown
|
|
|
33
33
|
[](https://www.bestpractices.dev/projects/10970)
|
|
34
34
|
[](https://pepy.tech/projects/codeaudit)
|
|
35
35
|
|
|
36
|
-
Python
|
|
36
|
+
Python Code Audit - A modern Python source code analyzer based on distrust.
|
|
37
37
|
|
|
38
|
-
|
|
38
|
+
Python Code Audit is a tool to find **security issues** in Python code. This static application security testing (SAST) tool has **great** features to simplify the necessary security tasks and make it fun and easy.
|
|
39
39
|
|
|
40
|
-
This tool is
|
|
41
|
-
* Anyone who want or must check security risks with Python programs.
|
|
42
|
-
* Anyone who loves to create functionality using Python. So not only professional programs , but also occasional Python programmers or programmers who are used to working with other languages.
|
|
43
|
-
* Anyone who wants an easy way to get insight in possible security risks Python programs.
|
|
40
|
+
This tool is designed for anyone who uses or creates Python programs and wants to understand and mitigate potential security risks.
|
|
44
41
|
|
|
42
|
+
This tool is created for:
|
|
43
|
+
* Python Users who want to assess the security risks in the Python code they use.
|
|
44
|
+
* Python Developers: Anyone, from professionals to hobbyists, who wants to deliver secure Python code.
|
|
45
|
+
* Security-Conscious Users: People seeking a simple, fast way to gain insight into potential security vulnerabilities within Python packages or files.
|
|
45
46
|
|
|
46
|
-
|
|
47
|
-
> Python Codeaudit is currently in *beta status*. Consider [contributing](CONTRIBUTING.md) to make Codeaudit the coolest open source Python SAST tool. Codeaudit is currently in a thorough testing phase. Use Python Codeaudit now and contribute to make it better!
|
|
47
|
+
Creating secure software can be challenging. This tool, with its comprehensive [documentation](https://nocomplexity.com/documents/codeaudit/intro.html), acts as your helpful security colleague, making it easier to identify and address vulnerabilities.
|
|
48
48
|
|
|
49
49
|
## Features
|
|
50
50
|
|
|
51
|
-
Python
|
|
51
|
+
Python Code Audit has the following features:
|
|
52
52
|
|
|
53
53
|
* **Vulnerability Detection**: Identifies security vulnerabilities in Python files, essential for package security research.
|
|
54
54
|
|
|
55
55
|
* **Complexity & Statistics**: Reports security-relevant complexity using a fast, lightweight [cyclomatic complexity](https://en.wikipedia.org/wiki/Cyclomatic_complexity) count via Python's AST.
|
|
56
56
|
|
|
57
|
-
* **Module Usage & External Vulnerabilities**: Detects used modules and reports vulnerabilities
|
|
57
|
+
* **Module Usage & External Vulnerabilities**: Detects used modules and reports known vulnerabilities for used external modules.
|
|
58
58
|
|
|
59
59
|
* **Inline Issue Reporting**: Shows potential security issues with line numbers and code snippets.
|
|
60
60
|
|
|
@@ -63,7 +63,7 @@ Python Codeaudit has the following features:
|
|
|
63
63
|
|
|
64
64
|
|
|
65
65
|
> [!IMPORTANT]
|
|
66
|
-
> Python
|
|
66
|
+
> Python Code Audit uses the Python's Abstract Syntax Tree (AST) to get robust and reliable result. Using the Python AST makes contextual Vulnerability Detection possible and false positive are minimized.
|
|
67
67
|
|
|
68
68
|
|
|
69
69
|
## Installation
|
|
@@ -74,11 +74,11 @@ pip install codeaudit
|
|
|
74
74
|
|
|
75
75
|
or use:
|
|
76
76
|
|
|
77
|
-
```
|
|
77
|
+
```console
|
|
78
78
|
pip install -U codeaudit
|
|
79
79
|
```
|
|
80
80
|
|
|
81
|
-
If you have installed
|
|
81
|
+
If you have installed Python `codeaudit` in the past and want to make sure you use the latest new validations and features.
|
|
82
82
|
|
|
83
83
|
## Usage
|
|
84
84
|
|
|
@@ -91,16 +91,13 @@ codeaudit
|
|
|
91
91
|
This will show all commands:
|
|
92
92
|
|
|
93
93
|
```text
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
/
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
| |___| (_) | (_| | __/ (_| | |_| | (_| | | |_
|
|
100
|
-
\_____\___/ \__,_|\___|\__,_|\__,_|\__,_|_|\__|
|
|
101
|
-
--------------------------------------------------
|
|
94
|
+
----------------------------------------------------
|
|
95
|
+
_ __ _
|
|
96
|
+
|_) \/_|_|_ _ __ / _ _| _ |_| _| o _|_
|
|
97
|
+
| / |_| |(_)| | \__(_)(_|(/_ | ||_|(_| | |_
|
|
98
|
+
----------------------------------------------------
|
|
102
99
|
|
|
103
|
-
|
|
100
|
+
Python Code Audit - A modern Python security source code analyzer based on distrust.
|
|
104
101
|
|
|
105
102
|
Commands to evaluate Python source code:
|
|
106
103
|
Usage: codeaudit COMMAND [PATH or FILE] [OUTPUTFILE]
|
|
@@ -109,19 +106,20 @@ Depending on the command, a directory or file name must be specified. The output
|
|
|
109
106
|
|
|
110
107
|
Commands:
|
|
111
108
|
overview Reports Complexity and statistics per Python file from a directory.
|
|
112
|
-
modulescan Reports module information per file.
|
|
113
|
-
filescan Reports potential security issues for a single Python file.
|
|
114
109
|
directoryscan Reports potential security issues for all Python files found in a directory.
|
|
115
|
-
|
|
116
|
-
|
|
110
|
+
filescan Reports potential security issues for a single Python file.
|
|
111
|
+
modulescan Reports module information per file.
|
|
112
|
+
checks Creates an HTML report of all implemented security checks.
|
|
113
|
+
version Prints the module version. Or use codeaudit [-v] [--v] [-version] or [--version].
|
|
117
114
|
|
|
118
|
-
Use the
|
|
115
|
+
Use the Codeaudit documentation to check the security of Python programs and make your Python programs more secure!
|
|
119
116
|
Check https://simplifysecurity.nocomplexity.com/
|
|
117
|
+
|
|
120
118
|
```
|
|
121
119
|
|
|
122
120
|
## Example
|
|
123
121
|
|
|
124
|
-
By running the `codeaudit filescan` command, detailed security information is determined for a Python file based on more than **
|
|
122
|
+
By running the `codeaudit filescan` command, detailed security information is determined for a Python file based on more than **70 validations** implemented.
|
|
125
123
|
|
|
126
124
|
The `codeaudit filescan` command shows all **potential** security issues that are detected in the source file in a HTML-report.
|
|
127
125
|
|
|
@@ -7,28 +7,28 @@
|
|
|
7
7
|
[](https://www.bestpractices.dev/projects/10970)
|
|
8
8
|
[](https://pepy.tech/projects/codeaudit)
|
|
9
9
|
|
|
10
|
-
Python
|
|
10
|
+
Python Code Audit - A modern Python source code analyzer based on distrust.
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
Python Code Audit is a tool to find **security issues** in Python code. This static application security testing (SAST) tool has **great** features to simplify the necessary security tasks and make it fun and easy.
|
|
13
13
|
|
|
14
|
-
This tool is
|
|
15
|
-
* Anyone who want or must check security risks with Python programs.
|
|
16
|
-
* Anyone who loves to create functionality using Python. So not only professional programs , but also occasional Python programmers or programmers who are used to working with other languages.
|
|
17
|
-
* Anyone who wants an easy way to get insight in possible security risks Python programs.
|
|
14
|
+
This tool is designed for anyone who uses or creates Python programs and wants to understand and mitigate potential security risks.
|
|
18
15
|
|
|
16
|
+
This tool is created for:
|
|
17
|
+
* Python Users who want to assess the security risks in the Python code they use.
|
|
18
|
+
* Python Developers: Anyone, from professionals to hobbyists, who wants to deliver secure Python code.
|
|
19
|
+
* Security-Conscious Users: People seeking a simple, fast way to gain insight into potential security vulnerabilities within Python packages or files.
|
|
19
20
|
|
|
20
|
-
|
|
21
|
-
> Python Codeaudit is currently in *beta status*. Consider [contributing](CONTRIBUTING.md) to make Codeaudit the coolest open source Python SAST tool. Codeaudit is currently in a thorough testing phase. Use Python Codeaudit now and contribute to make it better!
|
|
21
|
+
Creating secure software can be challenging. This tool, with its comprehensive [documentation](https://nocomplexity.com/documents/codeaudit/intro.html), acts as your helpful security colleague, making it easier to identify and address vulnerabilities.
|
|
22
22
|
|
|
23
23
|
## Features
|
|
24
24
|
|
|
25
|
-
Python
|
|
25
|
+
Python Code Audit has the following features:
|
|
26
26
|
|
|
27
27
|
* **Vulnerability Detection**: Identifies security vulnerabilities in Python files, essential for package security research.
|
|
28
28
|
|
|
29
29
|
* **Complexity & Statistics**: Reports security-relevant complexity using a fast, lightweight [cyclomatic complexity](https://en.wikipedia.org/wiki/Cyclomatic_complexity) count via Python's AST.
|
|
30
30
|
|
|
31
|
-
* **Module Usage & External Vulnerabilities**: Detects used modules and reports vulnerabilities
|
|
31
|
+
* **Module Usage & External Vulnerabilities**: Detects used modules and reports known vulnerabilities for used external modules.
|
|
32
32
|
|
|
33
33
|
* **Inline Issue Reporting**: Shows potential security issues with line numbers and code snippets.
|
|
34
34
|
|
|
@@ -37,7 +37,7 @@ Python Codeaudit has the following features:
|
|
|
37
37
|
|
|
38
38
|
|
|
39
39
|
> [!IMPORTANT]
|
|
40
|
-
> Python
|
|
40
|
+
> Python Code Audit uses the Python's Abstract Syntax Tree (AST) to get robust and reliable result. Using the Python AST makes contextual Vulnerability Detection possible and false positive are minimized.
|
|
41
41
|
|
|
42
42
|
|
|
43
43
|
## Installation
|
|
@@ -48,11 +48,11 @@ pip install codeaudit
|
|
|
48
48
|
|
|
49
49
|
or use:
|
|
50
50
|
|
|
51
|
-
```
|
|
51
|
+
```console
|
|
52
52
|
pip install -U codeaudit
|
|
53
53
|
```
|
|
54
54
|
|
|
55
|
-
If you have installed
|
|
55
|
+
If you have installed Python `codeaudit` in the past and want to make sure you use the latest new validations and features.
|
|
56
56
|
|
|
57
57
|
## Usage
|
|
58
58
|
|
|
@@ -65,16 +65,13 @@ codeaudit
|
|
|
65
65
|
This will show all commands:
|
|
66
66
|
|
|
67
67
|
```text
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
/
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
| |___| (_) | (_| | __/ (_| | |_| | (_| | | |_
|
|
74
|
-
\_____\___/ \__,_|\___|\__,_|\__,_|\__,_|_|\__|
|
|
75
|
-
--------------------------------------------------
|
|
68
|
+
----------------------------------------------------
|
|
69
|
+
_ __ _
|
|
70
|
+
|_) \/_|_|_ _ __ / _ _| _ |_| _| o _|_
|
|
71
|
+
| / |_| |(_)| | \__(_)(_|(/_ | ||_|(_| | |_
|
|
72
|
+
----------------------------------------------------
|
|
76
73
|
|
|
77
|
-
|
|
74
|
+
Python Code Audit - A modern Python security source code analyzer based on distrust.
|
|
78
75
|
|
|
79
76
|
Commands to evaluate Python source code:
|
|
80
77
|
Usage: codeaudit COMMAND [PATH or FILE] [OUTPUTFILE]
|
|
@@ -83,19 +80,20 @@ Depending on the command, a directory or file name must be specified. The output
|
|
|
83
80
|
|
|
84
81
|
Commands:
|
|
85
82
|
overview Reports Complexity and statistics per Python file from a directory.
|
|
86
|
-
modulescan Reports module information per file.
|
|
87
|
-
filescan Reports potential security issues for a single Python file.
|
|
88
83
|
directoryscan Reports potential security issues for all Python files found in a directory.
|
|
89
|
-
|
|
90
|
-
|
|
84
|
+
filescan Reports potential security issues for a single Python file.
|
|
85
|
+
modulescan Reports module information per file.
|
|
86
|
+
checks Creates an HTML report of all implemented security checks.
|
|
87
|
+
version Prints the module version. Or use codeaudit [-v] [--v] [-version] or [--version].
|
|
91
88
|
|
|
92
|
-
Use the
|
|
89
|
+
Use the Codeaudit documentation to check the security of Python programs and make your Python programs more secure!
|
|
93
90
|
Check https://simplifysecurity.nocomplexity.com/
|
|
91
|
+
|
|
94
92
|
```
|
|
95
93
|
|
|
96
94
|
## Example
|
|
97
95
|
|
|
98
|
-
By running the `codeaudit filescan` command, detailed security information is determined for a Python file based on more than **
|
|
96
|
+
By running the `codeaudit filescan` command, detailed security information is determined for a Python file based on more than **70 validations** implemented.
|
|
99
97
|
|
|
100
98
|
The `codeaudit filescan` command shows all **potential** security issues that are detected in the source file in a HTML-report.
|
|
101
99
|
|
|
@@ -15,7 +15,6 @@ To report a security issue, please use the GitHub Security Advisory ["Report a V
|
|
|
15
15
|
|
|
16
16
|
I will send a response indicating the next steps in handling your report. After the initial reply to your report, I will keep you informed of the progress towards a fix and full announcement, and may ask for additional information or guidance.
|
|
17
17
|
|
|
18
|
-
For context on Electron's security notification process, please see the Notifications section of the Security WG's Membership and Notifications Governance document.
|
|
19
18
|
|
|
20
19
|
## Learning More About Security
|
|
21
20
|
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
},
|
|
14
14
|
{
|
|
15
15
|
"cell_type": "code",
|
|
16
|
-
"execution_count":
|
|
16
|
+
"execution_count": 9,
|
|
17
17
|
"id": "923aba22-7103-4431-8545-ee5596efa371",
|
|
18
18
|
"metadata": {},
|
|
19
19
|
"outputs": [],
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
},
|
|
24
24
|
{
|
|
25
25
|
"cell_type": "code",
|
|
26
|
-
"execution_count":
|
|
26
|
+
"execution_count": 10,
|
|
27
27
|
"id": "057c9730-7b09-49a8-82f1-bc681d880c96",
|
|
28
28
|
"metadata": {},
|
|
29
29
|
"outputs": [],
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
},
|
|
34
34
|
{
|
|
35
35
|
"cell_type": "code",
|
|
36
|
-
"execution_count":
|
|
36
|
+
"execution_count": 11,
|
|
37
37
|
"id": "67576531-b66f-42a3-b6e4-460423ca28e0",
|
|
38
38
|
"metadata": {},
|
|
39
39
|
"outputs": [],
|
|
@@ -44,7 +44,7 @@
|
|
|
44
44
|
},
|
|
45
45
|
{
|
|
46
46
|
"cell_type": "code",
|
|
47
|
-
"execution_count":
|
|
47
|
+
"execution_count": 12,
|
|
48
48
|
"id": "2717fe66-9e66-4fcc-ae82-0d1ba26892c4",
|
|
49
49
|
"metadata": {},
|
|
50
50
|
"outputs": [],
|
|
@@ -54,19 +54,19 @@
|
|
|
54
54
|
},
|
|
55
55
|
{
|
|
56
56
|
"cell_type": "code",
|
|
57
|
-
"execution_count":
|
|
57
|
+
"execution_count": 13,
|
|
58
58
|
"id": "3f286724-0a8f-45b2-80fe-d6d061fe440a",
|
|
59
59
|
"metadata": {},
|
|
60
60
|
"outputs": [],
|
|
61
61
|
"source": [
|
|
62
62
|
"output += '% THIS FILE IS GENERATED! - Use CLIcommands.ipynb to make it better!\\n'\n",
|
|
63
|
-
"output += '# Overview
|
|
64
|
-
"output += f'
|
|
63
|
+
"output += '# Commands Overview\\n'\n",
|
|
64
|
+
"output += f'Python Code Audit commands for: {version_id}'"
|
|
65
65
|
]
|
|
66
66
|
},
|
|
67
67
|
{
|
|
68
68
|
"cell_type": "code",
|
|
69
|
-
"execution_count":
|
|
69
|
+
"execution_count": 14,
|
|
70
70
|
"id": "693c3354-530b-4a40-a561-ed722d9bb1fa",
|
|
71
71
|
"metadata": {},
|
|
72
72
|
"outputs": [],
|
|
@@ -77,7 +77,7 @@
|
|
|
77
77
|
},
|
|
78
78
|
{
|
|
79
79
|
"cell_type": "code",
|
|
80
|
-
"execution_count":
|
|
80
|
+
"execution_count": 15,
|
|
81
81
|
"id": "5fa9a420-bd9a-4641-99c9-de0bcf448dbc",
|
|
82
82
|
"metadata": {},
|
|
83
83
|
"outputs": [],
|
|
@@ -90,19 +90,18 @@
|
|
|
90
90
|
},
|
|
91
91
|
{
|
|
92
92
|
"cell_type": "code",
|
|
93
|
-
"execution_count":
|
|
93
|
+
"execution_count": 16,
|
|
94
94
|
"id": "bf6afe56-e0f7-4fa2-a3a5-968bad11bf9c",
|
|
95
95
|
"metadata": {},
|
|
96
96
|
"outputs": [],
|
|
97
97
|
"source": [
|
|
98
98
|
"commands = { \"overview\": 'overview_report', \n",
|
|
99
99
|
" \"modulescan\": 'report_module_information',\n",
|
|
100
|
-
" \"filescan\" : '
|
|
101
|
-
" \"directoryscan\" : 'directory_scan_report',\n",
|
|
100
|
+
" \"filescan\" : 'scan_report', \n",
|
|
102
101
|
" \"checks\" : 'report_implemented_tests',\n",
|
|
103
102
|
" \"version\" : 'display_version'} \n",
|
|
104
103
|
"for key, value in commands.items(): \n",
|
|
105
|
-
" output += f'##
|
|
104
|
+
" output += f'## Code Audit {key}\\n' # newlines matter when creating markdown\n",
|
|
106
105
|
" output += '```text\\n' # raw display \n",
|
|
107
106
|
" func_name = value\n",
|
|
108
107
|
" output += getattr(codeaudit, func_name).__doc__\n",
|
|
@@ -114,7 +113,7 @@
|
|
|
114
113
|
},
|
|
115
114
|
{
|
|
116
115
|
"cell_type": "code",
|
|
117
|
-
"execution_count":
|
|
116
|
+
"execution_count": 17,
|
|
118
117
|
"id": "0335783c-7676-4099-94c5-c98cc8f2f205",
|
|
119
118
|
"metadata": {
|
|
120
119
|
"editable": true,
|
|
@@ -135,7 +134,7 @@
|
|
|
135
134
|
},
|
|
136
135
|
{
|
|
137
136
|
"cell_type": "code",
|
|
138
|
-
"execution_count":
|
|
137
|
+
"execution_count": 18,
|
|
139
138
|
"id": "4667d7ec-3727-4e2d-97b5-6f597c697ec7",
|
|
140
139
|
"metadata": {},
|
|
141
140
|
"outputs": [],
|
|
@@ -160,7 +159,7 @@
|
|
|
160
159
|
"name": "python",
|
|
161
160
|
"nbconvert_exporter": "python",
|
|
162
161
|
"pygments_lexer": "ipython3",
|
|
163
|
-
"version": "3.13.
|
|
162
|
+
"version": "3.13.5"
|
|
164
163
|
}
|
|
165
164
|
},
|
|
166
165
|
"nbformat": 4,
|
|
@@ -4,6 +4,7 @@ Great that you want to contribute!
|
|
|
4
4
|
|
|
5
5
|
:::{tip}
|
|
6
6
|
All contributions are welcome!
|
|
7
|
+
|
|
7
8
|
Think of corrections on the manual, code and more or better tests.
|
|
8
9
|
:::
|
|
9
10
|
|
|
@@ -22,7 +23,7 @@ This simple tool is designed to be simple to use and maintain.
|
|
|
22
23
|
|
|
23
24
|
**Pull Requests are welcome!**
|
|
24
25
|
|
|
25
|
-
When you contribute to
|
|
26
|
+
When you contribute to Python Code Audit, your contributions are made under the same license as the file you are working on.
|
|
26
27
|
|
|
27
28
|
|
|
28
29
|
We adopt the [Collective Code Construction Contract(C4)](https://rfc.zeromq.org/spec/42/) to streamline collaboration. C4 is meant to provide a reusable optimal collaboration model for open source software projects.
|
|
@@ -7,11 +7,11 @@ parts:
|
|
|
7
7
|
- file: features
|
|
8
8
|
- file: userguide
|
|
9
9
|
sections:
|
|
10
|
-
- file: codeauditoverview
|
|
11
|
-
- file: directoryscan
|
|
10
|
+
- file: codeauditoverview
|
|
12
11
|
- file: filescan
|
|
13
12
|
- file: modulescan
|
|
14
13
|
- file: codeauditchecks
|
|
14
|
+
- file: howtoscan
|
|
15
15
|
- file: whysast
|
|
16
16
|
- file: issues
|
|
17
17
|
|
|
@@ -26,6 +26,7 @@ parts:
|
|
|
26
26
|
- file: checks/chmod_check
|
|
27
27
|
- file: checks/binding_check
|
|
28
28
|
- file: checks/directorycreation_check
|
|
29
|
+
- file: checks/dynamicimport_check
|
|
29
30
|
- file: checks/exception_check
|
|
30
31
|
- file: checks/tarfile_extract_check
|
|
31
32
|
- file: checks/hash_check
|
|
@@ -48,18 +49,19 @@ parts:
|
|
|
48
49
|
|
|
49
50
|
- caption: Architecture
|
|
50
51
|
chapters:
|
|
51
|
-
|
|
52
|
+
#- file: astlines
|
|
52
53
|
# - file: astlines2
|
|
54
|
+
- file: makeitbetter
|
|
53
55
|
- file: complexitycheck
|
|
54
56
|
- file: warnings
|
|
55
|
-
- file: codeauditcommands
|
|
56
|
-
|
|
57
|
+
- file: codeauditcommands
|
|
58
|
+
- file: changelog
|
|
57
59
|
|
|
58
60
|
|
|
59
61
|
# - caption: API Documentation
|
|
60
62
|
# chapters:
|
|
61
|
-
# - file:
|
|
62
|
-
|
|
63
|
+
# - file: codeaudit
|
|
64
|
+
|
|
63
65
|
|
|
64
66
|
- caption: About
|
|
65
67
|
chapters:
|
|
@@ -1,9 +1,18 @@
|
|
|
1
|
-
# AST
|
|
1
|
+
# Why use AST for code complexity
|
|
2
2
|
|
|
3
3
|
A simple way to count the number of lines of a file can be done with various unix commands.
|
|
4
|
-
Simple is to use the `wc` command.
|
|
4
|
+
Simple is to use the `wc` command.
|
|
5
|
+
|
|
6
|
+
But counting code lines is different than counting AST lines in a Python program.
|
|
7
|
+
|
|
8
|
+
:::{note}
|
|
9
|
+
AST lines give **good** indication for the complexity of a Python program.
|
|
10
|
+
|
|
11
|
+
And complexity is the enemy of security!
|
|
12
|
+
|
|
13
|
+
So a low number for complexity has several advantages from a security perspective!
|
|
14
|
+
:::
|
|
5
15
|
|
|
6
|
-
AST lines are needed to give a more precise indication of the complexity of a Python program.
|
|
7
16
|
|
|
8
17
|
|
|
9
18
|
To explain the difference between an **AST line** (as counted by the provided `count_ast_lines` function) and a **line counted by the Unix `wc` command**, let’s break it down:
|
|
@@ -8,6 +8,21 @@ The Python built-in functions:
|
|
|
8
8
|
* `compile`
|
|
9
9
|
Should always be reviewed within the full context. By default use of this function is a **red** alert from a security perspective.
|
|
10
10
|
|
|
11
|
+
Python Code Audit checks also on Builtin that are 'hidden':
|
|
12
|
+
|
|
13
|
+
* Confusable homoglyphs like: `ℯ𝓍ℯ𝒸("print(2 + 2)")` Statements are detected.
|
|
14
|
+
|
|
15
|
+
* Obfuscating usage of builtins module calls of `eval`, `exec` and `compile` like:
|
|
16
|
+
```python
|
|
17
|
+
import builtins
|
|
18
|
+
b = builtins
|
|
19
|
+
b.exec("2+2")
|
|
20
|
+
```
|
|
21
|
+
Or
|
|
22
|
+
```python
|
|
23
|
+
code_obj = d.compile('x = 5*5\nprint(x)', '<string>', 'exec')
|
|
24
|
+
result = d.exec(code_obj) #Input should not be obfuscated. Code Audit will detect this!
|
|
25
|
+
```
|
|
11
26
|
|
|
12
27
|
## Why check on `eval`
|
|
13
28
|
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# Dynamic Import Statements
|
|
2
|
+
|
|
3
|
+
Using dynamic imports are a potential security issues.
|
|
4
|
+
Especially if you can not validate upfront what is imported.
|
|
5
|
+
|
|
6
|
+
Python Code Audit checks on:
|
|
7
|
+
* `__import__`: This builtin function SHOULD never be used anymore. This is an advanced function that is not needed in everyday Python programming.
|
|
8
|
+
* `importlib.import_module` use. Using this function should be validated upfront.
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
Using the dynamic imports can be a potential security issue, especially when the module name comes from an untrusted source. Often modules are fetches from internet or are imported by cleaver user input constructs in the code. But an attacker could also import the `os` module and then find a way to call functions to run commands on the system.
|
|
12
|
+
|
|
13
|
+
:::{caution}
|
|
14
|
+
Allowing dynamically module imports makes it easy to execute arbitrary code.
|
|
15
|
+
:::
|
|
16
|
+
|
|
17
|
+
:::{tip}
|
|
18
|
+
If the Python code or package really must use dynamic module input:
|
|
19
|
+
|
|
20
|
+
Use:
|
|
21
|
+
`importlib.import_module()`
|
|
22
|
+
This offers a better way to handle dynamic imports. Avoid using `__import__`.
|
|
23
|
+
:::
|
|
24
|
+
|
|
25
|
+
* `importlib.import_module()` is part of the standard library's importlib module, which is the modern way to interact with Python's import system programmatically. Its name clearly indicates its purpose, unlike `__import__()`, which looks like a "magic method" and is often a last resort or still found in old Python programs.
|
|
26
|
+
|
|
27
|
+
* Using `importlib.import_module()` keeps dynamic import logic contained within the `importlib module`, which is maintained by the core Python developers. This is from a security point of view preferred over directly using the low-level built-in function `__import__`.
|
|
28
|
+
|
|
29
|
+
## Mitigation
|
|
30
|
+
|
|
31
|
+
There is always a security risk when `importlib.import_module()` is used.
|
|
32
|
+
|
|
33
|
+
Possible mitigations:
|
|
34
|
+
* **ALWAYS** use the Python Code Audit `modulescan` option for all modules within a file.
|
|
35
|
+
* Check and understand what will be imported and what security risks are involved. You **MUST** never trust that dynamic imports are safe. Most are not!
|
|
36
|
+
* Check if your Python program has or needs an API to download dynamic imports.
|
|
37
|
+
* If you do not trust it: Call a security expert to help you! See the [sponsor](../sponsors) page for companies that could help you!
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
## References
|
|
41
|
+
|
|
42
|
+
* https://docs.python.org/3/library/functions.html#import__
|
|
@@ -13,9 +13,10 @@ Other implemented checks on `shutil` module methods:
|
|
|
13
13
|
* shutil.copy2
|
|
14
14
|
* shutil.copytree
|
|
15
15
|
* shutil.chown
|
|
16
|
+
* shutil.rmtree
|
|
16
17
|
|
|
17
18
|
Note:
|
|
18
|
-
* `shutil.rmtree` can be dangerous. However this call is/will be depreciated within the `shutil` module.
|
|
19
|
+
* `shutil.rmtree` can be dangerous. However this call is/will be depreciated within the `shutil` module. For now Python Code Audit will check on usage.
|
|
19
20
|
|
|
20
21
|
|
|
21
22
|
## More information
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# Zipfiles extraction
|
|
2
|
+
|
|
3
|
+
When using the Python module `zipfile` there is a risk processing maliciously prepared `.zip files`. This can availability issues due to storage exhaustion.
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
Validations are done on `zipfile` methods:
|
|
7
|
+
* `.extractall`
|
|
8
|
+
* `.open` and more.
|
|
9
|
+
|
|
10
|
+
And the methods:
|
|
11
|
+
* `gzip.open`
|
|
12
|
+
* `bz2.open`
|
|
13
|
+
* `bz2.BZ2File`
|
|
14
|
+
* `lzma.open`
|
|
15
|
+
* `lzma.LZMAFile`
|
|
16
|
+
* `shutil.unpack_archive`
|
|
17
|
+
|
|
18
|
+
## Potential danger when opening compressed files
|
|
19
|
+
|
|
20
|
+
When using `gzip.open` or equivalent the potential security issue is related to resource consumption if the file is untrusted.
|
|
21
|
+
|
|
22
|
+
:::{caution}
|
|
23
|
+
Never extract archives from untrusted sources without prior inspection!
|
|
24
|
+
|
|
25
|
+
It is possible that files are created outside of the path specified in the extract_dir argument, e.g. members that have absolute filenames starting with “/” or filenames with two dots “..”.
|
|
26
|
+
|
|
27
|
+
:::
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
This accounts also for using `bz2`, `lzma` , `shutil.unpack_archive` or `tar` compressed files. All these great Python functions that can decompress files require defense in depth to be sure that only trusted files can be opened.
|
|
31
|
+
|
|
32
|
+
This can lead to:
|
|
33
|
+
* **Denial of Service via Resource Exhaustion**
|
|
34
|
+
If a gzip file is controlled by a malicious user, they could create a highly compressed file that expands to an enormous size when decompressed. This is known as a "zip bomb."
|
|
35
|
+
|
|
36
|
+
Such `gzip` file could quickly consume all of the system's available RAM, causing the application to crash or the server to become unresponsive. This is a common attack vector when processing user-uploaded or external compressed files.
|
|
37
|
+
|
|
38
|
+
* **Potential Path Traversal**
|
|
39
|
+
A path traversal vulnerability could arise if the file in the `gzip` file is constructed from user input. For example, if the path came from a web request, a user could provide a path like ../../../../etc/passwd.gz to access sensitive files outside of the intended directory. This is a critical security consideration for any code that handles file paths based on external data that is decompressed with `gzip.open`.
|
|
40
|
+
|
|
41
|
+
## Possible measures
|
|
42
|
+
|
|
43
|
+
1. Make sure by design that these Python functions will **Only decompress files from trusted sources**
|
|
44
|
+
|
|
45
|
+
2. Set a limit for the decompression size. This is not simple and always possible! The Python `lzma` library does not have a built-in parameter to do this directly. You would need to read the data in fixed-size chunks and check the total size as you go, raising an error if it exceeds a predefined limit.
|
|
46
|
+
|
|
47
|
+
3. Check File Metadata: If possible, check the uncompressed size of the file from its header before starting the decompression. While not all formats contain this information, it can be a useful first check. **Note: This mitigation measurement should NEVER be used without other safeguards**
|
|
48
|
+
|
|
49
|
+
4. Resource Monitoring: Monitor your application's memory, CPU and resource usage during the decompression process and terminate it if it begins to consume an unusual amount of resources. Note that this measurement is not fail-safe!
|
|
50
|
+
|
|
51
|
+
## More information
|
|
52
|
+
|
|
53
|
+
* https://docs.python.org/3/library/zipfile.html#zipfile-resources-limitations
|
|
54
|
+
* https://docs.python.org/3/library/gzip.html
|
|
55
|
+
* https://docs.python.org/3/library/bz2.html#bz2.open
|
|
56
|
+
* https://docs.python.org/3/library/shutil.html
|