fix(serial-console): decode binary WebSocket frames as UTF-8#9795
fix(serial-console): decode binary WebSocket frames as UTF-8#9795cjp256 wants to merge 2 commits intoAzure:mainfrom
Conversation
The `on_message` callback passed WebSocket messages directly to `PC.print`, which forwards to Python's built-in `print()`. Per RFC 6455, the `websocket-client` library delivers text-opcode frames as `str` and binary-opcode frames as `bytes`. When the server sends a binary frame, `print()` renders the `bytes` via `repr()` — emitting literals like `b'\x1b[0;32m OK \xe2\x80\xa6\r\r\n'` instead of interpreting ANSI escapes and UTF-8. The Azure Serial Console stream is semantically text (a guest TTY), but the client shouldn't rely on the server's choice of opcode. Decode `bytes`/`bytearray` messages as UTF-8 (with `errors="replace"`) before printing so the terminal renders colors, box-drawing characters, and line endings correctly regardless of which opcode the server uses. Signed-off-by: Chris Patterson <cpatterson@microsoft.com>
️✔️Azure CLI Extensions Breaking Change Test
|
|
Thank you for your contribution! We will review the pull request and get back to you soon. |
|
The git hooks are available for azure-cli and azure-cli-extensions repos. They could help you run required checks before creating the PR. Please sync the latest code with latest dev branch (for azure-cli) or main branch (for azure-cli-extensions). pip install azdev --upgrade
azdev setup -c <your azure-cli repo path> -r <your azure-cli-extensions repo path>
|
There was a problem hiding this comment.
Pull request overview
This PR improves az serial-console connect output rendering by ensuring WebSocket binary frames are decoded to UTF-8 before being printed, so ANSI escapes and multi-byte characters display correctly even when the server uses a binary opcode.
Changes:
- Bump the serial-console extension version to
1.0.0b4and add a matching HISTORY entry. - Add
_decode_ws_messageand use it in the WebSocketon_messagehandler to decodebytes/bytearrayas UTF-8 witherrors="replace". - Add unit tests covering decoding behavior for
str,bytes,bytearray, ANSI escapes, UTF-8 box-drawing characters, and invalid UTF-8 replacement.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/serial-console/setup.py |
Version bump to publish the fix as 1.0.0b4. |
src/serial-console/azext_serialconsole/custom.py |
Decode binary WebSocket payloads to UTF-8 before printing to the terminal. |
src/serial-console/azext_serialconsole/tests/latest/test_decode_ws_message.py |
Adds unit coverage for the decoding helper. |
src/serial-console/HISTORY.rst |
Documents the rendering fix in release notes. |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
|
Please fix CI issues |
|
/azp run |
|
Azure Pipelines successfully started running 2 pipeline(s). |
The
on_messagecallback passed WebSocket messages directly toPC.print, which forwards to Python's built-inprint(). Per RFC 6455, thewebsocket-clientlibrary delivers text-opcode frames asstrand binary-opcode frames asbytes. When the server sends a binary frame,print()renders thebytesviarepr()— emitting literals likeb'\x1b[0;32m OK \xe2\x80\xa6\r\r\n'instead of interpreting ANSI escapes and UTF-8.The Azure Serial Console stream is semantically text (a guest TTY), but the client shouldn't rely on the server's choice of opcode. Decode
bytes/bytearraymessages as UTF-8 (witherrors="replace") before printing so the terminal renders colors, box-drawing characters, and line endings correctly regardless of which opcode the server uses.This checklist is used to make sure that common guidelines for a pull request are followed.
Related command
az serial-console connect -n $name -g $rgGeneral Guidelines
azdev style <YOUR_EXT>locally? (pip install azdevrequired)python scripts/ci/test_index.py -qlocally? (pip install wheel==0.30.0required)About Extension Publish
There is a pipeline to automatically build, upload and publish extension wheels.
Once your pull request is merged into main branch, a new pull request will be created to update
src/index.jsonautomatically.You only need to update the version information in file setup.py and historical information in file HISTORY.rst in your PR but do not modify
src/index.json.Example
Bad screenshot:
Fixed: