Skip to content

fix(engine): gracefully handle binary responses and wrap decode failures in TransportLayerException - #999

Open
Reginald Alfret V (reginaldalfret) wants to merge 1 commit into
microsoft:mainfrom
reginaldalfret:fix/issue-976-binary-response-decoding
Open

Reginald Alfret V (reginaldalfret) wants to merge 1 commit into
microsoft:mainfrom
reginaldalfret:fix/issue-976-binary-response-decoding

Conversation

@reginaldalfret

Copy link
Copy Markdown

Description

When fuzzing or testing endpoints that return binary data (e.g. downloading .zip archives, application/octet-stream, image files, or other binary formats), the RESTler engine previously crashed with an unhandled UnicodeDecodeError: 'utf-8' codec can't decode byte ....

Root Cause

  1. In HttpSock._recvResponse, decode_buf(buf) unconditionally called buf.decode(UTF8).
  2. When receiving the initial buffer, socket recv returns the HTTP headers followed immediately by the binary response payload. Because the payload contains non-UTF-8 bytes, buf.decode(UTF8) failed.
  3. When ignore_decoding_failures was not enabled (False by default), decode_buf re-raised UnicodeDecodeError. Because sendRecv only caught TransportLayerException, the exception propagated unhandled up through the engine execution stack and aborted the fuzzing run.

Solution

  1. In decode_buf:
    • If self.ignore_decoding_failures is set, continue logging and decoding with "ignore".
    • If HTTP headers were already parsed (DELIM in data), decode the body using "replace" errors so the payload is safely preserved without crashing.
    • If the first buffer contains both headers and binary body (b"\r\n\r\n" in buf), cleanly partition headers and body at the delimiter, decode the headers, and decode the body with "replace".
    • If decode fails on raw header bytes before the delimiter, raise TransportLayerException instead of an unhandled UnicodeDecodeError, allowing sendRecv to handle and log the failure cleanly.
  2. Avoid circular imports by importing utils.logging.trace_db as trace_db.
  3. Add unit test coverage in unit_tests/test_messaging.py validating single-buffer and multi-buffer binary response handling, header decode failure handling, and setting override.

Closes #976.

…res in TransportLayerException

Signed-off-by: reginaldalfret <reginaldalfret@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Smoke test and fuzz failed with API download zip file, "utf-8' codec can't decode byte"

1 participant