Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

TypeError: __call()__ missing 1 required positional argument: ‘request’ #684

Closed
aoguai opened this issue Aug 24, 2023 · 9 comments
Closed
Labels
bug Something isn't working

Comments

@aoguai
Copy link

aoguai commented Aug 24, 2023

用 nb plugin install nonebot-plugin-gocqhttp 指令安装插件,同时在项目下 .env.prod 设置了GOCQ_WEBUI_USERNAME/GOCQ_WEBUI_PASSWORD后会出现报错:
IEN{ CVR3S5FP PNNT({D
配置如下:
IQMP3`V26T22C)UMTDU0{(8
删掉 GOCQ_WEBUI_USERNAME/GOCQ_WEBUI_PASSWORD 后恢复正常。
已知原始环境是用 nb-cli 引导构建,fastapi 0.101.1。

暂时解决办法:发现 #656 存在类似情况。
尝试对 fastapi 进行降级,降级到 0.96.0 报错消失。

@mnixry mnixry added the bug Something isn't working label Aug 25, 2023
@mnixry
Copy link
Owner

mnixry commented Aug 25, 2023

同样无法复现。如果可能能否提供完整的错误堆栈?

@aoguai
Copy link
Author

aoguai commented Aug 25, 2023

同样无法复现。如果可能能否提供完整的错误堆栈?

错误堆栈没有保存(悲
好不容易才找到的一张当时的截图(因为当时在群里问了所有截的图),能找到的话我就直接提供了😢...

@mnixry
Copy link
Owner

mnixry commented Aug 25, 2023

没事,我这边复现出来了

可能这个真的是FastAPI的Bug(也有可能给WebSocket设置HTTPBasic验证属于未定义行为,实践上应该避免),我会向上游报告,感谢提供线索。

@aoguai
Copy link
Author

aoguai commented Aug 25, 2023

好的👌🏻😉

@mnixry
Copy link
Owner

mnixry commented Aug 25, 2023

你确定你降级到的是 0.96.0 吗?我在 0.96.0 仍然能够复现该问题。

@mnixry
Copy link
Owner

mnixry commented Aug 25, 2023

related: tiangolo/fastapi#8983

看起来这个问题最晚从2021年开始就已经存在了。不知道为什么降低FastAPI版本会有效果。

@aoguai
Copy link
Author

aoguai commented Aug 25, 2023

你确定你降级到的是 0.96.0 吗?我在 0.96.0 仍然能够复现该问题。

是的。我也不知道什么情况。

(botenv) root@VM-24-12-debian:~/nonebot2/hummanbot# pip list
Package                  Version
------------------------ ---------
anyio                    3.7.1
argcomplete              3.1.1
certifi                  2023.7.22
chevron                  0.14.0
click                    8.1.7
exceptiongroup           1.1.3
fastapi                  0.96.0
h11                      0.14.0
h2                       4.1.0
hpack                    4.0.0
httpcore                 0.17.3
httptools                0.6.0
httpx                    0.24.1
humanize                 4.8.0
hyperframe               6.0.1
idna                     3.4
Jinja2                   3.1.2
loguru                   0.7.0
MarkupSafe               2.1.3
msgpack                  1.0.5
multidict                6.0.4
nonebot-adapter-onebot   2.2.4
nonebot-plugin-addfriend 2.4.6
nonebot-plugin-gocqhttp  0.6.11
nonebot-plugin-repeater  3.1.4
nonebot-plugin-status    0.7.1
nonebot2                 2.0.1
packaging                23.1
pip                      23.2.1
pipx                     1.2.0
psutil                   5.9.5
py-cpuinfo               9.0.0
pydantic                 1.10.12
pygtrie                  2.5.0
python-dotenv            1.0.0
PyYAML                   6.0.1
setuptools               68.0.0
sniffio                  1.3.0
starlette                0.27.0
tomli                    2.0.1
typing_extensions        4.7.1
userpath                 1.9.0
uvicorn                  0.23.2
uvloop                   0.17.0
watchfiles               0.19.0
websockets               11.0.3
wheel                    0.41.1
yarl                     1.9.2

@aoguai
Copy link
Author

aoguai commented Aug 25, 2023

related: tiangolo/fastapi#8983

看起来这个问题最晚从2021年开始就已经存在了。不知道为什么降低FastAPI版本会有效果。

看了一下,根据讨论内容,问题的核心在于 OAuth2PasswordBearer 这个依赖是设计用来在 HTTP 请求中获取并验证 JWT token 的,但是在 WebSocket 请求中,并没有 request 这个对象的概念,因此无法直接使用这个依赖。WebSocket 请求和 HTTP 请求的处理方式是不同的,而 FastAPI 的安全依赖目前只针对 HTTP 请求进行了设计和实现。有人建议在 WebSocket 的处理函数中,直接使用 websocket.headers 来获取 X-Authorization 头部信息,而不是使用 OAuth2PasswordBearer,或者可以为 WebSocket 请求创建一个类似于 OAuth2PasswordBearer 的自定义解决方案,用来处理验证逻辑。这可能需要一些额外的工作,但是可以满足 WebSocket 的需求。

等待更多的官方支持: 一些讨论者表示他们在 FastAPI 或 Starlette 的代码库中找到了与此相关的 Pull Request,这可能意味着未来的版本可能会添加对 WebSocket 安全的更好支持。

总的来说,问题的根本是 OAuth2PasswordBearer 这个安全依赖在设计时没有考虑到 WebSocket 请求的场景,因此当前的实现无法直接适用于 WebSocket。解决这个问题可能需要使用上述提到的方法之一,或者等待 FastAPI 更新,以便在未来的版本中获得更好的 WebSocket 安全支持。
(GPT总结的)

@mnixry mnixry closed this as completed in edf3ca5 Aug 26, 2023
@mnixry
Copy link
Owner

mnixry commented Aug 26, 2023

related: tiangolo/fastapi#10147

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants