Skip to content

Commit 8020766

Browse files
committed
fileaudit added
1 parent 3d065d1 commit 8020766

2 files changed

Lines changed: 114 additions & 1 deletion

File tree

securitytools/fileaudit.md

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
---
2+
title: Python File Audit
3+
short_title: Python File Audit
4+
---
5+
6+
Handling untrusted input files is one of the most common vectors for security vulnerabilities in Python applications. Accepting file uploads or processing third-party data without explicit validation violates zero-trust principles and exposes your system to Denial of Service (DoS), Remote Code Execution (RCE), and Arbitrary File Write attacks.
7+
8+
The [`fileaudit`](https://github.com/nocomplexity/fileaudit) library provides a lightweight, [open-source](https://nocomplexity.github.io/fileaudit/license.html#software-license) defense layer designed to intercept and validate files before your application processes them.
9+
10+
## File-Based Threat Vectors
11+
12+
When your application handles untrusted input, the attack surface varies depending on the archive or serialisation format.
13+
14+
### Archive Exploits (ZIP, TAR, GZ)
15+
16+
- **Archive / Decompression Bombs:** A heavily compressed file expands exponentially upon extraction (e.g., a few kilobytes expanding into hundreds of gigabytes), quickly exhausting disk space and memory.
17+
- **Inode & DoS Bombs:** Archives containing millions of microscopic files designed to consume all available filesystem inodes or exhaust CPU during unpacking.
18+
- **Path Traversal (Zip Slip):** Archive entries containing relative paths (e.g., `../../etc/passwd`) designed to overwrite system or application files outside the extraction directory.
19+
- **Symlink & Hardlink Attacks:** Archives embedded with symbolic or hard links targeting sensitive host paths, allowing subsequent file writes to redirect to critical system locations.
20+
- **Device Node Injection:** Archives containing character devices, block devices, or FIFOs (named pipes) intended to cause hangs or facilitate privilege escalation.
21+
- **Directory Traversal Depths:** Deeply nested folder structures intended to trigger recursive extraction stack overflows or degrade filesystem operations.
22+
23+
### XML Processing Exploits
24+
25+
Processing untrusted XML documents using standard parsers exposes the application to severe parser-level attacks:
26+
27+
- **Billion Laughs (Entity Expansion):** Exponential inline DTD entity expansion that consumes all available RAM in seconds.
28+
- **XML External Entity (XXE):** Misconfigured DTD parsers disclosing local system files or making SSRF calls via URI handlers.
29+
- **Attribute & Structure Bombs:** Elements containing thousands of attributes or extreme nesting depths designed to trigger stack overflows and CPU starvation.
30+
31+
### JSON & Structured Data Exploits
32+
33+
As highlighted in the [official Python documentation](https://docs.python.org/3/library/json.html), native decoders can be vulnerable to resource exhaustion when parsing malformed or excessively large payloads:
34+
35+
:::{warning}
36+
Unbounded JSON strings or deeply nested objects can force `json.loads()` to consume excessive CPU cycles and RAM. Enforcing strict size and recursion limits prior to parsing is required when dealing with untrusted origins.
37+
:::
38+
39+
## Defending Files with `fileaudit`
40+
41+
The `fileaudit` library mitigates these risks by applying safe extraction constraints, path sanitisation, and size boundaries prior to processing.
42+
43+
### Supported Formats
44+
45+
| Extension | Target File Format | Primary Enforced Protections |
46+
| :--- | :--- | :--- |
47+
| `.csv` | Tabular Data | Length limits, delimiter boundaries |
48+
| `.gz`, `.tgz`, `.tar.gz` | Compressed Archives | Decompression ratio caps, size limits |
49+
| `.json` | JSON Documents | String length, structure depth, raw size |
50+
| `.py` | Python Source Files | File size, static path checks |
51+
| `.tar` | Tape Archives | Member counts, symlink/FIFO rejection |
52+
| `.xml` | XML Documents | Entity expansion bounds, node depth limits |
53+
| `.zip` | ZIP Archives | Path traversal blocking, size limits |
54+
55+
### Key Protections Enforced
56+
57+
:::{admonition} Built-in Security Controls
58+
:class: tip
59+
60+
- **Decompression Caps:** Monitors GZip expansion ratios to catch decompression bombs early.
61+
- **Archive Size & Member Bounds:** Imposes hard upper limits on total extracted size, entry counts, and individual file sizes.
62+
- **Path & Type Sanitisation:** Automatically strips path traversal sequences (`../`), rejecting symlinks, hard links, FIFOs, and special device nodes.
63+
- **Structural Bounds:** Restricts maximum filename lengths, total directory depth, and nesting levels.
64+
:::
65+
66+
## Implementation
67+
68+
### Installation
69+
70+
Install [`fileaudit`](https://github.com/nocomplexity/fileaudit) from [PyPI](https://pypi.org/project/fileaudit/):
71+
72+
```bash
73+
pip install fileaudit
74+
75+
```
76+
77+
### Usage Modes
78+
79+
Each format validator (such as `validate_tar_gz`, `validate_zip`, or `validate_json`) supports two integration patterns depending on your architecture.
80+
81+
#### 1. Decorator Mode
82+
83+
Wrap processing functions directly. The validator inspects the input parameter and aborts execution before the function body runs if checks fail:
84+
85+
```python
86+
from fileaudit import validate_tar_gz
87+
88+
@validate_tar_gz
89+
def process_upload(file_path: str) -> None:
90+
# Function executes only if the archive passes all security bounds
91+
...
92+
93+
```
94+
95+
#### 2. Direct Functional Call
96+
97+
Perform explicit programmatic verification within your validation pipeline:
98+
99+
```python
100+
from fileaudit import validate_tar_gz
101+
102+
def handle_incoming_file(file_path: str) -> None:
103+
if not validate_tar_gz(file_path):
104+
raise ValueError("File failed security audit checks.")
105+
106+
# Safe to proceed with extraction
107+
...
108+
109+
```
110+
111+
For advanced configuration, parameter tuning, and custom threshold adjustments, consult the official [Python File Audit Documentation](https://nocomplexity.github.io/fileaudit/intro.html).
112+

toc.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ project:
2828
- file: securitytools/tools_intro.md
2929
children: #dropdown menu
3030
- file: securitytools/opensourceinsights.md
31-
- file: securitytools/pip_audit.md
31+
- file: securitytools/fileaudit.md
3232
- file: securitytools/codeaudit.md
33+
- file: securitytools/pip_audit.md
3334
- file: securitytools/complexitycheck.md
3435
- file: securitytools/osv.md
3536

0 commit comments

Comments
 (0)