p5-net-ssleay icon indicating copy to clipboard operation
p5-net-ssleay copied to clipboard

do_httpx2 throws undef warnings on malformed headers.

Open petdance opened this issue 3 years ago • 0 comments

The function do_httpx2:

sub do_httpx2 {
    my ($page, $response, $headers, $server_cert) = &do_httpx3;
    X509_free($server_cert) if defined $server_cert;
    return ($page, $response, defined $headers ?
        map( { ($h,$v)=/^(\S+)\:\s*(.*)$/; (uc($h),$v); }
        split(/\s?\n/, $headers)
        ) : ()
        );
}

throws undef warnings if the $headers it receives are malformed. If the header doesn't match the regex in map, $h will be undef and then uc($h) will throw a warning.

Here's a test program that illustrates it:

$ cat foo.pl
use strict;
use warnings;

use Data::Dumper;

my $headers = "foo: bar\n\nfoo\nbaz:\n";
my %hash = map(
    {my ($h,$v)=/^(\S+)\:\s*(.*)$/; (uc($h),$v); }
    split(/\s?\n/, $headers)
);
print Dumper(\%hash);

Running:

$ perl foo.pl
Use of uninitialized value $h in uc at foo.pl line 8.
$VAR1 = {
          'BAZ' => '',
          '' => undef,
          'FOO' => 'bar'
        };

Also, it looks like do_httpsx4 suffers from the same problem, but I haven't tested it.

I get this error 10-80 times/day in our production web server. Counts from September 2021:

01.err:19
02.err:47
03.err:38
04.err:0
05.err:0
06.err:10
07.err:38
08.err:43
09.err:25
10.err:83
11.err:0
12.err:2
13.err:10
14.err:20
15.err:3
16.err:17
17.err:17
18.err:2
19.err:18
20.err:32

petdance avatar Sep 20 '21 18:09 petdance