From: Dave Cross Date: Fri, 21 Aug 2026 13:41:51 +0100 Subject: Fix CVE-2026-77781 Origin: https://github.com/davorg-cpan/tie-hash-regex/commit/4239732cb76233543e2ded8ff5e0f238af152e0c Bug-Debian-Security: https://security-tracker.debian.org/tracker/CVE-2026-77781 [Salvatore Bonaccorso: Drop version bump to 2.0.0 in module] --- Changes | 6 ++ lib/Tie/Hash/Regex.pm | 20 ++++-- t/cvs-2026-77781.t | 152 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 174 insertions(+), 4 deletions(-) create mode 100644 t/cvs-2026-77781.t diff --git a/lib/Tie/Hash/Regex.pm b/lib/Tie/Hash/Regex.pm index 2b9492cfe473..faffd02356e8 100644 --- a/lib/Tie/Hash/Regex.pm +++ b/lib/Tie/Hash/Regex.pm @@ -100,7 +100,8 @@ sub FETCH { return $self->{$key} if !$is_re && exists $self->{$key}; - $key = qr/$key/ unless $is_re; + $key = _compile($key) unless $is_re; + return unless defined $key; # NOTE: wantarray will _never_ be true when FETCH is called # using the standard hash semantics. I've put that piece @@ -130,7 +131,8 @@ sub EXISTS { return 1 if !$is_re && exists $self->{$key}; - $key = qr/$key/ unless $is_re; + $key = _compile($key) unless $is_re; + return unless defined $key; /$key/ && return 1 for keys %$self; @@ -152,7 +154,8 @@ sub DELETE { return delete $self->{$key} if !$is_re && exists $self->{$key}; - $key = qr/$key/ unless $is_re; + $key = _compile($key) unless $is_re; + return unless defined $key; for (keys %$self) { if (/$key/) { @@ -161,6 +164,15 @@ sub DELETE { } } +sub _compile { + my ($key) = @_; + + my $re = eval { qr/$key/ }; + + # This will be undef if the "eval" failed + return $re; +} + 1; __END__ diff --git a/t/cvs-2026-77781.t b/t/cvs-2026-77781.t new file mode 100644 index 000000000000..22a1bcf3b699 --- /dev/null +++ b/t/cvs-2026-77781.t @@ -0,0 +1,152 @@ +use strict; +use warnings; + +use Test::More; + +use Tie::Hash::Regex; + +sub fresh { + my %h; + + tie %h, 'Tie::Hash::Regex'; + + $h{alice} = 'record-A'; + $h{bob} = 'record-B'; + $h{carol} = 'record-C'; + + return \%h; +} + +# +# First make sure the normal behaviour still works. +# + +{ + my $h = fresh(); + + is( + $h->{alice}, + 'record-A', + 'exact lookup works', + ); + + is( + $h->{'^car'}, + 'record-C', + 'regex lookup works', + ); + + is( + $h->{'^zzz'}, + undef, + 'well-formed regex with no match returns undef', + ); +} + +# +# Regression tests for CVE-2026-77781. +# +# A malformed regex used as a lookup key should be treated as a +# non-match, rather than throwing an exception from FETCH, EXISTS +# or DELETE. +# + +my @payloads = ( + '(', + '[', + '*', + '+', + '?', + '\\', + '(?<', + '(?{', +); + +for my $payload (@payloads) { + subtest "malformed pattern '$payload'" => sub { + { + my $h = fresh(); + + my ($value, $error); + + { + local $@; + eval { + $value = $h->{$payload}; + 1; + } or $error = $@; + } + + is( + $error, + undef, + 'FETCH does not die', + ); + + is( + $value, + undef, + 'FETCH treats malformed pattern as no match', + ); + } + + { + my $h = fresh(); + + my ($exists, $error); + + { + local $@; + eval { + $exists = exists $h->{$payload}; + 1; + } or $error = $@; + } + + is( + $error, + undef, + 'EXISTS does not die', + ); + + ok( + !$exists, + 'EXISTS treats malformed pattern as no match', + ); + } + + { + my $h = fresh(); + + my ($deleted, $error); + + { + local $@; + eval { + $deleted = delete $h->{$payload}; + 1; + } or $error = $@; + } + + is( + $error, + undef, + 'DELETE does not die', + ); + + is( + $deleted, + undef, + 'DELETE treats malformed pattern as no match', + ); + + is_deeply( + [sort keys %$h], + [qw(alice bob carol)], + 'failed DELETE leaves hash unchanged', + ); + } + }; +} + +done_testing; -- 2.55.0