Lesson 1 of 9 · 24m
Framing Before Payloads
Choose how receivers find message boundaries before designing fields, because ambiguous framing corrupts every feature built above it.
The first protocol bug I ever chased was not in crypto, compression, or business logic. It was a framing bug: two messages occasionally arrived in one TCP read, and the application treated the second header as payload. Everything above the framing layer was irrelevant because the receiver could not reliably answer the oldest networking question there is: where does one message end?
TCP is a stream, not a message bus
TCP preserves bytes in order, but it does not preserve the boundaries of your writes. This loop is wrong in every language:
data = sock.recv(4096)
handle_message(data)A correct receiver buffers bytes until a complete frame exists:
buf = bytearray()
while True:
chunk = sock.recv(65536)
if not chunk:
raise ConnectionError("peer closed")
buf.extend(chunk)
while len(buf) >= 4:
size = int.from_bytes(buf[:4], "big")
if len(buf) < 4 + size:
break
frame = bytes(buf[4:4 + size])
del buf[:4 + size]
handle_message(frame)That 4 + size framing is simple, debuggable, and endian-explicit. It also creates the first design question: what is the maximum legal frame size?
Pick a frame shape and document it
A minimal binary frame often looks like this:
0 1 2 3
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| magic | version | type | flags | length |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| payload bytes ... |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+Magic bytes catch cross-connected services quickly. Type tells the dispatcher what follows. Length bounds memory allocation. Flags should be sparse and meaningfully named. If you cannot explain why a field exists in one sentence, leave it out.
Text protocols frame too
HTTP/1.1 originally used delimiter-oriented framing for headers (CRLF CRLF) plus Content-Length, Transfer-Encoding: chunked, or connection close for bodies. SMTP uses lines ending in CRLF, which is why dot-stuffing exists. Text is not immune to framing ambiguity; it merely moves the ambiguity into delimiters and escaping rules.
Reject ambiguity early
A receiver should enforce exact rules: maximum frame size, minimum header length, legal type values, unknown flags behavior, and what happens after trailing bytes. Do not "be liberal" with memory allocation. A 32-bit attacker-controlled length is a remote memory-exhaustion primitive if you allocate before validating.
Field notes
- Never infer frame boundaries from packet captures; MSS, TLS record size, and TCP segmentation are unrelated to application messages.
- Put frame-length validation before allocation, decompression, and cryptographic work.
- Include a connection-close rule for malformed frames. Retrying inside a corrupt stream usually makes alignment worse.
- Test partial writes, split headers, coalesced frames, and zero-length payloads even if your transport "shouldn't" produce them.
Was this lesson helpful?