bpo-43813: Fixing DOS on http.server by limiting the characters getting logged. #26223
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.
Due to the large characters limit on the first line of the request, Remote users was able to submit a huge method name and endpoint on the request that they can keep sending until python uses a large amount of memory until it freezes that could be achieved on a strong machine with almost 1000 requests.
The logging part was the most part that's using memory during the attack. so I think limiting the data getting passed into the output functions:
log_request
andlog_message
that uses:sys.stderr.write
will be more fine to keep http.server handling the requests without stop.The fix limits the request method characters getting logged into 10 characters only. if the request method is 60k
A
characters for example. it will just print out:AAAAAAAAAA...
that doesn't consume memory.Before the fix the memory usage kept increasing. after it the memory usage doesn't pass 6.0% using 5k requests.
https://bugs.python.org/issue43813