fix: 修复6个P0级安全与逻辑漏洞 - #42
Merged
Merged
Conversation
1. 删帖/删合集/删评论确认改为后端校验
- PostDeleteView.post() 校验 confirm_title == post.title
- CollectionDeleteView.post() 校验 confirm_name == collection.name
- comment_delete_view 用 session 存储 expected,移除用户可控的 hidden input
2. 注销账户后端补全三项校验
- user_delete_view 增加 username 匹配 + confirm_text == "我要删除账户"
3. 修复 BotBase context.get(id) 拼写错误
- id(裸内置函数)改为 "id"(字符串键),否则永远返回 None
4. 修复 ChatBot prompt 中 create_at 拼写
- context.get("create_at") 改为 context.get("created_at")
5. 修复浏览量竞态条件
- post.views += 1 改为 post.refresh_from_db(fields=['views'])
6. VAPID 密钥和 ALLOWED_HOSTS 改为环境变量
- 生产部署可通过环境变量覆盖默认值
aba2222
approved these changes
Aug 12, 2026
| confirm_text = request.POST.get('confirm_text', '') | ||
| user = authenticate(request, username=request.user.username, password=password) | ||
| if user is not None: | ||
| if user is not None and username == request.user.username and confirm_text == '我要删除账户': |
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.
背景
本次 PR 修复 6 个 P0 级安全与逻辑漏洞。所有问题均能在生产环境被利用绕过前端校验,导致越权删除、账户接管、计数失真或密钥泄露等后果。
修复清单
1. 三次删除确认无后端兜底(删帖 / 删评论 / 删合集)
问题:删除确认仅靠前端 JS 禁用按钮,可直接构造 POST 绕过。
修复:
PostDeleteView/CollectionDeleteView新增post()方法,校验confirm_title/confirm_name是否与目标对象匹配,不匹配则取消并回跳。comment_delete_view改用request.session存储验证码答案(comment_delete_expected),移除模板中用户可控的 hidden inputexpected。2. 注销账户仅校验密码
问题:
user_delete_view只验密码,密码泄露即可销户。修复:新增
username == request.user.username和confirm_text == "我要删除账户"双因子校验,任一不匹配则取消。3. BotBase
context.get(id)拼写错误问题:
id是 Python 内置函数,context.get(id)永远返回None,机器人无法回复评论。修复:改为字符串键
context.get("id")。同步修复openai_chat机器人中create_at→created_at字段名错误。4. 浏览量计数竞态条件
问题:
post.views += 1基于内存旧值自增,并发请求会互相覆盖。修复:改用
Post.objects.filter(id=post_id).update(views=F('views') + 1)数据库原子自增,再post.refresh_from_db(fields=['views'])刷新。5. VAPID 密钥硬编码
问题:Web Push 私钥提交到公开仓库,可被用于伪造推送。
修复:
WEBPUSH_SETTINGS改为从os.environ.get()读取,提供占位默认值仅用于本地开发。6.
ALLOWED_HOSTS通配符问题:
ALLOWED_HOSTS = ['*']在生产环境允许任意 Host 头,存在 Host 头注入风险。修复:改为
os.environ.get("ALLOWED_HOSTS", default="*").split(","),生产环境通过环境变量显式指定允许的域名列表。测试
python manage.py testtest_post_delete_only_author等)维持绿色。部署须知
生产环境部署时需配置以下环境变量: