[kamailio/kamailio] Parsing enhancments (PR #4917)
<!-- Kamailio Pull Request Template --> <!-- IMPORTANT: - for detailed contributing guidelines, read: https://github.com/kamailio/kamailio/blob/master/.github/CONTRIBUTING.md - pull requests must be done to master branch, unless they are backports of fixes from master branch to a stable branch - backports to stable branches must be done with 'git cherry-pick -x ...' - code is contributed under BSD for core and main components (tm, sl, auth, tls) - code is contributed GPLv2 or a compatible license for the other components - GPL code is contributed with OpenSSL licensing exception --> #### Pre-Submission Checklist <!-- Go over all points below, and after creating the PR, tick all the checkboxes that apply --> <!-- All points should be verified, otherwise, read the CONTRIBUTING guidelines from above--> <!-- If you're unsure about any of these, don't hesitate to ask on sr-dev mailing list --> - [x] Commit message has the format required by CONTRIBUTING guide - [x] Commits are split per component (core, individual modules, libs, utils, ...) - [x] Each component has a single commit (if not, squash them into one commit) - [x] LLM/AI Coding Assistants were involved in creating the C code or other included content - [x] Code is formatted with `clang-format` using the config file `.clang-format` from source code folder - [x] No commits to README files for modules (changes must be done to docbook files in `doc/` subfolder, the README file is autogenerated) #### Type Of Change - [ ] Small bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds new functionality) - [x] Breaking change (fix or feature that would change existing functionality) #### Checklist: <!-- Go over all points below, and after creating the PR, tick the checkboxes that apply --> - [x] PR should be backported to stable branches - [ ] Tested changes locally - [ ] Related to issue #XXXX (replace XXXX with an open issue number) #### Description This PR is to make the sip parsing more deterministic when in both strict and non strict mode. **Strict mode changes** When in strict parsing mode ie. `sip_parser_mode=1` when receiving a new message the Kamailio core will test for what it calls the transaction quadruple, which is the `To`, `From`, `Call-ID`, `CSeq` and fail to enter the route if the SIP messages does not contain those headers. The parsing of the message is deliberately lazy in nature as opposed to eager parsing but it can leave the core in an odd state when strict parsing is set on. Example invalid message which with strict parsing on will fail to enter the route due to a duplicate quadruple(To in this case) ``` INVITE sip:user@domain.com SIP 2.0 To: <valid nameaddr> To: <valid nameaddr> From: <valid nameaddr> Call-ID: valid-callid CSeq: INVITE 1 X-Custom-Header: value .... Content-Length: 0 ``` Example of a request which will enter the route but result in undefined behaviour ``` INVITE sip:user@domain.com SIP 2.0 To: <valid nameaddr> From: <valid nameaddr> Call-ID: valid-callid CSeq: INVITE 1 X-Custom-Header: value To: <valid nameaddr> .... Content-Length: 0 ``` The reason why this happens is because the second To header is not parsed as the check_required_headers keeps going until it finds an instance of the quadruple and it will enter the route and depending on how you are using the route you could fail at another point where the rest of the headers would be parsed or when construct the SIP reply or request once the route finishes, given the expectation would be that strict parsing would fail straight away but in this case it would not. The change turns the strict parsing mode into an eager parser so it fails early, this shouldn't meaningfully change the behaviour of most configuration but it does in the case outlined above so it technically creates a backwards incompatible change. **Non strict mode changes** When the `sip_parser_mode=0` and there is a duplicate check it will only copy the first valid instance of the quadruple in the reply, the current behaviour is such that when constructing a reply eg. `400 Bad Request` both To headers are copied, this change ensures only one of them is copied which is the first instance of that header during parsing. One other change is when in this mode if there is a message such as this ``` INVITE sip:user@domain.com SIP 2.0 To: <invalid nameaddr> To: <valid nameaddr> From: <valid nameaddr> Call-ID: valid-callid CSeq: INVITE 1 X-Custom-Header: value .... Content-Length: 0 ``` It will still parse the quadruple but will take into account the valid `To` header only which in this case is the second instance of the header but the first instance is tracked as an error so that when calling `sanity_check` in the `sanity` module it can behave correctly if the check for required headers is set in the modparam. The reason for fixing this case is that if the request contains at least one valid instance of the quadruple then it can be used to generate a reply. In the above message case the core would drop the request. However the below example would not drop and then the sanity check would fail at this point and due to how the check required headers works it means we would not be able to reply but we would do the sanity check. ``` INVITE sip:user@domain.com SIP 2.0 To: <valid nameaddr> From: <valid nameaddr> Call-ID: valid-callid CSeq: INVITE 1 X-Custom-Header: value To: <invalid nameaddr> .... Content-Length: 0 ``` This just once again tightens up the behaviour so that is deterministic and the script writer can reasonably infer how the parsing will behave based on their requirements. You can view, comment on, or merge this pull request online at: https://github.com/kamailio/kamailio/pull/4917 -- Commit Summary -- * core: skip bad headers, copy one SIP quad when sip_parser_mode=0 * pua: pass NULL next to get_hdr_field() * sipcapture: check get_hdr_field() return * sanity: reply 400 on bad or duplicate SIP quad, check errored headers -- File Changes -- M src/core/msg_translator.c (35) M src/core/parser/msg_parser.c (129) M src/core/parser/msg_parser.h (5) M src/core/parser/parse_addr_spec.c (1) M src/core/parser/parse_to.c (1) M src/core/parser/parse_via.c (20) M src/modules/pua/send_subscribe.c (7) M src/modules/sanity/sanity.c (25) M src/modules/sipcapture/sipcapture.c (4) -- Patch Links -- https://github.com/kamailio/kamailio/pull/4917.patch https://github.com/kamailio/kamailio/pull/4917.diff -- Reply to this email directly or view it on GitHub: https://github.com/kamailio/kamailio/pull/4917 You are receiving this because you are subscribed to this thread. Message ID: <kamailio/kamailio/pull/4917@github.com>
miconda left a comment (kamailio/kamailio#4917) Thanks for contributing, the PR will be reviewed. For tests, if you want to contribute and what you have suits to that framework, look at the repository: - https://github.com/kamailio/kamailio-tests -- Reply to this email directly or view it on GitHub: https://github.com/kamailio/kamailio/pull/4917#issuecomment-5607217320 You are receiving this because you are subscribed to this thread. Message ID: <kamailio/kamailio/pull/4917/c5607217320@github.com>
@miconda commented on this pull request.
> @@ -336,12 +372,39 @@ int parse_headers(
}
memset(hf, 0, sizeof(struct hdr_field));
hf->type = HDR_ERROR_T;
- rest = get_hdr_field(tmp, end, hf);
+ rest = NULL;
+ if(get_hdr_field(tmp, end, hf, &rest) < 0) {
+ LOG(cfg_get(core, core_cfg, sip_parser_log),
+ "bad header field [%.*s]\n",
+ (end - tmp > 100) ? 100 : (int)(end - tmp), tmp);
+
+ if((ksr_sip_parser_mode & KSR_SIP_PARSER_MODE_STRICT)
+ || (ser_error == E_OUT_OF_MEM)) {
Upon a quick review, I am not sure it really has any impact here, but for proper clarification, is get_hdr_field() re-/setting ser_error? ser_error is global and can stay set from another part of code, which might make the above condition true.
--
Reply to this email directly or view it on GitHub:
https://github.com/kamailio/kamailio/pull/4917#pullrequestreview-5290644085
You are receiving this because you are subscribed to this thread.
Message ID: <kamailio/kamailio/pull/4917/review/5290644085@github.com>
@david-trihy commented on this pull request.
> @@ -336,12 +372,39 @@ int parse_headers(
}
memset(hf, 0, sizeof(struct hdr_field));
hf->type = HDR_ERROR_T;
- rest = get_hdr_field(tmp, end, hf);
+ rest = NULL;
+ if(get_hdr_field(tmp, end, hf, &rest) < 0) {
+ LOG(cfg_get(core, core_cfg, sip_parser_log),
+ "bad header field [%.*s]\n",
+ (end - tmp > 100) ? 100 : (int)(end - tmp), tmp);
+
+ if((ksr_sip_parser_mode & KSR_SIP_PARSER_MODE_STRICT)
+ || (ser_error == E_OUT_OF_MEM)) {
Yes that's correct, my idea at the time was that we needed the ability to stop parsing on pkg_malloc failures when in permissive mode instead of trying to continually parse. Looking at the code I'm unsure why I just didn't add another error code to get_hdr_field and return -2 when there's a memory allocation error and used this pattern instead, need to reread it to see if there was a particular reason.
I still need to port the tests to the framework you posted, I have them running against our internal tooling but they're pretty basic, just a simple sanity_check() call in the script when permissive parsing is on.
--
Reply to this email directly or view it on GitHub:
https://github.com/kamailio/kamailio/pull/4917#discussion_r4082454098
You are receiving this because you are subscribed to this thread.
Message ID: <kamailio/kamailio/pull/4917/review/5291004899@github.com>
@miconda commented on this pull request.
> @@ -336,12 +372,39 @@ int parse_headers(
}
memset(hf, 0, sizeof(struct hdr_field));
hf->type = HDR_ERROR_T;
- rest = get_hdr_field(tmp, end, hf);
+ rest = NULL;
+ if(get_hdr_field(tmp, end, hf, &rest) < 0) {
+ LOG(cfg_get(core, core_cfg, sip_parser_log),
+ "bad header field [%.*s]\n",
+ (end - tmp > 100) ? 100 : (int)(end - tmp), tmp);
+
+ if((ksr_sip_parser_mode & KSR_SIP_PARSER_MODE_STRICT)
+ || (ser_error == E_OUT_OF_MEM)) {
I think using a different return code is a better variant rather leveraging the global ser_error, but if after all is considered differently, ser_error should clearly reset so its value is not propagated from a previous assignment in other parts of the code.
--
Reply to this email directly or view it on GitHub:
https://github.com/kamailio/kamailio/pull/4917#discussion_r4208235999
You are receiving this because you are subscribed to this thread.
Message ID: <kamailio/kamailio/pull/4917/review/5443937869@github.com>
miconda left a comment (kamailio/kamailio#4917) @david-trihy: any opinion on changing so the function uses different return code rather than leveraging ser_error? -- Reply to this email directly or view it on GitHub: https://github.com/kamailio/kamailio/pull/4917#issuecomment-6190246620 You are receiving this because you are subscribed to this thread. Message ID: <kamailio/kamailio/pull/4917/c6190246620@github.com>
david-trihy left a comment (kamailio/kamailio#4917) @miconda I agree, I looked at the code to see why I went that route in the first place and I couldn't see a distinct reason why and having return code encapsulated in the stack frame calling it is far better than leveraging a global ERROR code handler, the only way I could envision that global error code being useful is if we decided to call a signal handler to exit early and needed some global error code and even though this error is caused by running out of package memory I don't think the appropriate action is call a signal handler and exit at this point. Apologies I have been quite busy but I should have some time to make this change and also port some tests to the Kamailio test framework. I have a few questions for you 1. There's guidelines on how the commits should be structured but for this change I can just put a new commit on top with a commit message that just details the change? 2. Regarding the parse mode, this is obviously more permissive but more deterministic, strict parsing will now do eager parsing, do we potentially want to add a third option so mode 0 is permissive, mode 1 is strict lazy(existing behaviour) and mode 2 is strict eager? That means there would be no surprises when you upgrade your Kamailio version, just a thought, with this change we're using `parser_mode=0` and using the `sanity` module to verify different aspects of the SIP 3. Other than the parser_mode question above and with the return code change are you happy with how the rest of the change is structured? -- Reply to this email directly or view it on GitHub: https://github.com/kamailio/kamailio/pull/4917#issuecomment-6190873549 You are receiving this because you are subscribed to this thread. Message ID: <kamailio/kamailio/pull/4917/c6190873549@github.com>
miconda left a comment (kamailio/kamailio#4917) A new commit is ok. Also the proposed options for parse_mode should be good, adding the 3rd one for more fine-grained control. -- Reply to this email directly or view it on GitHub: https://github.com/kamailio/kamailio/pull/4917#issuecomment-6210841568 You are receiving this because you are subscribed to this thread. Message ID: <kamailio/kamailio/pull/4917/c6210841568@github.com>
henningw left a comment (kamailio/kamailio#4917) Can you also create a PR in the wiki core docs to describe the new parameter (option)? https://www.kamailio.org/wikidocs/cookbooks/devel/core/ -- Reply to this email directly or view it on GitHub: https://github.com/kamailio/kamailio/pull/4917#issuecomment-6414070007 You are receiving this because you are subscribed to this thread. Message ID: <kamailio/kamailio/pull/4917/c6414070007@github.com>
@david-trihy pushed 2 commits. 3d36beb9a756f23ee9e9965dbab6196470c42ec2 get_hdr_field explicit return code for OOM dc3095ecec3eef1d7d0ea5ad680c9a6e9f94a0b5 Adding strict lazy and strict eager as options for parser_mode -- View it on GitHub: https://github.com/kamailio/kamailio/pull/4917/changes/3a4cae96e1e2dc73faf42... You are receiving this because you are subscribed to this thread. Message ID: <kamailio/kamailio/pull/4917/before/3a4cae96e1e2dc73faf42b84830a3dc523309b2a/after/dc3095ecec3eef1d7d0ea5ad680c9a6e9f94a0b5@github.com>
david-trihy left a comment (kamailio/kamailio#4917) @miconda I made the changes, thanks for the patience on this one, I need to get the tests in kamailio-tests, it's pretty straight forward so I just need the time to do it. The script is pretty barebones for any of the tests. @henningw I will create that PR pretty soon. -- Reply to this email directly or view it on GitHub: https://github.com/kamailio/kamailio/pull/4917#issuecomment-6442027402 You are receiving this because you are subscribed to this thread. Message ID: <kamailio/kamailio/pull/4917/c6442027402@github.com>
miconda left a comment (kamailio/kamailio#4917) The last two commits do not follow the commit message format requirements, so it cannot be merged from here unless you redo the commits and force push. Otherwise it has to be merged manually. -- Reply to this email directly or view it on GitHub: https://github.com/kamailio/kamailio/pull/4917#issuecomment-6531096208 You are receiving this because you are subscribed to this thread. Message ID: <kamailio/kamailio/pull/4917/c6531096208@github.com>
david-trihy left a comment (kamailio/kamailio#4917) @miconda sorry about that, let me fix them now and I'll force push -- Reply to this email directly or view it on GitHub: https://github.com/kamailio/kamailio/pull/4917#issuecomment-6531833276 You are receiving this because you are subscribed to this thread. Message ID: <kamailio/kamailio/pull/4917/c6531833276@github.com>
david-trihy left a comment (kamailio/kamailio#4917) That has been updated, I will open a PR for the docs as well soon, just need to update the parser_mode docs -- Reply to this email directly or view it on GitHub: https://github.com/kamailio/kamailio/pull/4917#issuecomment-6531996163 You are receiving this because you are subscribed to this thread. Message ID: <kamailio/kamailio/pull/4917/c6531996163@github.com>
henningw left a comment (kamailio/kamailio#4917) Thanks. Please check the format of the commits, probably some clang-format run missing. -- Reply to this email directly or view it on GitHub: https://github.com/kamailio/kamailio/pull/4917#issuecomment-6541249862 You are receiving this because you are subscribed to this thread. Message ID: <kamailio/kamailio/pull/4917/c6541249862@github.com>
miconda left a comment (kamailio/kamailio#4917) Merging it and following up with another related commit, trying to fix code formatting as well. -- Reply to this email directly or view it on GitHub: https://github.com/kamailio/kamailio/pull/4917#issuecomment-6572012138 You are receiving this because you are subscribed to this thread. Message ID: <kamailio/kamailio/pull/4917/c6572012138@github.com>
Merged #4917 into master. -- Reply to this email directly or view it on GitHub: https://github.com/kamailio/kamailio/pull/4917#event-34886613155 You are receiving this because you are subscribed to this thread. Message ID: <kamailio/kamailio/pull/4917/issue_event/34886613155@github.com>
david-trihy left a comment (kamailio/kamailio#4917) @miconda and @henningw thanks for the updates on this one and apologies the last commit was troublesome with CI I've opened a PR for the docs here https://github.com/kamailio/kamailio-wiki/pull/107 -- Reply to this email directly or view it on GitHub: https://github.com/kamailio/kamailio/pull/4917#issuecomment-6574994107 You are receiving this because you are subscribed to this thread. Message ID: <kamailio/kamailio/pull/4917/c6574994107@github.com>
participants (3)
-
Daniel-Constantin Mierla -
David Trihy -
Henning Westerholt