当前位置: 首页 > news >正文

CTFd CSRF 校验模块解读

实现上传头像功能的时候遇到了 403,隐约感觉到可能是 csrf 的问题,但是嫌麻烦一直懒得翻源码里的校验逻辑,反而让自己怀疑是 mime type 有限制,走了弯路。

机缘巧合发现了 csrf 的校验位置,总算是通了

定位代码位置

CTFd/__init__.py 文件下,观察如下代码片段

        init_request_processors(app)init_template_filters(app)init_template_globals(app)# Importing here allows tests to use sensible names (e.g. api instead of api_bp)from CTFd.admin import adminfrom CTFd.api import apifrom CTFd.auth import authfrom CTFd.challenges import challengesfrom CTFd.errors import render_errorfrom CTFd.events import eventsfrom CTFd.matches import matchesfrom CTFd.scoreboard import scoreboardfrom CTFd.share import socialfrom CTFd.teams import teamsfrom CTFd.users import usersfrom CTFd.views import viewsfrom CTFd.writeup import writeupapp.register_blueprint(views)app.register_blueprint(teams)app.register_blueprint(users)app.register_blueprint(matches)app.register_blueprint(challenges)

大概能猜出来 csrf 校验应该是在注册蓝图之前初始化的,也就是 init_request_processors。进去后发现一堆 @app.before_request 的装饰器,感觉对了。往下翻,查询 403 找到

@app.before_requestdef csrf():try:func = app.view_functions[request.endpoint]except KeyError:abort(404)if hasattr(func, "_bypass_csrf"):returnif request.headers.get("Authorization"):returnif not session.get("nonce"):session["nonce"] = generate_nonce()if request.method not in ("GET", "HEAD", "OPTIONS", "TRACE"):if request.content_type == "application/json":if session["nonce"] != request.headers.get("CSRF-Token"):abort(403)if request.content_type != "application/json":if session["nonce"] != request.form.get("nonce"):abort(403)

bingo。原来只有 json 的 csrf token 是放在 header 里的,难怪 403 了。

http://www.xdnf.cn/news/394147.html

相关文章:

  • 表加字段如何不停机
  • NCCL N卡通信机制
  • 《Effective Python》第1章 Pythonic 思维详解——始终用括号包裹单元素元组
  • 用一张网记住局域网核心概念:从拓扑结构到传输介质的具象化理解
  • 懒人美食帮SpringBoot订餐系统开发实现
  • Linux网络编程day9 libevent库
  • 代码随想录算法训练营第60期第三十二天打卡
  • RAII是什么?
  • 大学之大:东京工业大学2025.5.11
  • 误差函数(Error Function)的推导与物理意义
  • 【电机控制器】PY32MD310K18U7TR——ADC、UART
  • AAAI-2025 | 电子科大类比推理助力精准识别!SPAR:基于自提示类比推理的无人机目标探测技术
  • Java 线程池原理
  • 解决stm32HAL库使用vscode打开,识别不到头文件及uint8_t等问题
  • LOJ 6346 线段树:关于时间 Solution
  • 假如你的项目是springboot+vue怎么解决跨域问题
  • Anaconda环境中conda与pip命令的区别
  • Java--图书管理系统(简易版)
  • 信息安全管理与评估索引
  • 02.three官方示例+编辑器+AI快速学习webgl_animation_skinning_blending
  • C++类和对象--初阶
  • 英伟达微调qwen2.5-32B模型,开源推理模型:OpenCodeReasoning-Nemotron-32B
  • 关于 js:6. 网络与加密模块
  • JUC并发编程(上)
  • suricata之规则去重
  • 力扣刷题(第二十三天)
  • LLMs之MCP:2025年5月2日,Anthropic 宣布 Claude 重大更新:集成功能上线,研究能力大幅提升
  • 关于在使用getOutputStream()方法后续没有用到write()
  • 普通IT的股票交易成长史--20250511 美元与美股强相关性
  • 微服务架构中如何保证服务间通讯的安全