From 89048c3602424ee8c69fcea9b8e5916b196a1ef6 Mon Sep 17 00:00:00 2001 From: Jorijn van der Graaf Date: Sun, 18 Oct 2026 05:44:20 +0200 Subject: [PATCH] sip: a header is read from the header section only MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Header and Headers iterated every line of a message, body included, so a body line that spells a header name — a text someone typed, or the UCS-2 bytes of one — would be read as that header: a second Via for a final to echo, a P-Asserted-Identity that CallerId takes for the sender. ForEachLine now ends at the first empty line after a line of text (RFC 3261 7: the headers end at the empty line) and skips empty lines before it (a keepalive's CRLFs ahead of a message), so every reader of a header reads the header section. Pinned in tests/Sip: a body Via is no Via, a name that occurs only in the body is absent, leading CRLFs are skipped. With these the suite has more than 77 checks, so it returns 1 for any failure and no longer their count: `crafter-build test` takes an exit status of 77 for a skipped suite. --- AGENTS.md | 10 ++++++---- interfaces/Imsd-Sip.cppm | 17 ++++++++++++++--- tests/Sip/main.cpp | 13 ++++++++++++- 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 1675cf0..fc71418 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -39,7 +39,7 @@ PipeWire. The verified device/carrier combinations are in the README. | `project.cpp` | The canonical build description, for [Crafter Build](https://forgejo.catcrafts.net/Catcrafts/Crafter.Build). Chooses the product by `--product=`. | | `interfaces/Imsd.cppm` | The primary module interface of `imsd-core`: `export import`s the nine partitions below. | | `interfaces/Imsd-Util.cppm` | `imsd::util` — SIP tokens, IPv6 host/port formatting, the RFC 5952 §4 IPv6 text (`CanonicalIp6`/`CanonicalAddr`; an IPv4-mapped address spelled in hex stays hex, where §5 and `inet_ntop` write it dotted) and the comma-separated address-list parser (`ParseAddressList`), hex/base64, MD5 (RFC 1321), the fixed protected ports `PortUc`/`PortUs` (45061/45062). | -| `interfaces/Imsd-Sip.cppm` | `imsd::sip` — SIP as a text layer plus TCP framing: header extraction, status codes, the granted lifetime, `ResponseOf` transaction matching. Not a full RFC 3261 parser, deliberately. | +| `interfaces/Imsd-Sip.cppm` | `imsd::sip` — SIP as a text layer plus TCP framing: header extraction (from the header section only: a read ends at the first empty line after the start line, empty lines before it skipped), status codes, the granted lifetime, `ResponseOf` transaction matching. Not a full RFC 3261 parser, deliberately. | | `interfaces/Imsd-Sdp.cppm` | `imsd::sdp` — SDP offer/answer: the AMR-WB + AMR + telephone-event offer (optional IR.92 preconditions; `OfferedTelephoneEventPt` names its telephone-event numbers), the parser the media plane needs (the codec, its payload type and mode, and the telephone-event payload type the m-line lists at that codec's clock), `BuildAnswer` for the terminating side, the `CODECS` override. | | `interfaces/Imsd-Sms.cppm` | `imsd::sms` — the short message codecs SMS over IP rides on (TS 24.341 carries the bytes a 2G/3G phone puts in an RP-DATA, in a SIP MESSAGE body): the GSM 7-bit default alphabet with its extension table and the septet packing, UCS-2 (TS 23.038); the SM-TL PDUs (TS 23.040: SMS-SUBMIT built, one per segment with the concatenation header when the text needs more than one; SMS-DELIVER, SMS-STATUS-REPORT and SMS-SUBMIT-REPORT parsed; the SMS-DELIVER-REPORT that acknowledges a delivery; the user-data header — concatenation, application ports — and the `Reassembler` of a concatenated message); the SM-RL messages (TS 24.011: RP-DATA, RP-ACK, RP-ERROR in both directions, the cause values); EF_SMSP (TS 31.102 §4.2.27), where the (U)SIM names the service centre, and qmicli's hex dump of it. Byte-level only: no SIP, no clock, no I/O. | | `interfaces/Imsd-Aka.cppm` | `imsd::aka` — USIM AKA glue: the AKAv1-MD5 digest (RFC 3310), IMPI/IMPU/realm from IMSI + MCC/MNC, parsers for `qmicli --uim-get-card-status` and `mmcli` output, the AUTHENTICATE APDU and its response. | @@ -241,8 +241,8 @@ releases. This is the flow CI runs (§9.4). `crafter-build test` runs the nine suites. Each suite is a plain `main()` with a `Check(bool, message)` counter: failures print `FAIL: …`, success prints `: all tests passed`, and the exit status is -the failure count for seven suites — non-zero short of a multiple of 256 — and -1 for `Engine` and `Sms`. `crafter-build test` ending in `9 passed`, on every +the failure count for six suites — non-zero short of a multiple of 256 — and +1 for `Engine`, `Sip` and `Sms`. `crafter-build test` ending in `9 passed`, on every commit of a range (§11), is the project's definition of green. What the suites pin: @@ -280,7 +280,9 @@ What the suites pin: - `Messages` — every outgoing SIP message byte-for-byte, cross-generated from the Python reference stack imsd replaced. - `Sip` — the text layer's edge cases (empty header values, line anchoring, - keepalive discards), `ResponseOf`, refused finals with reason headers, + 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), `ResponseOf`, refused finals with reason headers, reginfo documents. A change that flips one of those tests is a behaviour change, not a cleanup. - `Sdp` — the offer's exact bytes, the parser (the telephone-event payload diff --git a/interfaces/Imsd-Sip.cppm b/interfaces/Imsd-Sip.cppm index 306d199..0421cb8 100644 --- a/interfaces/Imsd-Sip.cppm +++ b/interfaces/Imsd-Sip.cppm @@ -40,15 +40,26 @@ namespace imsd::sip { return s; } - // Iterate \n-separated lines (any trailing CR stays on the line and is - // stripped by Trim when a value is extracted). + // Iterate the \n-separated lines of the header section (any trailing CR + // stays on the line and is stripped by Trim when a value is extracted). + // The first empty line after a line of text ends the headers (RFC 3261 + // 7): a body line that spells a header name — a text someone typed, or + // UCS-2 bytes that happen to — is never read as one. Empty lines before + // the first line of text (a keepalive's CRLFs ahead of a message) are + // skipped. template void ForEachLine(std::string_view msg, F&& f) { std::size_t pos = 0; + bool started = false; while (pos <= msg.size()) { std::size_t nl = msg.find('\n', pos); std::string_view line = msg.substr(pos, nl == std::string_view::npos ? std::string_view::npos : nl - pos); - if (!f(line)) return; + const bool blank = line.empty() || line == "\r"; + if (blank && started) return; + if (!blank) { + started = true; + if (!f(line)) return; + } if (nl == std::string_view::npos) return; pos = nl + 1; } diff --git a/tests/Sip/main.cpp b/tests/Sip/main.cpp index 43e3454..5e418e5 100644 --- a/tests/Sip/main.cpp +++ b/tests/Sip/main.cpp @@ -54,6 +54,17 @@ int main() { Check(!Header(Resp200, "URI").has_value(), "Header anchors at line start"); // bare "Name:" (only CR after the colon) matches with empty value Check(Header("X-Empty:\r\nX-Real: v\r\n\r\n", "X-Empty") == "", "bare header matches with empty value"); + // a header read stops at the first empty line: a body line spelling a header name is no header + const std::string BodyVia = + "MESSAGE sip:a@b SIP/2.0\r\n" + "Via: SIP/2.0/TCP [2001:db8::1]:5060;branch=z9hG4bK1\r\n" + "Call-ID: c1@h\r\n" + "Content-Length: 36\r\n" + "\r\n" + "hi\r\nVia: SIP/2.0/UDP body:5060\r\nX-Body: here\r\n"; + Check(Headers(BodyVia, "Via").size() == 1 && Header(BodyVia, "Call-ID") == "c1@h", "Headers stops at the blank line: the body's Via is no Via"); + Check(!Header(BodyVia, "X-Body").has_value(), "Header never reads the body"); + Check(Header("\r\n\r\nSIP/2.0 200 OK\r\nCall-ID: k\r\n\r\n", "Call-ID") == "k", "empty lines before the first line of text (a keepalive's CRLFs) are skipped"); // ---- GrantedExpires Check(GrantedExpires(Resp200) == 600000, "GrantedExpires from Contact expires="); @@ -289,5 +300,5 @@ int main() { } if (Failures == 0) std::println("Sip: all tests passed"); - return Failures; + return Failures != 0; // not the count: `crafter-build test` takes an exit status of 77 for a skipped suite, and this suite has more checks than that }