Repository navigation
fix: 防止菜单名路径穿透写入/删除资源目录外的文件 - #45
Merged
Merged
Conversation
在添加或删除菜单时, 菜品名会被直接拼进文件路径, 未做任何校验。 构造 `../../xxx` 这样的名字即可把文件写到资源目录之外, 或者删除目录外的任意 .jpg 文件; 报错信息还会暴露 nonebot 的运行路径。 Changes: - 新增 resolve_pic_path(), 统一解析菜品图片路径 - 校验 img_type 仅允许 eat / drink - 拒绝含路径分隔符、空字节、`.` / `..` 的名字 - resolve() 后二次校验路径仍在资源目录内, 防止绕过字符检查 - save_pic() / delete_pic() 改用 resolve_pic_path() - save_pic 的 bytes 类型检查提到写文件之前 - matcher 捕获 ValueError, 向用户返回“菜名不合法”而不是抛未处理异常 Tests: - tests/test_files.py 覆盖正常名字、10 种恶意名字、非法 img_type, 并断言不会在资源目录外产生文件 - ruff check 通过 Refs Cvandia#43
|
|
Overall Grade |
Security Reliability Complexity Hygiene |
Code Review Summary
| Analyzer | Status | Updated (UTC) | Details |
|---|---|---|---|
| Python | Sep 16, 2026 12:23p.m. | Review ↗ |
Important
AI Review is run only on demand for your team. We're only showing results of static analysis review right now. To trigger AI Review, comment @deepsourcebot review on this thread.
huanyan77777
approved these changes
Oct 21, 2026
Cvandia
approved these changes
Oct 21, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题
添加/删除菜单时,菜品名
name未做任何校验就被拼进文件路径:构造
../../xxx这样的名字即可逃出资源目录,写入任意位置;delete_pic同理,可删除资源目录外的.jpg。失败时抛出的FileNotFoundError还会把 nonebot 的完整运行路径回显给用户。复现
资源目录设为
<tmp>/res时,该操作会在<tmp>/escaped.jpg落盘(已在本地对照验证,修复前成功写出文件,修复后被拦截)。变更
resolve_pic_path(),统一解析菜品图片路径:img_type仅允许eat/drink./..resolve()后二次校验结果仍在资源目录内,避免绕过字符检查save_pic()/delete_pic()改用resolve_pic_path()save_pic()的 bytes 类型检查提到写文件之前(原来失败时会先创建空文件)ValueError,向用户返回「菜名不合法」而不是未处理异常测试
新增
tests/test_files.py(16 项,本地全通过):../、..\\、a/b、绝对路径、空字节等)img_type被拒绝ruff check nonebot_plugin_whateat_pic/ tests/通过。Refs #43