Metadata-Version: 2.4
Name: tarslip-guard
Version: 0.1.0
Summary: Detect unsafe tar/zip extractall() calls (CWE-22 tar-slip / zip-slip) and extract archives safely at runtime, with no dependencies.
Project-URL: Homepage, https://github.com/krc7169/tarslip-guard
Project-URL: Issues, https://github.com/krc7169/tarslip-guard/issues
Author-email: krc7169 <krc7169@gmail.com>
License: MIT License
        
        Copyright (c) 2026 krc7169
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENSE
Keywords: cwe-22,extractall,path-traversal,security,tar-slip,tarfile,zip-slip,zipfile
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Security
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.9
Provides-Extra: dev
Requires-Dist: build; extra == 'dev'
Requires-Dist: pytest>=7; extra == 'dev'
Requires-Dist: twine; extra == 'dev'
Description-Content-Type: text/markdown

# tarslip-guard

Find and fix **tar-slip / zip-slip** path-traversal bugs (CWE-22). Two
complementary, **dependency-free** tools:

1. **A static checker** that flags `extractall()` calls with no `filter=` or
   `members=` argument — the pattern that lets a malicious archive write files
   outside the intended directory.
2. **A safe runtime extractor** (`safe_extractall`) that validates every member
   before writing it, so you can extract untrusted archives without traversal.

## Install

```bash
pip install tarslip-guard
```

## Static checking

```python
from tarslip_guard import check_source, check_path

check_source("import tarfile\ntarfile.open('x').extractall('out')\n")
# -> [Finding(... rule='TARSLIP-EXTRACTALL', cwe='CWE-22', line=2 ...)]

for finding in check_path("src/"):
    print(finding)
```

```bash
tarslip-guard src/ tests/      # exits 1 if any unsafe call is found
```

`extractall(..., filter="data")` (Python 3.12+) and `extractall(members=...)`
are treated as safe and not flagged.

## Safe extraction

```python
from tarslip_guard import safe_extractall, UnsafeArchiveError

try:
    safe_extractall("untrusted.tar.gz", "output_dir")
except UnsafeArchiveError as exc:
    print("refused:", exc)
```

- Accepts an open `tarfile.TarFile` / `zipfile.ZipFile`, or a path (type is
  auto-detected).
- Rejects absolute paths and `..` traversal for every member.
- Rejects symlink/hardlink members that escape the destination (pass
  `allow_symlinks=True` to permit links that stay inside it).
- `safe_extract_to_temp(archive)` extracts into a fresh temp dir and returns it.

## Why

`TarFile.extractall()` has no path sanitization before Python 3.12's data
filter, and even on newer versions a lot of code passes untrusted archives
without opting in. This library gives you a linter to find those call sites and
a safe function to replace them.

## License

MIT
