crate / crate/crate-python

[Security] tarfile.extractall without member validation in src/crate/testing/layer.py

オープン
#794 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る
主要言語
Python
スター
85
フォーク
34
平均マージ
3日 16時間
マージ済み PR(30日)
3

説明

**Severity**: HIGH (Bandit B202)
**File**: `src/crate/testing/layer.py`

## Vulnerability

`tarfile.extractall()` without member validation allows path traversal (zip slip). A malicious archive can write files outside the target directory.

## Fix

```python
import os

SAFE_ID = __import__("re").compile(r"^[a-zA-Z0-9_.-]+$")

def _is_within_directory(directory, target):
abs_directory = os.path.realpath(directory)
abs_target = os.path.realpath(target)
return abs_target.startswith(abs_directory + os.sep) or abs_target == abs_directory

def safe_extract(tar, path=".", members=None, *, numeric_owner=False):
for member in tar.getmembers():
member_path = os.path.join(path, member.name)
if not _is_within_directory(path, member_path):
raise Exception(f"Path traversal in tar: {member.name}")
tar.extractall(path, members, numeric_owner=numeric_owner)
```

## References
- CWE-22: Path Traversal
- Bandit B202
- [OWASP: Zip Slip](https://owasp.org/www-community/attacks/Zip_Slip)

コントリビューションガイド

コントリビューションガイドを開く

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。