| •1 |
use strict; |
| 2 |
use warnings; |
| 3 |
|
| 4 |
use Test::More; |
| 5 |
|
| 6 |
use Tie::Hash::Regex; |
| 7 |
|
| 8 |
sub fresh { |
| 9 |
my %h; |
| 10 |
|
| 11 |
tie %h, 'Tie::Hash::Regex'; |
| 12 |
|
| 13 |
$h{alice} = 'record-A'; |
| 14 |
$h{bob} = 'record-B'; |
| 15 |
$h{carol} = 'record-C'; |
| 16 |
|
| 17 |
return \%h; |
| 18 |
} |
| 19 |
|
| 20 |
# |
| 21 |
# First make sure the normal behaviour still works. |
| 22 |
# |
| 23 |
|
| 24 |
{ |
| 25 |
my $h = fresh(); |
| 26 |
|
| 27 |
is( |
| 28 |
$h->{alice}, |
| 29 |
'record-A', |
| 30 |
'exact lookup works', |
| 31 |
); |
| 32 |
|
| 33 |
is( |
| 34 |
$h->{'^car'}, |
| 35 |
'record-C', |
| 36 |
'regex lookup works', |
| 37 |
); |
| 38 |
|
| 39 |
is( |
| 40 |
$h->{'^zzz'}, |
| 41 |
undef, |
| 42 |
'well-formed regex with no match returns undef', |
| 43 |
); |
| 44 |
} |
| 45 |
|
| 46 |
# |
| 47 |
# Regression tests for CVE-2026-77781. |
| 48 |
# |
| 49 |
# A malformed regex used as a lookup key should be treated as a |
| 50 |
# non-match, rather than throwing an exception from FETCH, EXISTS |
| 51 |
# or DELETE. |
| 52 |
# |
| 53 |
|
| 54 |
my @payloads = ( |
| 55 |
'(', |
| 56 |
'[', |
| 57 |
'*', |
| 58 |
'+', |
| 59 |
'?', |
| 60 |
'\\', |
| 61 |
'(?<', |
| 62 |
'(?{', |
| 63 |
); |
| 64 |
|
| 65 |
for my $payload (@payloads) { |
| 66 |
subtest "malformed pattern '$payload'" => sub { |
| 67 |
{ |
| 68 |
my $h = fresh(); |
| 69 |
|
| 70 |
my ($value, $error); |
| 71 |
|
| 72 |
{ |
| 73 |
local $@; |
| 74 |
eval { |
| 75 |
$value = $h->{$payload}; |
| 76 |
1; |
| 77 |
} or $error = $@; |
| 78 |
} |
| 79 |
|
| 80 |
is( |
| 81 |
$error, |
| 82 |
undef, |
| 83 |
'FETCH does not die', |
| 84 |
); |
| 85 |
|
| 86 |
is( |
| 87 |
$value, |
| 88 |
undef, |
| 89 |
'FETCH treats malformed pattern as no match', |
| 90 |
); |
| 91 |
} |
| 92 |
|
| 93 |
{ |
| 94 |
my $h = fresh(); |
| 95 |
|
| 96 |
my ($exists, $error); |
| 97 |
|
| 98 |
{ |
| 99 |
local $@; |
| 100 |
eval { |
| 101 |
$exists = exists $h->{$payload}; |
| 102 |
1; |
| 103 |
} or $error = $@; |
| 104 |
} |
| 105 |
|
| 106 |
is( |
| 107 |
$error, |
| 108 |
undef, |
| 109 |
'EXISTS does not die', |
| 110 |
); |
| 111 |
|
| 112 |
ok( |
| 113 |
!$exists, |
| 114 |
'EXISTS treats malformed pattern as no match', |
| 115 |
); |
| 116 |
} |
| 117 |
|
| 118 |
{ |
| 119 |
my $h = fresh(); |
| 120 |
|
| 121 |
my ($deleted, $error); |
| 122 |
|
| 123 |
{ |
| 124 |
local $@; |
| 125 |
eval { |
| 126 |
$deleted = delete $h->{$payload}; |
| 127 |
1; |
| 128 |
} or $error = $@; |
| 129 |
} |
| 130 |
|
| 131 |
is( |
| 132 |
$error, |
| 133 |
undef, |
| 134 |
'DELETE does not die', |
| 135 |
); |
| 136 |
|
| 137 |
is( |
| 138 |
$deleted, |
| 139 |
undef, |
| 140 |
'DELETE treats malformed pattern as no match', |
| 141 |
); |
| 142 |
|
| 143 |
is_deeply( |
| 144 |
[sort keys %$h], |
| 145 |
[qw(alice bob carol)], |
| 146 |
'failed DELETE leaves hash unchanged', |
| 147 |
); |
| 148 |
} |
| 149 |
}; |
| 150 |
} |
| 151 |
|
| 152 |
done_testing; |