sip, messages, imsd: the client flow answers a request datagram cut short of the Content-Length it reads with a 400, unless an ACK or a CANCEL

RFC 3261 18.3 on a datagram that ends before its message's body does:
"If the message is a response, it MUST be discarded. If the message is
a request, the element SHOULD generate a 400 (Bad Request) response."
The client flow's datagram read ignored either, where the
Content-Length it read said more bytes than came, with one line, `UDP:
datagram truncated (…) — ignored`.

CutShortRequest gives the header section of a request whose datagram
has fewer body bytes than the Content-Length read from it says, and
BuildBadRequest the 400 for it, in the shape of BuildResponse's finals:
every Via row echoed, the To tagged unless it holds a tag. The client
flow sends that 400 and its line ends `— answered 400`. Such a request
is known by its first line, which ends with the SIP version where a
status line begins with it (7.1, 7.2), in upper or in lower case: a
response cut short so, bytes that are no SIP cut short so and a
datagram without an end of headers are ignored as before. imsd keeps
nothing of a request it answers so, and follows 8.2.7 for an element
that answers without state: a To tag it adds is made from the
request's header section and a secret drawn once for the run, "in a
manner that will generate the same tag for the same request
consistently", and an ACK or a CANCEL cut short so gets no answer. The
server ports and the init port hand a datagram on as it came, as
before this commit.

Tested by tests/Sip (CutShortRequest: the header section of a request
cut short, with line breaks before it, under a compact Content-Length
and with its version in lower case; nothing for a body that is all
there, no Content-Length, no end of headers, a response in either
letter case, bytes that are no SIP, an ACK, a CANCEL) and by
tests/Messages (the 400 by its lines, its tag against Python's
hashlib; the same request, the same 400; another request or another
secret, another tag). On the workstation's loopback, SipFlow built
around a bench main (the fp6 journal's 2026-11-20-issues-2-5 capture,
cut-short, at this commit and at the one before): a MESSAGE with five
body bytes of nine draws the 400, and the same one when it is sent
again, where the commit before ignored it; a response cut short the
same way, in upper and in lower case, an ACK, a CANCEL, bytes that are
no SIP and a request without an end of headers draw nothing at either
commit; the MESSAGE with its Content-Length folded is handed on at
either commit, a folded value being read by its first line; a whole
request sent after them is returned by the flow.
This commit is contained in:
Jorijn van der Graaf 2026-11-22 17:35:11 +01:00
commit 7cae603c0c
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
6 changed files with 164 additions and 11 deletions

View file

