fix(webrtc): harden /sdp HTTP server against memory-amplification DoS - #1396
Open
yashksaini-coder wants to merge 2 commits into
Open
fix(webrtc): harden /sdp HTTP server against memory-amplification DoS#1396yashksaini-coder wants to merge 2 commits into
yashksaini-coder wants to merge 2 commits into
Conversation
The POST /sdp dev-harness handler in _aiortc_helpers.py had two unbounded
read paths:
- A `while True` header loop with only a per-line timeout, letting an
attacker hold a task indefinitely by sending header lines slowly.
- `reader.readexactly(content_length)` on an attacker-controlled value;
Content-Length: 2147483647 causes asyncio to grow a ~2 GiB buffer.
Fix: three bounded constants + early rejection before any body allocation.
_MAX_SDP_BODY_SIZE = 32 KiB -> Content-Length > limit -> 413
_MAX_HEADER_LINES = 64 -> no blank line after N reads -> 400
_MAX_HEADER_BYTES = 8 KiB -> accumulated header size -> 400
Non-integer and negative Content-Length values also return 400.
The existing 15s per-readline/readexactly timeout is unchanged.
Adds regression tests covering all acceptance criteria, plus a
wait_for_peer stabilization for the identify-aware publish tests.
Closes libp2p#1354.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Closes #1354. Recreates #1380, which was auto-closed when the source fork was recreated — same diff, no code changes.
The
POST /sdpHTTP handler in_aiortc_helpers.py(run_signaling_server) had two unbounded read paths that let a remote attacker consume server memory or hold async tasks indefinitely:while True:with only a per-readline 15s timeout. A slow attacker could send one header line every 14s and hold the connection forever.reader.readexactly(content_length)wherecontent_lengthis attacker-controlled.Content-Length: 2147483647grows a ~2 GiB asyncio buffer per connection before the timeout fires.Fix
Three constants + early rejection before any body buffer is allocated:
_MAX_SDP_BODY_SIZE413 Payload Too Large_MAX_HEADER_LINES400 Bad Request_MAX_HEADER_BYTES400 Bad RequestNon-integer and negative
Content-Lengthvalues also return400. The existing_SDP_HTTP_TIMEOUT(15s per readline/readexactly) is unchanged. Uses Python'sfor/elseidiom: theelsefires when the header loop exhausts_MAX_HEADER_LINESreads without finding the blank-line terminator.Test plan
Content-Length: 999999999→413, body buffer not allocatedContent-Length: notanumber→400Content-Length: -1→400Content-Lengthexactly at limit →200(happy path still works)_MAX_HEADER_LINES + 1non-blank header lines →400_MAX_HEADER_BYTES→400200,on_offercallback receives correct bodytest_aiortc_helpers.pyregression suite)Scope note
This harness is documented as a temporary py-to-py scaffold (not interop-compatible with go/js libp2p). The fix raises the bar from "trivially DoSable over LAN" to "needs a real attacker model" while the STUN-based listener (#1352) is in spike. Refs #546, #1309, #1352.