Harden API and WebSocket receive paths - #1845
Draft
skot wants to merge 1 commit into
Draft
Conversation
This was referenced Aug 5, 2026
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
Harden inbound HTTP API and WebSocket handling against oversized, fragmented, and malformed client input.
size_t, require space for the terminating NUL, and reject zero or oversized JSON bodies.Why
PATCH /api/systempreviously narrowed the network-controlledreq->content_lenfromsize_ttoint. On ESP32, a value such as4294967294becomes-2, bypasses the upper-bound check, and reachesbuf[total_len] = '\0', producing an out-of-bounds write before a request body is required.Several JSON handlers also called
httpd_req_recv()only once, so ordinary TCP fragmentation could produce partial JSON parsing and leave request data unread. The WebSocket handler allocatedframe_length + 1bytes without an application-level maximum, allowing a client to consume heap or leave the stream unsynchronized after allocation failure.Validation
Build
idf.py buildgit diff --checkHardware testing
Flashed the resulting firmware over USB to a physical bitaxeGamma 601 with one BM1370 and monitored the serial console throughout the tests.
The miner booted normally, joined Wi-Fi, reached its configured 525 MHz frequency, connected to CKPool, received new work, and continued submitting accepted shares during and after the malformed API traffic. At the end of the test it was hashing at approximately 1.09 TH/s with 29 accepted shares, zero rejected shares, approximately 7.65 MB free heap, and no panic, watchdog reset, allocation failure, or unexpected reboot in the serial log.
Live API cases exercised against the device included:
4294967294were safely rejected; the device remained healthy and the next system-info request returned HTTP 200.0xffffffffwas rejected and its client session was removed.ESP-IDF limitation
ESP-IDF v5.5.1 stores the protocol's 64-bit WebSocket payload length in the ESP32's 32-bit
size_t. This change enforces a strict application limit on the parsed value and removes all peer-sized allocation, but an encoded value above 32 bits that truncates to a small value cannot be distinguished in application code. Rejecting that form before narrowing requires a corresponding ESP-IDF parser change.