@ -502,6 +502,36 @@ int main() {
"Content-Length: 0\r\n\r\n", "a 488 for an inbound MESSAGE echoes every Via and tags To");
const std::string tagged = "MESSAGE sip:me@ims SIP/2.0\r\nVia: SIP/2.0/UDP [2001:db8::105]:6000;branch=z9hG4bKnet2\r\nFrom: <sip:ipsmgw@ims>;tag=N2\r\nTo: <sip:me@ims>;tag=had\r\nCall-ID: in2@net\r\nCSeq: 1 MESSAGE\r\nContent-Length: 0\r\n\r\n";
Check(BuildResponse(c, tagged, 200, "OK", "unused").contains("\r\nTo: <sip:me@ims>;tag=had\r\n"), "a To that already carries a tag is echoed as it is");
// the 400 to a request whose datagram ended before its body did (RFC 3261 18.3), built from what imsd::sip::CutShortRequest gives of it
{
std::string cut = inbound;
cut.replace(cut.find("Content-Length: 0"), 17, "Content-Length: 12");
const std::string head = imsd::sip::CutShortRequest(cut + "seven b").value_or("");
Check(!head.empty(), "an inbound MESSAGE with seven body bytes of twelve is a request cut short");
// the To tag below was made apart from imsd's MD5: Python's hashlib over the secret and the header section with its empty line, the first sixteen digits
const std::string_view secret = "run-secret-of-16";
Eq(BuildBadRequest(head, secret),
"SIP/2.0 400 Bad Request\r\n"
"Via: SIP/2.0/UDP [2001:db8::105]:6000;branch=z9hG4bKnet1\r\n"
"Via: SIP/2.0/UDP [2001:db8::77]:5060;branch=z9hG4bKgw0\r\n"
"From: <sip:ipsmgw@ims>;tag=N1\r\n"
"To: <sip:me@ims>;tag=d930d9cad06d5cba\r\n"
"Call-ID: in1@net\r\n"
"CSeq: 9 MESSAGE\r\n"
"Content-Length: 0\r\n\r\n", "its 400 echoes every Via and tags To with sixteen hex digits of the MD5 of the secret and the header section");
Eq(BuildBadRequest(imsd::sip::CutShortRequest(cut + "s").value_or(""), secret), BuildBadRequest(head, secret), "the same request cut short elsewhere draws the same 400, tag and all");
const auto toTag = [](std::string_view r) { const std::size_t at = r.find(";tag=", r.find("\r\nTo: ")); return r.substr(at, r.find("\r\n", at) - at); };
const std::string r1 = BuildBadRequest(head, secret);
std::string other = cut;
other.replace(other.find("in1@net"), 7, "in2@net");
const std::string r2 = BuildBadRequest(imsd::sip::CutShortRequest(other + "seven b").value_or(""), secret);
Check(r2.contains("\r\nCall-ID: in2@net\r\n") && toTag(r2).size() == 5 + 16 && toTag(r2) != toTag(r1), "another request draws another tag, of sixteen digits too");
const std::string r3 = BuildBadRequest(head, "another-secret-16");
Check(toTag(r3).size() == 5 + 16 && toTag(r3) != toTag(r1), "and the same request under another secret draws another tag");
std::string had = tagged;
had.replace(had.find("Content-Length: 0"), 17, "Content-Length: 12");
Check(BuildBadRequest(imsd::sip::CutShortRequest(had + "seven b").value_or(""), secret).contains("\r\nTo: <sip:me@ims>;tag=had\r\n"), "and the 400 echoes a To that already carries a tag as it is");
}
// a request whose header names come in compact form (RFC 3261 7.3.3): the finals echo them under the long names
const std::string compact =
"MESSAGE sip:me@[2001:db8::db43]:45062 SIP/2.0\r\n"

View file

