crate / crate/crate-python

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

未关闭 适合新手
#794 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
Python
星标
85
派生
34
平均合并
3 天 1 小时
30 天内合并 PR
4

描述

**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)

贡献指南

打开贡献指南

调研方向

从 src/crate/testing/layer.py 开始,检查 tarfile.extractall() 调用及其调用方。审查提议的 safe_extract 逻辑,并验证归档成员无法写入目标目录之外。完成的标准是:在不破坏现有 layer 提取行为的情况下解决 Bandit B202 漏洞。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
security
Issue 类型
缺陷
难度
2/5
预计耗时
1-3 小时
活跃度
冷清
描述清晰度
描述清楚
新手友好度
68/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。