Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StreamParser hangs when processing a Contact line with multiple bindings #21

Closed
Marian-Kechlibar opened this issue May 18, 2023 · 3 comments

Comments

@Marian-Kechlibar
Copy link

A Contact: header in a response to REGISTER request may contain one or more values separated by commas, like this:

Contact: <sip:e52@78.102.16.243:55497;transport=tls>;expires=111;received="sip:e52@78.102.16.243:55497";reg-id=1;+sip.instance="<d79966b435633699badcf12507380d51d4e97eab>",<sip:e52@78.102.16.243:17127;transport=tls>;expires=258;received="sip:e52@78.102.16.243:17127";reg-id=2;+sip.instance="<65be01c100f2ccd762df61057569f037bd46a367>"

This actually happens in practice if the indicated user is registered from multiple devices.

The StreamParser, as of version 0.7.0., does not anticipate this possibility and will enter an infinite loop in some cases. The main problem seems to be that the method

ContactHeader.parse(array $hbody)

receives only an array of count 1, while it should receive an array of count N, exploded by the delimiter ,.

I made a crude and ugly fix in ContactHeader.parse like this:


    public static function parse(array $hbody): ContactHeader
    {
        $ret = new static;
        $hbody2 = array();
        foreach ($hbody as $hline) {
            $explodedLine = explode(',', $hline);
            $hbody2 = array_merge($hbody2, $explodedLine);
        }
        $hbody = $hbody2;

It works, but it is also very ugly. I suppose that the original author has a better solution.

AFAIK, in case of SIP, if a header contains commas, it is as if the same header was present multiple times, so

Contact: XX,YY

is equivalent to

Contact: XX
Contact: YY
@Marian-Kechlibar
Copy link
Author

@cdosoftei Feel free to contact me for more info. Unfortunately whenever I trigger this bug, my entire Apache hangs (I am developing on XAMPP) and it takes a few minutes to get it running again, so the amount of collected examples is very limited.

@cdosoftei
Copy link
Contributor

Hi @Marian-Kechlibar!

Thanks for the detailed feedback; indeed the provided sample should have parsed out just fine. I've addressed the bug in the v0.7.1 branch. Your sample parses out to the following JSONified output:

{
    "uri": {
        "scheme": "sip",
        "user": "e52",
        "host": "78.102.16.243",
        "ipv6": false,
        "port": 55497,
        "transport": "tls",
        "params": [],
        "headers": []
    },
    "expires": 111,
    "params": {
        "received": "\"sip:e52@78.102.16.243:55497\"",
        "reg-id": "1",
        "+sip.instance": "\"<d79966b435633699badcf12507380d51d4e97eab>\""
    }
},
{
    "uri": {
        "scheme": "sip",
        "user": "e52",
        "host": "78.102.16.243",
        "ipv6": false,
        "port": 17127,
        "transport": "tls",
        "params": [],
        "headers": []
    },
    "expires": 258,
    "params": {
        "received": "\"sip:e52@78.102.16.243:17127\"",
        "reg-id": "2",
        "+sip.instance": "\"<65be01c100f2ccd762df61057569f037bd46a367>\""
    }
}

Please let me know how it works for you. Barring any issues, I want to make a few more CI updates before releasing it (switch to GitHub Actions and drop Travis).

@Marian-Kechlibar
Copy link
Author

I can confirm that the patched version works for me. I am looking forward for release of 0.7.1 :)

@cdosoftei cdosoftei mentioned this issue May 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants