fileaudit 0.2.1__tar.gz → 0.2.2__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.
- {fileaudit-0.2.1 → fileaudit-0.2.2}/.github/workflows/docbuild.yml +2 -0
- fileaudit-0.2.2/.github/workflows/testsuite.yml +33 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/.gitignore +1 -0
- fileaudit-0.2.2/CHANGELOG.md +34 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/PKG-INFO +1 -1
- {fileaudit-0.2.1 → fileaudit-0.2.2}/docs/SECURITY.md +1 -1
- {fileaudit-0.2.1 → fileaudit-0.2.2}/docs/_toc.yml +22 -22
- fileaudit-0.2.2/docs/csv_validations.md +48 -0
- fileaudit-0.2.2/docs/examples.md +4 -0
- fileaudit-0.2.2/docs/gz_validations.md +81 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/docs/intro.md +3 -2
- fileaudit-0.2.2/docs/json_validations.md +63 -0
- fileaudit-0.2.2/docs/notebooks/json_check_demo.ipynb +486 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/docs/notebooks/validate_csv_examples.ipynb +1 -1
- fileaudit-0.2.2/docs/python_validations.md +63 -0
- fileaudit-0.2.2/docs/tar_validations.md +91 -0
- fileaudit-0.2.2/docs/targz_validations.md +48 -0
- fileaudit-0.2.2/docs/xml_validations.md +141 -0
- fileaudit-0.2.2/docs/zip_validations.md +106 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/src/fileaudit/__about__.py +1 -1
- {fileaudit-0.2.1 → fileaudit-0.2.2}/src/fileaudit/cli.py +8 -6
- {fileaudit-0.2.1 → fileaudit-0.2.2}/src/fileaudit/json_check.py +20 -13
- {fileaudit-0.2.1 → fileaudit-0.2.2}/src/fileaudit/xml_check.py +194 -93
- {fileaudit-0.2.1 → fileaudit-0.2.2}/tests/test_unittest_json.py +19 -13
- fileaudit-0.2.2/tests/test_xml_files.py +75 -0
- fileaudit-0.2.2/tests/validationfiles/xml/cyclic.xml +5 -0
- fileaudit-0.2.2/tests/validationfiles/xml/expansion.xml +14 -0
- fileaudit-0.2.2/tests/validationfiles/xml/external.xml +4 -0
- fileaudit-0.2.2/tests/validationfiles/xml/xmlbomb.xml +7 -0
- fileaudit-0.2.2/tests/validationfiles/xml/xmlbomb2.xml +8 -0
- fileaudit-0.2.2/tests/validationfiles/xml/xmlbomb3.xml +15 -0
- fileaudit-0.2.1/CHANGELOG.md +0 -13
- fileaudit-0.2.1/docs/check_csv.md +0 -5
- fileaudit-0.2.1/tests/validationfiles/xml/.ipynb_checkpoints/nocx_rss-checkpoint.xml +0 -1157
- fileaudit-0.2.1/tests/validationfiles/xml/.ipynb_checkpoints/nocx_rss_xml-checkpoint +0 -1157
- {fileaudit-0.2.1 → fileaudit-0.2.2}/CONTRIBUTE.md +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/LICENSE +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/README.md +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/SECURITY.md +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/docs/CONTRIBUTE.md +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/docs/_config.yml +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/docs/_static/nocxstyle.css +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/docs/about.md +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/docs/changelog.md +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/docs/general_use.md +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/docs/help.md +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/docs/images/OO.png +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/docs/images/ROI_logo.png +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/docs/images/YourLogoHere.png +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/docs/images/ai_use.png +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/docs/images/architecture_overview.png +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/docs/images/ca_logo.png +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/docs/images/codeauditlogo.png +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/docs/images/filescan_screenshot_16012026.png +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/docs/images/modulescan_screenshot_16012026.png +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/docs/images/nocxbanner.png +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/docs/images/overview_linkaudit.png +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/docs/images/overview_screenshot_16012026.png +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/docs/installation.md +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/docs/license.md +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/docs/reference/fileaudit.cli.rst +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/docs/reference/fileaudit.csv_check.rst +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/docs/reference/fileaudit.gz_check.rst +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/docs/reference/fileaudit.json_check.rst +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/docs/reference/fileaudit.python_check.rst +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/docs/reference/fileaudit.rst +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/docs/reference/fileaudit.tar_check.rst +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/docs/reference/fileaudit.targz_check.rst +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/docs/reference/fileaudit.xml_check.rst +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/docs/reference/fileaudit.zip_check.rst +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/docs/reference/modules.rst +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/docs/sponsors.md +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/htmlcov/.gitignore +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/htmlcov/class_index.html +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/htmlcov/coverage_html_cb_dd2e7eb5.js +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/htmlcov/favicon_32_cb_c827f16f.png +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/htmlcov/function_index.html +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/htmlcov/index.html +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/htmlcov/keybd_closed_cb_900cfef5.png +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/htmlcov/status.json +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/htmlcov/style_cb_4667309f.css +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/htmlcov/z_65b8f32785d686bf___about___py.html +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/htmlcov/z_65b8f32785d686bf___init___py.html +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/htmlcov/z_65b8f32785d686bf_cli_py.html +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/htmlcov/z_65b8f32785d686bf_csv_check_py.html +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/htmlcov/z_65b8f32785d686bf_gz_check_py.html +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/htmlcov/z_65b8f32785d686bf_json_check_py.html +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/htmlcov/z_65b8f32785d686bf_python_check_py.html +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/htmlcov/z_65b8f32785d686bf_tar_check_py.html +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/htmlcov/z_65b8f32785d686bf_targz_check_py.html +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/htmlcov/z_65b8f32785d686bf_xml_check_py.html +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/htmlcov/z_65b8f32785d686bf_zip_check_py.html +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/pyproject.toml +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/src/fileaudit/__init__.py +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/src/fileaudit/csv_check.py +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/src/fileaudit/gz_check.py +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/src/fileaudit/python_check.py +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/src/fileaudit/tar_check.py +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/src/fileaudit/targz_check.py +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/src/fileaudit/zip_check.py +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/tests/test_cli.py +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/tests/test_csv.py +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/tests/test_csv_extra.py +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/tests/test_gz.py +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/tests/test_gz_extra.py +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/tests/test_gz_validation.py +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/tests/test_json.py +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/tests/test_json_core.py +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/tests/test_python.py +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/tests/test_python_timeouts.py +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/tests/test_tar.py +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/tests/test_targz.py +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/tests/test_targz_core.py +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/tests/test_validate_tar.py +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/tests/test_xml.py +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/tests/test_xml_extra.py +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/tests/test_zip.py +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/tests/validationfiles/csv/sample.csv +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/tests/validationfiles/gz/samplefile.txt.gz +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/tests/validationfiles/json/codeaudit_scan.json +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/tests/validationfiles/json/nojsonfile.json +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/tests/validationfiles/python/sample_with_null.py +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/tests/validationfiles/xml/nlnetfeed.atom +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/tests/validationfiles/xml/nocx_rss.xml +0 -0
- {fileaudit-0.2.1 → fileaudit-0.2.2}/tests/validationfiles/zip/sample.zip +0 -0
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
name: File Audit Validation Tests
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
paths:
|
|
6
|
+
- "src/fileaudit/**"
|
|
7
|
+
- "tests/**"
|
|
8
|
+
- "pyproject.toml"
|
|
9
|
+
pull_request:
|
|
10
|
+
paths:
|
|
11
|
+
- "src/fileaudit/**"
|
|
12
|
+
- "tests/**"
|
|
13
|
+
- "pyproject.toml"
|
|
14
|
+
|
|
15
|
+
jobs:
|
|
16
|
+
test:
|
|
17
|
+
runs-on: ubuntu-latest
|
|
18
|
+
|
|
19
|
+
steps:
|
|
20
|
+
- name: Check out repository
|
|
21
|
+
uses: actions/checkout@v4
|
|
22
|
+
|
|
23
|
+
- name: Set up Python
|
|
24
|
+
uses: actions/setup-python@v6
|
|
25
|
+
with:
|
|
26
|
+
python-version: "3.14"
|
|
27
|
+
|
|
28
|
+
- name: Install Hatch
|
|
29
|
+
run: |
|
|
30
|
+
python -m pip install --upgrade pip hatch
|
|
31
|
+
|
|
32
|
+
- name: Run tests
|
|
33
|
+
run: hatch test -ar
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# Change Log
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
## Version 0.2.2
|
|
6
|
+
|
|
7
|
+
### Added
|
|
8
|
+
* Added `-v` or `--version` flag for CLI to display `fileaudit` version information.
|
|
9
|
+
|
|
10
|
+
### Changed
|
|
11
|
+
|
|
12
|
+
* Updated and added more comprehensive documentation across modules.
|
|
13
|
+
* Minor code improvements and updates for `xml_validator`.
|
|
14
|
+
|
|
15
|
+
### Fixed
|
|
16
|
+
|
|
17
|
+
* Corrected automatic documentation building script for GitHub Pages deployment.
|
|
18
|
+
* Test corrections and minor code improvements for `json_validator`.
|
|
19
|
+
|
|
20
|
+
### Documentation
|
|
21
|
+
|
|
22
|
+
* Expanded usage examples, API reference, and setup guides.
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
## Version 0.2.1
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
### Added
|
|
30
|
+
- Added Open Source Security Foundation (OpenSSF) Best Practices badge to `README.md`.
|
|
31
|
+
|
|
32
|
+
### Documentation
|
|
33
|
+
- Updated project manual.
|
|
34
|
+
- Applied text corrections and proofreading fixes to the manual.
|
|
@@ -3,8 +3,7 @@ root: intro
|
|
|
3
3
|
|
|
4
4
|
parts:
|
|
5
5
|
- caption: Getting Started
|
|
6
|
-
chapters:
|
|
7
|
-
- file: features
|
|
6
|
+
chapters:
|
|
8
7
|
- file: installation
|
|
9
8
|
- url: https://securitytesting.nocomplexity.com/
|
|
10
9
|
title: Security Testing for Python
|
|
@@ -16,34 +15,35 @@ parts:
|
|
|
16
15
|
- caption: User Guide
|
|
17
16
|
chapters:
|
|
18
17
|
- file: general_use
|
|
18
|
+
- file: json_validations
|
|
19
|
+
- file: targz_validations
|
|
20
|
+
- file: tar_validations
|
|
21
|
+
- file: gz_validations
|
|
22
|
+
- file: zip_validations
|
|
23
|
+
- file: xml_validations
|
|
24
|
+
- file: csv_validations
|
|
25
|
+
- file: python_validations
|
|
26
|
+
- file: examples
|
|
19
27
|
sections:
|
|
20
28
|
- file: notebooks/validate_csv_examples
|
|
29
|
+
- file: notebooks/json_check_demo
|
|
21
30
|
|
|
22
31
|
|
|
23
|
-
# - caption: Architecture
|
|
24
|
-
# chapters:
|
|
25
|
-
# - file: architecture
|
|
26
|
-
# - file: makeitbetter
|
|
27
|
-
# - file: project_philosophy
|
|
28
|
-
# - file: codeauditcommands
|
|
29
|
-
# - file: changelog
|
|
30
32
|
|
|
31
33
|
|
|
32
34
|
- caption: API Documentation
|
|
33
|
-
chapters:
|
|
34
|
-
#- file: apidocs/api_intro
|
|
35
|
-
# sections:
|
|
36
|
-
# - file: examples/ca_api_example_overview
|
|
37
|
-
# - file: examples/example_risk_heatmap
|
|
38
|
-
# - file: examples/example_weakness_perfile_view
|
|
39
|
-
# - file: examples/ca_api_example_checks
|
|
40
|
-
# - file: examples/ca_api_example_scanning
|
|
41
|
-
# - file: examples/ca_api_example_json
|
|
42
|
-
# - file: examples/ca_api_example_basic
|
|
35
|
+
chapters:
|
|
43
36
|
- file: reference/modules
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
- caption: Architecture
|
|
40
|
+
chapters:
|
|
41
|
+
- file: SECURITY
|
|
42
|
+
- file: changelog
|
|
43
|
+
# - file: architecture
|
|
44
|
+
# - file: project_philosophy
|
|
45
|
+
|
|
46
|
+
|
|
47
47
|
|
|
48
48
|
- caption: About
|
|
49
49
|
chapters:
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# CSV Validation
|
|
2
|
+
|
|
3
|
+
## Why Security Checks Are Needed
|
|
4
|
+
|
|
5
|
+
The Comma Separated Values (CSV) file format is the most common import format for spreadsheets and databases. But loading `csv` files can result in a security nightmare.
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
CSV files from untrusted sources can trigger security issues or resource exhaustion.
|
|
9
|
+
Formula injection (cells starting with `=`, `+`, `-`, or `@`) can execute code when opened in spreadsheets.
|
|
10
|
+
Control characters may cause parsing failures or injection attacks.
|
|
11
|
+
Unbounded size, row count, column count, or field length can lead to denial-of-service.
|
|
12
|
+
|
|
13
|
+
Validating before processing mitigates these risks.
|
|
14
|
+
|
|
15
|
+
## Capabilities
|
|
16
|
+
|
|
17
|
+
`validate_csv` provides configurable protection against common CSV risks:
|
|
18
|
+
|
|
19
|
+
- **Size & resource limits** — file size, row count, column count, individual field size, total fields, row size, and filename length (prevents resource exhaustion and oversized records)
|
|
20
|
+
- **Content safety** — rejects dangerous spreadsheet formulas (formula injection) and disallowed control characters
|
|
21
|
+
- **Encoding control** — configurable character encoding (default UTF-8)
|
|
22
|
+
- **Flexible input** — local paths, `pathlib.Path` objects, and URLs
|
|
23
|
+
- **Dual usage modes** — direct validation (returns `True`/`False`) or decorator that guards a function argument and raises `CsvValidationError` on failure
|
|
24
|
+
|
|
25
|
+
## How to Use
|
|
26
|
+
|
|
27
|
+
**Direct validation** (returns `True`/`False`):
|
|
28
|
+
|
|
29
|
+
```python
|
|
30
|
+
validate_csv("data.csv")
|
|
31
|
+
validate_csv("https://example.com/data.csv")
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
**As a decorator** (raises `CsvValidationError` on failure):
|
|
35
|
+
|
|
36
|
+
```python
|
|
37
|
+
@validate_csv
|
|
38
|
+
def process_csv(csv_path):
|
|
39
|
+
...
|
|
40
|
+
|
|
41
|
+
@validate_csv(max_file_size=10*1024*1024, max_rows=10_000)
|
|
42
|
+
def process_csv(csv_path):
|
|
43
|
+
...
|
|
44
|
+
|
|
45
|
+
@validate_csv("input_file")
|
|
46
|
+
def process_csv(input_file):
|
|
47
|
+
...
|
|
48
|
+
```
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# GZ Validations
|
|
2
|
+
|
|
3
|
+
## Why Security Checks Are Needed Before Processing GZip Files
|
|
4
|
+
|
|
5
|
+
Processing untrusted GZip files without validation can lead to:
|
|
6
|
+
|
|
7
|
+
- **Zip bombs** (extreme decompression ratios causing resource exhaustion)
|
|
8
|
+
- **Denial of service** (massive uncompressed sizes consuming memory/disk)
|
|
9
|
+
- **Path traversal** (symbolic links escaping the intended extraction directory)
|
|
10
|
+
- **TOCTOU attacks** (file modification during validation)
|
|
11
|
+
- **Remote file abuse** (malicious servers serving oversized or malformed files)
|
|
12
|
+
- **Corrupted/malformed data** (invalid GZip CRC, trailers, or concatenated members causing crashes)
|
|
13
|
+
|
|
14
|
+
## Capabilities
|
|
15
|
+
|
|
16
|
+
The `validate_gz` function performs comprehensive security checks:
|
|
17
|
+
|
|
18
|
+
| Check | Description |
|
|
19
|
+
|-------|-------------|
|
|
20
|
+
| **Compressed Size** | Enforces maximum archive file size (bytes) |
|
|
21
|
+
| **Uncompressed Size** | Limits total decompressed output to prevent bombs |
|
|
22
|
+
| **Decompression Ratio** | Rejects files with excessive compression ratios (e.g., >100x) |
|
|
23
|
+
| **Streaming Validation** | Decompresses incrementally without loading entire file into memory |
|
|
24
|
+
| **GZip Format Validation** | Validates CRC, trailer, and concatenated members |
|
|
25
|
+
| **Symlink Protection** | Rejects symbolic links (O_NOFOLLOW support where available) |
|
|
26
|
+
| **Regular File Enforcement** | Ensures local paths are regular files (not directories, devices, etc.) |
|
|
27
|
+
| **TOCTOU Detection** | Detects file size changes during validation (local files) |
|
|
28
|
+
| **Remote File Restriction** | Strictly restricts remote access to HTTPS only |
|
|
29
|
+
|
|
30
|
+
## How the Checks Can Be Used
|
|
31
|
+
|
|
32
|
+
The function operates in three modes:
|
|
33
|
+
|
|
34
|
+
### 1. Direct Call / CLI Mode
|
|
35
|
+
|
|
36
|
+
```python
|
|
37
|
+
# Validate a local GZip file
|
|
38
|
+
result = validate_gz("path/to/file.gz", max_uncompressed_size=1000000)
|
|
39
|
+
|
|
40
|
+
# Validate remote file (HTTPS only)
|
|
41
|
+
result = validate_gz("https://example.com/file.gz", max_file_size=10*1024*1024)
|
|
42
|
+
|
|
43
|
+
# Returns True if valid, False if invalid (validation errors printed to stdout)
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### 2. Decorator Mode
|
|
47
|
+
|
|
48
|
+
```python
|
|
49
|
+
# Bare decorator (uses default limits)
|
|
50
|
+
@validate_gz
|
|
51
|
+
def decompress_data(file_path):
|
|
52
|
+
# File already validated before function body runs
|
|
53
|
+
return process_gzip(file_path)
|
|
54
|
+
|
|
55
|
+
# Decorator with custom limits
|
|
56
|
+
@validate_gz(max_file_size=5_000_000, max_uncompressed_ratio=50)
|
|
57
|
+
def decompress_secure(file_input):
|
|
58
|
+
return safe_decompress(file_input)
|
|
59
|
+
|
|
60
|
+
# Explicit argument name targeting
|
|
61
|
+
@validate_gz("input_path", max_uncompressed_size=100*1024*1024)
|
|
62
|
+
def process_file(input_path):
|
|
63
|
+
return handle_decompressed_data(input_path)
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### 3. Factory Mode (Preconfigured Validator)
|
|
67
|
+
|
|
68
|
+
```python
|
|
69
|
+
# Create a reusable validator with custom defaults
|
|
70
|
+
gz_validator = validate_gz(
|
|
71
|
+
max_file_size=10*1024*1024,
|
|
72
|
+
max_uncompressed_ratio=100,
|
|
73
|
+
max_uncompressed_size=500*1024*1024
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
@gz_validator
|
|
77
|
+
def process_large_gzip(archive_path):
|
|
78
|
+
# Uses preconfigured limits
|
|
79
|
+
return handle_large(archive_path)
|
|
80
|
+
```
|
|
81
|
+
|
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
[](https://github.com/nocomplexity/codeaudit)
|
|
4
4
|
[](https://www.bestpractices.dev/projects/14110)
|
|
5
5
|
[](https://pypi.org/project/fileaudit)
|
|
6
|
-
[](https://nocomplexity.
|
|
7
|
-
[](https://github.com/nocomplexity/fileaudit/docs/license.md)
|
|
6
|
+
[](https://nocomplexity.github.io/fileaudit/intro.html)
|
|
7
|
+
[](https://github.com/nocomplexity/fileaudit/blob/main/docs/license.md)
|
|
8
8
|
|
|
9
9
|
|
|
10
10
|
**Python File Audit**: Build secure Python applications by default. Validate files before you use them.
|
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
:alt: logo
|
|
15
15
|
:width: 200px
|
|
16
16
|
:align: center
|
|
17
|
+
:target: https://codeaudit.nocomplexity.com
|
|
17
18
|
```
|
|
18
19
|
|
|
19
20
|
::::{grid} 2
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# JSON Validation
|
|
2
|
+
|
|
3
|
+
## Why Security Checks Are Needed
|
|
4
|
+
|
|
5
|
+
Be cautious when parsing JSON data from untrusted sources or third parties. From a zero trust principle you **SHOULD** always verify all input.
|
|
6
|
+
|
|
7
|
+
:::{warning}
|
|
8
|
+
[A malicious JSON string may cause the decoder to consume considerable CPU and memory resources. Limiting the size of data to be parsed is recommended.](https://docs.python.org/3/library/json.html)
|
|
9
|
+
:::
|
|
10
|
+
|
|
11
|
+
- JSON files from untrusted sources can cause resource exhaustion or crashes.
|
|
12
|
+
- Excessive nesting depth can trigger stack overflows or recursive parsing attacks.
|
|
13
|
+
- Very large files can consume excessive memory or CPU.
|
|
14
|
+
- Validating size and structure before processing mitigates these risks.
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
## Capabilities
|
|
18
|
+
|
|
19
|
+
`validate_json` provides configurable protection against common JSON risks:
|
|
20
|
+
|
|
21
|
+
- **Nesting depth limit** — rejects excessively deep structures (prevents stack/recursion attacks)
|
|
22
|
+
- **File size limit** — rejects oversized files before full loading (prevents resource exhaustion); remote files are size-checked via HEAD and streamed with a hard byte cap
|
|
23
|
+
- **Strict UTF-8 encoding** — only valid UTF-8 content is accepted; non-UTF-8 data raises a validation error
|
|
24
|
+
- **HTTPS-only remote access** — local paths, `pathlib.Path` objects, and HTTPS URLs are supported; other schemes are rejected
|
|
25
|
+
- **Network timeouts** — remote requests use explicit timeouts (10 s for HEAD size check, 30 s for GET download) to avoid hanging on unresponsive servers
|
|
26
|
+
- **Dual usage modes** — direct validation (returns `True`/`False`) or decorator that guards a function argument and raises `FileValidationError` on failure
|
|
27
|
+
|
|
28
|
+
## How to Use
|
|
29
|
+
|
|
30
|
+
**Direct validation** (returns `True`/`False`):
|
|
31
|
+
|
|
32
|
+
```python
|
|
33
|
+
validate_json("data.json")
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
```python
|
|
37
|
+
validate_json("https://example.com/data.json", max_depth=10)
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
**As a decorator** (raises FileValidationError on failure):
|
|
41
|
+
|
|
42
|
+
```python
|
|
43
|
+
@validate_json
|
|
44
|
+
def process_json(file_path):
|
|
45
|
+
...
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
```python
|
|
49
|
+
@validate_json(max_depth=50, max_file_size=5000)
|
|
50
|
+
def process_json(file_path):
|
|
51
|
+
...
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
```python
|
|
55
|
+
@validate_json("config_path", max_depth=10)
|
|
56
|
+
def process_json(config_path, other_arg):
|
|
57
|
+
...
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
:::{note}
|
|
62
|
+
JSON Schema checks are **not covered**! This is application specific and not considered as a general security validation.
|
|
63
|
+
:::
|