@ -6,10 +6,11 @@
// 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),
// 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
// with the Content-Length they read —, a request whose datagram ends before
// its body does (CutShortRequest), 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;
@ -326,6 +327,33 @@ int main() {
Check(!DatagramMessage(hdr + "L : 40\r\n\r\nv=0\n", &why).has_value() && why.contains("truncated"), "`L :` larger than the datagram: discarded, problem set");
}
// ---- CutShortRequest (a request whose datagram ends before its body does: the header section its 400 is built from, RFC 3261 18.3)
{
const std::string req = "MESSAGE sip:a@example.net SIP/2.0\r\nVia: SIP/2.0/UDP [2001:db8::2]:6000;branch=z9hG4bK1\r\nCall-ID: c\r\nCSeq: 7 MESSAGE\r\n";
Check(CutShortRequest(req + "Content-Length: 9\r\n\r\nhello") == req + "Content-Length: 9\r\n\r\n", "a request with five body bytes of nine: its header section and the empty line, no body");
Check(CutShortRequest("\r\n\r\n" + req + "l: 6\r\n\r\nhello") == req + "l: 6\r\n\r\n", "line breaks before it are no part of it, and the length is found under its compact form");
Check(CutShortRequest(req + "Content-Length: 0006\r\n\r\nhello").has_value(), "zeros before the length are no part of the number");
Check(!CutShortRequest(req + "Content-Length: 5\r\n\r\nhello") && !CutShortRequest(req + "Content-Length: 4\r\n\r\nhello"), "a body that is all there, with or without bytes after it: none");
Check(!CutShortRequest(req + "\r\nhello") && !CutShortRequest(req + "Content-Length: x9\r\n\r\nhello"), "no Content-Length, or one that begins with no digit: none (the body is the rest of the datagram)");
Check(!CutShortRequest(req + "Content-Length: 9\r\n") && !CutShortRequest("\r\n\r\n") && !CutShortRequest(""), "no end of headers, line breaks alone, no bytes: none");
Check(!CutShortRequest("SIP/2.0 200 OK\r\nCall-ID: c\r\nContent-Length: 9\r\n\r\nhello") && !CutShortRequest("sip/2.0 200 OK\r\nCall-ID: c\r\nContent-Length: 9\r\n\r\nhello"), "a response cut short: none (it is discarded), its version in upper or in lower case");
Check(!CutShortRequest("SIP/2.0 200 SIP/2.0\r\nContent-Length: 9\r\n\r\nhello") && !CutShortRequest("sip/2.0 200 sip/2.0\r\nContent-Length: 9\r\n\r\nhello"), "nor is a status line a request's first line because its phrase ends like a version");
Check(CutShortRequest("MESSAGE sip:a@example.net sip/2.0\r\nContent-Length: 9\r\n\r\nhello").has_value(), "a request whose version is in lower case is a request");
Check(!CutShortRequest("MESSAGE sip:a@example.net SIP/2.01\r\nContent-Length: 9\r\n\r\nhello"), "a first line that ends in more than the version: none");
Check(!CutShortRequest("MESSAGE sip:a@example.net SIP/3.0\r\nContent-Length: 9\r\n\r\nhello") && !CutShortRequest("MESSAGE sip:a@example.net bar1234\r\nContent-Length: 9\r\n\r\nhello"), "nor one that ends in another version, or in seven other bytes");
Check(!CutShortRequest("l:9\r\n\r\n") && !CutShortRequest("HTTP/1.1 200 OK\r\nContent-Length: 9\r\n\r\nhello"), "bytes that are no SIP, a Content-Length larger than what follows among them: none");
Check(!CutShortRequest("ACK sip:a@example.net SIP/2.0\r\nCall-ID: c\r\nContent-Length: 9\r\n\r\nhello"), "an ACK cut short: none");
Check(!CutShortRequest("CANCEL sip:a@example.net SIP/2.0\r\nCall-ID: c\r\nContent-Length: 9\r\n\r\nhello"), "a CANCEL cut short: none");
Check(CutShortRequest("ACKNOWLEDGE sip:a@example.net SIP/2.0\r\nContent-Length: 9\r\n\r\nhello").has_value() && CutShortRequest("ack sip:a@example.net SIP/2.0\r\nContent-Length: 9\r\n\r\nhello").has_value(), "a method that begins with ACK, or spells it in other letter case, is no ACK");
Check(CutShortRequest("CANCELLED sip:a@example.net SIP/2.0\r\nContent-Length: 9\r\n\r\nhello").has_value() && CutShortRequest("cancel sip:a@example.net SIP/2.0\r\nContent-Length: 9\r\n\r\nhello").has_value(), "nor is one that begins with CANCEL, or spells it in other letter case, a CANCEL");
// the two agree on what is cut short: the same bytes skipped, the same length read, the same count of body bytes
for (std::string_view tail : {"Content-Length: 9\r\n\r\nhello", "Content-Length: 5\r\n\r\nhello", "Content-Length: 4\r\n\r\nhello", "\r\nhello", "Content-Length: 9\r\n"}) {
std::string why;
const bool truncated = !DatagramMessage(req + std::string(tail), &why).has_value() && why.contains("truncated");
Check(CutShortRequest(req + std::string(tail)).has_value() == truncated, std::format("a MESSAGE's header section comes exactly where DatagramMessage calls its datagram truncated ({:?})", tail));
}
}
// ---- RetryAfter / RefusalSummary (what a refused final is logged as)
{
std::string r503 =