From 69ae87044ca499aa401c73a9a1d764013539fb2d Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sat, 14 Dec 2019 01:02:56 +0000 Subject: address: use Email::Address::XS if available Email::Address::XS is a dependency of modern versions of Email::MIME, so it's likely loaded and installed on newer systems, already; and capable of handling more corner-cases than our pure-Perl fallback. We still fallback to the imperfect-but-good-enough-in-practice pure-Perl code while avoiding the non-XS Email::Address (which was susceptible to DoS attacks (CVE-2015-7686)). We just need to keep "git fast-import" happy. --- lib/PublicInbox/Address.pm | 44 ++++++++++++++++++-------------------------- lib/PublicInbox/AddressPP.pm | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 26 deletions(-) create mode 100644 lib/PublicInbox/AddressPP.pm (limited to 'lib/PublicInbox') diff --git a/lib/PublicInbox/Address.pm b/lib/PublicInbox/Address.pm index a58d1eff..433b36eb 100644 --- a/lib/PublicInbox/Address.pm +++ b/lib/PublicInbox/Address.pm @@ -4,35 +4,27 @@ package PublicInbox::Address; use strict; use warnings; -# very loose regexes, here. We don't need RFC-compliance, -# just enough to make thing sanely displayable and pass to git +sub xs_emails { map { $_->address() } parse_email_addresses($_[0]) } -sub emails { - ($_[0] =~ /([\w\.\+=\?"\(\)\-!#\$%&'\*\/\^\`\|\{\}~]+\@[\w\.\-\(\)]+) - (?:\s[^>]*)?>?\s*(?:\(.*?\))?(?:,\s*|\z)/gx) +sub xs_names { + map { + my $n = $_->name; + $n = $_->user if $n eq $_->address; + $n; + } parse_email_addresses($_[0]); } -sub names { - my @p = split(/]+)\@[\w\.\-]+>?\s*(\(.*?\))?(?:,\s*|\z)/, - $_[0]); - my @ret; - for (my $i = 0; $i <= $#p;) { - my $phrase = $p[$i++]; - $phrase =~ tr/\r\n\t / /s; - $phrase =~ s/\A['"\s]*//; - $phrase =~ s/['"\s]*\z//; - my $user = $p[$i++] // ''; - my $comment = $p[$i++] // ''; - if ($phrase =~ /\S/) { - $phrase =~ s/\@\S+\z//; - push @ret, $phrase; - } elsif ($comment =~ /\A\((.*?)\)\z/) { - push @ret, $1; - } else { - push @ret, $user; - } - } - @ret; +eval { + require Email::Address::XS; + Email::Address::XS->import(qw(parse_email_addresses)); + *emails = \&xs_emails; + *names = \&xs_names; +}; + +if ($@) { + require PublicInbox::AddressPP; + *emails = \&PublicInbox::AddressPP::emails; + *names = \&PublicInbox::AddressPP::names; } 1; diff --git a/lib/PublicInbox/AddressPP.pm b/lib/PublicInbox/AddressPP.pm new file mode 100644 index 00000000..cd7aedb9 --- /dev/null +++ b/lib/PublicInbox/AddressPP.pm @@ -0,0 +1,38 @@ +# Copyright (C) 2016-2019 all contributors +# License: AGPL-3.0+ +package PublicInbox::AddressPP; +use strict; + +# very loose regexes, here. We don't need RFC-compliance, +# just enough to make thing sanely displayable and pass to git +# We favor Email::Address::XS for conformance if available + +sub emails { + ($_[0] =~ /([\w\.\+=\?"\(\)\-!#\$%&'\*\/\^\`\|\{\}~]+\@[\w\.\-\(\)]+) + (?:\s[^>]*)?>?\s*(?:\(.*?\))?(?:,\s*|\z)/gx) +} + +sub names { + my @p = split(/]+)\@[\w\.\-]+>?\s*(\(.*?\))?(?:,\s*|\z)/, + $_[0]); + my @ret; + for (my $i = 0; $i <= $#p;) { + my $phrase = $p[$i++]; + $phrase =~ tr/\r\n\t / /s; + $phrase =~ s/\A['"\s]*//; + $phrase =~ s/['"\s]*\z//; + my $user = $p[$i++] // ''; + my $comment = $p[$i++] // ''; + if ($phrase =~ /\S/) { + $phrase =~ s/\@\S+\z//; + push @ret, $phrase; + } elsif ($comment =~ /\A\((.*?)\)\z/) { + push @ret, $1; + } else { + push @ret, $user; + } + } + @ret; +} + +1; -- cgit v1.2.3-24-ge0c7