sip: both framers read a Content-Length as a number

DatagramMessage read the first nine characters of a Content-Length, the
zeros before its number among them: a length written 0000000005 came out
as 0, and the body after it was cut off as bytes past the message.
FrameBuffer read the value into a long, and one too long for that
counted as no Content-Length: the body was framed as the start of the
next message. RFC 3261 25.1 writes the value as 1*DIGIT.

LengthOf reads it for both: the number its leading digits spell, zeros
before it aside; of a number of more than nine digits the first nine,
more than a datagram holds and enough that a stream waits. A value that
begins with no digit is no length, as before.

Tested by tests/Sip through both framers: a length of one digit written
with ten zeros before it frames that many bytes; a twenty-digit length
that wraps to zero in 64 bits is a datagram cut short and a stream's
body waited for; a value without a leading digit leaves a datagram's
body the rest of it and a stream's empty.
This commit is contained in:
Jorijn van der Graaf 2026-11-22 17:35:11 +01:00
commit c81dd3d267
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
3 changed files with 40 additions and 10 deletions

View file

@ -291,7 +291,9 @@ What the suites pin:
- `Sip` — the text layer's edge cases (empty header values, line anchoring, - `Sip` — the text layer's edge cases (empty header values, line anchoring,
keepalive discards, a header read that stops at the first empty line — a keepalive discards, a header read that stops at the first empty line — a
body line spelling a header name picks nothing, leading empty lines are body line spelling a header name picks nothing, leading empty lines are
skipped), the twelve compact forms (each through `Header`, in lower and skipped), the Content-Length the two framers read (zeros before its
number no part of it; a number past any that fits not read as zero; a
value that begins with no digit read as none), the twelve compact forms (each through `Header`, in lower and
in upper case, and through `Headers`; no other letter taken for a in upper case, and through `Headers`; no other letter taken for a
header name the suite lists; `l:` through the framer and header name the suite lists; `l:` through the framer and
`DatagramMessage`), `ViaResponsePort`'s reading of a Via row — `rport` by its name (in `DatagramMessage`), `ViaResponsePort`'s reading of a Via row — `rport` by its name (in

View file

@ -137,6 +137,20 @@ namespace imsd::sip {
return v; return v;
} }
// The length a Content-Length value names: the number its leading
// digits spell (RFC 3261 25.1: 1*DIGIT), zeros before it aside; nullopt
// where the value begins with no digit. Of a number of more than nine
// digits the first nine are read — more than a datagram holds, and
// enough that a stream waits for a body that does not come.
std::optional<std::size_t> LengthOf(std::string_view v) {
std::string_view digits = v.substr(0, v.find_first_not_of("0123456789"));
if (digits.empty()) return std::nullopt;
while (digits.starts_with('0')) digits.remove_prefix(1);
std::size_t n = 0;
for (char c : digits.substr(0, 9)) n = n * 10 + static_cast<std::size_t>(c - '0');
return n;
}
// A CSeq value as its sequence number and its method (RFC 3261 25.1: // A CSeq value as its sequence number and its method (RFC 3261 25.1:
// 1*DIGIT LWS Method): the digits the trimmed value begins with, less // 1*DIGIT LWS Method): the digits the trimmed value begins with, less
// the zeros before the number, the spaces or tabs after them, then the // the zeros before the number, the spaces or tabs after them, then the
@ -432,7 +446,7 @@ export namespace imsd::sip {
if (idx == std::string::npos) return std::nullopt; if (idx == std::string::npos) return std::nullopt;
std::string_view head = std::string_view(buf_).substr(0, idx); std::string_view head = std::string_view(buf_).substr(0, idx);
long clen = 0; long clen = 0;
if (auto v = Header(head, "Content-Length")) clen = LeadingDigits(*v).value_or(0); if (auto v = Header(head, "Content-Length")) clen = static_cast<long>(LengthOf(*v).value_or(0));
std::size_t total = idx + 4 + static_cast<std::size_t>(clen); std::size_t total = idx + 4 + static_cast<std::size_t>(clen);
if (buf_.size() < total) return std::nullopt; if (buf_.size() < total) return std::nullopt;
std::string msg = buf_.substr(0, total); std::string msg = buf_.substr(0, total);
@ -465,9 +479,8 @@ export namespace imsd::sip {
std::size_t bodyStart = idx + 4; std::size_t bodyStart = idx + 4;
std::size_t body = d.size() - bodyStart; std::size_t body = d.size() - bodyStart;
if (auto v = Header(d.substr(0, idx), "Content-Length")) { if (auto v = Header(d.substr(0, idx), "Content-Length")) {
std::size_t clen = 0; int digits = 0; if (const auto length = LengthOf(*v)) {
for (char c : *v) { if (c < '0' || c > '9' || digits == 9) break; clen = clen * 10 + static_cast<std::size_t>(c - '0'); digits++; } const std::size_t clen = *length;
if (digits) {
if (clen > body) { if (clen > body) {
if (problem) *problem = std::format("datagram truncated (Content-Length {} > {} body bytes)", clen, body); if (problem) *problem = std::format("datagram truncated (Content-Length {} > {} body bytes)", clen, body);
return std::nullopt; return std::nullopt;

View file

@ -5,11 +5,12 @@
// the colon among it), status-line parsing, granted-expires extraction, // the colon among it), status-line parsing, granted-expires extraction,
// caller-identity extraction, the port a Via names for the response // caller-identity extraction, the port a Via names for the response
// (ViaResponsePort), the two framers — the TCP frame buffer (split delivery, // (ViaResponsePort), the two framers — the TCP frame buffer (split delivery,
// keepalive pongs, pipelined messages) and a datagram's (DatagramMessage) —, // keepalive pongs, pipelined messages) and a datagram's (DatagramMessage),
// a refused final's Retry-After and summary (RetryAfter, RefusalSummary), // with the Content-Length they read —, a refused final's Retry-After and
// transaction matching (ResponseOf), our binding in a reg-event document // summary (RetryAfter, RefusalSummary), transaction matching (ResponseOf),
// (RegInfoContactState), and HeaderUri and Body. These pin the module's // our binding in a reg-event document (RegInfoContactState), and HeaderUri
// edge-case behavior; see the module header before "fixing" one. // and Body. These pin the module's edge-case behavior; see the module header
// before "fixing" one.
import std; import std;
import Imsd; import Imsd;
@ -181,6 +182,16 @@ int main() {
Check(fb.TryExtract() == msg, "no Content-Length -> body length 0"); Check(fb.TryExtract() == msg, "no Content-Length -> body length 0");
Check(fb.Size() == 8, "trailing bytes stay buffered"); Check(fb.Size() == 8, "trailing bytes stay buffered");
} }
{
// the Content-Length is read as a number: zeros before it do not count, and one too long to be a length is no zero
const std::string head = "SIP/2.0 200 OK\r\nCall-ID: x\r\n";
FrameBuffer fb;
fb.Append(head + "Content-Length: 00000000005\r\n\r\nhello" + head + "Content-Length: 000\r\n\r\n" + head + "Content-Length: x\r\n\r\n");
Check(fb.TryExtract() == head + "Content-Length: 00000000005\r\n\r\nhello", "a Content-Length of 5 written with ten zeros before it frames a body of five bytes");
Check(fb.TryExtract() == head + "Content-Length: 000\r\n\r\n" && fb.TryExtract() == head + "Content-Length: x\r\n\r\n" && fb.Size() == 0, "zero in three digits is zero, and a value without a digit counts as none");
fb.Append(head + "Content-Length: 18446744073709551616\r\n\r\nhello");
Check(!fb.TryExtract().has_value() && fb.Size() > 0, "a Content-Length past any number that fits is not read as zero: the body is waited for");
}
{ {
// the body's length under the compact name (RFC 3261 7.3.3), and with a space before the colon (7.3.1) // the body's length under the compact name (RFC 3261 7.3.3), and with a space before the colon (7.3.1)
const std::string compact = "MESSAGE sip:a@b SIP/2.0\r\ni: c1@h\r\nl: 5\r\n\r\nhello"; const std::string compact = "MESSAGE sip:a@b SIP/2.0\r\ni: c1@h\r\nl: 5\r\n\r\nhello";
@ -305,6 +316,10 @@ int main() {
Check(m3 && *m3 == hdr + "X-Note: Content-Length: 99\r\n\r\nv=0\n", "a header name inside a value does not match"); Check(m3 && *m3 == hdr + "X-Note: Content-Length: 99\r\n\r\nv=0\n", "a header name inside a value does not match");
auto m4 = DatagramMessage(hdr + "Content-Length: 99999999999999999999\r\n\r\nv=0\n", &why); auto m4 = DatagramMessage(hdr + "Content-Length: 99999999999999999999\r\n\r\nv=0\n", &why);
Check(!m4.has_value(), "absurd Content-Length: discarded, no overflow"); Check(!m4.has_value(), "absurd Content-Length: discarded, no overflow");
why.clear();
Check(!DatagramMessage(hdr + "Content-Length: 18446744073709551616\r\n\r\nv=0\n", &why).has_value() && why.contains("truncated"), "nor is one that would wrap to zero: discarded as truncated");
Check(DatagramMessage(hdr + "Content-Length: 00000000004\r\n\r\nv=0\nJUNK") == hdr + "Content-Length: 00000000004\r\n\r\nv=0\n", "a Content-Length of 4 written with ten zeros before it cuts the body at four bytes");
Check(DatagramMessage(hdr + "Content-Length: x4\r\n\r\nv=0\nJUNK") == hdr + "Content-Length: x4\r\n\r\nv=0\nJUNK", "a Content-Length that begins with no digit: the body is the rest of the datagram");
auto m5 = DatagramMessage(hdr + "l: 4\r\n\r\nv=0\nJUNK"); auto m5 = DatagramMessage(hdr + "l: 4\r\n\r\nv=0\nJUNK");
Check(m5 && *m5 == hdr + "l: 4\r\n\r\nv=0\n", "`l:` that fits truncates the body like Content-Length"); Check(m5 && *m5 == hdr + "l: 4\r\n\r\nv=0\n", "`l:` that fits truncates the body like Content-Length");
why.clear(); why.clear();