diff --git a/AGENTS.md b/AGENTS.md index 720f6fe..23f6b4f 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -291,7 +291,9 @@ What the suites pin: - `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 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 header name the suite lists; `l:` through the framer and `DatagramMessage`), `ViaResponsePort`'s reading of a Via row — `rport` by its name (in diff --git a/interfaces/Imsd-Sip.cppm b/interfaces/Imsd-Sip.cppm index c91a332..f0d97c6 100644 --- a/interfaces/Imsd-Sip.cppm +++ b/interfaces/Imsd-Sip.cppm @@ -137,6 +137,20 @@ namespace imsd::sip { 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 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(c - '0'); + return n; + } + // 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 // 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; std::string_view head = std::string_view(buf_).substr(0, idx); 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(LengthOf(*v).value_or(0)); std::size_t total = idx + 4 + static_cast(clen); if (buf_.size() < total) return std::nullopt; std::string msg = buf_.substr(0, total); @@ -465,9 +479,8 @@ export namespace imsd::sip { std::size_t bodyStart = idx + 4; std::size_t body = d.size() - bodyStart; if (auto v = Header(d.substr(0, idx), "Content-Length")) { - std::size_t clen = 0; int digits = 0; - for (char c : *v) { if (c < '0' || c > '9' || digits == 9) break; clen = clen * 10 + static_cast(c - '0'); digits++; } - if (digits) { + if (const auto length = LengthOf(*v)) { + const std::size_t clen = *length; if (clen > body) { if (problem) *problem = std::format("datagram truncated (Content-Length {} > {} body bytes)", clen, body); return std::nullopt; diff --git a/tests/Sip/main.cpp b/tests/Sip/main.cpp index 62ce946..6c45f4f 100644 --- a/tests/Sip/main.cpp +++ b/tests/Sip/main.cpp @@ -5,11 +5,12 @@ // the colon among it), status-line parsing, granted-expires extraction, // caller-identity extraction, the port a Via names for the response // (ViaResponsePort), the two framers — the TCP frame buffer (split delivery, -// keepalive pongs, pipelined messages) and a datagram's (DatagramMessage) —, -// a refused final's Retry-After and summary (RetryAfter, RefusalSummary), -// transaction matching (ResponseOf), our binding in a reg-event document -// (RegInfoContactState), and HeaderUri and Body. These pin the module's -// edge-case behavior; see the module header before "fixing" one. +// keepalive pongs, pipelined messages) and a datagram's (DatagramMessage), +// with the Content-Length they read —, a refused final's Retry-After and +// summary (RetryAfter, RefusalSummary), transaction matching (ResponseOf), +// our binding in a reg-event document (RegInfoContactState), and HeaderUri +// and Body. These pin the module's edge-case behavior; see the module header +// before "fixing" one. import std; import Imsd; @@ -181,6 +182,16 @@ int main() { Check(fb.TryExtract() == msg, "no Content-Length -> body length 0"); 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) 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"); auto m4 = DatagramMessage(hdr + "Content-Length: 99999999999999999999\r\n\r\nv=0\n", &why); 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"); Check(m5 && *m5 == hdr + "l: 4\r\n\r\nv=0\n", "`l:` that fits truncates the body like Content-Length"); why.clear();