You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The bug is reproducible against the latest release and/or master.
There are no similar issues or pull requests to fix it yet.
Currently both async and sync client requests are succeptible to low-and-slow "attacks" unless explicitly handled. In other words when request is being streamed as long as 1 byte every 2 seconds (or aprox.) is being generated the connection will not close or timeout with none of the current available settings.
It's possible for the server to serve single html page almost idefinitely and hang the client thread.
Maybe httpx should introduce some optional or even default handlers for this? Maybe it's already possible to hook something like this in easily?
The above code snippet will take 30+ seconds to complete (though this could be practically infinity) disregarding any timeout settings. The only way to avoid this is to explitictly wrap everything in either asyncio.wait_for() for async code and for sync code seems to be much more complicated (?).
Reacted by Povilas Balciunas, Lev Lybin, T-256, Paula Kammler, Falko Schindler, George Sakkis, Callum, Alexander Kuzmik, Alon Sirota, Marcin Endraszka and 1 more
This is an interesting topic. To be honest, I'm not sure yet if/how we would be adding security knobs like these. Security folks tend to be paranoid 😄 and I'm not sure if they wouldn't just like to put things like slow lorry defense measures in place themselves, rather than relying on the underlying HTTP lib to do it for them. There's also a case to be made to include some minimal support built-in, but then we need to discuss opt-in vs opt-out; if opt-out then what is the default; etc.
Luckily, HTTPX already enables people to implement these measures themselves without having to do advanced concurrency. I was pinged on the tuf project recently to expose a bit more how HTTPX deals with timeouts, and discussed slow retrieval there as well: theupdateframework/python-tuf#1213 (comment)
From my POV, the best bet for folks might be to write a custom transport that wraps the stream and checks against slow sending of bytes. Something like this:
importtimefromtypingimportIteratorimporthttpcoreimporthttpxclassTooSlowError(Exception):
passclassSlowGuardTransport(httpcore.SyncHTTPTransport):
def__init__(self, transport: httpcore.SyncHTTPTransport, min_rate: str) ->None:
self._transport=transportvalue, unit=min_rate.split("/")
self._min_rate=float(value) / {"second": 1, "minute": 60}[unit]
def_wrap(self, stream: Iterator[bytes]) ->Iterator[bytes]:
last_chunk_date=time.time()
forchunkinstream:
# Compute current recv rate.# TODO: Maybe use a rolling average to reduce false positives.now=time.time()
elapsed=now-last_chunk_daterate=len(chunk) /elapsedifrate<=self._min_rate:
raiseTooSlowError(f"Server is sending data too slowly: {rate:.2f} < {self._min_rate:.2f}")
last_chunk_date=nowyieldchunkdefrequest(self, *args, **kwargs):
status_code, headers, stream, ext=self._transport.request(*args, **kwargs)
returnstatus_code, headers, self._wrap(stream), exttransport=httpcore.SyncConnectionPool() # See docs for kwargs, or wait for upcoming `httpx.HTTPTransport()`.transport=SlowGuardTransport(transport, min_rate="10/second")
withhttpx.Client(transport=transport) asclient:
# 20/second: OKresponse=client.get("http://httpbin.org/drip?duration=5&numbytes=100&code=200")
print(response)
# 1/second: fails with `TooSlowError`# 'TooSlowError: Server is sending data too slowly: 0.93 < 10.00'response=client.get("http://httpbin.org/drip?duration=5&numbytes=5&code=200")
I'm not sure yet whether something like this would benefit from being built-in, with what API, or if it's okay for folks to just copy-paste and tweak a bit of this code using custom transports. (The Transport API is here exactly for enabling this kind of high-precision behaviors.)
I'd potentially be a bit cautious about labelling this under "attacks"/"security", since controlling the server and attacking the client isn't a very typical scenario, but yes I do think there's scope for a few more resource limits, which I've so far only noted in passing.
pause - I'm going to follow up on this more in a moment, but first lunch is up...
@tomchristie As far as security scenarios go, slowloris seems like a typical one to take into account to me. Eg one is building a service that uses HTTPX to interact with some other server-side service that you can't trust to not be compromised and controlled by malicious agent. Clearly that's entering paranoia land, but one thing I've learnt is dealing with security is a lot about dealing with paranoia. 😆
Anyway, I've dropped the "security" label for now as I've realized it might come off as "here's a security breach we found in HTTPX" whereas that's not what this issue is about (which might be what you were getting at).
It's just that slowlorris makes sense as a client-side attack on the server, allowing attackers to DDOS a server. Whereas in the converse case, where the server has been owned by the attacker, it's an odd scenario to envisage an "attack" that is "let's make the clients using this service really slow". Maybe that's a thing that's happened(?), but I've never heard of it.
You'd more typically see simply this kind of issue on the client side framed as "resource limits to ensure graceful degradation vs. overloaded servers".
Anyways, I think it's valid either ways around, and I think a useful guideline for considering resource limits is to think about it in the same way as services like Heroku treat resource limiting.
The cases that seem like likely candidates to me might be:
Maximum total time allowed for an HTTP request. (Eg. 60 seconds)
Maximum total size allowed for the body of an HTTP response. (Eg. 100MB)
Maximum total number of concurrent requests on a client. (Eg. 100)
Reacted by Florimond Manca, Bernardas Ališauskas, Povilas Balciunas, Walther Lee, Lev Lybin, Rafael and linxxx3
Yeah I did put "attack" in quotes because it's not really an attack but it serves a very similar function.
For example a way for a server to maliciously deal with web-scrapers (or any unwanted connections) would be to stream bytes slowly since many of http client libraries have no default handlers for that - so web-scrapers would hang indefinitely consuming resources and blocking flows which could be interpreted as a malicious action against the client.
I think those 3 proposals by @tomchristie would be more than enough to deal with this!
I think about this problem a lot. I've written a lot of code which accepts an untrusted URL from a user and attempts to fetch that URL - a few examples:
OpenID and IndieAuth, where a user enters the URL to their identity provider and my backend needs to fetch it.
A really common one these days is retrieving a URL in order to "unfurl" it - extract the title, any og:image tags etc in order to display a summary of the link on a page. Chat programs, comment systems etc all do this now.
Features that allow users to provide a URL to a CSV file in order to import data.
For all of these I really appreciate being able to use an HTTP client that makes it easy to "safely" consume a malicious URL. I want to be able to set aggressive timeouts AND limit the number of bytes retrieved too (see #1392 for a previous question I've had around limiting the number of bytes).
The attacks I'm worried about here are deliberate denial-of-service attacks - users providing malicious URLs that exhaust my server resources.
The three limits Tom suggests above are exactly what I'm after.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Hello everyone!
I stumbled over this issue while looking for an option to timeout our requests to ensure we are not waiting longer for a response than absolutely necessary. For us something like timeout = httpx.Timeout(total=1.0) would also work to achieve this.
Will something like this or like @rthalley described be implemented in an upcoming release?
Should the overall timeout include the pool timeout? If yes the implementation is easy, at least for the async client; just wrap AsyncClient.request in asyncio.wait_for(). However at least in my use case a more useful timeout is to bound the network time: the total time for connection, write and read, excluding the time waiting for an available connection. The reason is that the latter is a client limit while the others have to do with the server.
I took a stab at implementing this but got stuck because there is not a single method or code block to guard with asyncio.wait_for(). AsyncHTTPConnection.handle_async_request involves connecting to the server, sending the request and receiving the response headers but not the response body; the latter is wrapped in a lazy HTTP11ConnectionByteStream (or HTTP2ConnectionByteStream) and read in a totally different part of the stack, in AsyncClient.send. Any guidance or suggestions how to go about it would be great.
Thanks for mentioning asyncio.wait_for(), @gsakkis!
I've been searching for such a total timeout for quite a while now, but haven't thought of using asyncio to enforce it. This is a sufficient solution for my needs:
@falkoschindler you don't need asyncio.wait_for for this url, the httpx timeout (or read timeout) is sufficient. An example where it's not sufficient is the one in the original post, where the response is returned slowly with a short interval between the chunks.
@gsakkis You're right, the other URL "http://httpbin.org/drip" is even better for testing the total timeout. But even with "https://httpbin.org/delay/3" the normal httpx timeout takes around 1.4s instead of 1.0s, while wait_for terminates after 1.0s.
Implementing total-request-timeouts would mostly be beneficial for the sync case, since there's no cancellation semantics available to us with threaded code.
Any guidance or suggestions how to go about it would be great.
I think the timeout would need to be implemented as two separate blocks...
Well said Tom! And yeah, it's indeed much easier with trio as you just scope your whole task under a cancelation timeout and don't worry about the details, and you don't have to have each individual thing the blocks figure out how much time it has left. It's the sync case for httpx that's the open ticket for me. The idea you propose would work for my purposes.
Python 3.11 comes with asyncio.timeout, which is favoured overasyncio.wait_for and seems very similar to the trio cancellation context block, while it is probably too new to be used in httpx/httpcore, it might already be of use to anyone writing applications using httpx that have encountered this problem.
I have used it to prevent servers doing a "reverse slowloris" (unintentionally I assume) in a webhook delivery system at work, where a few slow servers would respond slower than the timeout (300+ vs 5 seconds), and would eventually slow the system so much down it was unable to keep up with the stream of new events.
A total timeout would be a great addition to the timeout class, particularly for the sync api.
Creating a timestamp on the request and checking how much time is elapsed for each operation (pool, connect, write, read) should do the trick.
As others have mentioned, a network timeout which includes only connect, write & read timeouts would be also nice to have.
Besides I would like to carefully suggest that an additional timeout check for reading each chunk might be a good idea in iter_raw().
So far I can tell reading chunks doesn't handle timeouts? I'm not sure.
But this probably needs an extra timestamp defined before for reading all chunks. This could also help on slow servers.
If this situation is already handled somewhere else (I did not inspect all the code), I excuse myself in advance.
Should the overall timeout include the pool timeout? If yes the implementation is easy, at least for the async client; just wrap AsyncClient.request in asyncio.wait_for(). However at least in my use case a more useful timeout is to bound the network time: the total time for connection, write and read, excluding the time waiting for an available connection. The reason is that the latter is a client limit while the others have to do with the server.
I took a stab at implementing this but got stuck because there is not a single method or code block to guard with asyncio.wait_for(). AsyncHTTPConnection.handle_async_request involves connecting to the server, sending the request and receiving the response headers but not the response body; the latter is wrapped in a lazy HTTP11ConnectionByteStream (or HTTP2ConnectionByteStream) and read in a totally different part of the stack, in AsyncClient.send. Any guidance or suggestions how to go about it would be great.
@gsakkis I think the library can't provide an API to cover all the use cases users may have. However, HTTPX has a great design that allows you to easily customize parts of your client by simply overriding the necessary components.
If you want to set a timeout for all read operations, you can simply inherit from the Response class and override the appropriate method, like so:
When you have your own Response implementation, keep in mind that the only way HTTPX retrieves the response from the request is through the Transport. Therefore, you will also need to write your own transport that utilizes your custom Response like so:
Now you can use that transport in cases where you want control over the total time the read operation takes, like this:
asyncdefmain():
asyncwithhttpx.AsyncClient(
transport=AsyncTransportWithTimeout(httpx.AsyncHTTPTransport())
) asclient:
response=awaitclient.get("https://example.org", extensions={"timeout": {"read_total": 2}}) # 2 second total readprint(response.text)
asyncio.run(main())
Also note that I've written the transport to accept next_transport, which is how I usually structure my transports. This allows me to combine them and create a chain of useful transports.
However, HTTPX has a great design that allows you to easily customize parts of your client by simply overriding the necessary components.
@karpetrosyan apologies for the belated response and many thanks for the elaborate answer! Sadly even if my use case is possible to implement (it's not clear how the sample code above can be extended to enforce a connect+read+write timeout), I wouldn't call it "easy" or "simple". That's a lot of non-trivial code that really belongs to a library (if not httpx itself, at least some httpx-utils auxiliary library) than the end user application. So it would be really great if the PR for this issue provided a way for the caller to specify what "overall timeout" means as I suggested here.
Activity
florimondmanca commentedon Jan 11, 2021
@Granitosaurus Hi!
This is an interesting topic. To be honest, I'm not sure yet if/how we would be adding security knobs like these. Security folks tend to be paranoid 😄 and I'm not sure if they wouldn't just like to put things like slow lorry defense measures in place themselves, rather than relying on the underlying HTTP lib to do it for them. There's also a case to be made to include some minimal support built-in, but then we need to discuss opt-in vs opt-out; if opt-out then what is the default; etc.
Luckily, HTTPX already enables people to implement these measures themselves without having to do advanced concurrency. I was pinged on the
tufproject recently to expose a bit more how HTTPX deals with timeouts, and discussed slow retrieval there as well: theupdateframework/python-tuf#1213 (comment)From my POV, the best bet for folks might be to write a custom transport that wraps the stream and checks against slow sending of bytes. Something like this:
I'm not sure yet whether something like this would benefit from being built-in, with what API, or if it's okay for folks to just copy-paste and tweak a bit of this code using custom transports. (The Transport API is here exactly for enabling this kind of high-precision behaviors.)
lovelydinosaur commentedon Jan 11, 2021
Heya, thanks for raising this.
I'd potentially be a bit cautious about labelling this under "attacks"/"security", since controlling the server and attacking the client isn't a very typical scenario, but yes I do think there's scope for a few more resource limits, which I've so far only noted in passing.
pause - I'm going to follow up on this more in a moment, but first lunch is up...
florimondmanca commentedon Jan 11, 2021
@tomchristie As far as security scenarios go, slowloris seems like a typical one to take into account to me. Eg one is building a service that uses HTTPX to interact with some other server-side service that you can't trust to not be compromised and controlled by malicious agent. Clearly that's entering paranoia land, but one thing I've learnt is dealing with security is a lot about dealing with paranoia. 😆
Anyway, I've dropped the "security" label for now as I've realized it might come off as "here's a security breach we found in HTTPX" whereas that's not what this issue is about (which might be what you were getting at).
lovelydinosaur commentedon Jan 11, 2021
It's just that slowlorris makes sense as a client-side attack on the server, allowing attackers to DDOS a server. Whereas in the converse case, where the server has been owned by the attacker, it's an odd scenario to envisage an "attack" that is "let's make the clients using this service really slow". Maybe that's a thing that's happened(?), but I've never heard of it.
You'd more typically see simply this kind of issue on the client side framed as "resource limits to ensure graceful degradation vs. overloaded servers".
Anyways, I think it's valid either ways around, and I think a useful guideline for considering resource limits is to think about it in the same way as services like Heroku treat resource limiting.
The cases that seem like likely candidates to me might be:
Granitosaurus commentedon Jan 12, 2021
Yeah I did put "attack" in quotes because it's not really an attack but it serves a very similar function.
For example a way for a server to maliciously deal with web-scrapers (or any unwanted connections) would be to stream bytes slowly since many of http client libraries have no default handlers for that - so web-scrapers would hang indefinitely consuming resources and blocking flows which could be interpreted as a malicious action against the client.
I think those 3 proposals by @tomchristie would be more than enough to deal with this!
simonw commentedon Jan 23, 2021
I think about this problem a lot. I've written a lot of code which accepts an untrusted URL from a user and attempts to fetch that URL - a few examples:
og:imagetags etc in order to display a summary of the link on a page. Chat programs, comment systems etc all do this now.For all of these I really appreciate being able to use an HTTP client that makes it easy to "safely" consume a malicious URL. I want to be able to set aggressive timeouts AND limit the number of bytes retrieved too (see #1392 for a previous question I've had around limiting the number of bytes).
The attacks I'm worried about here are deliberate denial-of-service attacks - users providing malicious URLs that exhaust my server resources.
The three limits Tom suggests above are exactly what I'm after.
stale commentedon Feb 20, 2022
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
18 remaining items
codingpaula commentedon Feb 2, 2024
Hello everyone!
I stumbled over this issue while looking for an option to timeout our requests to ensure we are not waiting longer for a response than absolutely necessary. For us something like
timeout = httpx.Timeout(total=1.0)would also work to achieve this.Will something like this or like @rthalley described be implemented in an upcoming release?
lovelydinosaur commentedon Feb 2, 2024
That'd be a really neat feature, yep. I'd also like us to have this.☺️ ⏳
Couple of different things here...
httpcore- I'd be happy to help guide someone through a PR there.Here's our recently updated not quite live yet timeout docs.
Currently we support
timeout=[httpx.Timeout|int|None]. I think this is a bit awkward, because...If we switched to enforcing an explicit style everywhere, then we could ensure it's always more clear. Eg...
gsakkis commentedon Feb 8, 2024
Should the overall timeout include the pool timeout? If yes the implementation is easy, at least for the async client; just wrap
AsyncClient.requestinasyncio.wait_for(). However at least in my use case a more useful timeout is to bound the network time: the total time for connection, write and read, excluding the time waiting for an available connection. The reason is that the latter is a client limit while the others have to do with the server.I took a stab at implementing this but got stuck because there is not a single method or code block to guard with
asyncio.wait_for().AsyncHTTPConnection.handle_async_requestinvolves connecting to the server, sending the request and receiving the response headers but not the response body; the latter is wrapped in a lazyHTTP11ConnectionByteStream(orHTTP2ConnectionByteStream) and read in a totally different part of the stack, inAsyncClient.send. Any guidance or suggestions how to go about it would be great.falkoschindler commentedon Feb 8, 2024
Thanks for mentioning
asyncio.wait_for(), @gsakkis!I've been searching for such a total timeout for quite a while now, but haven't thought of using asyncio to enforce it. This is a sufficient solution for my needs:
gsakkis commentedon Feb 8, 2024
@falkoschindler you don't need
asyncio.wait_forfor this url, the httpx timeout (or read timeout) is sufficient. An example where it's not sufficient is the one in the original post, where the response is returned slowly with a short interval between the chunks.falkoschindler commentedon Feb 8, 2024
@gsakkis You're right, the other URL "http://httpbin.org/drip" is even better for testing the total timeout. But even with "https://httpbin.org/delay/3" the normal httpx timeout takes around 1.4s instead of 1.0s, while
wait_forterminates after 1.0s.lovelydinosaur commentedon Feb 9, 2024
Yep. Using async primitives for timeouts is the sensible approach for the
asyncioandtriocases.I'd suggest that trio has the neater design here, because it gets that timeouts make sense as context blocks.
The same design is also available for asyncio codebases via
anyio.Implementing total-request-timeouts would mostly be beneficial for the sync case, since there's no cancellation semantics available to us with threaded code.
I think the timeout would need to be implemented as two separate blocks...
Also useful reading: https://stackoverflow.com/questions/21965484/timeout-for-python-requests-get-entire-response
rthalley commentedon Feb 9, 2024
Well said Tom! And yeah, it's indeed much easier with trio as you just scope your whole task under a cancelation timeout and don't worry about the details, and you don't have to have each individual thing the blocks figure out how much time it has left. It's the sync case for httpx that's the open ticket for me. The idea you propose would work for my purposes.
cknv commentedon Mar 4, 2024
Python 3.11 comes with
asyncio.timeout, which is favoured overasyncio.wait_forand seems very similar to the trio cancellation context block, while it is probably too new to be used in httpx/httpcore, it might already be of use to anyone writing applications using httpx that have encountered this problem.I have used it to prevent servers doing a "reverse slowloris" (unintentionally I assume) in a webhook delivery system at work, where a few slow servers would respond slower than the timeout (300+ vs 5 seconds), and would eventually slow the system so much down it was unable to keep up with the stream of new events.
jpramosi commentedon Jul 12, 2024
A total timeout would be a great addition to the timeout class, particularly for the sync api.
Creating a timestamp on the request and checking how much time is elapsed for each operation (pool, connect, write, read) should do the trick.
As others have mentioned, a network timeout which includes only connect, write & read timeouts would be also nice to have.
Besides I would like to carefully suggest that an additional timeout check for reading each chunk might be a good idea in iter_raw().
So far I can tell reading chunks doesn't handle timeouts? I'm not sure.
But this probably needs an extra timestamp defined before for reading all chunks. This could also help on slow servers.
If this situation is already handled somewhere else (I did not inspect all the code), I excuse myself in advance.
scruel commentedon Aug 28, 2024
I found that httpx will be easily hang even I set the timeout parameter...
After debugging, I found that the program will be paused at
return self._sslobj.read(len).Stack trace:
karpetrosyan commentedon Aug 28, 2024
I think it's should be resolved in encode/httpcore#936
karpetrosyan commentedon Sep 4, 2024
@gsakkis I think the library can't provide an API to cover all the use cases users may have. However, HTTPX has a great design that allows you to easily customize parts of your client by simply overriding the necessary components.
If you want to set a timeout for all read operations, you can simply inherit from the Response class and override the appropriate method, like so:
When you have your own
Responseimplementation, keep in mind that the only wayHTTPXretrieves the response from the request is through theTransport. Therefore, you will also need to write your own transport that utilizes your customResponselike so:Now you can use that transport in cases where you want control over the total time the read operation takes, like this:
Also note that I've written the transport to accept
next_transport, which is how I usually structure my transports. This allows me to combine them and create a chain of useful transports.gsakkis commentedon Oct 16, 2024
@karpetrosyan apologies for the belated response and many thanks for the elaborate answer! Sadly even if my use case is possible to implement (it's not clear how the sample code above can be extended to enforce a
connect+read+writetimeout), I wouldn't call it "easy" or "simple". That's a lot of non-trivial code that really belongs to a library (if not httpx itself, at least some httpx-utils auxiliary library) than the end user application. So it would be really great if the PR for this issue provided a way for the caller to specify what "overall timeout" means as I suggested here.lovelydinosaur commentedon Oct 16, 2024
@gsakkis Appreciate the suggestion, tho we won't support that, no. Perhaps the trace functionality is useful here?