fix: preserve binary message bodies when serializing to the wire#126
Open
WaelJami wants to merge 1 commit into
Open
fix: preserve binary message bodies when serializing to the wire#126WaelJami wants to merge 1 commit into
WaelJami wants to merge 1 commit into
Conversation
SIP message bodies are opaque octets (RFC 3261 §7.4), but messages were serialized via Display/to_string(), which renders the body with String::from_utf8_lossy. Any non-UTF-8 body (e.g. an application/ISUP part or a binary eCall MSD) was corrupted: each invalid byte became U+FFFD, which changes the payload and breaks Content-Length. The receive path already preserves binary bodies (the parser slices by Content-Length), so send and receive were asymmetric. Add to_bytes() to Request/Response/SipMessage (start line + headers as ASCII, body appended verbatim) and route the UDP and TCP/TLS transports through it. From<_> for Vec<u8> now delegates to to_bytes(). Display is left lossy for human-readable logging. WebSocket is unchanged: SIP-over-WS (RFC 7118) uses UTF-8 text frames. Adds regression tests for Request/Response round-trips and the stream codec.
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
SIP message bodies are opaque octets (RFC 3261 §7.4), but
rsipstackserialized them as UTF-8 text, corrupting any body that isn't valid UTF-8. This makesto_bytes()the canonical wire serializer (preserving the body verbatim) and routes the UDP/TCP/TLS transports through it.Problem
A message with a binary body, e.g. an
application/ISUPpart, or an eCall MSD (binary ASN.1 PER), is sent with the body mangled. Every non-UTF-8 byte is replaced byU+FFFD(EF BF BD), which both changes the payload and breaksContent-Length.Root cause
All message→bytes paths went through
Display/to_string(), which renders the body withString::from_utf8_lossy:and the transports serialized via
to_string()(transport/udp.rs,transport/stream.rs).Notably, the receive path already preserves binary bodies (the parser slices the body by
Content-Length), so send and receive were asymmetric.Fix
to_bytes()toRequest,Response, andSipMessage: start line + headers as ASCII, body appended verbatim.From<…> for Vec<u8>now delegates toto_bytes().send, the TCP/TLSSipCodecencoder, andsend_to_streamuseto_bytes().Scope
WebSocket transport is intentionally unchanged: SIP-over-WS (RFC 7118 §5) uses UTF-8 text frames, so binary bodies aren't representable there regardless.
Testing
Request/Responseround-trip and the stream codec, each asserting a non-UTF-8 body survives serialization byte-for-byte.cargo test --lib→ 238 passed, clippy clean.Compatibility
Backward compatible.
Display/to_string()behaviour is unchanged; only the byte serialization used by transports is corrected.