Skip to content

Tighten heuristics for representing control frames. - #1770

Open
aaugustin wants to merge 1 commit into
mainfrom
improve-ctrl-frames-repr
Open

aaugustin wants to merge 1 commit into
mainfrom
improve-ctrl-frames-repr

Conversation

@aaugustin

Copy link
Copy Markdown
Member

This only affects debug logs.

  • When a close frame cannot be parsed, always show it as binary.
  • Treat ping and pong frames as text only when it's printable ASCII or
    spaces (which will be escaped in the representation). This avoids
    treating our random 4-bytes payloads as text when they're UTF-8,
    which happens a bit more that 1 time out of 16. (Chances that it's
    ASCII are 1 in 16, and then in can be UTF-8 in a few more cases.)

Refs #1763.

This only affects debug logs.

* When a close frame cannot be parsed, always show it as binary.
* Treat ping and pong frames as text only when it's printable ASCII or
  spaces (which will be escaped in the representation). This avoids
  treating our random 4-bytes payloads as text when they're UTF-8,
  which happens a bit more that 1 time out of 16. (Chances that it's
  ASCII are 1 in 16, and then in can be UTF-8 in a few more cases.)

Refs #1763.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Control-frame formatting can still raise exceptions for supported memoryview payloads.

Get a fresh assessment by requesting another Copilot review.

Review effort: Balanced
Findings: 2 Medium severity · 2 Low severity

Open (4)
What changed in this PR

Refines debug-log rendering for WebSocket control frames.

Changes:

  • Renders malformed close frames as binary.
  • Restricts textual ping/pong payloads to printable ASCII and whitespace.
  • Adds regression tests for control-frame formatting.
File Description
tests/​test_frames.py Updates ping and pong representation tests.
src/​websockets/​frames.py Implements stricter control-frame rendering heuristics.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/websockets/frames.py
if self.opcode is CLOSE:
try:
return str(Close.parse(self.data)), True
data_repr = str(Close.parse(self.data))
Comment thread src/websockets/frames.py
# 4-bytes binary payloads are accidentally valid UTF-8 sequences.

elif self.opcode in CTRL_OPCODES:
if is_space_or_printable_ascii(self.data):
Comment thread src/websockets/frames.py
Comment on lines +200 to +202
# Control frames: display printable ASCII payloads as text, else binary.
# We could decode UTF-8 payloads, but this causes confusion when random
# 4-bytes binary payloads are accidentally valid UTF-8 sequences.
Comment thread tests/test_frames.py
Comment on lines +375 to +376
str(Frame(PING, b"dG\x04I")),
"PING 64 47 04 49 [binary, 4 bytes]",
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.

2 participants