| 1 |
From: Dave Cross <[email protected]> |
| 2 |
Date: Fri, 21 Aug 2026 13:41:51 +0100 |
| 3 |
Subject: Fix CVE-2026-77781 |
| 4 |
Origin: https://github.com/davorg-cpan/tie-hash-regex/commit/4239732cb76233543e2ded8ff5e0f238af152e0c |
| 5 |
Bug-Debian-Security: https://security-tracker.debian.org/tracker/CVE-2026-77781 |
| 6 |
|
| 7 |
[Salvatore Bonaccorso: Drop version bump to 2.0.0 in module] |
| 8 |
--- |
| 9 |
Changes | 6 ++ |
| 10 |
lib/Tie/Hash/Regex.pm | 20 ++++-- |
| 11 |
t/cvs-2026-77781.t | 152 ++++++++++++++++++++++++++++++++++++++++++ |
| 12 |
3 files changed, 174 insertions(+), 4 deletions(-) |
| 13 |
create mode 100644 t/cvs-2026-77781.t |
| 14 |
|
| 15 |
diff --git a/lib/Tie/Hash/Regex.pm b/lib/Tie/Hash/Regex.pm |
| 16 |
index 2b9492cfe473..faffd02356e8 100644 |
| 17 |
--- a/lib/Tie/Hash/Regex.pm |
| 18 |
+++ b/lib/Tie/Hash/Regex.pm |
| 19 |
@@ -100,7 +100,8 @@ sub FETCH { |
| 20 |
|
| 21 |
return $self->{$key} if !$is_re && exists $self->{$key}; |
| 22 |
|
| 23 |
- $key = qr/$key/ unless $is_re; |
| 24 |
+ $key = _compile($key) unless $is_re; |
| 25 |
+ return unless defined $key; |
| 26 |
|
| 27 |
# NOTE: wantarray will _never_ be true when FETCH is called |
| 28 |
# using the standard hash semantics. I've put that piece |
| 29 |
@@ -130,7 +131,8 @@ sub EXISTS { |
| 30 |
|
| 31 |
return 1 if !$is_re && exists $self->{$key}; |
| 32 |
|
| 33 |
- $key = qr/$key/ unless $is_re; |
| 34 |
+ $key = _compile($key) unless $is_re; |
| 35 |
+ return unless defined $key; |
| 36 |
|
| 37 |
/$key/ && return 1 for keys %$self; |
| 38 |
|
| 39 |
@@ -152,7 +154,8 @@ sub DELETE { |
| 40 |
|
| 41 |
return delete $self->{$key} if !$is_re && exists $self->{$key}; |
| 42 |
|
| 43 |
- $key = qr/$key/ unless $is_re; |
| 44 |
+ $key = _compile($key) unless $is_re; |
| 45 |
+ return unless defined $key; |
| 46 |
|
| 47 |
for (keys %$self) { |
| 48 |
if (/$key/) { |
| 49 |
@@ -161,6 +164,15 @@ sub DELETE { |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
+sub _compile { |
| 54 |
+ my ($key) = @_; |
| 55 |
+ |
| 56 |
+ my $re = eval { qr/$key/ }; |
| 57 |
+ |
| 58 |
+ # This will be undef if the "eval" failed |
| 59 |
+ return $re; |
| 60 |
+} |
| 61 |
+ |
| 62 |
1; |
| 63 |
__END__ |
| 64 |
|
| 65 |
diff --git a/t/cvs-2026-77781.t b/t/cvs-2026-77781.t |
| 66 |
new file mode 100644 |
| 67 |
index 000000000000..22a1bcf3b699 |
| 68 |
--- /dev/null |
| 69 |
+++ b/t/cvs-2026-77781.t |
| 70 |
@@ -0,0 +1,152 @@ |
| 71 |
+use strict; |
| 72 |
+use warnings; |
| 73 |
+ |
| 74 |
+use Test::More; |
| 75 |
+ |
| 76 |
+use Tie::Hash::Regex; |
| 77 |
+ |
| 78 |
+sub fresh { |
| 79 |
+ my %h; |
| 80 |
+ |
| 81 |
+ tie %h, 'Tie::Hash::Regex'; |
| 82 |
+ |
| 83 |
+ $h{alice} = 'record-A'; |
| 84 |
+ $h{bob} = 'record-B'; |
| 85 |
+ $h{carol} = 'record-C'; |
| 86 |
+ |
| 87 |
+ return \%h; |
| 88 |
+} |
| 89 |
+ |
| 90 |
+# |
| 91 |
+# First make sure the normal behaviour still works. |
| 92 |
+# |
| 93 |
+ |
| 94 |
+{ |
| 95 |
+ my $h = fresh(); |
| 96 |
+ |
| 97 |
+ is( |
| 98 |
+ $h->{alice}, |
| 99 |
+ 'record-A', |
| 100 |
+ 'exact lookup works', |
| 101 |
+ ); |
| 102 |
+ |
| 103 |
+ is( |
| 104 |
+ $h->{'^car'}, |
| 105 |
+ 'record-C', |
| 106 |
+ 'regex lookup works', |
| 107 |
+ ); |
| 108 |
+ |
| 109 |
+ is( |
| 110 |
+ $h->{'^zzz'}, |
| 111 |
+ undef, |
| 112 |
+ 'well-formed regex with no match returns undef', |
| 113 |
+ ); |
| 114 |
+} |
| 115 |
+ |
| 116 |
+# |
| 117 |
+# Regression tests for CVE-2026-77781. |
| 118 |
+# |
| 119 |
+# A malformed regex used as a lookup key should be treated as a |
| 120 |
+# non-match, rather than throwing an exception from FETCH, EXISTS |
| 121 |
+# or DELETE. |
| 122 |
+# |
| 123 |
+ |
| 124 |
+my @payloads = ( |
| 125 |
+ '(', |
| 126 |
+ '[', |
| 127 |
+ '*', |
| 128 |
+ '+', |
| 129 |
+ '?', |
| 130 |
+ '\\', |
| 131 |
+ '(?<', |
| 132 |
+ '(?{', |
| 133 |
+); |
| 134 |
+ |
| 135 |
+for my $payload (@payloads) { |
| 136 |
+ subtest "malformed pattern '$payload'" => sub { |
| 137 |
+ { |
| 138 |
+ my $h = fresh(); |
| 139 |
+ |
| 140 |
+ my ($value, $error); |
| 141 |
+ |
| 142 |
+ { |
| 143 |
+ local $@; |
| 144 |
+ eval { |
| 145 |
+ $value = $h->{$payload}; |
| 146 |
+ 1; |
| 147 |
+ } or $error = $@; |
| 148 |
+ } |
| 149 |
+ |
| 150 |
+ is( |
| 151 |
+ $error, |
| 152 |
+ undef, |
| 153 |
+ 'FETCH does not die', |
| 154 |
+ ); |
| 155 |
+ |
| 156 |
+ is( |
| 157 |
+ $value, |
| 158 |
+ undef, |
| 159 |
+ 'FETCH treats malformed pattern as no match', |
| 160 |
+ ); |
| 161 |
+ } |
| 162 |
+ |
| 163 |
+ { |
| 164 |
+ my $h = fresh(); |
| 165 |
+ |
| 166 |
+ my ($exists, $error); |
| 167 |
+ |
| 168 |
+ { |
| 169 |
+ local $@; |
| 170 |
+ eval { |
| 171 |
+ $exists = exists $h->{$payload}; |
| 172 |
+ 1; |
| 173 |
+ } or $error = $@; |
| 174 |
+ } |
| 175 |
+ |
| 176 |
+ is( |
| 177 |
+ $error, |
| 178 |
+ undef, |
| 179 |
+ 'EXISTS does not die', |
| 180 |
+ ); |
| 181 |
+ |
| 182 |
+ ok( |
| 183 |
+ !$exists, |
| 184 |
+ 'EXISTS treats malformed pattern as no match', |
| 185 |
+ ); |
| 186 |
+ } |
| 187 |
+ |
| 188 |
+ { |
| 189 |
+ my $h = fresh(); |
| 190 |
+ |
| 191 |
+ my ($deleted, $error); |
| 192 |
+ |
| 193 |
+ { |
| 194 |
+ local $@; |
| 195 |
+ eval { |
| 196 |
+ $deleted = delete $h->{$payload}; |
| 197 |
+ 1; |
| 198 |
+ } or $error = $@; |
| 199 |
+ } |
| 200 |
+ |
| 201 |
+ is( |
| 202 |
+ $error, |
| 203 |
+ undef, |
| 204 |
+ 'DELETE does not die', |
| 205 |
+ ); |
| 206 |
+ |
| 207 |
+ is( |
| 208 |
+ $deleted, |
| 209 |
+ undef, |
| 210 |
+ 'DELETE treats malformed pattern as no match', |
| 211 |
+ ); |
| 212 |
+ |
| 213 |
+ is_deeply( |
| 214 |
+ [sort keys %$h], |
| 215 |
+ [qw(alice bob carol)], |
| 216 |
+ 'failed DELETE leaves hash unchanged', |
| 217 |
+ ); |
| 218 |
+ } |
| 219 |
+ }; |
| 220 |
+} |
| 221 |
+ |
| 222 |
+done_testing; |
| 223 |
-- |
| 224 |
2.55.0 |
| 225 |
|