Parsing invalid Content-Length header, try to change it to a valid one

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Parsing invalid Content-Length header, try to change it to a valid one

joel.oliveira
Hi all,

From a little time until now I got some faulty equipments ( mainly Fritzbox ) that send a bad Content-Length header (e.g. "Content-Length: 4294967294 361" ) resulting in the next messages on OpenSIPs debug and ending the call without even forwarding the first Invite:

Jun 13 11:23:23 opensips01 /sbin/opensips[5174]: ERROR:core:parse_content_length: parse error near char [54][6]
Jun 13 11:23:23 opensips01 /sbin/opensips[5174]: ERROR:core:get_hdr_field: bad content_length header
Jun 13 11:23:23 opensips01 /sbin/opensips[5174]: INFO:core:parse_headers: bad header field
Jun 13 11:23:23 opensips01 /sbin/opensips[5174]: ERROR:nathelper:extract_body: failed to get the message body
Jun 13 11:23:23 opensips01 /sbin/opensips[5174]: ERROR:nathelper:fix_nated_sdp_f: cannot extract body from msg!
Jun 13 11:23:23 opensips01 /sbin/opensips[5174]: ERROR:core:parse_content_length: parse error near char [54][6]
Jun 13 11:23:23 opensips01 /sbin/opensips[5174]: ERROR:core:get_hdr_field: bad content_length header
Jun 13 11:23:23 opensips01 /sbin/opensips[5174]: INFO:core:parse_headers: bad header field
Jun 13 11:23:23 opensips01 /sbin/opensips[5174]: ERROR:core:pv_get_hdr: error parsing headers
Jun 13 11:23:23 opensips01 /sbin/opensips[5174]: ERROR:core:parse_content_length: parse error near char [54][6]
Jun 13 11:23:23 opensips01 /sbin/opensips[5174]: ERROR:core:get_hdr_field: bad content_length header
Jun 13 11:23:23 opensips01 /sbin/opensips[5174]: INFO:core:parse_headers: bad header field
Jun 13 11:23:23 opensips01 /sbin/opensips[5174]: ERROR:auth:find_credentials: failed to parse headers
Jun 13 11:23:23 opensips01 /sbin/opensips[5174]: ERROR:auth:pre_auth: failed to find credentials
Jun 13 11:23:23 opensips01 /sbin/opensips[5174]: ERROR:core:parse_content_length: parse error near char [54][6]
Jun 13 11:23:23 opensips01 /sbin/opensips[5174]: ERROR:core:get_hdr_field: bad content_length header
Jun 13 11:23:23 opensips01 /sbin/opensips[5174]: INFO:core:parse_headers: bad header field
Jun 13 11:23:23 opensips01 /sbin/opensips[5174]: ERROR:core:build_res_buf_from_sip_req: parse_headers failed
Jun 13 11:23:23 opensips01 /sbin/opensips[5174]: ERROR:sl:sl_send_reply_helper: response building failed
Jun 13 11:23:23 opensips01 /sbin/opensips[5174]: ERROR:signaling:sig_send_reply_mod: failed to send reply with sl module
Jun 13 11:23:23 opensips01 /sbin/opensips[5174]: ERROR:auth:pre_auth: failed to send 400 reply

So, what I wanted to do would be to look into the Content-Length header and in case it has two or more values change it so it becomes valid.

Something like :

if ( is_present_hf("Content-Length") )
{
        $avp(s:content-length) = $(hdr(Content-Length){s.select,1, });
       
        if ( $avp(s:content-length) != NULL )
        {
                remove_hf("Content-Length");
                append_hf("Content-Length: $avp(s:content-length)", "Allow");
        }
}


So for now my problem is where can I change the Content-Length? In main route I only change the forwarded message and in error_route I cannot access the headers.

Can anyone shed me a little light on the right direction.

Thanks for your time,
Joel Oliveira
Reply | Threaded
Open this post in threaded view
|

Re: Parsing invalid Content-Length header, try to change it to a valid one

joel.oliveira
This was the answer on the #opensips IRC channel from the user lirakis:

<lirakis>        joel_oliveira, to the best of my knowledge, opensips maintains a struct for the received sip packet that is not modifyable
<lirakis>        you can get data out of it and do transforms etc. and set headers on the outbound packet, but there is no "preprocessing" that can be done to the received packet before opensips parses it.
<joel_oliveira>        lirakis: yes, it looks that way. It seems to me that if I want to do some change to the behaviour of OpenSIPs regarding this kind of parsing in particular, I have to change the parse_content_length function itself, no?
<lirakis>        ... well .. i suppose you could - but I wouldnt do that unless it is "okay" by the RFC to have a space in the content length
<lirakis>        my guess is that it is not okay
<lirakis>        and thus - you want to reject the header or packet entirely as being malformed
<lirakis>        i just checked the rfc ABNF, space is not allowed
<joel_oliveira>        lirakis: you're right. I should just plain reject it right in the first Invite.
<lirakis>        yeah i mean if you pass it through .. its going to induce unknown behavior in whoever has to handle the packet next - so better to drop it on first detection
<joel_oliveira>        lirakis: thanks for your time. You did enlighten me
<lirakis>        np

I guess I'll drop my option since is not RFC complaint. Thanks.

Best Regards,
Joel Oliveira