400ms for pwd_context.verify ? Won't this block the main thread for 400ms if called in async func ? #9365
-
First Check
Commit to Help
Example Codedef x():
a=time.time()
pwd_context.verify("123",pwd_context.hash("123"))
print(time.time()-a)
x() # 0.3718242645263672
**400ms**DescriptionThis is used everywhere in examples, but won't this block main thread for 400ms for all async endpoints?? With a guessed username an attacker could block all async endpoints with a single request every 400ms. Am I missing something? If that's the case, easy fix should be always using synchronous functions (def) instead of async def for endpoints that use pwd_context.verify, so that handling is done in the background thread pool. I feel like this should be documented, or I might just be thinking wrong. Operating SystemWindows Operating System DetailsNo response FastAPI Version0.95.0 Python VersionPython 3.10.10 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 2 replies
-
|
If an async endpoint calls this function, it will indeed block the main thread for around 400ms, which could cause performance issues if many requests are being made simultaneously. To avoid this, you could use a synchronous function instead of an async function, as you suggested. It's worth noting that the pwd_context library is designed to be computationally expensive, in order to make it more difficult for attackers to brute-force passwords. However, if you are concerned about the performance impact of password verification on your application, you could consider using a different password hashing library with lower computational requirements. |
Beta Was this translation helpful? Give feedback.
-
|
You can also run your webserver eg: |
Beta Was this translation helpful? Give feedback.
-
|
Thanks guys for the answers, I'll look into adding a warning to docs. Most people will deploy the most commonly used method to prod. This is really susceptible to both timing and bruteforce attacks to find valid usernames as well, once attacker finds a valid username, he can just keep spamming it (that would be the slowest yet the most efficient DOS attack ever, doable from a toaster with a dialup connection) and block the whole API that was supposed to be lightning fast because it's fully async. Thinking of fully async approach both excites me and scares me so much because of things like this. |
Beta Was this translation helpful? Give feedback.
-
|
I use |
Beta Was this translation helpful? Give feedback.
You can also run your webserver eg:
gunicornwith multiple workers with the idea that one of the other workers may not be busy computing these blocking calls.