Skip to content

fix SIP body truncation with AudioCodes SBC - #45

Merged
loreste merged 1 commit into
mainfrom
fix/body-truncation-crlf-normalizer
Sep 9, 2026
Merged

fix SIP body truncation with AudioCodes SBC#45
loreste merged 1 commit into
mainfrom
fix/body-truncation-crlf-normalizer

Conversation

@loreste

@loreste loreste commented Sep 9, 2026

Copy link
Copy Markdown
Owner

What happened

The CRLF normalizer (added for sipgo#292 bare-LF compat) was normalizing the entire UDP packet — headers and body. When an AudioCodes SBC sends a multipart SIPREC INVITE with bare \n line endings in the body, the normalizer expands those to \r\n, making the body longer than the Content-Length header says. sipgo then reads only Content-Length bytes, chopping the body mid-XML. The multipart reader hits EOF before finding the rs-metadata part and the call gets rejected.

What this fixes

normalizeCRLF now finds the header/body separator first and only normalizes the header section. The body passes through byte-for-byte, so Content-Length stays accurate and the multipart parser gets the full payload.

  • Added findHeaderBodySplit() — handles all four separator variants (\r\n\r\n, \n\n, \r\n\n, \n\r\n)
  • Renamed the raw normalizer to normalizeCRLFBytes (internal)
  • Zero-copy fast path when all line endings are already \r\n

Tests

  • AudioCodes end-to-end reproduction: builds a bare-LF SIPREC multipart packet, normalizes it, verifies Content-Length matches body length, then parses both SDP and rs-metadata parts through Go's multipart reader
  • Binary body safety: raw 0x0a bytes in body aren't mistaken for line endings
  • 100-line XML body: large metadata body preserved byte-for-byte
  • All separator variants: CRLF+CRLF, LF+LF, CRLF+LF, LF+CRLF
  • Real UDP socket test: sends through crlfPacketConn and checks body integrity
  • 97 sip / 63 siprec / 81 integration tests pass

Closes #44

Summary by CodeRabbit

  • Bug Fixes
    • Corrected line-ending normalization so SIP message bodies remain unchanged.
    • Prevented Content-Length mismatches and body-size errors when headers use inconsistent line endings.
    • Improved handling of multipart, binary, large, UDP, and TCP SIP messages.
    • Preserved messages that already use standard CRLF line endings without unnecessary changes.

The CRLF normalizer was converting bare \n to \r\n across the entire
UDP packet, including the message body. When an SBC (e.g. AudioCodes)
sends bare LFs in a multipart SIPREC body, this expansion makes the
body longer than Content-Length declares, so sipgo reads only
Content-Length bytes and truncates the rest. The multipart reader then
hits unexpected EOF before finding the rs-metadata part.

Fix: find the header/body separator first, normalize only the header
section, and pass the body through untouched. Adds findHeaderBodySplit
which handles all four separator variants (\r\n\r\n, \n\n, \r\n\n,
\n\r\n). Zero-copy fast path when no normalization is needed.

Closes #44
@coderabbitai

coderabbitai Bot commented Sep 9, 2026

Copy link
Copy Markdown

Review Change StackReview Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Advanced

Run ID: 745a1e93-7f63-4a86-8a3f-960446d2fa32

📥 Commits

Reviewing files that changed from the base of the PR and between 6aeee2f and 7a1389d.

📒 Files selected for processing (2)
  • pkg/sip/crlf_normalizer.go
  • pkg/sip/crlf_normalizer_test.go

📝 Walkthrough

Walkthrough

The CRLF normalizer now detects the SIP header/body boundary, normalizes bare line feeds in headers only, and preserves body bytes. Tests cover multipart, binary, UDP, separator, passthrough, and benchmark cases.

Changes

SIP CRLF normalization

Layer / File(s) Summary
Header and body normalization
pkg/sip/crlf_normalizer.go
The normalizer finds header/body separators, converts header line endings to CRLF, preserves the body, and updates UDP and TCP wrapper comments.
Normalization regression coverage
pkg/sip/crlf_normalizer_test.go
Tests validate separator variants, multipart and binary body preservation, Content-Length consistency, UDP behavior, unchanged CRLF input, and normalization performance.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Severity of issue fixed: Medium

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/body-truncation-crlf-normalizer

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@loreste
loreste merged commit ac7ae21 into main Sep 9, 2026
9 of 12 checks passed
@loreste
loreste deleted the fix/body-truncation-crlf-normalizer branch September 9, 2026 20:43
@loreste loreste mentioned this pull request Sep 9, 2026
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.

body truncated

1 participant