blib/lib/Email/Abuse/Investigator.pm

filter: Statementstmt 98.9% 2435 / 2460 Branchbran 69.7% 737 / 1056 Conditioncond 45.7% 385 / 841 MC/DCmcdc 32.1% 146 / 454 Subroutinesub 100% 744 / 744 Podpod 100% 21 / 21 totaltotal 80.1% 4468 / 5576 CC 2120 SCAR 43.8
CC
2120
Cov
82%
form_contacts
453.0
form_contacts
453.0
form_contacts
453.0
CRAP 80.2
↑ Summary bin/submit_abuse_report »
1 package Email::Abuse::Investigator;
2  
3 24 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
4 24 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
5 24 ▶use autodie qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 24 ▶use Carp qw(croak carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
8 24 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
9 24 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
10 24 ▶BEGIN { $Sub::Private::config{mode} = 'enforce' }
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
11 24 ▶use Sub::Private;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
12 24 ▶use Sub::Protected;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
13 24 ▶use MIME::QuotedPrint qw( decode_qp );
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
14 24 ▶use MIME::Base64 qw( decode_base64 );
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
15 24 ▶use Object::Configure;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
16 24 ▶use Params::Get;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
17 24 ▶use Params::Validate::Strict 0.34;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
18 24 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
19 24 ▶use Readonly::Values::Months;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
20 24 ▶use Socket qw( inet_aton inet_ntoa AF_INET );
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
21 24 ▶use Time::Piece;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 =head1 NAME
24  
25 Email::Abuse::Investigator - Analyse spam email to identify originating hosts,
26 hosted URLs, and suspicious domains
27  
28 =head1 VERSION
29  
30 Version 0.14
31  
32 =cut
33  
34 25 our $VERSION = '0.14';
35  
36 =head1 SYNOPSIS
37  
38 use Email::Abuse::Investigator;
39  
40 my $analyser = Email::Abuse::Investigator->new( verbose => 1 );
41 $analyser->parse_email($raw_email_text);
42  
43 # Originating IP and its network owner
44 my $origin = $analyser->originating_ip();
45  
46 # All HTTP/HTTPS URLs found in the body
47 my @urls = $analyser->embedded_urls();
48  
49 # All domains extracted from mailto: links and bare addresses in the body
50 my @mdoms = $analyser->mailto_domains();
51  
52 # All domains mentioned anywhere (union of the above)
53 my @adoms = $analyser->all_domains();
54  
55 # Full printable report
56 print $analyser->report();
57  
58 =head1 DESCRIPTION
59  
60 C<Email::Abuse::Investigator> examines the raw source of a spam/phishing e-mail
61 and answers the questions manual abuse investigators ask:
62  
63 =over 4
64  
65 =item 1. Where did the message really come from?
66  
67 Walks the C<Received:> chain, skips private/trusted IPs, and identifies the
68 first external hop. Enriches with rDNS, WHOIS/RDAP org name and abuse
69 contact. Both IPv4 and IPv6 addresses are supported.
70  
71 =item 2. Who hosts the advertised web sites?
72  
73 Extracts every C<http://> and C<https://> URL from both plain-text and HTML
74 parts, resolves each hostname to an IP, and looks up the network owner.
75  
76 =item 3. Who owns the reply-to / contact domains?
77  
78 Extracts domains from C<mailto:> links, bare e-mail addresses in the body,
79 the C<From:>/C<Reply-To:>/C<Sender:>/C<Return-Path:> headers, C<DKIM-Signature: d=>
80 (the signing domain), C<List-Unsubscribe:> (the ESP or bulk-sender domain), and the
81 C<Message-ID:> domain. For each unique domain it gathers:
82  
83 =over 8
84  
85 =item * Domain registrar and registrant (WHOIS)
86  
87 =item * Web-hosting IP and network owner (A record -> RDAP)
88  
89 =item * Mail-hosting IP and network owner (MX record -> RDAP)
90  
91 =item * DNS nameserver operator (NS record -> RDAP)
92  
93 =item * Whether the domain was recently registered (potential flag)
94  
95 =back
96  
97 =back
98  
99 =cut
100  
101 # -----------------------------------------------------------------------
102 # Optional modules -- gracefully degraded when absent
103 # -----------------------------------------------------------------------
104  
105 # Net::DNS enables MX, NS, AAAA lookups; falls back to gethostbyname
106 25 my $HAS_NET_DNS;
107  
108 # LWP::UserAgent enables RDAP queries; falls back to raw WHOIS
109 my $HAS_LWP;
110 21 my $HAS_CONN_CACHE;
111  
112 # HTML::LinkExtor enables structural HTML link extraction
113 21 my $HAS_HTML_LINKEXTOR;
114  
115 # CHI enables a persistent cross-message cache for IP/domain data
116 21 my $HAS_CHI;
117  
118 # IO::Socket::IP provides dual-stack (IPv4+IPv6) socket support
119 23 my $HAS_IO_SOCKET_IP;
120  
121 # Domain::PublicSuffix enables accurate eTLD+1 normalisation
122 23 my $HAS_PUBLIC_SUFFIX;
123  
124 # AnyEvent::DNS enables parallel DNS queries
125 23 my $HAS_ANYEVENT_DNS;
126  
127 24 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
128 0 $HAS_NET_DNS = eval { require Net::DNS; 1 };
129 0 $HAS_LWP = eval { require LWP::UserAgent; 1 };
130 0 $HAS_CONN_CACHE = eval { require LWP::ConnCache; 1 };
131 0 $HAS_HTML_LINKEXTOR= eval { require HTML::LinkExtor; 1 };
132 0 $HAS_CHI = eval { require CHI; 1 };
133 0 $HAS_IO_SOCKET_IP = eval { require IO::Socket::IP; 1 };
134 0 $HAS_PUBLIC_SUFFIX = eval { require Domain::PublicSuffix; 1 };
135 0 $HAS_ANYEVENT_DNS = eval { require AnyEvent::DNS; 1 };
136 }
137  
138 # -----------------------------------------------------------------------
139 # Constants -- all magic numbers and strings live here
140 # -----------------------------------------------------------------------
141  
142 # WHOIS protocol port (IANA-assigned)
143 23 Readonly::Scalar my $WHOIS_PORT => 43;
144  
145 # Bytes to read per sysread() call from a WHOIS socket
146 23 Readonly::Scalar my $WHOIS_READ_CHUNK => 4096;
147  
148 # Maximum WHOIS response bytes stored in whois_raw (keep reports compact)
149 22 Readonly::Scalar my $WHOIS_RAW_MAX => 2048;
150  
151 # Maximum multipart nesting depth (recursion guard -- RFC 2046 has no limit
152 # but we cap it to prevent stack exhaustion on crafted messages)
153 23 Readonly::Scalar my $MAX_MULTIPART_DEPTH => 20;
154  
155 # Number of days before registration that triggers recently_registered flag
156 23 Readonly::Scalar my $RECENT_REG_DAYS => 180;
157  
158 # Number of days ahead of expiry that triggers domain_expires_soon flag
159 23 Readonly::Scalar my $EXPIRY_WARN_DAYS => 30;
160  
161 # Seconds in a day -- used in date arithmetic throughout
162 23 Readonly::Scalar my $SECS_PER_DAY => 86400;
163  
164 # Suspicious date window: dates outside +/- 7 days raise a flag
165 23 Readonly::Scalar my $DATE_SKEW_DAYS => 7;
166  
167 # Maximum positive timezone offset in minutes (+14:00 = Line Islands)
168 23 Readonly::Scalar my $TZ_MAX_POS_MINS => 840;
169  
170 # Maximum negative timezone offset in minutes (-12:00 = Baker Island)
171 23 Readonly::Scalar my $TZ_MAX_NEG_MINS => 720;
172  
173 # High-risk score threshold
174 23 Readonly::Scalar my $SCORE_HIGH => 9;
175  
176 # Medium-risk score threshold
177 23 Readonly::Scalar my $SCORE_MEDIUM => 5;
178  
179 # Low-risk score threshold
180 23 Readonly::Scalar my $SCORE_LOW => 2;
181  
182 # Flag severity weights (contribute to the numeric risk score)
183 22 Readonly::Hash my %FLAG_WEIGHT => (
184 HIGH => 3,
185 MEDIUM => 2,
186 LOW => 1,
187 INFO => 0,
188 );
189  
190 # Maximum merged-role display string length before summarisation kicks in
191 23 Readonly::Scalar my $ROLE_MAX_LEN => 80;
192  
193 # CHI cache TTL in seconds (1 hour -- IP allocations change slowly)
194 23 Readonly::Scalar my $CACHE_TTL_SECS => 3600;
195  
196 # Default constructor timeout for network operations (seconds)
197 23 Readonly::Scalar my $DEFAULT_TIMEOUT => 10;
198  
199 # Maximum role string length before truncation
200 22 Readonly::Scalar my $ROLE_WRAP_LEN => 66;
201  
202 # Maximum redirect hops to follow when resolving shortener/redirect-cloaker URLs
203 22 Readonly::Scalar my $REDIRECT_MAX_HOPS => 3;
204  
205 # Brand names checked in lookalike-domain detection.
206 # Overridable at runtime via Object::Configure.
207 22 Readonly::Array my @LOOKALIKE_BRANDS => qw(
208 paypal apple google amazon microsoft netflix ebay
209 instagram facebook twitter linkedin bankofamerica
210 wellsfargo chase barclays hsbc lloyds santander
211 );
212  
213 # -----------------------------------------------------------------------
214 # Private ranges -- IPs that are never actionable abuse targets
215 # -----------------------------------------------------------------------
216  
217 # Both IPv4 and IPv6 private/reserved ranges. Each entry is a compiled
218 # regex; _is_private() iterates over them and returns true on first match.
219 22 my @PRIVATE_RANGES = (
220 # IPv4 ranges
221 qr/^0\./, # 0.0.0.0/8 this-network (RFC 1122)
222 qr/^127\./, # 127.0.0.0/8 loopback
223 qr/^10\./, # 10.0.0.0/8 RFC 1918
224 qr/^192\.168\./, # 192.168.0.0/16 RFC 1918
225 qr/^172\.(?:1[6-9]|2\d|3[01])\./, # 172.16.0.0/12 RFC 1918
226 qr/^169\.254\./, # 169.254.0.0/16 link-local
227 qr/^100\.(?:6[4-9]|[7-9]\d|1(?:[01]\d|2[0-7]))\./, # 100.64.0.0/10 CGN (RFC 6598)
228 qr/^192\.0\.0\./, # 192.0.0.0/24 IETF protocol (RFC 6890)
229 qr/^192\.0\.2\./, # 192.0.2.0/24 TEST-NET-1 (RFC 5737)
230 qr/^198\.51\.100\./, # 198.51.100.0/24 TEST-NET-2 (RFC 5737)
231 qr/^203\.0\.113\./, # 203.0.113.0/24 TEST-NET-3 (RFC 5737)
232 qr/^255\./, # 255.0.0.0/8 broadcast
233 # IPv6 ranges
234 qr/^::1$/, # IPv6 loopback
235 qr/^fe80:/i, # IPv6 link-local (fe80::/10)
236 qr/^fc/i, # IPv6 ULA fc00::/7
237 qr/^fd/i, # IPv6 ULA fd00::/8
238 qr/^2001:db8:/i, # IPv6 documentation range (RFC 3849)
239 qr/^64:ff9b:/i, # IPv6 NAT64 well-known prefix
240 );
241  
242 # Priority-ordered patterns for extracting IPs from Received: headers.
243 # Covers bracketed IPv4, bracketed IPv6, parenthesised address, and bare dotted-quad.
244 22 my @RECEIVED_IP_RE = (
245 qr/\[\s*([\d.]+)\s*\]/, # [1.2.3.4]
246 qr/\[\s*([0-9a-fA-F:]+)\s*\]/, # [IPv6 address]
247 qr/\(\s*[\w.-]*\s*\[?\s*([\d.]+)\s*\]?\s*\)/, # (hostname [1.2.3.4])
248 qr/from\s+[\w.-]+\s+([\d.]+)/, # from hostname addr
249 qr/([\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3})/, # bare dotted-quad fallback
250 );
251  
252 # -----------------------------------------------------------------------
253 # Default configuration -- overridable via Object::Configure
254 # -----------------------------------------------------------------------
255  
256 # Object::Configure may overlay
257 # values from a file before new() uses them. Use Readonly for constants
258 # that should never be overridden at runtime.
259  
260 # -----------------------------------------------------------------------
261 # Trusted domains (infrastructure -- never report these as abuse targets)
262 # Can be overrideen at runtime by Object::Configure
263 # -----------------------------------------------------------------------
264  
265 23 my %TRUSTED_DOMAINS = map { $_ => 1 } qw(
266 gmail.com googlemail.com yahoo.com outlook.com hotmail.com
267 google.com microsoft.com apple.com amazon.com
268 googlegroups.com groups.google.com
269 fonts.googleapis.com fonts.gstatic.com
270 ajax.googleapis.com maps.googleapis.com
271 w3.org
272 fedex.com ups.com dhl.com usps.com royalmail.com
273 );
274  
275 # -----------------------------------------------------------------------
276 # URL shortener domains (real destination is hidden behind these)
277 # -----------------------------------------------------------------------
278  
279 23 my %URL_SHORTENERS = map { $_ => 1 } qw(
280 bit.ly bitly.com tinyurl.com t.co ow.ly
281 goo.gl is.gd buff.ly ift.tt dlvr.it
282 short.link rebrand.ly tiny.cc cutt.ly rb.gy
283 shorturl.at bl.ink smarturl.it yourls.org clicky.me
284 snip.ly adf.ly bc.vc lnkd.in fb.me
285 youtu.be
286 );
287  
288 # Cloud object-stores and CDN hosting paths commonly abused to serve redirect
289 # pages that hide the real phishing destination (same evasion as a URL shortener
290 # but using legitimate cloud infrastructure to pass spam filters).
291 # Exact-match hosts; suffix patterns are in @REDIRECT_HOST_SUFFIXES below.
292 22 my %REDIRECT_HOSTS = map { $_ => 1 } qw(
293 storage.googleapis.com
294 blob.core.windows.net
295 pages.dev
296 firebaseapp.com
297 web.app
298 );
299  
300 # Subdomain-suffix patterns for bucket-style hosting (e.g. mybucket.s3.amazonaws.com).
301 # Checked by _is_redirect_cloaker() using a suffix match against the bare hostname.
302 62 Readonly::Array my @REDIRECT_HOST_SUFFIXES => qw(
303 .s3.amazonaws.com
304 .s3-website.amazonaws.com
305 .cloudfront.net
306 .github.io
307 .firebaseapp.com
308 .web.app
309 .translate.goog
310 );
311  
312 # -----------------------------------------------------------------------
313 # Well-known provider abuse contacts
314 # Can be overrideen at runtime by Object::Configure
315 # -----------------------------------------------------------------------
316  
317 # Curated table of provider abuse contacts. Entries with only a 'form'
318 # key (no 'email') require web-form submission; abuse_contacts() suppresses
319 # email addresses for those providers and form_contacts() surfaces them.
320 22 my %PROVIDER_ABUSE = (
321 # Google / Gmail
322 'google.com' => { email => 'abuse@google.com', note => 'Also report Gmail accounts via https://support.google.com/mail/contact/abuse' },
323 'gmail.com' => { email => 'abuse@google.com', note => 'Report Gmail spam via https://support.google.com/mail/contact/abuse' },
324 'googlemail.com' => { email => 'abuse@google.com', note => 'Report via https://support.google.com/mail/contact/abuse' },
325 '1e100.net' => { email => 'abuse@google.com', note => 'Google infrastructure' },
326 'blogspot.com' => { email => 'abuse@google.com', note => 'Blogger/Blogspot -- report via https://support.google.com/blogger/answer/76315' },
327 'blogger.com' => { email => 'abuse@google.com', note => 'Blogger platform abuse' },
328 'sites.google.com' => { email => 'abuse@google.com', note => 'Google Sites hosted content' },
329 'storage.googleapis.com' => { email => 'google-cloud-compliance@google.com', note => 'Google Cloud Storage bucket abuse -- also report via https://support.google.com/code/go/gce_abuse_report' },
330 'gappssmtp.com' => { email => 'abuse@google.com', note => 'Google Workspace SMTP signing service -- report account abuse' },
331 'translate.goog' => { email => 'abuse@google.com', note => 'Google Translate URL proxy used to cloak phishing URLs -- report via https://support.google.com/translate/answer/4431190' },
332 'googleusercontent.com' => { email => 'abuse@google.com', note => 'Google user content hosting (Docs, Drive, Sites) -- also report via https://support.google.com/drive/answer/2408000' },
333 # Microsoft
334 'microsoft.com' => { email => 'abuse@microsoft.com', note => 'Also report via https://www.microsoft.com/en-us/wdsi/support/report-unsafe-site' },
335 'outlook.com' => { email => 'abuse@microsoft.com', note => 'Report Outlook spam: https://support.microsoft.com/en-us/office/report-phishing' },
336 'hotmail.com' => { email => 'abuse@microsoft.com', note => 'Report via https://support.microsoft.com/en-us/office/report-phishing' },
337 'live.com' => { email => 'abuse@microsoft.com', note => 'Microsoft consumer mail' },
338 'office365.com' => { email => 'abuse@microsoft.com', note => 'Microsoft 365 infrastructure' },
339 'protection.outlook.com' => { email => 'abuse@microsoft.com', note => 'Microsoft EOP gateway' },
340 # Yahoo
341 'yahoo.com' => { email => 'abuse@yahoo-inc.com', note => 'Also use https://io.help.yahoo.com/contact/index' },
342 'yahoo.co.uk' => { email => 'abuse@yahoo-inc.com', note => 'Yahoo UK' },
343 # Apple
344 'apple.com' => { email => 'reportphishing@apple.com', note => 'iCloud / Apple Mail abuse' },
345 'icloud.com' => { email => 'reportphishing@apple.com', note => 'iCloud abuse' },
346 'me.com' => { email => 'reportphishing@apple.com', note => 'Apple legacy mail' },
347 # Amazon / AWS
348 'amazon.com' => { email => 'abuse@amazonaws.com', note => 'Also https://aws.amazon.com/forms/report-abuse' },
349 'amazonaws.com' => { email => 'abuse@amazonaws.com', note => 'AWS abuse form: https://aws.amazon.com/forms/report-abuse' },
350 'amazonses.com' => { email => 'abuse@amazonaws.com', note => 'Amazon SES sending infrastructure' },
351 # Cloudflare
352 'cloudflare.com' => { email => 'abuse@cloudflare.com', note => 'Report via https://www.cloudflare.com/abuse/' },
353 # Fastly / Akamai
354 'fastly.net' => { email => 'abuse@fastly.com', note => 'Fastly CDN' },
355 'akamai.com' => { email => 'abuse@akamai.com', note => 'Akamai CDN' },
356 'akamaitechnologies.com' => { email => 'abuse@akamai.com', note => 'Akamai CDN' },
357 # Namecheap
358 'namecheap.com' => { email => 'abuse@namecheap.com', note => 'Registrar abuse' },
359 # GoDaddy -- web form only; email bounces
360 'godaddy.com' => {
361 form => 'https://supportcenter.godaddy.com/AbuseReport',
362 form_paste => 'Select the abuse type (spam, phishing, malware etc). '
363 . 'Enter the domain name in the Domain field. '
364 . 'Paste the originating IP, risk flags, and the relevant '
365 . 'Received: headers from the report below.',
366 form_upload => 'Take a screenshot of the report as a .png or .jpg, '
367 . 'or export it as a .pdf.',
368 note => 'Registrar/host -- email reports not monitored, use web form',
369 },
370 # SendGrid / Twilio
371 'sendgrid.net' => { email => 'abuse@sendgrid.com', note => 'ESP -- include full headers' },
372 'sendgrid.com' => { email => 'abuse@sendgrid.com', note => 'ESP -- include full headers' },
373 # Mailchimp / Mandrill
374 'mailchimp.com' => { email => 'abuse@mailchimp.com', note => 'ESP abuse' },
375 'mandrillapp.com' => { email => 'abuse@mailchimp.com', note => 'Mandrill transactional ESP' },
376 # OVH
377 'ovh.net' => { email => 'abuse@ovh.net', note => 'OVH hosting' },
378 'ovh.com' => { email => 'abuse@ovh.com', note => 'OVH hosting' },
379 # Hetzner
380 'hetzner.com' => { email => 'abuse@hetzner.com', note => 'Hetzner hosting' },
381 # Digital Ocean
382 'digitalocean.com' => { email => 'abuse@digitalocean.com',note => 'DO abuse form: https://www.digitalocean.com/company/contact/#abuse' },
383 # Linode / Akamai
384 'linode.com' => { email => 'abuse@linode.com', note => 'Linode/Akamai Cloud' },
385 # Constant Contact
386 'constantcontact.com' => { email => 'abuse@constantcontact.com', note => 'ESP abuse' },
387 'r.constantcontact.com' => { email => 'abuse@constantcontact.com', note => 'Constant Contact sending infrastructure' },
388 # HubSpot
389 'hubspot.com' => { email => 'abuse@hubspot.com', note => 'ESP abuse' },
390 'hs-analytics.net' => { email => 'abuse@hubspot.com', note => 'HubSpot analytics infrastructure' },
391 # Campaign Monitor
392 'createsend.com' => { email => 'abuse@campaignmonitor.com', note => 'Campaign Monitor ESP' },
393 'cmail20.com' => { email => 'abuse@campaignmonitor.com', note => 'Campaign Monitor sending infrastructure' },
394 # Klaviyo
395 'klaviyo.com' => { email => 'abuse@klaviyo.com', note => 'ESP abuse' },
396 # Brevo (formerly Sendinblue)
397 'sendinblue.com' => { email => 'abuse@sendinblue.com', note => 'ESP abuse' },
398 'brevo.com' => { email => 'abuse@brevo.com', note => 'ESP abuse' },
399 # Mailgun
400 'mailgun.com' => { email => 'abuse@mailgun.com', note => 'ESP abuse' },
401 'mailgun.org' => { email => 'abuse@mailgun.com', note => 'Mailgun sending infrastructure' },
402 # Postmark
403 'postmarkapp.com' => { email => 'abuse@postmarkapp.com', note => 'ESP abuse' },
404 # WordPress.com
405 'wordpress.com' => { email => 'abuse@wordpress.com', note => 'WordPress.com hosted blog -- report via https://en.wordpress.com/abuse/' },
406 'wp.com' => { email => 'abuse@wordpress.com', note => 'WordPress.com short domain' },
407 # Substack
408 'substack.com' => { email => 'abuse@substack.com', note => 'Substack newsletter platform abuse' },
409 # ActiveCampaign
410 'activecampaign.com' => { email => 'abuse@activecampaign.com', note => 'ActiveCampaign ESP' },
411 'ac-tinker.com' => { email => 'abuse@activecampaign.com', note => 'ActiveCampaign tracking infrastructure' },
412 # Salesforce Marketing Cloud
413 'salesforce.com' => { email => 'abuse@salesforce.com', note => 'Salesforce Marketing Cloud / ExactTarget ESP' },
414 'mc.salesforce.com' => { email => 'abuse@salesforce.com', note => 'Salesforce Marketing Cloud sending infrastructure' },
415 'exacttarget.com' => { email => 'abuse@salesforce.com', note => 'ExactTarget / Salesforce Marketing Cloud ESP' },
416 'et.exacttarget.com' => { email => 'abuse@salesforce.com', note => 'ExactTarget sending infrastructure' },
417 # Vultr
418 'vultr.com' => { email => 'abuse@vultr.com', note => 'Vultr hosting' },
419 # Contabo
420 'contabo.com' => { email => 'abuse@contabo.com', note => 'Contabo hosting' },
421 # Leaseweb
422 'leaseweb.com' => { email => 'abuse@leaseweb.com', note => 'Leaseweb hosting' },
423 # M247
424 'm247.com' => { email => 'abuse@m247.com', note => 'M247 hosting' },
425 # MarkMonitor -- web form only
426 'markmonitor.com' => {
427 form => 'https://corp.markmonitor.com/domain/ui/abuse-report',
428 form_paste => 'Complete all fields including the domain name and your '
429 . 'description of the abuse. Paste the originating IP, '
430 . 'risk flags, and the relevant Received: headers from the '
431 . 'report below.',
432 form_upload => 'Take a screenshot of the report as a .png or .jpg, or export it as a .pdf. MarkMonitor does not accept .eml files.',
433 note => 'Brand-protection registrar -- email reports not processed',
434 },
435 # URL shortener operators
436 'is.gd' => { email => 'abuse@is.gd', note => 'URL shortener -- report via https://is.gd/contact.php' },
437 'bitly.com' => { email => 'abuse@bitly.com', note => 'URL shortener abuse' },
438 'bit.ly' => { email => 'abuse@bitly.com', note => 'URL shortener abuse' },
439 'tinyurl.com' => { email => 'abuse@tinyurl.com', note => 'URL shortener abuse' },
440 'ow.ly' => { email => 'abuse@hootsuite.com', note => 'Hootsuite URL shortener' },
441 'buff.ly' => { email => 'abuse@buffer.com', note => 'Buffer URL shortener' },
442 'rb.gy' => { email => 'abuse@rb.gy', note => 'URL shortener abuse' },
443 'cutt.ly' => { email => 'abuse@cutt.ly', note => 'URL shortener abuse' },
444 'shorturl.at' => { email => 'abuse@shorturl.at', note => 'URL shortener abuse' },
445 # Dynadot -- web form only
446 'dynadot.com' => {
447 form => 'https://www.dynadot.com/report-abuse',
448 form_paste => 'Complete all fields including the domain name and your '
449 . 'description of the abuse. Paste the originating IP, '
450 . 'risk flags, and the relevant Received: headers from the '
451 . 'report below.',
452 form_upload => 'Take a screenshot of the report as a .png or .jpg, '
453 . 'or export it as a .pdf.',
454 note => 'Registrar -- email reports not monitored, use web form',
455 },
456 # Global Domain Group -- web form only
457 'globaldomaingroup.com' => {
458 form => 'https://globaldomaingroup.com/report-abuse',
459 form_paste => 'Complete all fields including the domain name and your '
460 . 'description of the abuse. Paste the originating IP, '
461 . 'risk flags, and the relevant Received: headers from the '
462 . 'report below.',
463 form_upload => 'Attach the original spam message as an .eml file.',
464 note => 'Registrar -- email reports explicitly not accepted',
465 },
466 # TPG / Internode (Australia)
467 'tpgi.com.au' => { email => 'abuse@tpg.com.au', note => 'TPG Telecom Australia' },
468 'tpg.com.au' => { email => 'abuse@tpg.com.au', note => 'TPG Telecom Australia' },
469 'internode.on.net' => { email => 'abuse@internode.on.net',note => 'Internode Australia' },
470 );
471  
472 # -----------------------------------------------------------------------
473 # Constructor
474 # -----------------------------------------------------------------------
475  
476 =head1 METHODS
477  
478 =head2 new( %options )
479  
480 Constructs and returns a new C<Email::Abuse::Investigator> analyser object. The
481 object is stateless until C<parse_email()> is called; all analysis results
482 are stored on the object and retrieved via the public accessor methods
483 documented below.
484  
485 A single object may be reused for multiple emails by calling C<parse_email()>
486 again: all per-message cached state from the previous message is discarded
487 automatically. Cross-message IP and domain lookup results are retained
488 in a shared CHI cache (if C<CHI> is installed) to avoid redundant network
489 queries across messages processed in the same process.
490  
491 =head3 Usage
492  
493 # Minimal -- all options take safe defaults
494 my $analyser = Email::Abuse::Investigator->new();
495  
496 # With options
497 my $analyser = Email::Abuse::Investigator->new(
498 timeout => 15,
499 trusted_relays => ['203.0.113.0/24', '10.0.0.0/8'],
500 verbose => 0,
501 );
502  
503 $analyser->parse_email($raw_rfc2822_text);
504 my $origin = $analyser->originating_ip();
505 my @urls = $analyser->embedded_urls();
506 my @domains = $analyser->mailto_domains();
507 my $risk = $analyser->risk_assessment();
508 my @contacts = $analyser->abuse_contacts();
509 print $analyser->report();
510  
511 =head3 Arguments
512  
513 All arguments are optional named parameters passed as a flat key-value list.
514  
515 =over 4
516  
517 =item C<timeout> (integer, default 10)
518  
519 Maximum seconds to wait for any single network operation. Set to 0 to
520 disable timeouts (not recommended for production use).
521  
522 =item C<trusted_relays> (arrayref of strings, default [])
523  
524 IP addresses or CIDR blocks to skip during Received: chain analysis.
525 Each element may be an exact IPv4 address (C<'192.0.2.1'>) or a CIDR
526 block (C<'192.0.2.0/24'>).
527  
528 =item C<verbose> (boolean, default 0)
529  
530 When true, diagnostic messages are written to STDERR.
531  
532 =back
533  
534 =head3 Returns
535  
536 A blessed C<Email::Abuse::Investigator> object. No network I/O is performed
537 during construction.
538  
539 =head3 Side Effects
540  
541 If C<CHI> is installed, a shared in-memory cache is initialised (or
542 re-used if a cache was already created by a prior call to C<new()>).
543 This cache persists for the lifetime of the process.
544  
545 =head3 Notes
546  
547 =over 4
548  
549 =item *
550  
551 Unknown option keys are silently ignored.
552  
553 =item *
554  
555 The object is not thread-safe. Use a separate object per thread.
556  
557 =item *
558  
559 WHOIS read timeouts use C<IO::Select> rather than C<alarm()>, so they
560 work correctly on Windows and in threaded Perl interpreters.
561  
562 =back
563  
564 =head3 API Specification
565  
566 =head4 Input
567  
568 {
569 timeout => {
570 type => 'integer',
571 optional => 1,
572 min => 0,
573 default => 10,
574 },
575 trusted_relays => {
576 type => 'arrayref',
577 element_type => 'string',
578 optional => 1,
579 default => [],
580 },
581 verbose => {
582 type => 'boolean',
583 optional => 1,
584 default => 0,
585 },
586 }
587  
588 =head4 Output
589  
590 {
591 type => 'object',
592 isa => 'Email::Abuse::Investigator',
593 }
594  
595 =cut
596  
597 # Class-level cross-message CHI cache (shared across all instances).
598 # Populated lazily on first call to new() when CHI is available.
599 72 my $_cache;
600  
601 829 ▶sub new {
Subroutine new: called CC=4 SCAR=15.5
Subroutine new: called CC=4 SCAR=15.5
Subroutine new: called CC=4 SCAR=15.5
Subroutine new: called CC=4 SCAR=15.5
Pod documented
Pod documented
Pod documented
Pod documented
602 829 my $class = shift;
603  
604 # Accept hash or hashref arguments uniformly
605 829 ▶ my $params = Params::Validate::Strict::validate_strict({
Condition22%2/9
Params::Get::get_params(undef, \@_) || {}
l!l
30796
Params::Get::get_params(undef, \@_) || {}
l!l
000
Params::Get::get_params(undef, \@_) || {}
l!l
00
Params::Get::get_params(undef, \@_) || {}
l!l
00
MC/DC100%1/1
Params::Get::get_params(undef, \@_) || {}
Params::Get::get_params(undef, \@_)
MC/DC0%0/1
Params::Get::get_params(undef, \@_) || {}
Params::Get::get_params(undef, \@_)
MC/DC0%0/1
Params::Get::get_params(undef, \@_) || {}
Params::Get::get_params(undef, \@_)
MC/DC0%0/1
Params::Get::get_params(undef, \@_) || {}
Params::Get::get_params(undef, \@_)
Truth table100%2/2
A
A= Params::Get::get_params(undef, \@_)
Aresult
11
00
Truth table0%0/2
A
A= Params::Get::get_params(undef, \@_)
Aresult
11
00
Truth table0%0/2
A
A= Params::Get::get_params(undef, \@_)
Aresult
11
00
Truth table0%0/2
A
A= Params::Get::get_params(undef, \@_)
Aresult
11
00
606 args => Params::Get::get_params(undef, \@_) || {},
607 schema => {
608 timeout => {
609 'type' => 'integer',
610 'optional' => 1,
611 'min' => 0,
612 },
613 trusted_relays => {
614 'type' => 'arrayref',
615 'element_type' => 'string',
616 'optional' => 1,
617 },
618 verbose => {
619 'type' => 'boolean',
620 'optional' => 1,
621 },
622 },
623 });
624  
625 # Merge in any file-based configuration via Object::Configure
626 828 $params = Object::Configure::configure($class, $params);
627  
628 # Initialise the cross-message CHI cache on first construction
629 828 ▶ if ($HAS_CHI && !$_cache) {
Branch: 4 branches
BranchTrueFalseTotal
if ($HAS_CHI and not $_cache) 13 812 825
if ($HAS_CHI and not $_cache) 1 0 1
if ($HAS_CHI and not $_cache) 1 0 1
if ($HAS_CHI and not $_cache) 0 1 1
Condition16%2/12
$HAS_CHI and not $_cache
!ll&&!rl&&r
081213
$HAS_CHI and not $_cache
!ll&&!rl&&r
000
$HAS_CHI and not $_cache
!ll&&!rl&&r
000
$HAS_CHI and not $_cache
!ll&&!rl&&r
000
MC/DC50%1/2
$HAS_CHI and not $_cache
$HAS_CHI not $_cache
MC/DC0%0/2
$HAS_CHI and not $_cache
$HAS_CHI not $_cache
MC/DC0%0/2
$HAS_CHI and not $_cache
$HAS_CHI not $_cache
MC/DC0%0/2
$HAS_CHI and not $_cache
$HAS_CHI not $_cache
Truth table66%2/3
A and not(B)
A= $HAS_CHIB= not $_cache
ABresult
0X0
100
111
Truth table0%0/3
A and not(B)
A= $HAS_CHIB= not $_cache
ABresult
0X0
100
111
Truth table0%0/3
A and not(B)
A= $HAS_CHIB= not $_cache
ABresult
0X0
100
111
Truth table0%0/3
A and not(B)
A= $HAS_CHIB= not $_cache
ABresult
0X0
100
111
630 16 $_cache = CHI->new(
631 driver => 'Memory',
632 global => 1,
633 expires_in => $CACHE_TTL_SECS,
634 );
635 }
636  
637 # Build and bless the object with default slot values
638 return bless {
639 timeout => $DEFAULT_TIMEOUT,
640 trusted_relays => [],
641 verbose => 0,
642 _raw => '',
643 _headers => [],
644 _body_plain => '',
645 _body_html => '',
646 _received => [],
647 _origin => undef,
648 _urls => undef, # lazy-computed by embedded_urls()
649 _mailto_domains=> undef, # lazy-computed by mailto_domains()
650 _contacts => undef, # lazy-computed by abuse_contacts()
651 _domain_info => {}, # per-message domain analysis cache
652 _sending_sw => [], # X-Mailer / X-PHP-Originating-Script etc.
653 _rcvd_tracking => [], # per-hop tracking IDs from Received: headers
654 828 %{$params}, # Overlay Object::Configure and caller-supplied values
655 }, $class;
656 }
657  
658 # -----------------------------------------------------------------------
659 # Public: parse
660 # -----------------------------------------------------------------------
661  
662 =head2 parse_email( $text )
663  
664 Feeds a raw RFC 2822 email message to the analyser and prepares it for
665 subsequent interrogation. This is the only method that must be called
666 before any other public method.
667  
668 If the same object is used for a second message, calling C<parse_email()>
669 again completely replaces all per-message state from the first message.
670 The cross-message CHI cache is B<not> flushed; IP and domain lookups
671 cached from prior messages are retained.
672  
673 =head3 Usage
674  
675 my $raw = do { local $/; <STDIN> };
676 $analyser->parse_email($raw);
677  
678 # Scalar reference (avoids copying large messages)
679 $analyser->parse_email(\$raw);
680  
681 # Chained
682 my $analyser = Email::Abuse::Investigator->new()->parse_email($raw);
683  
684 =head3 Arguments
685  
686 =over 4
687  
688 =item C<$text> (string or string reference, required)
689  
690 Complete raw RFC 2822 email message, including all headers and the body.
691 Both LF-only and CRLF line endings are accepted.
692  
693 =back
694  
695 =head3 Returns
696  
697 The object itself (C<$self>), enabling method chaining.
698  
699 =head3 Side Effects
700  
701 Parses headers, decodes the body (quoted-printable, base64, multipart),
702 extracts sending-software fingerprints, and populates per-hop tracking
703 data. All previously computed lazy results are discarded.
704  
705 =head3 Notes
706  
707 =over 4
708  
709 =item *
710  
711 If C<$text> is empty or contains no header/body separator, all public
712 methods will return empty/safe values.
713  
714 =item *
715  
716 Decoding errors in base64 or quoted-printable payloads are silenced; raw
717 bytes are used in place of correct output to prevent exceptions.
718  
719 =back
720  
721 =head3 API Specification
722  
723 =head4 Input
724  
725 [
726 {
727 type => [ 'string', 'stringref' ]
728 },
729 ]
730  
731 =head4 Output
732  
733 {
734 type => 'object',
735 isa => 'Email::Abuse::Investigator',
736 }
737  
738 =cut
739  
740 # TODO: Allow a Mail::Message object to be passed in
741 728 ▶sub parse_email {
Subroutine parse_email: called CC=7 SCAR=20.1
Subroutine parse_email: called CC=7 SCAR=20.1
Subroutine parse_email: called CC=7 SCAR=20.1
Pod documented
Pod documented
Pod documented
742 728 my $self = shift;
743  
744 # Accept both positional string and named 'text' argument
745 728 my $args = Params::Get::get_params('text', \@_);
746 727 my $text = $args->{text};
747  
748 # Dereference a scalar-ref in a single clean pass
749 726 ▶ $text = $$text if ref($text) eq 'SCALAR';
Branch: 3 branches
BranchTrueFalseTotal
if ref $text eq "SCALAR" 2 721 723
if ref $text eq "SCALAR" 1 0 1
if ref $text eq "SCALAR" 0 1 1
750  
751 # Any other reference type is a programming error
752 726 ▶ Carp::croak(__PACKAGE__ . ': parse_email() requires a string or scalar reference') if ref($text);
Branch: 3 branches
BranchTrueFalseTotal
if ref $text 5 718 723
if ref $text 1 0 1
if ref $text 0 0 0
753  
754 # Sanitise: strip control characters that could affect terminal output.
755 # Keep \t (tabs in headers), \n (line endings), \r (CRLF mail format).
756 723 ▶ $text =~ s/[^\x09\x0A\x0D\x20-\x7E\x80-\xFF]//g if defined $text;
Branch: 3 branches
BranchTrueFalseTotal
if defined $text 717 2 719
if defined $text 1 0 1
if defined $text 0 0 0
757  
758 # Store the sanitised raw text for later reproduction in reports
759 723 ▶ $self->{_raw} = $text // '';
Condition33%2/6
$text // ""
l!l
7164
$text // ""
l!l
00
$text // ""
l!l
00
MC/DC100%1/1
$text // ""
$text
MC/DC0%0/1
$text // ""
$text
MC/DC0%0/1
$text // ""
$text
Truth table100%2/2
A
A= $text
Aresult
11
00
Truth table0%0/2
A
A= $text
Aresult
11
00
Truth table0%0/2
A
A= $text
Aresult
11
00
760  
761 # Invalidate all per-message lazy caches
762 723 $self->{_origin} = undef;
763 723 $self->{_urls} = undef;
764 723 $self->{_mailto_domains} = undef;
765 723 $self->{_contacts} = undef;
766 723 $self->{_domain_info} = {};
767 723 $self->{_risk} = undef;
768 723 $self->{_auth_results} = undef;
769 722 $self->{_sending_sw} = [];
770 722 $self->{_rcvd_tracking} = [];
771  
772 # Perform synchronous header/body parsing (no network I/O)
773 722 ▶ $self->_split_message($text) if defined $text && $text =~ /\S/;
Branch: 3 branches
BranchTrueFalseTotal
if defined $text and $text =~ /\S/ 713 6 719
if defined $text and $text =~ /\S/ 0 1 1
if defined $text and $text =~ /\S/ 1 0 1
Condition44%4/9
defined $text and $text =~ /\S/
!ll&&!rl&&r
24714
defined $text and $text =~ /\S/
!ll&&!rl&&r
000
defined $text and $text =~ /\S/
!ll&&!rl&&r
100
MC/DC100%2/2
defined $text and $text =~ /\S/
defined $text $text =~ /\S/
MC/DC0%0/2
defined $text and $text =~ /\S/
defined $text $text =~ /\S/
MC/DC0%0/2
defined $text and $text =~ /\S/
defined $text $text =~ /\S/
Truth table100%3/3
A and B
A= defined $textB= $text =~ /\S/
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $textB= $text =~ /\S/
ABresult
0X0
100
111
Truth table33%1/3
A and B
A= defined $textB= $text =~ /\S/
ABresult
0X0
100
111
774 0 return $self;
775 }
776  
777 # -----------------------------------------------------------------------
778 # Public: originating host
779 # -----------------------------------------------------------------------
780  
781 =head2 originating_ip()
782  
783 Identifies the IP address of the machine that originally injected the
784 message into the mail system by walking the C<Received:> chain, skipping
785 private/trusted hops, and enriching the first external hop with rDNS,
786 WHOIS/RDAP organisation name, abuse contact, and country code.
787  
788 Both IPv4 and IPv6 addresses are extracted and evaluated.
789  
790 The result is cached; subsequent calls return the same hashref without
791 repeating network I/O.
792  
793 =head3 Usage
794  
795 my $orig = $analyser->originating_ip();
796 if (defined $orig) {
797 printf "Origin: %s (%s)\n", $orig->{ip}, $orig->{rdns};
798 printf "Owner: %s\n", $orig->{org};
799 }
800  
801 =head3 Arguments
802  
803 None. C<parse_email()> must have been called first.
804  
805 =head3 Returns
806  
807 A hashref with keys C<ip>, C<rdns>, C<org>, C<abuse>, C<confidence>,
808 C<note>, and C<country> (may be undef). Returns C<undef> if no suitable
809 originating IP can be determined.
810  
811 =head3 Side Effects
812  
813 On first call: one PTR lookup and one RDAP/WHOIS query. Results are cached
814 in the object and in the cross-message CHI cache (if available).
815  
816 =head3 Notes
817  
818 Only the first (oldest) external IP in the chain is reported. See
819 C<received_trail()> for the full chain.
820  
821 =head3 API Specification
822  
823 =head4 Input
824  
825 []
826  
827 =head4 Output
828  
829 {
830 type => [ 'hashref', 'undef' ],
831 keys => {
832 ip => { type => 'string', regex => qr/[\d.:a-fA-F]/ },
833 rdns => { type => 'string' },
834 org => { type => 'string' },
835 abuse => { type => 'string' },
836 confidence => { type => 'string', memberof => [ 'high', 'medium', 'low' ] },
837 note => { type => 'string' },
838 country => { type => 'string', optional => 1 },
839 },
840 }
841  
842 =cut
843  
844 890 ▶sub originating_ip {
Subroutine originating_ip: called CC=2 SCAR=7.2
Subroutine originating_ip: called CC=2 SCAR=7.2
Pod documented
Pod documented
845 889 my $self = $_[0];
846  
847 # Return the cached result if we already have it
848 889 ▶ $self->{_origin} //= $self->_find_origin();
Condition50%3/6
$self->{"_origin"} //= $self->_find_origin
l!l&&r!l&&!r
461136290
$self->{"_origin"} //= $self->_find_origin
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"_origin"} //= $self->_find_origin
$self->{"_origin"} $self->_find_origin
MC/DC0%0/2
$self->{"_origin"} //= $self->_find_origin
$self->{"_origin"} $self->_find_origin
Truth table0%0/3
A //= B
A= $self->{"_origin"}B= $self->_find_origin
ABresult
1X1
011
000
Truth table0%0/3
A //= B
A= $self->{"_origin"}B= $self->_find_origin
ABresult
1X1
011
000
849 889 return $self->{_origin};
850 }
851  
852 # -----------------------------------------------------------------------
853 # Public: HTTP/HTTPS URLs
854 # -----------------------------------------------------------------------
855  
856 =head2 embedded_urls()
857  
858 Extracts every HTTP and HTTPS URL from the message body and enriches each
859 one with the hosting IP address, network organisation name, abuse contact,
860 and country code. Both IPv4 and IPv6 host addresses are supported.
861  
862 URL extraction runs across both plain-text and HTML body parts. DNS
863 lookups for each unique hostname are optionally parallelised via
864 C<AnyEvent::DNS> if that module is installed.
865  
866 The result is cached; subsequent calls return the same list without
867 repeating network I/O.
868  
869 =head3 Usage
870  
871 my @urls = $analyser->embedded_urls();
872 for my $u (@urls) {
873 printf "URL: %s host: %s org: %s\n",
874 $u->{url}, $u->{host}, $u->{org};
875 }
876  
877 =head3 Arguments
878  
879 None. C<parse_email()> must have been called first.
880  
881 =head3 Returns
882  
883 A list of hashrefs, one per unique URL, in first-seen order. Returns an
884 empty list if no HTTP/HTTPS URLs are present. Each hashref has keys
885 C<url>, C<host>, C<ip>, C<org>, C<abuse>, C<country>.
886  
887 =head3 Side Effects
888  
889 Per unique hostname: one A/AAAA lookup and one RDAP/WHOIS query. Results
890 are cached in the object and in the cross-message CHI cache.
891  
892 =head3 Notes
893  
894 Only C<http://> and C<https://> URLs are extracted. URL shortener hosts
895 are included in the returned list (they are flagged by C<risk_assessment()>).
896  
897 When L<LWP::UserAgent> is available, URLs whose host is a known URL shortener
898 or cloud-storage redirect cloaker (Google Cloud Storage C<storage.googleapis.com>,
899 Azure Blob Storage C<blob.core.windows.net>, Cloudflare Pages C<pages.dev>,
900 S3 buckets, CloudFront distributions, etc.) are automatically resolved by
901 following HTTP 3xx redirects and HTML C<meta http-equiv="refresh"> or
902 C<window.location> patterns up to C<$REDIRECT_MAX_HOPS> hops. The resolved
903 destination URL is added to the returned list alongside the original, so abuse
904 contacts for the real phishing target are always reported even when the email
905 body contains only an object-store redirect URL.
906  
907 =head3 API Specification
908  
909 =head4 Input
910  
911 []
912  
913 =head4 Output
914  
915 (
916 {
917 type => 'hashref',
918 keys => {
919 url => { type => 'string', regex => qr{^https?://}i },
920 host => { type => 'string' },
921 ip => { type => 'string' },
922 org => { type => 'string' },
923 abuse => { type => 'string' },
924 country => { type => 'string', optional => 1 },
925 },
926 },
927 ...
928 )
929  
930 =cut
931  
932 994 ▶sub embedded_urls {
Subroutine embedded_urls: called CC=2 SCAR=7.4
Subroutine embedded_urls: called CC=2 SCAR=7.4
Subroutine embedded_urls: called CC=2 SCAR=7.4
Pod documented
933 995 my $self = $_[0];
934  
935 995 ▶ $self->{_urls} //= $self->_extract_and_resolve_urls();
Condition33%3/9
$self->{"_urls"} //= $self->_extract_and_resolve_urls
l!l&&r!l&&!r
7612284
$self->{"_urls"} //= $self->_extract_and_resolve_urls
l!l&&r!l&&!r
000
$self->{"_urls"} //= $self->_extract_and_resolve_urls
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"_urls"} //= $self->_extract_and_resolve_urls
$self->{"_urls"} $self->_extract_and_resolve_urls
MC/DC0%0/2
$self->{"_urls"} //= $self->_extract_and_resolve_urls
$self->{"_urls"} $self->_extract_and_resolve_urls
MC/DC0%0/2
$self->{"_urls"} //= $self->_extract_and_resolve_urls
$self->{"_urls"} $self->_extract_and_resolve_urls
Truth table0%0/3
A //= B
A= $self->{"_urls"}B= $self->_extract_and_resolve_urls
ABresult
1X1
011
000
Truth table0%0/3
A //= B
A= $self->{"_urls"}B= $self->_extract_and_resolve_urls
ABresult
1X1
011
000
Truth table0%0/3
A //= B
A= $self->{"_urls"}B= $self->_extract_and_resolve_urls
ABresult
1X1
011
000
936 995 return @{ $self->{_urls} };
937 }
938  
939 # -----------------------------------------------------------------------
940 # Public: mailto / reply-to / from domains
941 # -----------------------------------------------------------------------
942  
943 =head2 mailto_domains()
944  
945 Identifies every domain associated with the message as a contact, reply,
946 or delivery address, then runs a full intelligence pipeline on each one
947 (A record, MX, NS, WHOIS) to determine hosting and registration details.
948  
949 The result is cached; subsequent calls return the same list without
950 repeating network I/O.
951  
952 =head3 Usage
953  
954 my @domains = $analyser->mailto_domains();
955 for my $d (@domains) {
956 printf "Domain: %s registrar: %s\n",
957 $d->{domain}, $d->{registrar} // 'unknown';
958 }
959  
960 =head3 Arguments
961  
962 None. C<parse_email()> must have been called first.
963  
964 =head3 Returns
965  
966 A list of hashrefs, one per unique domain. See the main POD for the full
967 set of possible keys. Returns an empty list if no qualifying domains are
968 found.
969  
970 =head3 Side Effects
971  
972 Per unique domain: up to three A lookups, one MX lookup, one NS lookup,
973 and two WHOIS queries. Results are cached in the object and in the
974 cross-message CHI cache.
975  
976 =head3 Notes
977  
978 MX and NS lookups require C<Net::DNS>. Without it those keys are absent
979 from every returned hashref.
980  
981 =head3 API Specification
982  
983 =head4 Input
984  
985 []
986  
987 =head4 Output
988  
989 (
990 {
991 type => 'hashref',
992 keys => {
993 domain => { type => 'string' },
994 source => { type => 'string' },
995 # All other keys optional -- see main POD
996 },
997 },
998 ...
999 )
1000  
1001 =cut
1002  
1003 909 ▶sub mailto_domains {
Subroutine mailto_domains: called CC=2 SCAR=7.1
Subroutine mailto_domains: called CC=2 SCAR=7.1
Pod documented
1004 910 my $self = $_[0];
1005  
1006 910 ▶ $self->{_mailto_domains} //= $self->_extract_and_analyse_domains();
Condition50%3/6
$self->{"_mailto_domains"} //= $self->_extract_and_analyse_domains
l!l&&r!l&&!r
6892170
$self->{"_mailto_domains"} //= $self->_extract_and_analyse_domains
l!l&&r!l&&!r
100
MC/DC0%0/2
$self->{"_mailto_domains"} //= $self->_extract_and_analyse_domains
$self->{"_mailto_domains"} $self->_extract_and_analyse_domains
MC/DC0%0/2
$self->{"_mailto_domains"} //= $self->_extract_and_analyse_domains
$self->{"_mailto_domains"} $self->_extract_and_analyse_domains
Truth table0%0/3
A //= B
A= $self->{"_mailto_domains"}B= $self->_extract_and_analyse_domains
ABresult
1X1
011
000
Truth table0%0/3
A //= B
A= $self->{"_mailto_domains"}B= $self->_extract_and_analyse_domains
ABresult
1X1
011
000
1007 913 return @{ $self->{_mailto_domains} };
1008 }
1009  
1010 =head2 all_domains()
1011  
1012 Returns the deduplicated union of every registrable domain seen anywhere
1013 in the message -- URL hosts from C<embedded_urls()> and contact domains
1014 from C<mailto_domains()> -- normalised to eTLD+1 form.
1015  
1016 Triggers C<embedded_urls()> and C<mailto_domains()> lazily.
1017  
1018 =head3 Usage
1019  
1020 my @domains = $analyser->all_domains();
1021 print "$_\n" for @domains;
1022  
1023 =head3 Arguments
1024  
1025 None.
1026  
1027 =head3 Returns
1028  
1029 A list of plain strings (registrable domain names), lower-cased, no
1030 duplicates, in first-seen order.
1031  
1032 =head3 Side Effects
1033  
1034 Triggers C<embedded_urls()> and C<mailto_domains()> if not already cached.
1035  
1036 =head3 Notes
1037  
1038 Normalisation to eTLD+1 uses C<Domain::PublicSuffix> if installed, falling
1039 back to a built-in heuristic otherwise.
1040  
1041 =head3 API Specification
1042  
1043 =head4 Input
1044  
1045 []
1046  
1047 =head4 Output
1048  
1049 (
1050 { type => 'string', regex => qr/^[a-z0-9][a-z0-9.-]*\.[a-z]{2,}$/ },
1051 ...
1052 )
1053  
1054 =cut
1055  
1056 41 ▶sub all_domains {
Subroutine all_domains: called CC=8 SCAR=23.1
Subroutine all_domains: called CC=8 SCAR=23.1
Pod documented
1057 42 my $self = $_[0];
1058 38 my (%seen, @out);
1059  
1060 # Collect registrable domains from URL hosts first
1061 0 for my $u ($self->embedded_urls()) {
1062 31 my $dom = _registrable($u->{host});
1063 32 ▶ push @out, $dom if $dom && !$seen{$dom}++;
Branch: 2 branches
BranchTrueFalseTotal
if $dom and not $seen{$dom}++ 16 4 20
if $dom and not $seen{$dom}++ 1 0 1
Condition50%3/6
$dom and not $seen{$dom}++
!ll&&!rl&&r
10418
$dom and not $seen{$dom}++
!ll&&!rl&&r
000
MC/DC0%0/2
$dom and not $seen{$dom}++
$dom not $seen{$dom}++
MC/DC0%0/2
$dom and not $seen{$dom}++
$dom not $seen{$dom}++
Truth table33%1/3
A and not(B)
A= $domB= not $seen{$dom}++
ABresult
0X0
100
111
Truth table0%0/3
A and not(B)
A= $domB= not $seen{$dom}++
ABresult
0X0
100
111
1064 }
1065  
1066 # Then from contact domains (normalise subdomains to registrable parent)
1067 0 for my $d ($self->mailto_domains()) {
1068 41 ▶ my $dom = _registrable($d->{domain}) // $d->{domain};
Condition33%2/6
_registrable($d->{"domain"}) // $d->{"domain"}
l!l&&r!l&&!r
3340
_registrable($d->{"domain"}) // $d->{"domain"}
l!l&&r!l&&!r
000
MC/DC0%0/2
_registrable($d->{"domain"}) // $d->{"domain"}
_registrable($d->{"domain"}) $d->{"domain"}
MC/DC0%0/2
_registrable($d->{"domain"}) // $d->{"domain"}
_registrable($d->{"domain"}) $d->{"domain"}
Truth table33%1/3
A // B
A= _registrable($d->{"domain"})B= $d->{"domain"}
ABresult
1X1
011
000
Truth table0%0/3
A // B
A= _registrable($d->{"domain"})B= $d->{"domain"}
ABresult
1X1
011
000
1069 41 ▶ push @out, $dom if $dom && !$seen{$dom}++;
Branch: 2 branches
BranchTrueFalseTotal
if $dom and not $seen{$dom}++ 21 8 29
if $dom and not $seen{$dom}++ 0 1 1
Condition50%3/6
$dom and not $seen{$dom}++
!ll&&!rl&&r
131022
$dom and not $seen{$dom}++
!ll&&!rl&&r
000
MC/DC50%1/2
$dom and not $seen{$dom}++
$dom not $seen{$dom}++
MC/DC0%0/2
$dom and not $seen{$dom}++
$dom not $seen{$dom}++
Truth table66%2/3
A and not(B)
A= $domB= not $seen{$dom}++
ABresult
0X0
100
111
Truth table0%0/3
A and not(B)
A= $domB= not $seen{$dom}++
ABresult
0X0
100
111
1070 }
1071 44 return @out;
1072 }
1073  
1074 =head2 unresolved_contacts()
1075  
1076 Returns a list of domains and URL hosts found in the message for which no
1077 abuse contact could be determined. Useful for surfacing parties that may
1078 warrant manual investigation.
1079  
1080 =head3 Usage
1081  
1082 my @unresolved = $analyser->unresolved_contacts();
1083 for my $u (@unresolved) {
1084 printf "Unresolved: %s (%s) via %s\n",
1085 $u->{domain}, $u->{type}, $u->{source};
1086 }
1087  
1088 =head3 Arguments
1089  
1090 None.
1091  
1092 =head3 Returns
1093  
1094 A list of hashrefs, each with keys C<domain>, C<type> (C<'url_host'> or
1095 C<'domain'>), and C<source> (where the domain was found).
1096  
1097 =head3 Side Effects
1098  
1099 Triggers C<embedded_urls()>, C<mailto_domains()>, C<abuse_contacts()>,
1100 and C<form_contacts()> if not already cached.
1101  
1102 =head3 Notes
1103  
1104 Domains sourced only from spoofable sending headers (C<From:>,
1105 C<Return-Path:>, C<Sender:>) are excluded.
1106  
1107 =head3 API Specification
1108  
1109 =head4 Input
1110  
1111 []
1112  
1113 =head4 Output
1114  
1115 (
1116 {
1117 type => 'hashref',
1118 keys => {
1119 domain => { type => 'string' },
1120 type => { type => 'string', memberof => [ 'url_host', 'domain' ] },
1121 source => { type => 'string' },
1122 },
1123 },
1124 ...
1125 )
1126  
1127 =cut
1128  
1129 49 ▶sub unresolved_contacts {
Subroutine unresolved_contacts: called CC=18 SCAR=29.7
Subroutine unresolved_contacts: called CC=18 SCAR=29.7
Pod documented
1130 57 my $self = $_[0];
1131  
1132 # Build a set of domains already covered by email or form contacts
1133 52 my %covered;
1134 52 for my $c ($self->abuse_contacts(), $self->form_contacts()) {
1135 31 my $dom = $c->{form_domain};
1136 31 ▶ unless ($dom) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($dom) 15 0 15
unless ($dom) 1 0 1
1137 # Extract domain from abuse email address
1138 31 ▶ ($dom) = ($c->{address} // '') =~ /\@([\w.-]+)/;
Condition66%4/6
$c->{"address"} // ""
l!l
2761
$c->{"address"} // ""
l!l
100
MC/DC100%1/1
$c->{"address"} // ""
$c->{"address"}
MC/DC0%0/1
$c->{"address"} // ""
$c->{"address"}
Truth table100%2/2
A
A= $c->{"address"}
Aresult
11
00
Truth table50%1/2
A
A= $c->{"address"}
Aresult
11
00
1139 }
1140 32 ▶ $covered{lc $dom}++ if $dom;
Branch: 2 branches
BranchTrueFalseTotal
if $dom 15 1 16
if $dom 0 1 1
1141 }
1142  
1143 # Also mark URL hosts that already have a resolved abuse address
1144 50 for my $u ($self->embedded_urls()) {
1145 32 (my $bare = lc $u->{host}) =~ s/^www\.//;
1146 28 ▶ $covered{$bare}++ if $u->{abuse} && $u->{abuse} ne '(unknown)';
Branch: 2 branches
BranchTrueFalseTotal
if $u->{"abuse"} and $u->{"abuse"} ne "(unknown)" 4 14 18
if $u->{"abuse"} and $u->{"abuse"} ne "(unknown)" 1 0 1
Condition50%3/6
$u->{"abuse"} and $u->{"abuse"} ne "(unknown)"
!ll&&!rl&&r
12145
$u->{"abuse"} and $u->{"abuse"} ne "(unknown)"
!ll&&!rl&&r
000
MC/DC100%2/2
$u->{"abuse"} and $u->{"abuse"} ne "(unknown)"
$u->{"abuse"} $u->{"abuse"} ne "(unknown)"
MC/DC0%0/2
$u->{"abuse"} and $u->{"abuse"} ne "(unknown)"
$u->{"abuse"} $u->{"abuse"} ne "(unknown)"
Truth table100%3/3
A and B
A= $u->{"abuse"}B= $u->{"abuse"} ne "(unknown)"
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $u->{"abuse"}B= $u->{"abuse"} ne "(unknown)"
ABresult
0X0
100
111
1147 }
1148  
1149 48 my (@out, %seen);
1150  
1151 # Check URL hosts first
1152 45 for my $u ($self->embedded_urls()) {
1153 28 (my $bare = lc $u->{host}) =~ s/^www\.//;
1154 28 ▶ next if $covered{$bare};
Branch: 2 branches
BranchTrueFalseTotal
if $covered{$bare} 4 13 17
if $covered{$bare} 1 0 1
1155 24 ▶ next if $self->_provider_abuse_for_host($bare);
Branch: 2 branches
BranchTrueFalseTotal
if $self->_provider_abuse_for_host($bare) 1 12 13
if $self->_provider_abuse_for_host($bare) 1 0 1
1156 22 ▶ next if $seen{"url:$bare"}++;
Branch: 2 branches
BranchTrueFalseTotal
if $seen{"url:$bare"}++ 2 11 13
if $seen{"url:$bare"}++ 1 0 1
1157 push @out, {
1158 domain => $u->{host},
1159 21 type => 'url_host',
1160 source => 'URL in body',
1161 };
1162 }
1163  
1164 # Then check contact domains, skipping spoofable-header-only sources
1165 43 for my $d ($self->mailto_domains()) {
1166 47 my $dom = $d->{domain};
1167 49 ▶ my $source = $d->{source} // '';
Condition60%3/5
$d->{"source"} // ""
l!l
4441
$d->{"source"} // ""
l!l
00
MC/DC100%1/1
$d->{"source"} // ""
$d->{"source"}
MC/DC0%0/1
$d->{"source"} // ""
$d->{"source"}
Truth table100%2/2
A
A= $d->{"source"}
Aresult
11
00
Truth table0%0/2
A
A= $d->{"source"}
Aresult
11
00
1168 49 ▶ next if $source =~ /^(?:From:|Return-Path:|Sender:) header$/;
Branch: 2 branches
BranchTrueFalseTotal
if $source =~ /^(?:From:|Return-Path:|Sender:) header$/ 26 12 38
if $source =~ /^(?:From:|Return-Path:|Sender:) header$/ 0 2 2
1169 23 ▶ next if $covered{lc $dom};
Branch: 2 branches
BranchTrueFalseTotal
if $covered{lc $dom} 1 11 12
if $covered{lc $dom} 0 2 2
1170 21 ▶ next if $self->_provider_abuse_for_host($dom);
Branch: 2 branches
BranchTrueFalseTotal
if $self->_provider_abuse_for_host($dom) 1 10 11
if $self->_provider_abuse_for_host($dom) 0 2 2
1171 15 ▶ next if $seen{"dom:$dom"}++;
Branch: 2 branches
BranchTrueFalseTotal
if $seen{"dom:$dom"}++ 1 10 11
if $seen{"dom:$dom"}++ 0 2 2
1172 15 push @out, {
1173 domain => $dom,
1174 type => 'domain',
1175 source => $source,
1176 };
1177 }
1178  
1179 37 return @out;
1180 }
1181  
1182 # -----------------------------------------------------------------------
1183 # Public: sending software fingerprint
1184 # -----------------------------------------------------------------------
1185  
1186 =head2 sending_software()
1187  
1188 Returns information extracted from headers that identify the software or
1189 server-side infrastructure used to compose or inject the message. Headers
1190 such as C<X-PHP-Originating-Script> reveal the exact PHP script and Unix
1191 account responsible on shared-hosting platforms.
1192  
1193 Data is extracted during C<parse_email()> with no network I/O.
1194  
1195 =head3 Usage
1196  
1197 my @sw = $analyser->sending_software();
1198 for my $s (@sw) {
1199 printf "%-30s : %s\n", $s->{header}, $s->{value};
1200 }
1201  
1202 =head3 Arguments
1203  
1204 None. C<parse_email()> must have been called first.
1205  
1206 =head3 Returns
1207  
1208 A list of hashrefs in alphabetical header-name order. Returns an empty
1209 list if none of the watched headers are present. Each hashref has keys
1210 C<header>, C<value>, and C<note>.
1211  
1212 =head3 Side Effects
1213  
1214 None. Data is pre-collected during C<parse_email()>.
1215  
1216 =head3 Notes
1217  
1218 Header names are lower-cased. Header values are stored verbatim.
1219  
1220 =head3 API Specification
1221  
1222 =head4 Input
1223  
1224 []
1225  
1226 =head4 Output
1227  
1228 (
1229 {
1230 type => 'hashref',
1231 keys => {
1232 header => { type => 'string' },
1233 value => { type => 'string' },
1234 note => { type => 'string' },
1235 },
1236 },
1237 ...
1238 )
1239  
1240 =cut
1241  
1242 117 ▶sub sending_software {
Subroutine sending_software: called CC=1 SCAR=0.0
Subroutine sending_software: called CC=1 SCAR=0.0
Pod documented
1243 99 my $self = $_[0];
1244  
1245 96 return @{ $self->{_sending_sw} };
1246 }
1247  
1248 # -----------------------------------------------------------------------
1249 # Public: per-hop tracking IDs
1250 # -----------------------------------------------------------------------
1251  
1252 =head2 received_trail()
1253  
1254 Returns per-hop tracking data extracted from the C<Received:> header chain:
1255 the IP address, envelope recipient address, and server session ID for each
1256 relay. ISP postmasters use these identifiers to locate the SMTP session in
1257 their logs.
1258  
1259 =head3 Usage
1260  
1261 my @trail = $analyser->received_trail();
1262 for my $hop (@trail) {
1263 printf "IP: %s ID: %s\n",
1264 $hop->{ip} // '?', $hop->{id} // '?';
1265 }
1266  
1267 =head3 Arguments
1268  
1269 None. C<parse_email()> must have been called first.
1270  
1271 =head3 Returns
1272  
1273 A list of hashrefs in oldest-first order. Returns an empty list if no
1274 C<Received:> headers are present or none yielded extractable data. Each
1275 hashref has keys C<received>, C<ip> (may be undef), C<for> (may be undef),
1276 C<id> (may be undef).
1277  
1278 =head3 Side Effects
1279  
1280 None. Data is pre-collected during C<parse_email()>.
1281  
1282 =head3 Notes
1283  
1284 Private IPs are NOT filtered here; all IPs including RFC 1918 addresses
1285 are returned as found. Filtering is applied only by C<originating_ip()>.
1286  
1287 =head3 API Specification
1288  
1289 =head4 Input
1290  
1291 []
1292  
1293 =head4 Output
1294  
1295 (
1296 {
1297 type => 'hashref',
1298 keys => {
1299 received => { type => 'string' },
1300 ip => { type => 'string', optional => 1 },
1301 for => { type => 'string', optional => 1 },
1302 id => { type => 'string', optional => 1 },
1303 },
1304 },
1305 ...
1306 )
1307  
1308 =cut
1309  
1310 119 ▶sub received_trail {
Subroutine received_trail: called CC=1 SCAR=0.0
Subroutine received_trail: called CC=1 SCAR=0.0
Pod documented
1311 99 my $self = $_[0];
1312  
1313 96 return @{ $self->{_rcvd_tracking} };
1314 }
1315  
1316 # -----------------------------------------------------------------------
1317 # Public: risk assessment
1318 # -----------------------------------------------------------------------
1319  
1320 =head2 risk_assessment()
1321  
1322 Evaluates the message against heuristic checks and returns an overall risk
1323 level, a weighted numeric score, and a list of every specific red flag.
1324  
1325 The assessment covers five categories: originating IP, email authentication,
1326 Date: header validity, identity/header consistency, and URL/domain properties.
1327  
1328 The result is cached; subsequent calls return the same hashref without
1329 repeating any analysis.
1330  
1331 =head3 Usage
1332  
1333 my $risk = $analyser->risk_assessment();
1334 printf "Risk: %s (score: %d)\n", $risk->{level}, $risk->{score};
1335 for my $f (@{ $risk->{flags} }) {
1336 printf " [%s] %s\n", $f->{severity}, $f->{detail};
1337 }
1338  
1339 =head3 Arguments
1340  
1341 None. C<parse_email()> must have been called first.
1342  
1343 =head3 Returns
1344  
1345 A hashref with keys C<level> (HIGH/MEDIUM/LOW/INFO), C<score> (integer),
1346 and C<flags> (arrayref of hashrefs with C<severity>, C<flag>, C<detail>).
1347  
1348 =head3 Side Effects
1349  
1350 Triggers C<originating_ip()>, C<embedded_urls()>, and C<mailto_domains()>
1351 if not already cached.
1352  
1353 =head3 Notes
1354  
1355 Scores: HIGH >= 9, MEDIUM >= 5, LOW >= 2, INFO < 2.
1356 Flag weights: HIGH=3, MEDIUM=2, LOW=1, INFO=0.
1357  
1358 =head3 API Specification
1359  
1360 =head4 Input
1361  
1362 []
1363  
1364 =head4 Output
1365  
1366 {
1367 type => 'hashref',
1368 keys => {
1369 level => { type => 'string', memberof => ['HIGH', 'MEDIUM', 'LOW', 'INFO'] },
1370 score => { type => 'integer' },
1371 flags => { type => 'arrayref' },
1372 },
1373 }
1374  
1375 =cut
1376  
1377 417 ▶sub risk_assessment {
Subroutine risk_assessment: called CC=5 SCAR=16.1
Subroutine risk_assessment: called CC=5 SCAR=16.1
Pod documented
1378 406 my $self = $_[0];
1379  
1380 406 ▶ return $self->{_risk} if $self->{_risk};
Branch: 2 branches
BranchTrueFalseTotal
if $self->{"_risk"} 27 374 401
if $self->{"_risk"} 0 2 2
1381  
1382 379 my (@flags, $score);
1383 380 $score = 0;
1384  
1385 # Closure to record a flag and accumulate its weight
1386 398 ▶ my $flag = sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
Subroutine __ANON__: called CC=2 SCAR=7.1
1387 390 my ($severity, $name, $detail) = @_;
1388 390 ▶ $score += $FLAG_WEIGHT{$severity} // 1;
Condition60%3/5
$FLAG_WEIGHT{$severity} // 1
l!l
38713
$FLAG_WEIGHT{$severity} // 1
l!l
00
MC/DC0%0/1
$FLAG_WEIGHT{$severity} // 1
$FLAG_WEIGHT{$severity}
MC/DC0%0/1
$FLAG_WEIGHT{$severity} // 1
$FLAG_WEIGHT{$severity}
Truth table50%1/2
A
A= $FLAG_WEIGHT{$severity}
Aresult
11
00
Truth table0%0/2
A
A= $FLAG_WEIGHT{$severity}
Aresult
11
00
1389 389 push @flags, { severity => $severity, flag => $name, detail => $detail };
1390 380 };
1391  
1392 380 $self->_risk_check_origin($flag);
1393 380 $self->_risk_check_auth($flag);
1394 380 $self->_risk_check_date($flag);
1395 380 $self->_risk_check_identity($flag);
1396 379 $self->_risk_check_urls_and_domains($flag);
1397  
1398 # Determine overall risk level from accumulated score
1399 379 ▶ my $level = $score >= $SCORE_HIGH ? 'HIGH'
Branch: 6 branches
BranchTrueFalseTotal
$score >= $SCORE_HIGH ? : 6 368 374
$score >= $SCORE_MEDIUM ? : 26 342 368
$score >= $SCORE_LOW ? : 156 187 343
$score >= $SCORE_HIGH ? : 1 1 2
$score >= $SCORE_MEDIUM ? : 1 0 1
$score >= $SCORE_LOW ? : 1 0 1
1400 : $score >= $SCORE_MEDIUM ? 'MEDIUM'
1401 : $score >= $SCORE_LOW ? 'LOW'
1402 : 'INFO';
1403  
1404 379 $self->{_risk} = { level => $level, score => $score, flags => \@flags };
1405 379 return $self->{_risk};
1406 }
1407  
1408 # _risk_check_origin( $flag )
1409 #
1410 # Purpose:
1411 # Evaluate the originating IP for residential rDNS, absent rDNS,
1412 # low-confidence origin, and high-spam-volume country.
1413 #
1414 # Entry criteria:
1415 # $flag -- coderef( severity, name, detail ) that accumulates flags.
1416 #
1417 # Exit status:
1418 # Returns nothing; side effects via $flag closure.
1419  
1420 386 ▶sub _risk_check_origin :Private {
Subroutine _risk_check_origin: called CC=10 SCAR=24.9
Subroutine _risk_check_origin: called CC=10 SCAR=24.9
1421 379 my ($self, $flag) = @_;
1422 379 my $orig = $self->originating_ip();
1423 378 ▶ return unless $orig;
Branch: 2 branches
BranchTrueFalseTotal
unless $orig 65 311 376
unless $orig 0 2 2
1424  
1425 313 ▶ return if(!defined($orig->{ip}));
Branch: 2 branches
BranchTrueFalseTotal
unless defined $orig->{"ip"} 1 310 311
unless defined $orig->{"ip"} 0 0 0
1426  
1427 # Residential / broadband rDNS patterns suggest a compromised host
1428 313 ▶ if ($orig->{rdns} && $orig->{rdns} =~ /
Branch: 2 branches
BranchTrueFalseTotal
if ($orig->{"rdns"} and $orig->{"rdns"} =~ / \d+[-_.]\d+[-_.]\d+[-_.]\d+ # dotted-quad in rDNS | (?:dsl|adsl|cable|broad|dial|dynamic|dhcp|ppp| residential|cust|home|pool|client|user| static\d|host\d) /xi) 30 280 310
if ($orig->{"rdns"} and $orig->{"rdns"} =~ / \d+[-_.]\d+[-_.]\d+[-_.]\d+ # dotted-quad in rDNS | (?:dsl|adsl|cable|broad|dial|dynamic|dhcp|ppp| residential|cust|home|pool|client|user| static\d|host\d) /xi) 0 0 0
Condition66%4/6
$orig->{"rdns"} and $orig->{"rdns"} =~ / \d+[-_.]\d+[-_.]\d+[-_.]\d+ # dotted-quad in rDNS | (?:dsl|adsl|cable|broad|dial|dynamic|dhcp|ppp| residential|cust|home|pool|client|user| static\d|host\d) /xi
!ll&&!rl&&r
927928
$orig->{"rdns"} and $orig->{"rdns"} =~ / \d+[-_.]\d+[-_.]\d+[-_.]\d+ # dotted-quad in rDNS | (?:dsl|adsl|cable|broad|dial|dynamic|dhcp|ppp| residential|cust|home|pool|client|user| static\d|host\d) /xi
!ll&&!rl&&r
100
MC/DC100%2/2
$orig->{"rdns"} and $orig->{"rdns"} =~ / \d+[-_.]\d+[-_.]\d+[-_.]\d+ # dotted-quad in rDNS | (?:dsl|adsl|cable|broad|dial|dynamic|dhcp|ppp| residential|cust|home|pool|client|user| static\d|host\d) /xi
$orig->{"rdns"} $orig->{"rdns"} =~ / \d+[-_.]\d+[-_.]\d+[-_.]\d+ # dotted-quad in rDNS | (?:dsl|adsl|cable|broad|dial|dynamic|dhcp|ppp| residential|cust|home|pool|client|user| static\d|host\d) /xi
MC/DC0%0/2
$orig->{"rdns"} and $orig->{"rdns"} =~ / \d+[-_.]\d+[-_.]\d+[-_.]\d+ # dotted-quad in rDNS | (?:dsl|adsl|cable|broad|dial|dynamic|dhcp|ppp| residential|cust|home|pool|client|user| static\d|host\d) /xi
$orig->{"rdns"} $orig->{"rdns"} =~ / \d+[-_.]\d+[-_.]\d+[-_.]\d+ # dotted-quad in rDNS | (?:dsl|adsl|cable|broad|dial|dynamic|dhcp|ppp| residential|cust|home|pool|client|user| static\d|host\d) /xi
Truth table100%3/3
A and B
A= $orig->{"rdns"}B= $orig->{"rdns"} =~ / \d+[-_.]\d+[-_.]\d+[-_.]\d+ # dotted-quad in rDNS | (?:dsl|adsl|cable|broad|dial|dynamic|dhcp|ppp| residential|cust|home|pool|client|user| static\d|host\d) /xi
ABresult
0X0
100
111
Truth table33%1/3
A and B
A= $orig->{"rdns"}B= $orig->{"rdns"} =~ / \d+[-_.]\d+[-_.]\d+[-_.]\d+ # dotted-quad in rDNS | (?:dsl|adsl|cable|broad|dial|dynamic|dhcp|ppp| residential|cust|home|pool|client|user| static\d|host\d) /xi
ABresult
0X0
100
111
1429 \d+[-_.]\d+[-_.]\d+[-_.]\d+ # dotted-quad in rDNS
1430 | (?:dsl|adsl|cable|broad|dial|dynamic|dhcp|ppp|
1431 residential|cust|home|pool|client|user|
1432 static\d|host\d)
1433 /xi) {
1434 34 $flag->('HIGH', 'residential_sending_ip',
1435 "Sending IP $orig->{ip} rDNS '$orig->{rdns}' looks like a broadband/residential line, not a legitimate mail server");
1436 }
1437  
1438 # Absence of rDNS is a strong spam indicator
1439 313 ▶ if (!$orig->{rdns} || $orig->{rdns} eq '(no reverse DNS)') {
Branch: 2 branches
BranchTrueFalseTotal
if (not $orig->{"rdns"} or $orig->{"rdns"} eq "(no reverse DNS)") 17 293 310
if (not $orig->{"rdns"} or $orig->{"rdns"} eq "(no reverse DNS)") 0 0 0
Condition50%3/6
not $orig->{"rdns"} or $orig->{"rdns"} eq "(no reverse DNS)"
l!l&&r!l&&!r
213294
not $orig->{"rdns"} or $orig->{"rdns"} eq "(no reverse DNS)"
l!l&&r!l&&!r
000
MC/DC100%2/2
not $orig->{"rdns"} or $orig->{"rdns"} eq "(no reverse DNS)"
not $orig->{"rdns"} $orig->{"rdns"} eq "(no reverse DNS)"
MC/DC0%0/2
not $orig->{"rdns"} or $orig->{"rdns"} eq "(no reverse DNS)"
not $orig->{"rdns"} $orig->{"rdns"} eq "(no reverse DNS)"
Truth table100%3/3
not(A) or B
A= not $orig->{"rdns"}B= $orig->{"rdns"} eq "(no reverse DNS)"
ABresult
1X1
011
000
Truth table0%0/3
not(A) or B
A= not $orig->{"rdns"}B= $orig->{"rdns"} eq "(no reverse DNS)"
ABresult
1X1
011
000
1440 21 $flag->('HIGH', 'no_reverse_dns',
1441 "Sending IP $orig->{ip} has no reverse DNS -- legitimate mail servers always have rDNS");
1442 }
1443  
1444 # Low-confidence origin means the IP came from an unverifiable header
1445 313 ▶ if ($orig->{confidence} eq 'low') {
Branch: 2 branches
BranchTrueFalseTotal
if ($orig->{"confidence"} eq "low") 5 305 310
if ($orig->{"confidence"} eq "low") 0 2 2
1446 10 $flag->('MEDIUM', 'low_confidence_origin',
1447 "Originating IP taken from unverified header ($orig->{note})");
1448 }
1449  
1450 # Statistically high-volume spam countries (informational only)
1451 314 ▶ if ($orig->{country} && $orig->{country} =~ /^(?:CN|RU|NG|VN|IN|PK|BD)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($orig->{"country"} and $orig->{"country"} =~ /^(?:CN|RU|NG|VN|IN|PK|BD)$/) 32 277 309
if ($orig->{"country"} and $orig->{"country"} =~ /^(?:CN|RU|NG|VN|IN|PK|BD)$/) 0 0 0
Condition50%3/6
$orig->{"country"} and $orig->{"country"} =~ /^(?:CN|RU|NG|VN|IN|PK|BD)$/
!ll&&!rl&&r
2522532
$orig->{"country"} and $orig->{"country"} =~ /^(?:CN|RU|NG|VN|IN|PK|BD)$/
!ll&&!rl&&r
000
MC/DC100%2/2
$orig->{"country"} and $orig->{"country"} =~ /^(?:CN|RU|NG|VN|IN|PK|BD)$/
$orig->{"country"} $orig->{"country"} =~ /^(?:CN|RU|NG|VN|IN|PK|BD)$/
MC/DC0%0/2
$orig->{"country"} and $orig->{"country"} =~ /^(?:CN|RU|NG|VN|IN|PK|BD)$/
$orig->{"country"} $orig->{"country"} =~ /^(?:CN|RU|NG|VN|IN|PK|BD)$/
Truth table100%3/3
A and B
A= $orig->{"country"}B= $orig->{"country"} =~ /^(?:CN|RU|NG|VN|IN|PK|BD)$/
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $orig->{"country"}B= $orig->{"country"} =~ /^(?:CN|RU|NG|VN|IN|PK|BD)$/
ABresult
0X0
100
111
1452 $flag->('INFO', 'high_spam_country',
1453 39 'Sending IP is in ' . _country_name($orig->{country}) .
1454 " ($orig->{country}) -- statistically high spam volume country");
1455 }
1456 0 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine BEGIN: called CC=1 SCAR=0.2
1457  
1458 # _risk_check_auth( $flag )
1459 #
1460 # Purpose:
1461 # Evaluate SPF, DKIM, DMARC results and DKIM signing domain alignment.
1462 #
1463 # Entry criteria:
1464 # $flag -- accumulator coderef.
1465 #
1466 # Exit status:
1467 # Returns nothing; side effects via $flag closure.
1468  
1469 387 ▶sub _risk_check_auth :Private {
Subroutine _risk_check_auth: called CC=17 SCAR=32.9
Subroutine _risk_check_auth: called CC=17 SCAR=32.9
1470 379 my ($self, $flag) = @_;
1471 379 my $auth = $self->_parse_auth_results_cached();
1472  
1473 379 ▶ if (defined $auth->{spf}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $auth->{"spf"}) 35 340 375
if (defined $auth->{"spf"}) 0 0 0
1474 40 ▶ if ($auth->{spf} =~ /^fail/i) {
Branch: 6 branches
BranchTrueFalseTotal
if ($auth->{'spf'} =~ /^fail/i) { } 14 22 36
elsif ($auth->{'spf'} =~ /^softfail/i) { } 7 18 25
elsif (not $auth->{'spf'} =~ /^pass/i) { } 0 18 18
if ($auth->{'spf'} =~ /^fail/i) { } 0 0 0
elsif ($auth->{'spf'} =~ /^softfail/i) { } 1 0 1
elsif (not $auth->{'spf'} =~ /^pass/i) { } 2 0 2
1475 18 $flag->('HIGH', 'spf_fail',
1476 "SPF result: $auth->{spf} -- sending IP not authorised by domain's SPF record");
1477 } elsif ($auth->{spf} =~ /^softfail/i) {
1478 13 $flag->('MEDIUM', 'spf_softfail',
1479 "SPF result: softfail (~all) -- sending IP not explicitly authorised");
1480 } elsif ($auth->{spf} !~ /^pass/i) {
1481 6 $flag->('HIGH', 'spf_fail',
1482 "SPF result: $auth->{spf} -- sending IP not authorised");
1483 }
1484 }
1485 379 ▶ if (defined $auth->{dkim} && $auth->{dkim} !~ /^pass/i) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $auth->{"dkim"} and not $auth->{"dkim"} =~ /^pass/i) 15 361 376
if (defined $auth->{"dkim"} and not $auth->{"dkim"} =~ /^pass/i) 0 1 1
Condition50%3/6
defined $auth->{"dkim"} and not $auth->{"dkim"} =~ /^pass/i
!ll&&!rl&&r
3421716
defined $auth->{"dkim"} and not $auth->{"dkim"} =~ /^pass/i
!ll&&!rl&&r
000
MC/DC100%2/2
defined $auth->{"dkim"} and not $auth->{"dkim"} =~ /^pass/i
defined $auth->{"dkim"} not $auth->{"dkim"} =~ /^pass/i
MC/DC0%0/2
defined $auth->{"dkim"} and not $auth->{"dkim"} =~ /^pass/i
defined $auth->{"dkim"} not $auth->{"dkim"} =~ /^pass/i
Truth table100%3/3
A and not(B)
A= defined $auth->{"dkim"}B= not $auth->{"dkim"} =~ /^pass/i
ABresult
0X0
100
111
Truth table0%0/3
A and not(B)
A= defined $auth->{"dkim"}B= not $auth->{"dkim"} =~ /^pass/i
ABresult
0X0
100
111
1486 20 $flag->('HIGH', 'dkim_fail',
1487 "DKIM result: $auth->{dkim} -- message signature invalid or absent");
1488 }
1489 379 ▶ if (defined $auth->{dmarc} && $auth->{dmarc} !~ /^pass/i) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $auth->{"dmarc"} and not $auth->{"dmarc"} =~ /^pass/i) 9 367 376
if (defined $auth->{"dmarc"} and not $auth->{"dmarc"} =~ /^pass/i) 0 0 0
Condition50%3/6
defined $auth->{"dmarc"} and not $auth->{"dmarc"} =~ /^pass/i
!ll&&!rl&&r
355128
defined $auth->{"dmarc"} and not $auth->{"dmarc"} =~ /^pass/i
!ll&&!rl&&r
000
MC/DC100%2/2
defined $auth->{"dmarc"} and not $auth->{"dmarc"} =~ /^pass/i
defined $auth->{"dmarc"} not $auth->{"dmarc"} =~ /^pass/i
MC/DC0%0/2
defined $auth->{"dmarc"} and not $auth->{"dmarc"} =~ /^pass/i
defined $auth->{"dmarc"} not $auth->{"dmarc"} =~ /^pass/i
Truth table100%3/3
A and not(B)
A= defined $auth->{"dmarc"}B= not $auth->{"dmarc"} =~ /^pass/i
ABresult
0X0
100
111
Truth table0%0/3
A and not(B)
A= defined $auth->{"dmarc"}B= not $auth->{"dmarc"} =~ /^pass/i
ABresult
0X0
100
111
1490 13 $flag->('HIGH', 'dmarc_fail', "DMARC result: $auth->{dmarc}");
1491 }
1492  
1493 # DKIM signing domain vs From: domain mismatch check
1494 379 ▶ return unless $auth->{dkim_domain};
Branch: 2 branches
BranchTrueFalseTotal
unless $auth->{"dkim_domain"} 364 11 375
unless $auth->{"dkim_domain"} 0 0 0
1495 15 ▶ my ($from_domain) = ($self->_header_value('from') // '') =~ /\@([\w.-]+)/;
Condition80%4/5
$self->_header_value("from") // ""
l!l
1001
$self->_header_value("from") // ""
l!l
22
MC/DC100%1/1
$self->_header_value("from") // ""
$self->_header_value("from")
MC/DC100%1/1
$self->_header_value("from") // ""
$self->_header_value("from")
Truth table100%2/2
A
A= $self->_header_value("from")
Aresult
11
00
Truth table100%2/2
A
A= $self->_header_value("from")
Aresult
11
00
1496 15 ▶ return unless $from_domain;
Branch: 2 branches
BranchTrueFalseTotal
unless $from_domain 0 11 11
unless $from_domain 0 0 0
1497 15 ▶ my $reg_dkim = _registrable($auth->{dkim_domain}) // $auth->{dkim_domain};
Condition33%2/6
_registrable($auth->{"dkim_domain"}) // $auth->{"dkim_domain"}
l!l&&r!l&&!r
1000
_registrable($auth->{"dkim_domain"}) // $auth->{"dkim_domain"}
l!l&&r!l&&!r
200
MC/DC0%0/2
_registrable($auth->{"dkim_domain"}) // $auth->{"dkim_domain"}
_registrable($auth->{"dkim_domain"}) $auth->{"dkim_domain"}
MC/DC0%0/2
_registrable($auth->{"dkim_domain"}) // $auth->{"dkim_domain"}
_registrable($auth->{"dkim_domain"}) $auth->{"dkim_domain"}
Truth table33%1/3
A // B
A= _registrable($auth->{"dkim_domain"})B= $auth->{"dkim_domain"}
ABresult
1X1
011
000
Truth table0%0/3
A // B
A= _registrable($auth->{"dkim_domain"})B= $auth->{"dkim_domain"}
ABresult
1X1
011
000
1498 15 ▶ my $reg_from = _registrable(lc $from_domain) // lc $from_domain;
Condition40%2/5
_registrable(lc $from_domain) // lc $from_domain
l!l
901
_registrable(lc $from_domain) // lc $from_domain
l!l
00
MC/DC0%0/1
_registrable(lc $from_domain) // lc $from_domain
_registrable(lc $from_domain)
MC/DC0%0/1
_registrable(lc $from_domain) // lc $from_domain
_registrable(lc $from_domain)
Truth table50%1/2
A
A= _registrable(lc $from_domain)
Aresult
11
00
Truth table0%0/2
A
A= _registrable(lc $from_domain)
Aresult
11
00
1499 15 ▶ return if $reg_dkim eq $reg_from;
Branch: 2 branches
BranchTrueFalseTotal
if $reg_dkim eq $reg_from 4 7 11
if $reg_dkim eq $reg_from 0 0 0
1500  
1501 # Passing DKIM with a different domain is normal for ESPs
1502 12 ▶ if ($auth->{dkim} && $auth->{dkim} =~ /^pass/i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($auth->{'dkim'} and $auth->{'dkim'} =~ /^pass/i) { } 4 4 8
if ($auth->{'dkim'} and $auth->{'dkim'} =~ /^pass/i) { } 0 0 0
Condition33%2/6
$auth->{"dkim"} and $auth->{"dkim"} =~ /^pass/i
!ll&&!rl&&r
033
$auth->{"dkim"} and $auth->{"dkim"} =~ /^pass/i
!ll&&!rl&&r
000
MC/DC50%1/2
$auth->{"dkim"} and $auth->{"dkim"} =~ /^pass/i
$auth->{"dkim"} $auth->{"dkim"} =~ /^pass/i
MC/DC0%0/2
$auth->{"dkim"} and $auth->{"dkim"} =~ /^pass/i
$auth->{"dkim"} $auth->{"dkim"} =~ /^pass/i
Truth table66%2/3
A and B
A= $auth->{"dkim"}B= $auth->{"dkim"} =~ /^pass/i
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $auth->{"dkim"}B= $auth->{"dkim"} =~ /^pass/i
ABresult
0X0
100
111
1503 9 $flag->('INFO', 'dkim_domain_mismatch',
1504 "DKIM signed by '$auth->{dkim_domain}' but From: domain is '$from_domain'"
1505 . ' -- message sent via third-party sender (normal for bulk/ESP mail)');
1506 } else {
1507 # Failing DKIM plus mismatched domain is more suspicious
1508 9 $flag->('MEDIUM', 'dkim_domain_mismatch',
1509 "DKIM signed by '$auth->{dkim_domain}' but From: domain is '$from_domain'"
1510 . ' and DKIM did not pass -- possible impersonation');
1511 }
1512 24 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
1513  
1514 # _risk_check_date( $flag )
1515 #
1516 # Purpose:
1517 # Validate the Date: header for presence, plausible timezone, and
1518 # date not too far in the past or future.
1519 #
1520 # Entry criteria:
1521 # $flag -- accumulator coderef.
1522 #
1523 # Exit status:
1524 # Returns nothing; side effects via $flag closure.
1525  
1526 379 ▶sub _risk_check_date :Private {
Subroutine _risk_check_date: called CC=12 SCAR=27.3
Subroutine _risk_check_date: called CC=12 SCAR=27.3
1527 379 my ($self, $flag) = @_;
1528 379 my $date_raw = $self->_header_value('date');
1529  
1530 379 ▶ if (!$date_raw || $date_raw !~ /\S/) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($date_raw and $date_raw =~ /\S/) 45 331 376
unless ($date_raw and $date_raw =~ /\S/) 0 0 0
Condition33%2/6
$date_raw and $date_raw =~ /\S/
!ll&&!rl&&r
440329
$date_raw and $date_raw =~ /\S/
!ll&&!rl&&r
000
MC/DC50%1/2
$date_raw and $date_raw =~ /\S/
$date_raw $date_raw =~ /\S/
MC/DC0%0/2
$date_raw and $date_raw =~ /\S/
$date_raw $date_raw =~ /\S/
Truth table66%2/3
A and B
A= $date_rawB= $date_raw =~ /\S/
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $date_rawB= $date_raw =~ /\S/
ABresult
0X0
100
111
1531 50 $flag->('MEDIUM', 'missing_date',
1532 'No Date: header -- violates RFC 5322; common in spam');
1533 49 return;
1534 }
1535  
1536 # Check for an implausible timezone offset (outside real-world bounds)
1537 334 ▶ if ($date_raw =~ /([+-])(\d{2})(\d{2})\s*$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($date_raw =~ /([+-])(\d{2})(\d{2})\s*$/) 329 2 331
if ($date_raw =~ /([+-])(\d{2})(\d{2})\s*$/) 0 0 0
1538 334 my ($sign, $hh, $mm) = ($1, $2, $3);
1539 334 my $offset_mins = $hh * 60 + $mm;
1540 334 ▶ my $implausible = $mm >= 60
Condition50%12/24
$mm >= 60 || $sign eq '+' && $offset_mins > $TZ_MAX_POS_MINS || $sign eq '-' && $offset_mins > $TZ_MAX_NEG_MINS
l!l&&r!l&&!r
93317
$mm >= 60 or $sign eq "+" and $offset_mins > $TZ_MAX_POS_MINS
l!l&&r!l&&!r
36320
$sign eq "+" and $offset_mins > $TZ_MAX_POS_MINS
!ll&&!rl&&r
73136
$sign eq "-" and $offset_mins > $TZ_MAX_NEG_MINS
!ll&&!rl&&r
31343
$mm >= 60 || $sign eq '+' && $offset_mins > $TZ_MAX_POS_MINS || $sign eq '-' && $offset_mins > $TZ_MAX_NEG_MINS
l!l&&r!l&&!r
000
$mm >= 60 or $sign eq "+" and $offset_mins > $TZ_MAX_POS_MINS
l!l&&r!l&&!r
000
$sign eq "+" and $offset_mins > $TZ_MAX_POS_MINS
!ll&&!rl&&r
000
$sign eq "-" and $offset_mins > $TZ_MAX_NEG_MINS
!ll&&!rl&&r
000
MC/DC80%4/5
$mm >= 60 || $sign eq '+' && $offset_mins > $TZ_MAX_POS_MINS || $sign eq '-' && $offset_mins > $TZ_MAX_NEG_MINS
$mm >= 60 $sign eq "+" $offset_mins > $TZ_MAX_POS_MINS $sign eq "-" $offset_mins > $TZ_MAX_NEG_MINS
MC/DC0%0/5
$mm >= 60 || $sign eq '+' && $offset_mins > $TZ_MAX_POS_MINS || $sign eq '-' && $offset_mins > $TZ_MAX_NEG_MINS
$mm >= 60 $sign eq "+" $offset_mins > $TZ_MAX_POS_MINS $sign eq "-" $offset_mins > $TZ_MAX_NEG_MINS
Truth table62%5/8
A or B and C || D and E
A= $mm >= 60B= $sign eq "+"C= $offset_mins > $TZ_MAX_POS_MINSD= $sign eq "-"E= $offset_mins > $TZ_MAX_NEG_MINS
ABCDEresult
1XXXX1
011XX1
00X111
010111
00X0X0
00X100
0100X0
010100
Truth table0%0/8
A or B and C || D and E
A= $mm >= 60B= $sign eq "+"C= $offset_mins > $TZ_MAX_POS_MINSD= $sign eq "-"E= $offset_mins > $TZ_MAX_NEG_MINS
ABCDEresult
1XXXX1
011XX1
00X111
010111
00X0X0
00X100
0100X0
010100
1541 || ($sign eq '+' && $offset_mins > $TZ_MAX_POS_MINS)
1542 || ($sign eq '-' && $offset_mins > $TZ_MAX_NEG_MINS);
1543 334 ▶ if ($implausible) {
Branch: 2 branches
BranchTrueFalseTotal
if ($implausible) 13 318 331
if ($implausible) 0 0 0
1544 17 $flag->('MEDIUM', 'implausible_timezone',
1545 "Date: '$date_raw' contains an implausible timezone offset "
1546 . "($sign$hh$mm) -- header is likely forged");
1547 }
1548 }
1549  
1550 # Check for dates more than DATE_SKEW_DAYS outside the analysis window
1551 334 my $date_epoch = _parse_rfc2822_date($date_raw);
1552 334 ▶ return unless defined $date_epoch;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $date_epoch 2 329 331
unless defined $date_epoch 0 6 6
1553 334 my $delta = time() - $date_epoch;
1554 334 ▶ if ($delta > $DATE_SKEW_DAYS * $SECS_PER_DAY) {
Branch: 4 branches
BranchTrueFalseTotal
if ($delta > $DATE_SKEW_DAYS * $SECS_PER_DAY) { } 52 279 331
elsif ($delta < -($DATE_SKEW_DAYS * $SECS_PER_DAY)) { } 5 274 279
if ($delta > $DATE_SKEW_DAYS * $SECS_PER_DAY) { } 0 6 6
elsif ($delta < -($DATE_SKEW_DAYS * $SECS_PER_DAY)) { } 0 1 1
1555 56 $flag->('LOW', 'suspicious_date',
1556 "Date: '$date_raw' is more than $DATE_SKEW_DAYS days in the past");
1557 } elsif ($delta < -($DATE_SKEW_DAYS * $SECS_PER_DAY)) {
1558 10 $flag->('LOW', 'suspicious_date',
1559 "Date: '$date_raw' is more than $DATE_SKEW_DAYS days in the future");
1560 }
1561 24 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
1562  
1563 # _risk_check_identity( $flag )
1564 #
1565 # Purpose:
1566 # Check From: display-name spoofing, free webmail, Reply-To mismatch,
1567 # undisclosed recipients, and MIME-encoded Subject.
1568 #
1569 # Entry criteria:
1570 # $flag -- accumulator coderef.
1571 #
1572 # Exit status:
1573 # Returns nothing; side effects via $flag closure.
1574  
1575 379 ▶sub _risk_check_identity :Private {
Subroutine _risk_check_identity: called CC=19 SCAR=34.0
Subroutine _risk_check_identity: called CC=19 SCAR=34.0
1576 379 my ($self, $flag) = @_;
1577 379 ▶ my $from_raw = $self->_header_value('from') // '';
Condition33%2/6
$self->_header_value("from") // ""
l!l
37030
$self->_header_value("from") // ""
l!l
000
MC/DC100%1/1
$self->_header_value("from") // ""
$self->_header_value("from")
MC/DC0%0/1
$self->_header_value("from") // ""
$self->_header_value("from")
Truth table100%2/2
A
A= $self->_header_value("from")
Aresult
11
00
Truth table0%0/2
A
A= $self->_header_value("from")
Aresult
11
00
1578 378 my $from_decoded = $self->_decode_mime_words($from_raw);
1579  
1580 # Display-name domain spoofing: "PayPal paypal.com" <phish@evil.example>
1581 378 ▶ if ($from_decoded =~ /^"?([^"<]+?)"?\s*<([^>]+)>/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($from_decoded =~ /^"?([^"<]+?)"?\s*<([^>]+)>/) 194 180 374
if ($from_decoded =~ /^"?([^"<]+?)"?\s*<([^>]+)>/) 1 0 1
1582 199 my ($display, $addr) = ($1, $2);
1583 199 ▶ while ($display =~ /\b([\w-]+\.(?:com|net|org|io|co|uk|au|gov|edu))\b/gi) {
Branch: 2 branches
BranchTrueFalseTotal
if ($display =~ /\b([\w-]+\.(?:com|net|org|io|co|uk|au|gov|edu))\b/gi) 6 195 201
if ($display =~ /\b([\w-]+\.(?:com|net|org|io|co|uk|au|gov|edu))\b/gi) 1 0 1
1584 11 my $disp_domain = lc $1;
1585 11 my ($addr_domain) = $addr =~ /\@([\w.-]+)/;
1586 11 ▶ $addr_domain = lc($addr_domain // '');
Condition20%1/5
$addr_domain // ""
l!l
600
$addr_domain // ""
l!l
00
MC/DC0%0/1
$addr_domain // ""
$addr_domain
MC/DC0%0/1
$addr_domain // ""
$addr_domain
Truth table50%1/2
A
A= $addr_domain
Aresult
11
00
Truth table0%0/2
A
A= $addr_domain
Aresult
11
00
1587 11 my $reg_disp = _registrable($disp_domain);
1588 11 my $reg_addr = _registrable($addr_domain);
1589 11 ▶ if ($reg_disp && $reg_addr && $reg_disp ne $reg_addr) {
Branch: 2 branches
BranchTrueFalseTotal
if ($reg_disp and $reg_addr and $reg_disp ne $reg_addr) 6 0 6
if ($reg_disp and $reg_addr and $reg_disp ne $reg_addr) 0 1 1
Condition16%2/12
$reg_disp and $reg_addr and $reg_disp ne $reg_addr
!ll&&!rl&&r
006
$reg_disp and $reg_addr
!ll&&!rl&&r
006
$reg_disp and $reg_addr and $reg_disp ne $reg_addr
!ll&&!rl&&r
000
$reg_disp and $reg_addr
!ll&&!rl&&r
000
MC/DC0%0/3
$reg_disp and $reg_addr and $reg_disp ne $reg_addr
$reg_disp $reg_addr $reg_disp ne $reg_addr
MC/DC0%0/3
$reg_disp and $reg_addr and $reg_disp ne $reg_addr
$reg_disp $reg_addr $reg_disp ne $reg_addr
Truth table25%1/4
A and B and C
A= $reg_dispB= $reg_addrC= $reg_disp ne $reg_addr
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/4
A and B and C
A= $reg_dispB= $reg_addrC= $reg_disp ne $reg_addr
ABCresult
0XX0
10X0
1100
1111
1590 11 $flag->('HIGH', 'display_name_domain_spoof',
1591 "From: display name mentions '$disp_domain' but actual address is <$addr>");
1592 }
1593 }
1594 }
1595  
1596 # Free webmail sender flag (no corporate infrastructure)
1597 379 ▶ if ($from_raw =~ /\@(gmail|yahoo|hotmail|outlook|live|aol|protonmail|yandex)\./i
Branch: 2 branches
BranchTrueFalseTotal
if ($from_raw =~ /\@(gmail|yahoo|hotmail|outlook|live|aol|protonmail|yandex)\./i or $from_raw =~ /\@mail\.ru(?:[\s>]|$)/i) 32 341 373
if ($from_raw =~ /\@(gmail|yahoo|hotmail|outlook|live|aol|protonmail|yandex)\./i or $from_raw =~ /\@mail\.ru(?:[\s>]|$)/i) 1 0 1
Condition66%4/6
$from_raw =~ /\@(gmail|yahoo|hotmail|outlook|live|aol|protonmail|yandex)\./i or $from_raw =~ /\@mail\.ru(?:[\s>]|$)/i
l!l&&r!l&&!r
311341
$from_raw =~ /\@(gmail|yahoo|hotmail|outlook|live|aol|protonmail|yandex)\./i or $from_raw =~ /\@mail\.ru(?:[\s>]|$)/i
l!l&&r!l&&!r
100
MC/DC100%2/2
$from_raw =~ /\@(gmail|yahoo|hotmail|outlook|live|aol|protonmail|yandex)\./i or $from_raw =~ /\@mail\.ru(?:[\s>]|$)/i
$from_raw =~ /\@(gmail|yahoo|hotmail|outlook|live|aol|protonmail|yandex)\./i $from_raw =~ /\@mail\.ru(?:[\s>]|$)/i
MC/DC0%0/2
$from_raw =~ /\@(gmail|yahoo|hotmail|outlook|live|aol|protonmail|yandex)\./i or $from_raw =~ /\@mail\.ru(?:[\s>]|$)/i
$from_raw =~ /\@(gmail|yahoo|hotmail|outlook|live|aol|protonmail|yandex)\./i $from_raw =~ /\@mail\.ru(?:[\s>]|$)/i
Truth table100%3/3
A or B
A= $from_raw =~ /\@(gmail|yahoo|hotmail|outlook|live|aol|protonmail|yandex)\./iB= $from_raw =~ /\@mail\.ru(?:[\s>]|$)/i
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= $from_raw =~ /\@(gmail|yahoo|hotmail|outlook|live|aol|protonmail|yandex)\./iB= $from_raw =~ /\@mail\.ru(?:[\s>]|$)/i
ABresult
1X1
011
000
1598 || $from_raw =~ /\@mail\.ru(?:[\s>]|$)/i) {
1599 38 $flag->('MEDIUM', 'free_webmail_sender',
1600 "Message sent from free webmail address ($from_raw)");
1601 }
1602  
1603 # Reply-To differs from From: -- replies harvested by different address
1604 378 my $reply_to = $self->_header_value('reply-to');
1605 378 ▶ if ($reply_to) {
Branch: 2 branches
BranchTrueFalseTotal
if ($reply_to) 7 366 373
if ($reply_to) 1 0 1
1606 12 my ($from_addr) = $from_raw =~ /([\w.+%-]+\@[\w.-]+)/;
1607 12 my ($reply_addr) = $reply_to =~ /([\w.+%-]+\@[\w.-]+)/;
1608 12 ▶ if ($from_addr && $reply_addr && lc($from_addr) ne lc($reply_addr)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($from_addr and $reply_addr and lc $from_addr ne lc $reply_addr) 5 2 7
if ($from_addr and $reply_addr and lc $from_addr ne lc $reply_addr) 1 0 1
Condition25%3/12
$from_addr and $reply_addr and lc $from_addr ne lc $reply_addr
!ll&&!rl&&r
025
$from_addr and $reply_addr
!ll&&!rl&&r
007
$from_addr and $reply_addr and lc $from_addr ne lc $reply_addr
!ll&&!rl&&r
000
$from_addr and $reply_addr
!ll&&!rl&&r
000
MC/DC33%1/3
$from_addr and $reply_addr and lc $from_addr ne lc $reply_addr
$from_addr $reply_addr lc $from_addr ne lc $reply_addr
MC/DC0%0/3
$from_addr and $reply_addr and lc $from_addr ne lc $reply_addr
$from_addr $reply_addr lc $from_addr ne lc $reply_addr
Truth table50%2/4
A and B and C
A= $from_addrB= $reply_addrC= lc $from_addr ne lc $reply_addr
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/4
A and B and C
A= $from_addrB= $reply_addrC= lc $from_addr ne lc $reply_addr
ABCresult
0XX0
10X0
1100
1111
1609 10 $flag->('MEDIUM', 'reply_to_differs_from_from',
1610 "Reply-To ($reply_addr) differs from From: ($from_addr)");
1611 }
1612 }
1613  
1614 # Undisclosed or absent To: header
1615 378 ▶ my $to = $self->_header_value('to') // '';
Condition50%2/4
$self->_header_value("to") // ""
l!l
33538
$self->_header_value("to") // ""
l!l
00
MC/DC100%1/1
$self->_header_value("to") // ""
$self->_header_value("to")
MC/DC0%0/1
$self->_header_value("to") // ""
$self->_header_value("to")
Truth table100%2/2
A
A= $self->_header_value("to")
Aresult
11
00
Truth table0%0/2
A
A= $self->_header_value("to")
Aresult
11
00
1616 378 ▶ if ($to =~ /undisclosed|:;/ || $to eq '') {
Branch: 2 branches
BranchTrueFalseTotal
if ($to =~ /undisclosed|:;/ or $to eq "") 46 327 373
if ($to =~ /undisclosed|:;/ or $to eq "") 0 1 1
Condition66%4/6
$to =~ /undisclosed|:;/ or $to eq ""
l!l&&r!l&&!r
640327
$to =~ /undisclosed|:;/ or $to eq ""
l!l&&r!l&&!r
010
MC/DC100%2/2
$to =~ /undisclosed|:;/ or $to eq ""
$to =~ /undisclosed|:;/ $to eq ""
MC/DC0%0/2
$to =~ /undisclosed|:;/ or $to eq ""
$to =~ /undisclosed|:;/ $to eq ""
Truth table100%3/3
A or B
A= $to =~ /undisclosed|:;/B= $to eq ""
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= $to =~ /undisclosed|:;/B= $to eq ""
ABresult
1X1
011
000
1617 51 $flag->('MEDIUM', 'undisclosed_recipients',
1618 "To: header is '$to' -- message was bulk-sent with hidden recipient list");
1619 }
1620  
1621 # MIME-encoded Subject (potential filter evasion)
1622 378 ▶ my $subj_raw = $self->_header_value('subject') // '';
Condition50%2/4
$self->_header_value("subject") // ""
l!l
33835
$self->_header_value("subject") // ""
l!l
00
MC/DC100%1/1
$self->_header_value("subject") // ""
$self->_header_value("subject")
MC/DC0%0/1
$self->_header_value("subject") // ""
$self->_header_value("subject")
Truth table100%2/2
A
A= $self->_header_value("subject")
Aresult
11
00
Truth table0%0/2
A
A= $self->_header_value("subject")
Aresult
11
00
1623 378 ▶ if ($subj_raw =~ /=\?[^?]+\?[BQ]\?/i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($subj_raw =~ /=\?[^?]+\?[BQ]\?/i) 11 362 373
if ($subj_raw =~ /=\?[^?]+\?[BQ]\?/i) 0 1 1
1624 15 $flag->('LOW', 'encoded_subject',
1625 "Subject line is MIME-encoded: '$subj_raw' (decoded: '"
1626 . $self->_decode_mime_words($subj_raw) . "')");
1627 }
1628 24 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
1629  
1630 # _risk_check_urls_and_domains( $flag )
1631 #
1632 # Purpose:
1633 # Check embedded URLs for shorteners and plain HTTP, and contact domains
1634 # for recent registration, imminent expiry, and lookalike brand names.
1635 #
1636 # Entry criteria:
1637 # $flag -- accumulator coderef.
1638 #
1639 # Exit status:
1640 # Returns nothing; side effects via $flag closure.
1641  
1642 378 ▶sub _risk_check_urls_and_domains :Private {
Subroutine _risk_check_urls_and_domains: called CC=24 SCAR=37.6
Subroutine _risk_check_urls_and_domains: called CC=24 SCAR=37.6
1643 377 my ($self, $flag) = @_;
1644 377 my (%shortener_seen, %cloaker_seen, %url_host_seen);
1645  
1646 377 for my $u ($self->embedded_urls()) {
1647 # Skip trusted infrastructure -- these are not spam indicators
1648 70 ▶ next unless($u->{host});
Branch: 2 branches
BranchTrueFalseTotal
unless $u->{"host"} 1 65 66
unless $u->{"host"} 0 0 0
1649 69 my $bare = lc $u->{host};
1650 69 ▶ next unless defined($bare);
Branch: 2 branches
BranchTrueFalseTotal
unless defined $bare 0 65 65
unless defined $bare 0 0 0
1651 69 $bare =~ s/^www\.//;
1652 69 ▶ next if $self->{trusted_domains}->{$bare};
Branch: 2 branches
BranchTrueFalseTotal
if $self->{"trusted_domains"}{$bare} 0 65 65
if $self->{"trusted_domains"}{$bare} 1 0 1
1653 69 ▶ next if $TRUSTED_DOMAINS{$bare};
Branch: 2 branches
BranchTrueFalseTotal
if $TRUSTED_DOMAINS{$bare} 2 63 65
if $TRUSTED_DOMAINS{$bare} 1 0 1
1654  
1655 # URL shortener hides real destination
1656 67 ▶ if(($URL_SHORTENERS{$bare} || $self->{url_shorteners}->{$bare}) && !$shortener_seen{$bare}++) {
Branch: 2 branches
BranchTrueFalseTotal
if ($URL_SHORTENERS{$bare} || $self->{"url_shorteners"}{$bare} and not $shortener_seen{$bare}++) 8 55 63
if ($URL_SHORTENERS{$bare} || $self->{"url_shorteners"}{$bare} and not $shortener_seen{$bare}++) 0 1 1
Condition33%4/12
$URL_SHORTENERS{$bare} || $self->{"url_shorteners"}{$bare} and not $shortener_seen{$bare}++
!ll&&!rl&&r
5508
$URL_SHORTENERS{$bare} or $self->{"url_shorteners"}{$bare}
l!l&&r!l&&!r
8055
$URL_SHORTENERS{$bare} || $self->{"url_shorteners"}{$bare} and not $shortener_seen{$bare}++
!ll&&!rl&&r
000
$URL_SHORTENERS{$bare} or $self->{"url_shorteners"}{$bare}
l!l&&r!l&&!r
000
MC/DC33%1/3
$URL_SHORTENERS{$bare} || $self->{"url_shorteners"}{$bare} and not $shortener_seen{$bare}++
$URL_SHORTENERS{$bare} $self->{"url_shorteners"}{$bare} not $shortener_seen{$bare}++
MC/DC0%0/3
$URL_SHORTENERS{$bare} || $self->{"url_shorteners"}{$bare} and not $shortener_seen{$bare}++
$URL_SHORTENERS{$bare} $self->{"url_shorteners"}{$bare} not $shortener_seen{$bare}++
Truth table40%2/5
A or B and not(C)
A= $URL_SHORTENERS{$bare}B= $self->{"url_shorteners"}{$bare}C= not $shortener_seen{$bare}++
ABCresult
00X0
1X00
0100
1X11
0111
Truth table0%0/5
A or B and not(C)
A= $URL_SHORTENERS{$bare}B= $self->{"url_shorteners"}{$bare}C= not $shortener_seen{$bare}++
ABCresult
00X0
1X00
0100
1X11
0111
1657 12 $flag->('MEDIUM', 'url_shortener',
1658 "$u->{host} is a URL shortener -- the real destination is hidden");
1659 }
1660 # Cloud object-store / CDN used as a redirect cloaker
1661 67 ▶ if ($self->_is_redirect_cloaker($bare) && !$cloaker_seen{$bare}++) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_is_redirect_cloaker($bare) and not $cloaker_seen{$bare}++) 1 62 63
if ($self->_is_redirect_cloaker($bare) and not $cloaker_seen{$bare}++) 1 0 1
Condition50%3/6
$self->_is_redirect_cloaker($bare) and not $cloaker_seen{$bare}++
!ll&&!rl&&r
6201
$self->_is_redirect_cloaker($bare) and not $cloaker_seen{$bare}++
!ll&&!rl&&r
200
MC/DC50%1/2
$self->_is_redirect_cloaker($bare) and not $cloaker_seen{$bare}++
$self->_is_redirect_cloaker($bare) not $cloaker_seen{$bare}++
MC/DC0%0/2
$self->_is_redirect_cloaker($bare) and not $cloaker_seen{$bare}++
$self->_is_redirect_cloaker($bare) not $cloaker_seen{$bare}++
Truth table66%2/3
A and not(B)
A= $self->_is_redirect_cloaker($bare)B= not $cloaker_seen{$bare}++
ABresult
0X0
100
111
Truth table33%1/3
A and not(B)
A= $self->_is_redirect_cloaker($bare)B= not $cloaker_seen{$bare}++
ABresult
0X0
100
111
1662 5 $flag->('MEDIUM', 'redirect_cloaker',
1663 "$u->{host} is a cloud storage or CDN host used as a redirect cloaker -- the real phishing destination is hidden behind a client-side redirect");
1664 }
1665 # Plain HTTP provides no encryption
1666 67 ▶ if ($u->{url} =~ m{^http://}i && !$url_host_seen{ $u->{host} }++) {
Branch: 2 branches
BranchTrueFalseTotal
if ($u->{"url"} =~ m[^http://]i and not $url_host_seen{$u->{"host"}}++) 9 54 63
if ($u->{"url"} =~ m[^http://]i and not $url_host_seen{$u->{"host"}}++) 0 1 1
Condition33%2/6
$u->{"url"} =~ m[^http://]i and not $url_host_seen{$u->{"host"}}++
!ll&&!rl&&r
5409
$u->{"url"} =~ m[^http://]i and not $url_host_seen{$u->{"host"}}++
!ll&&!rl&&r
000
MC/DC50%1/2
$u->{"url"} =~ m[^http://]i and not $url_host_seen{$u->{"host"}}++
$u->{"url"} =~ m[^http://]i not $url_host_seen{$u->{"host"}}++
MC/DC0%0/2
$u->{"url"} =~ m[^http://]i and not $url_host_seen{$u->{"host"}}++
$u->{"url"} =~ m[^http://]i not $url_host_seen{$u->{"host"}}++
Truth table66%2/3
A and not(B)
A= $u->{"url"} =~ m[^http://]iB= not $url_host_seen{$u->{"host"}}++
ABresult
0X0
100
111
Truth table0%0/3
A and not(B)
A= $u->{"url"} =~ m[^http://]iB= not $url_host_seen{$u->{"host"}}++
ABresult
0X0
100
111
1667 13 $flag->('LOW', 'http_not_https',
1668 "$u->{host} linked over plain HTTP -- no encryption");
1669 }
1670 }
1671  
1672 # Domain-level checks against contact/reply domains
1673 377 for my $d ($self->mailto_domains()) {
1674 # Recently registered domain is a common phishing indicator
1675 183 ▶ if ($d->{recently_registered}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($d->{"recently_registered"}) 8 171 179
if ($d->{"recently_registered"}) 1 0 1
1676 12 $flag->('HIGH', 'recently_registered_domain',
1677 11 "$d->{domain} was registered $d->{registered} (less than ${\$RECENT_REG_DAYS} days ago)");
1678 }
1679  
1680 # Domain expiry checks
1681 182 ▶ if ($d->{expires}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($d->{"expires"}) 17 162 179
if ($d->{"expires"}) 1 0 1
1682 20 ▶ if(my $exp = $self->_parse_date_to_epoch($d->{expires})) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $exp = $self->_parse_date_to_epoch($d->{"expires"})) 17 0 17
if (my $exp = $self->_parse_date_to_epoch($d->{"expires"})) 1 0 1
1683 20 my $now = time();
1684 20 my $remaining = $exp - $now;
1685 20 ▶ if ($remaining > 0 && $remaining < $EXPIRY_WARN_DAYS * $SECS_PER_DAY) {
Branch: 4 branches
BranchTrueFalseTotal
if ($remaining > 0 and $remaining < $EXPIRY_WARN_DAYS * $SECS_PER_DAY) { } 5 12 17
elsif ($remaining <= 0) { } 5 7 12
if ($remaining > 0 and $remaining < $EXPIRY_WARN_DAYS * $SECS_PER_DAY) { } 1 0 1
elsif ($remaining <= 0) { } 0 2 2
Condition50%3/6
$remaining > 0 and $remaining < $EXPIRY_WARN_DAYS * $SECS_PER_DAY
!ll&&!rl&&r
575
$remaining > 0 and $remaining < $EXPIRY_WARN_DAYS * $SECS_PER_DAY
!ll&&!rl&&r
000
MC/DC100%2/2
$remaining > 0 and $remaining < $EXPIRY_WARN_DAYS * $SECS_PER_DAY
$remaining > 0 $remaining < $EXPIRY_WARN_DAYS * $SECS_PER_DAY
MC/DC0%0/2
$remaining > 0 and $remaining < $EXPIRY_WARN_DAYS * $SECS_PER_DAY
$remaining > 0 $remaining < $EXPIRY_WARN_DAYS * $SECS_PER_DAY
Truth table100%3/3
A and B
A= $remaining > 0B= $remaining < $EXPIRY_WARN_DAYS * $SECS_PER_DAY
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $remaining > 0B= $remaining < $EXPIRY_WARN_DAYS * $SECS_PER_DAY
ABresult
0X0
100
111
1686 8 $flag->('HIGH', 'domain_expires_soon',
1687 "$d->{domain} expires $d->{expires} -- may be a throwaway domain");
1688 } elsif ($remaining <= 0) {
1689 8 $flag->('HIGH', 'domain_expired',
1690 "$d->{domain} expired $d->{expires} -- domain has lapsed");
1691 }
1692 }
1693 }
1694  
1695 # Lookalike domain check (brand name in a non-brand domain)
1696 182 for my $brand (@LOOKALIKE_BRANDS) {
1697 3079 ▶ next if(!defined($d->{domain}));
Branch: 2 branches
BranchTrueFalseTotal
unless defined $d->{"domain"} 18 3058 3076
unless defined $d->{"domain"} 0 2 2
1698 3061 ▶ if ($d->{domain} =~ /\Q$brand\E/i &&
Branch: 2 branches
BranchTrueFalseTotal
if ($d->{"domain"} =~ /\Q$brand\E/i and not $d->{"domain"} =~ /^\Q$brand\E\.(?:com|co\.uk|net|org)$/) 9 3049 3058
if ($d->{"domain"} =~ /\Q$brand\E/i and not $d->{"domain"} =~ /^\Q$brand\E\.(?:com|co\.uk|net|org)$/) 0 2 2
Condition50%3/6
$d->{"domain"} =~ /\Q$brand\E/i and not $d->{"domain"} =~ /^\Q$brand\E\.(?:com|co\.uk|net|org)$/
!ll&&!rl&&r
3039109
$d->{"domain"} =~ /\Q$brand\E/i and not $d->{"domain"} =~ /^\Q$brand\E\.(?:com|co\.uk|net|org)$/
!ll&&!rl&&r
000
MC/DC100%2/2
$d->{"domain"} =~ /\Q$brand\E/i and not $d->{"domain"} =~ /^\Q$brand\E\.(?:com|co\.uk|net|org)$/
$d->{"domain"} =~ /\Q$brand\E/i not $d->{"domain"} =~ /^\Q$brand\E\.(?:com|co\.uk|net|org)$/
MC/DC0%0/2
$d->{"domain"} =~ /\Q$brand\E/i and not $d->{"domain"} =~ /^\Q$brand\E\.(?:com|co\.uk|net|org)$/
$d->{"domain"} =~ /\Q$brand\E/i not $d->{"domain"} =~ /^\Q$brand\E\.(?:com|co\.uk|net|org)$/
Truth table100%3/3
A and not(B)
A= $d->{"domain"} =~ /\Q$brand\E/iB= not $d->{"domain"} =~ /^\Q$brand\E\.(?:com|co\.uk|net|org)$/
ABresult
0X0
100
111
Truth table0%0/3
A and not(B)
A= $d->{"domain"} =~ /\Q$brand\E/iB= not $d->{"domain"} =~ /^\Q$brand\E\.(?:com|co\.uk|net|org)$/
ABresult
0X0
100
111
1699 $d->{domain} !~ /^\Q$brand\E\.(?:com|co\.uk|net|org)$/) {
1700 12 $flag->('HIGH', 'lookalike_domain',
1701 "$d->{domain} contains brand name '$brand' but is not the real domain -- possible phishing");
1702 12 last;
1703 }
1704 }
1705 }
1706 24 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
1707  
1708 # -----------------------------------------------------------------------
1709 # Public: abuse report text
1710 # -----------------------------------------------------------------------
1711  
1712 =head2 abuse_report_text()
1713  
1714 Produces a compact, plain-text string suitable for sending as the body of
1715 an abuse report email. It summarises risk level, red flags, originating IP,
1716 abuse contacts, and original message headers. The message body is omitted
1717 to keep the report concise.
1718  
1719 Use C<abuse_contacts()> to get the recipient addresses and this method for
1720 the body text.
1721  
1722 =head3 Usage
1723  
1724 my $text = $analyser->abuse_report_text();
1725 my @contacts = $analyser->abuse_contacts();
1726 for my $c (@contacts) {
1727 send_email(to => $c->{address}, body => $text);
1728 }
1729  
1730 =head3 Arguments
1731  
1732 None. C<parse_email()> must have been called first.
1733  
1734 =head3 Returns
1735  
1736 A plain scalar string, newline-terminated, Unix line endings. Never empty
1737 or undef.
1738  
1739 =head3 Side Effects
1740  
1741 Calls C<risk_assessment()>, C<originating_ip()>, and C<abuse_contacts()>
1742 if not already cached.
1743  
1744 =head3 Notes
1745  
1746 Output text is sanitised: control characters that could affect terminal or
1747 HTML rendering are stripped from all user-derived content before inclusion.
1748  
1749 =head3 API Specification
1750  
1751 =head4 Input
1752  
1753 []
1754  
1755 =head4 Output
1756  
1757 { type => 'string' }
1758  
1759 =cut
1760  
1761 28 ▶sub abuse_report_text {
Subroutine abuse_report_text: called CC=20 SCAR=32.3
Subroutine abuse_report_text: called CC=20 SCAR=32.3
Pod documented
1762 28 my $self = $_[0];
1763 28 my @out;
1764  
1765 28 push @out, 'This is an automated abuse report generated by Email::Abuse::Investigator.',
1766 'Please investigate the following spam/phishing message.',
1767 '';
1768  
1769 28 my $risk = $self->risk_assessment();
1770 28 push @out, "RISK LEVEL: $risk->{level} (score: $risk->{score})",
1771 '';
1772  
1773 # List each red flag with its severity prefix
1774 28 ▶ if (@{ $risk->{flags} }) {
Branch: 2 branches
BranchTrueFalseTotal
if (@{$risk->{"flags"};}) 13 12 25
if (@{$risk->{"flags"};}) 1 1 2
1775 16 push @out, 'RED FLAGS IDENTIFIED:';
1776 16 for my $f (@{ $risk->{flags} }) {
1777 26 push @out, " [$f->{severity}] " . _sanitise_output($f->{detail});
1778 }
1779 16 push @out, '';
1780 }
1781  
1782 # Originating IP summary block
1783 28 my $orig = $self->originating_ip();
1784 28 ▶ if ($orig) {
Branch: 2 branches
BranchTrueFalseTotal
if ($orig) 17 8 25
if ($orig) 1 1 2
1785 push @out, 'ORIGINATING IP: ' . _sanitise_output("$orig->{ip} ($orig->{rdns})"),
1786 20 'NETWORK OWNER: ' . _sanitise_output($orig->{org}),
1787 '';
1788 }
1789  
1790 # List every reported URL so the receiving abuse desk knows exactly
1791 # which resources to investigate or suspend.
1792 28 my @urls = $self->embedded_urls();
1793 28 ▶ if (@urls) {
Branch: 2 branches
BranchTrueFalseTotal
if (@urls) 7 18 25
if (@urls) 1 1 2
1794 10 push @out, 'REPORTED URLs:';
1795 10 for my $u (@urls) {
1796 10 push @out, ' ' . _sanitise_output($u->{url});
1797 10 my @meta;
1798 10 ▶ push @meta, "host: $u->{host}" if $u->{host};
Branch: 2 branches
BranchTrueFalseTotal
if $u->{"host"} 7 0 7
if $u->{"host"} 1 0 1
1799 10 ▶ push @meta, "IP: $u->{ip}" if $u->{ip} && $u->{ip} ne '(unresolved)';
Branch: 2 branches
BranchTrueFalseTotal
if $u->{"ip"} and $u->{"ip"} ne "(unresolved)" 5 2 7
if $u->{"ip"} and $u->{"ip"} ne "(unresolved)" 1 0 1
Condition50%3/6
$u->{"ip"} and $u->{"ip"} ne "(unresolved)"
!ll&&!rl&&r
025
$u->{"ip"} and $u->{"ip"} ne "(unresolved)"
!ll&&!rl&&r
200
MC/DC50%1/2
$u->{"ip"} and $u->{"ip"} ne "(unresolved)"
$u->{"ip"} $u->{"ip"} ne "(unresolved)"
MC/DC0%0/2
$u->{"ip"} and $u->{"ip"} ne "(unresolved)"
$u->{"ip"} $u->{"ip"} ne "(unresolved)"
Truth table66%2/3
A and B
A= $u->{"ip"}B= $u->{"ip"} ne "(unresolved)"
ABresult
0X0
100
111
Truth table33%1/3
A and B
A= $u->{"ip"}B= $u->{"ip"} ne "(unresolved)"
ABresult
0X0
100
111
1800 10 ▶ push @meta, "org: $u->{org}" if $u->{org} && $u->{org} ne '(unknown)';
Branch: 2 branches
BranchTrueFalseTotal
if $u->{"org"} and $u->{"org"} ne "(unknown)" 5 2 7
if $u->{"org"} and $u->{"org"} ne "(unknown)" 0 2 2
Condition33%2/6
$u->{"org"} and $u->{"org"} ne "(unknown)"
!ll&&!rl&&r
025
$u->{"org"} and $u->{"org"} ne "(unknown)"
!ll&&!rl&&r
000
MC/DC50%1/2
$u->{"org"} and $u->{"org"} ne "(unknown)"
$u->{"org"} $u->{"org"} ne "(unknown)"
MC/DC0%0/2
$u->{"org"} and $u->{"org"} ne "(unknown)"
$u->{"org"} $u->{"org"} ne "(unknown)"
Truth table66%2/3
A and B
A= $u->{"org"}B= $u->{"org"} ne "(unknown)"
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $u->{"org"}B= $u->{"org"} ne "(unknown)"
ABresult
0X0
100
111
1801 10 ▶ push @out, ' (' . join(' ', @meta) . ')' if @meta;
Branch: 2 branches
BranchTrueFalseTotal
if @meta 7 0 7
if @meta 0 0 0
1802 }
1803 10 push @out, '';
1804 }
1805  
1806 # Email abuse contacts
1807 28 my @contacts = $self->abuse_contacts();
1808 28 ▶ if (@contacts) {
Branch: 2 branches
BranchTrueFalseTotal
if (@contacts) 20 5 25
if (@contacts) 0 0 0
1809 23 push @out, 'ABUSE CONTACTS:';
1810 23 push @out, ' ' . _sanitise_output("$_->{address} ($_->{role})") for @contacts;
1811 23 push @out, '';
1812 }
1813  
1814 # Web-form contacts (providers that reject email)
1815 28 ▶ if(my @form_cs = $self->form_contacts()) {
Branch: 2 branches
BranchTrueFalseTotal
if (my(@form_cs) = $self->form_contacts) 2 23 25
if (my(@form_cs) = $self->form_contacts) 0 0 0
1816 5 push @out, 'WEB-FORM REPORTS REQUIRED:',
1817 ' The following parties do not accept email -- submit manually:';
1818 5 for my $c (@form_cs) {
1819 push @out, " [$c->{role}]",
1820 5 ' Form : ' . _sanitise_output($c->{form});
1821 5 ▶ push @out, ' Domain : ' . _sanitise_output($c->{form_domain}) if $c->{form_domain};
Branch: 2 branches
BranchTrueFalseTotal
if $c->{"form_domain"} 1 1 2
if $c->{"form_domain"} 0 2 2
1822 5 ▶ push @out, ' Paste : ' . _sanitise_output($c->{form_paste}) if $c->{form_paste};
Branch: 2 branches
BranchTrueFalseTotal
if $c->{"form_paste"} 2 0 2
if $c->{"form_paste"} 0 0 0
1823 5 ▶ push @out, ' Upload : ' . _sanitise_output($c->{form_upload}) if $c->{form_upload};
Branch: 2 branches
BranchTrueFalseTotal
if $c->{"form_upload"} 2 0 2
if $c->{"form_upload"} 0 0 0
1824 }
1825 5 push @out, '';
1826 }
1827  
1828 # Separator and raw headers (body excluded for brevity)
1829 28 push @out, '-' x 72,
1830 'ORIGINAL MESSAGE HEADERS:',
1831 '-' x 72;
1832  
1833 28 for my $h (@{ $self->{_headers} }) {
1834 185 push @out, _sanitise_output("$h->{name}: $h->{value}");
1835 }
1836 28 push @out, '';
1837  
1838 28 return join("\n", @out);
1839 }
1840  
1841 # -----------------------------------------------------------------------
1842 # Public: abuse contacts
1843 # -----------------------------------------------------------------------
1844  
1845 =head2 abuse_contacts()
1846  
1847 Collates the complete set of parties that should receive an abuse report:
1848 the sending ISP, URL host operators, contact domain web/mail/DNS/registrar
1849 contacts, account providers identified from key headers, the DKIM signer,
1850 and the ESP identified via List-Unsubscribe.
1851  
1852 Addresses are deduplicated globally; if the same address is found via
1853 multiple routes, a single entry is kept and role strings are merged.
1854  
1855 =head3 Usage
1856  
1857 my @contacts = $analyser->abuse_contacts();
1858 my @addrs = map { $_->{address} } @contacts;
1859  
1860 =head3 Arguments
1861  
1862 None. C<parse_email()> must have been called first.
1863  
1864 =head3 Returns
1865  
1866 A list of hashrefs, one per unique abuse address, in discovery order.
1867 Each hashref has keys C<role>, C<roles> (arrayref), C<address>, C<note>,
1868 C<via>. Returns an empty list if no contacts can be determined.
1869  
1870 =head3 Side Effects
1871  
1872 Triggers C<originating_ip()>, C<embedded_urls()>, and C<mailto_domains()>
1873 if not already cached.
1874  
1875 =head3 Notes
1876  
1877 The result is cached on the object; subsequent calls return the same list
1878 without repeating the contact-collection logic. The underlying per-IP and
1879 per-domain lookups are themselves cached by C<originating_ip()>,
1880 C<embedded_urls()>, and C<mailto_domains()>.
1881  
1882 =head3 API Specification
1883  
1884 =head4 Input
1885  
1886 []
1887  
1888 =head4 Output
1889  
1890 (
1891 {
1892 type => 'hashref',
1893 keys => {
1894 role => { type => 'string' },
1895 roles => { type => 'arrayref' },
1896 address => { type => 'string', regex => qr/\@/ },
1897 note => { type => 'string' },
1898 via => { type => 'string', memberof => [ 'provider-table', 'ip-whois', 'domain-whois' ] }
1899 },
1900 },
1901 ...
1902 )
1903  
1904 =cut
1905  
1906 219 ▶sub abuse_contacts {
Subroutine abuse_contacts: called CC=2 SCAR=7.6
Subroutine abuse_contacts: called CC=2 SCAR=7.6
Pod documented
1907 219 my $self = $_[0];
1908 219 ▶ $self->{_contacts} //= [ $self->_compute_abuse_contacts() ];
Condition40%2/5
$self->{"_contacts"} //= [$self->_compute_abuse_contacts]
l!l
341820
$self->{"_contacts"} //= [$self->_compute_abuse_contacts]
l!l
00
MC/DC100%1/1
$self->{"_contacts"} //= [$self->_compute_abuse_contacts]
$self->{"_contacts"}
MC/DC0%0/1
$self->{"_contacts"} //= [$self->_compute_abuse_contacts]
$self->{"_contacts"}
Truth table100%2/2
A
A= $self->{"_contacts"}
Aresult
11
00
Truth table0%0/2
A
A= $self->{"_contacts"}
Aresult
11
00
1909 219 return @{ $self->{_contacts} };
1910 }
1911  
1912 # _compute_abuse_contacts() -> list of contact hashrefs
1913 #
1914 # Purpose:
1915 # Actual implementation of abuse_contacts(). Separated so the public
1916 # method can cache without duplicating logic.
1917 #
1918 # Entry criteria:
1919 # parse_email() must have been called.
1920 #
1921 # Exit status:
1922 # Returns list of deduplicated contact hashrefs.
1923  
1924 186 ▶sub _compute_abuse_contacts :Private {
Subroutine _compute_abuse_contacts: called CC=51 SCAR=53.1
Subroutine _compute_abuse_contacts: called CC=51 SCAR=53.1
1925 186 my $self = $_[0];
1926  
1927 186 my (@contacts, %seen_idx);
1928  
1929 # Inner closure: add one contact entry, merging roles for duplicate addresses
1930 317 ▶ my $add = sub {
Subroutine __ANON__: called CC=19 SCAR=35.0
Subroutine __ANON__: called CC=19 SCAR=35.0
1931 316 my (%args) = @_;
1932 316 ▶ my $addr = lc($args{address} // '');
Condition40%2/5
$args{"address"} // ""
l!l
30850
$args{"address"} // ""
l!l
00
MC/DC100%1/1
$args{"address"} // ""
$args{"address"}
MC/DC0%0/1
$args{"address"} // ""
$args{"address"}
Truth table100%2/2
A
A= $args{"address"}
Aresult
11
00
Truth table0%0/2
A
A= $args{"address"}
Aresult
11
00
1933 316 ▶ return unless $addr && $addr =~ /\@/;
Branch: 2 branches
BranchTrueFalseTotal
unless $addr and $addr =~ /\@/ 7 306 313
unless $addr and $addr =~ /\@/ 0 0 0
Condition66%4/6
$addr and $addr =~ /\@/
!ll&&!rl&&r
52306
$addr and $addr =~ /\@/
!ll&&!rl&&r
100
MC/DC100%2/2
$addr and $addr =~ /\@/
$addr $addr =~ /\@/
MC/DC0%0/2
$addr and $addr =~ /\@/
$addr $addr =~ /\@/
Truth table100%3/3
A and B
A= $addrB= $addr =~ /\@/
ABresult
0X0
100
111
Truth table33%1/3
A and B
A= $addrB= $addr =~ /\@/
ABresult
0X0
100
111
1934  
1935 # Suppress addresses belonging to form-only providers (no email accepted)
1936 309 ▶ if ($addr =~ /\@([\w.-]+)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($addr =~ /\@([\w.-]+)$/) 306 0 306
if ($addr =~ /\@([\w.-]+)$/) 0 0 0
1937 309 my $dom = $1;
1938 309 my $pa = $self->_provider_abuse_for_host($dom);
1939 309 ▶ return if $pa && $pa->{form} && !$pa->{email};
Branch: 2 branches
BranchTrueFalseTotal
if $pa and $pa->{"form"} and not $pa->{"email"} 7 299 306
if $pa and $pa->{"form"} and not $pa->{"email"} 0 0 0
Condition41%5/12
$pa and $pa->{"form"} and not $pa->{"email"}
!ll&&!rl&&r
29907
$pa and $pa->{"form"}
!ll&&!rl&&r
1981017
$pa and $pa->{"form"} and not $pa->{"email"}
!ll&&!rl&&r
000
$pa and $pa->{"form"}
!ll&&!rl&&r
000
MC/DC66%2/3
$pa and $pa->{"form"} and not $pa->{"email"}
$pa $pa->{"form"} not $pa->{"email"}
MC/DC0%0/3
$pa and $pa->{"form"} and not $pa->{"email"}
$pa $pa->{"form"} not $pa->{"email"}
Truth table75%3/4
A and B and not(C)
A= $paB= $pa->{"form"}C= not $pa->{"email"}
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/4
A and B and not(C)
A= $paB= $pa->{"form"}C= not $pa->{"email"}
ABCresult
0XX0
10X0
1100
1111
1940 }
1941  
1942 302 ▶ if (exists $seen_idx{$addr}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $seen_idx{$addr}) 140 159 299
if (exists $seen_idx{$addr}) 0 0 0
1943 # Merge the new role into the existing entry
1944 143 my $entry = $contacts[ $seen_idx{$addr} ];
1945 143 push @{ $entry->{roles} }, $args{role};
1946  
1947 # Collapse repeated role labels to avoid unreadable strings
1948 143 my (%role_counts, @ordered_roles);
1949 143 for my $r (@{ $entry->{roles} }) {
1950 1216 ▶ push @ordered_roles, $r unless $role_counts{$r}++;
Branch: 2 branches
BranchTrueFalseTotal
unless $role_counts{$r}++ 1196 17 1213
unless $role_counts{$r}++ 0 0 0
1951 }
1952 my @display = map {
1953 143 ▶ $role_counts{$_} > 1 ? "$_ (x$role_counts{$_})" : $_
Branch: 2 branches
BranchTrueFalseTotal
$role_counts{$_} > 1 ? : 17 1179 1196
$role_counts{$_} > 1 ? : 0 0 0
1954 } @ordered_roles;
1955 143 my $joined = join(' and ', @display);
1956  
1957 # Summarise if the merged string is too long to read.
1958 # "URL host: hostname" entries are grouped by listing the actual
1959 # hostnames so the summary is actionable (e.g. "URL host: a.example,
1960 # b.example" rather than the unhelpful "URL host, URL host").
1961 # All other role types fall back to stripping at the first [:(\d]
1962 # boundary to produce a compact type label.
1963 143 ▶ if (length($joined) > $ROLE_MAX_LEN) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $joined > $ROLE_MAX_LEN) 77 63 140
if (length $joined > $ROLE_MAX_LEN) 0 0 0
1964 80 my (@url_hosts, %seen_short, @short);
1965 80 for my $r (@display) {
1966 1071 ▶ if ($r =~ /^URL host:\s*(.+)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($r =~ /^URL host:\s*(.+)$/) { } 29 1039 1068
if ($r =~ /^URL host:\s*(.+)$/) { } 0 0 0
1967 32 push @url_hosts, $1;
1968 } else {
1969 1042 (my $s = $r) =~ s/[:(\d].*//;
1970 1042 $s =~ s/\s+$//;
1971 1042 ▶ push @short, $s unless $seen_short{$s}++;
Branch: 2 branches
BranchTrueFalseTotal
unless $seen_short{$s}++ 283 756 1039
unless $seen_short{$s}++ 0 0 0
1972 }
1973 }
1974 80 my @parts;
1975 80 ▶ if (@url_hosts) {
Branch: 2 branches
BranchTrueFalseTotal
if (@url_hosts) 15 62 77
if (@url_hosts) 0 0 0
1976 18 ▶ my $extra = @url_hosts > 3
Branch: 2 branches
BranchTrueFalseTotal
@url_hosts > 3 ? : 3 12 15
@url_hosts > 3 ? : 0 0 0
1977 ? ' and ' . (@url_hosts - 3) . ' more' : '';
1978 18 ▶ my @shown = @url_hosts > 3 ? @url_hosts[0..2] : @url_hosts;
Branch: 2 branches
BranchTrueFalseTotal
@url_hosts > 3 ? : 3 12 15
@url_hosts > 3 ? : 0 0 0
1979 18 push @parts, 'URL host: ' . join(', ', @shown) . $extra;
1980 }
1981 80 push @parts, @short;
1982 80 $joined = scalar(@display) . ' routes: ' . join(', ', @parts);
1983 }
1984 143 $entry->{role} = $joined;
1985 143 return;
1986 }
1987  
1988 # First time seeing this address -- record and store
1989 162 $seen_idx{$addr} = scalar @contacts;
1990 162 $args{roles} = [ $args{role} ];
1991 162 push @contacts, \%args;
1992 186 };
1993  
1994 # Route 1 -- Sending ISP (originating IP)
1995 186 my $orig = $self->originating_ip();
1996 186 ▶ if ($orig) {
Branch: 2 branches
BranchTrueFalseTotal
if ($orig) 93 89 182
if ($orig) 0 0 0
1997 97 my $pa = $self->_provider_abuse_for_ip($orig->{ip}, $orig->{rdns});
1998 97 ▶ if ($pa) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pa) 14 79 93
if ($pa) 1 0 1
1999 $add->(
2000 role => 'Sending ISP',
2001 address => $pa->{email},
2002 18 note => "$orig->{ip} ($orig->{rdns}) -- $pa->{note}",
2003 via => 'provider-table',
2004 );
2005 }
2006 98 ▶ if ($orig->{abuse} && $orig->{abuse} ne '(unknown)') {
Branch: 2 branches
BranchTrueFalseTotal
if ($orig->{"abuse"} and $orig->{"abuse"} ne "(unknown)") 65 28 93
if ($orig->{"abuse"} and $orig->{"abuse"} ne "(unknown)") 2 0 2
Condition33%2/6
$orig->{"abuse"} and $orig->{"abuse"} ne "(unknown)"
!ll&&!rl&&r
02965
$orig->{"abuse"} and $orig->{"abuse"} ne "(unknown)"
!ll&&!rl&&r
000
MC/DC50%1/2
$orig->{"abuse"} and $orig->{"abuse"} ne "(unknown)"
$orig->{"abuse"} $orig->{"abuse"} ne "(unknown)"
MC/DC0%0/2
$orig->{"abuse"} and $orig->{"abuse"} ne "(unknown)"
$orig->{"abuse"} $orig->{"abuse"} ne "(unknown)"
Truth table66%2/3
A and B
A= $orig->{"abuse"}B= $orig->{"abuse"} ne "(unknown)"
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $orig->{"abuse"}B= $orig->{"abuse"} ne "(unknown)"
ABresult
0X0
100
111
2007 $add->(
2008 role => 'Sending ISP',
2009 address => $orig->{abuse},
2010 70 note => "Network owner of originating IP $orig->{ip} ($orig->{org})",
2011 via => 'ip-whois',
2012 );
2013 }
2014 }
2015  
2016 # Route 2 -- URL hosts
2017 184 my %url_host_seen;
2018 187 for my $u ($self->embedded_urls()) {
2019 96 ▶ next if $url_host_seen{ $u->{host} }++;
Branch: 2 branches
BranchTrueFalseTotal
if $url_host_seen{$u->{"host"}}++ 12 80 92
if $url_host_seen{$u->{"host"}}++ 0 1 1
2020 84 my $bare_host = lc $u->{host};
2021 84 $bare_host =~ s/^www\.//;
2022 # Skip trusted infrastructure (Google, W3C, etc.)
2023 84 ▶ next if $self->{trusted_domains}->{$bare_host};
Branch: 2 branches
BranchTrueFalseTotal
if $self->{"trusted_domains"}{$bare_host} 0 80 80
if $self->{"trusted_domains"}{$bare_host} 0 0 0
2024 84 ▶ next if $TRUSTED_DOMAINS{$bare_host};
Branch: 2 branches
BranchTrueFalseTotal
if $TRUSTED_DOMAINS{$bare_host} 3 77 80
if $TRUSTED_DOMAINS{$bare_host} 0 0 0
2025 81 my $pa = $self->_provider_abuse_for_host($u->{host});
2026 81 ▶ if ($pa) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pa) 19 58 77
if ($pa) 0 0 0
2027 $add->(
2028 role => "URL host: $u->{host}",
2029 address => $pa->{email},
2030 22 note => "$u->{host} -- $pa->{note}",
2031 via => 'provider-table',
2032 );
2033 }
2034 82 ▶ if ($u->{abuse} && $u->{abuse} ne '(unknown)') {
Branch: 2 branches
BranchTrueFalseTotal
if ($u->{"abuse"} and $u->{"abuse"} ne "(unknown)") 47 30 77
if ($u->{"abuse"} and $u->{"abuse"} ne "(unknown)") 0 0 0
Condition33%2/6
$u->{"abuse"} and $u->{"abuse"} ne "(unknown)"
!ll&&!rl&&r
03147
$u->{"abuse"} and $u->{"abuse"} ne "(unknown)"
!ll&&!rl&&r
000
MC/DC50%1/2
$u->{"abuse"} and $u->{"abuse"} ne "(unknown)"
$u->{"abuse"} $u->{"abuse"} ne "(unknown)"
MC/DC0%0/2
$u->{"abuse"} and $u->{"abuse"} ne "(unknown)"
$u->{"abuse"} $u->{"abuse"} ne "(unknown)"
Truth table66%2/3
A and B
A= $u->{"abuse"}B= $u->{"abuse"} ne "(unknown)"
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $u->{"abuse"}B= $u->{"abuse"} ne "(unknown)"
ABresult
0X0
100
111
2035 $add->(
2036 role => "URL host: $u->{host}",
2037 address => $u->{abuse},
2038 52 note => "Hosting $u->{host} ($u->{ip}, $u->{org})",
2039 via => 'ip-whois',
2040 );
2041 }
2042 }
2043  
2044 # Route 3 -- Contact domain hosting and registration
2045 186 for my $d ($self->mailto_domains()) {
2046 190 my $dom = $d->{domain};
2047  
2048 # Web host contact
2049 188 ▶ if ($d->{web_abuse}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($d->{"web_abuse"}) 57 128 185
if ($d->{"web_abuse"}) 0 0 0
2050 60 my $pa = $self->_provider_abuse_for_host($dom);
2051 60 ▶ if ($pa) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pa) 0 57 57
if ($pa) 0 0 0
2052 $add->(role => "Web host of $dom", address => $pa->{email},
2053 3 note => $pa->{note}, via => 'provider-table');
2054 }
2055 $add->(
2056 role => "Web host of $dom",
2057 address => $d->{web_abuse},
2058 note => sprintf('Hosting %s (%s, %s)',
2059 $dom // '(unknown domain)',
2060 $d->{web_ip} // '(unknown IP)',
2061 57 ▶ $d->{web_org} // '(unknown org)'),
Condition64%9/14
$dom // "(unknown domain)"
l!l
5720
$d->{"web_ip"} // "(unknown IP)"
l!l
4612
$d->{"web_org"} // "(unknown org)"
l!l
4612
$dom // "(unknown domain)"
l!l
22
$d->{"web_ip"} // "(unknown IP)"
l!l
200
$d->{"web_org"} // "(unknown org)"
l!l
00
MC/DC0%0/1
$dom // "(unknown domain)"
$dom
MC/DC100%1/1
$d->{"web_ip"} // "(unknown IP)"
$d->{"web_ip"}
MC/DC100%1/1
$d->{"web_org"} // "(unknown org)"
$d->{"web_org"}
MC/DC100%1/1
$dom // "(unknown domain)"
$dom
MC/DC0%0/1
$d->{"web_ip"} // "(unknown IP)"
$d->{"web_ip"}
MC/DC0%0/1
$d->{"web_org"} // "(unknown org)"
$d->{"web_org"}
Truth table50%1/2
A
A= $dom
Aresult
11
00
Truth table100%2/2
A
A= $d->{"web_ip"}
Aresult
11
00
Truth table100%2/2
A
A= $d->{"web_org"}
Aresult
11
00
Truth table100%2/2
A
A= $dom
Aresult
11
00
Truth table50%1/2
A
A= $d->{"web_ip"}
Aresult
11
00
Truth table0%0/2
A
A= $d->{"web_org"}
Aresult
11
00
2062 via => 'ip-whois',
2063 );
2064 }
2065  
2066 # MX (mail host) contact
2067 188 ▶ if ($d->{mx_abuse}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($d->{"mx_abuse"}) 24 161 185
if ($d->{"mx_abuse"}) 0 0 0
2068 $add->(
2069 role => "Mail host (MX) for $dom",
2070 address => $d->{mx_abuse},
2071 note => sprintf('MX %s (%s, %s)',
2072 $d->{mx_host} // '(unknown host)',
2073 $d->{mx_ip} // '(unknown IP)',
2074 27 ▶ $d->{mx_org} // '(unknown org)'),
Condition46%6/13
$d->{"mx_host"} // "(unknown host)"
l!l
818
$d->{"mx_ip"} // "(unknown IP)"
l!l
717
$d->{"mx_org"} // "(unknown org)"
l!l
717
$d->{"mx_host"} // "(unknown host)"
l!l
00
$d->{"mx_ip"} // "(unknown IP)"
l!l
00
$d->{"mx_org"} // "(unknown org)"
l!l
000
MC/DC100%1/1
$d->{"mx_host"} // "(unknown host)"
$d->{"mx_host"}
MC/DC100%1/1
$d->{"mx_ip"} // "(unknown IP)"
$d->{"mx_ip"}
MC/DC100%1/1
$d->{"mx_org"} // "(unknown org)"
$d->{"mx_org"}
MC/DC0%0/1
$d->{"mx_host"} // "(unknown host)"
$d->{"mx_host"}
MC/DC0%0/1
$d->{"mx_ip"} // "(unknown IP)"
$d->{"mx_ip"}
MC/DC0%0/1
$d->{"mx_org"} // "(unknown org)"
$d->{"mx_org"}
Truth table100%2/2
A
A= $d->{"mx_host"}
Aresult
11
00
Truth table100%2/2
A
A= $d->{"mx_ip"}
Aresult
11
00
Truth table100%2/2
A
A= $d->{"mx_org"}
Aresult
11
00
Truth table0%0/2
A
A= $d->{"mx_host"}
Aresult
11
00
Truth table0%0/2
A
A= $d->{"mx_ip"}
Aresult
11
00
Truth table0%0/2
A
A= $d->{"mx_org"}
Aresult
11
00
2075 via => 'ip-whois',
2076 );
2077 }
2078  
2079 # NS (DNS host) contact
2080 185 ▶ if ($d->{ns_abuse}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($d->{"ns_abuse"}) 15 170 185
if ($d->{"ns_abuse"}) 0 0 0
2081 $add->(
2082 role => "DNS host (NS) for $dom",
2083 address => $d->{ns_abuse},
2084 note => sprintf('NS %s (%s, %s)',
2085 $d->{ns_host} // '(unknown host)',
2086 $d->{ns_ip} // '(unknown IP)',
2087 18 ▶ $d->{ns_org} // '(unknown org)'),
Condition50%6/12
$d->{"ns_host"} // "(unknown host)"
l!l
510
$d->{"ns_ip"} // "(unknown IP)"
l!l
510
$d->{"ns_org"} // "(unknown org)"
l!l
510
$d->{"ns_host"} // "(unknown host)"
l!l
00
$d->{"ns_ip"} // "(unknown IP)"
l!l
00
$d->{"ns_org"} // "(unknown org)"
l!l
00
MC/DC100%1/1
$d->{"ns_host"} // "(unknown host)"
$d->{"ns_host"}
MC/DC100%1/1
$d->{"ns_ip"} // "(unknown IP)"
$d->{"ns_ip"}
MC/DC100%1/1
$d->{"ns_org"} // "(unknown org)"
$d->{"ns_org"}
MC/DC0%0/1
$d->{"ns_host"} // "(unknown host)"
$d->{"ns_host"}
MC/DC0%0/1
$d->{"ns_ip"} // "(unknown IP)"
$d->{"ns_ip"}
MC/DC0%0/1
$d->{"ns_org"} // "(unknown org)"
$d->{"ns_org"}
Truth table100%2/2
A
A= $d->{"ns_host"}
Aresult
11
00
Truth table100%2/2
A
A= $d->{"ns_ip"}
Aresult
11
00
Truth table100%2/2
A
A= $d->{"ns_org"}
Aresult
11
00
Truth table0%0/2
A
A= $d->{"ns_host"}
Aresult
11
00
Truth table0%0/2
A
A= $d->{"ns_ip"}
Aresult
11
00
Truth table0%0/2
A
A= $d->{"ns_org"}
Aresult
11
00
2088 via => 'ip-whois',
2089 );
2090 }
2091  
2092 # Domain registrar (skip if domain only seen in spoofable headers)
2093 188 ▶ if ($d->{registrar_abuse}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($d->{"registrar_abuse"}) 34 151 185
if ($d->{"registrar_abuse"}) 0 0 0
2094 my $spoofable_only =
2095 $d->{source} =~ /^(?:From:|Return-Path:|Sender:) header$/ &&
2096 !scalar(grep {
2097 37 ▶ $_->{host} &&
Condition50%3/6
$d->{'source'} =~ /^(?:From:|Return-Path:|Sender:) header$/ && !scalar(grep {$_->{'host'} and _registrable($_->{'host'}) eq (_registrable($dom) // $dom);} $self->embedded_urls)
!ll&&!rl&&r
2437
$d->{'source'} =~ /^(?:From:|Return-Path:|Sender:) header$/ && !scalar(grep {$_->{'host'} and _registrable($_->{'host'}) eq (_registrable($dom) // $dom);} $self->embedded_urls)
!ll&&!rl&&r
000
MC/DC100%2/2
$d->{'source'} =~ /^(?:From:|Return-Path:|Sender:) header$/ && !scalar(grep {$_->{'host'} and _registrable($_->{'host'}) eq (_registrable($dom) // $dom);} $self->embedded_urls)
$d->{'source'} =~ /^(?:From:|Return-Path:|Sender:) header$/ !scalar(grep {$_->{'host'} and _registrable($_->{'host'}) eq (_registrable($dom) // $dom);} $self->embedded_urls)
MC/DC0%0/2
$d->{'source'} =~ /^(?:From:|Return-Path:|Sender:) header$/ && !scalar(grep {$_->{'host'} and _registrable($_->{'host'}) eq (_registrable($dom) // $dom);} $self->embedded_urls)
$d->{'source'} =~ /^(?:From:|Return-Path:|Sender:) header$/ !scalar(grep {$_->{'host'} and _registrable($_->{'host'}) eq (_registrable($dom) // $dom);} $self->embedded_urls)
Truth table100%3/3
A && not(B)
A= $d->{'source'} =~ /^(?:From:|Return-Path:|Sender:) header$/B= !scalar(grep {$_->{'host'} and _registrable($_->{'host'}) eq (_registrable($dom) // $dom);} $self->embedded_urls)
ABresult
0X0
100
111
Truth table0%0/3
A && not(B)
A= $d->{'source'} =~ /^(?:From:|Return-Path:|Sender:) header$/B= !scalar(grep {$_->{'host'} and _registrable($_->{'host'}) eq (_registrable($dom) // $dom);} $self->embedded_urls)
ABresult
0X0
100
111
2098 7 ▶ _registrable($_->{host}) eq (_registrable($dom) // $dom)
Condition25%3/12
$_->{'host'} && _registrable($_->{'host'}) eq (_registrable($dom) // $dom)
!ll&&!rl&&r
013
_registrable($dom) // $dom
l!l&&r!l&&!r
400
$_->{'host'} && _registrable($_->{'host'}) eq (_registrable($dom) // $dom)
!ll&&!rl&&r
000
_registrable($dom) // $dom
l!l&&r!l&&!r
000
MC/DC50%1/2
$_->{'host'} && _registrable($_->{'host'}) eq (_registrable($dom) // $dom)
$_->{'host'} _registrable($_->{'host'}) eq (_registrable($dom) // $dom)
MC/DC0%0/2
_registrable($dom) // $dom
_registrable($dom) $dom
MC/DC0%0/2
$_->{'host'} && _registrable($_->{'host'}) eq (_registrable($dom) // $dom)
$_->{'host'} _registrable($_->{'host'}) eq (_registrable($dom) // $dom)
MC/DC0%0/2
_registrable($dom) // $dom
_registrable($dom) $dom
Truth table66%2/3
A && B
A= $_->{'host'}B= _registrable($_->{'host'}) eq (_registrable($dom) // $dom)
ABresult
0X0
100
111
Truth table33%1/3
A // B
A= _registrable($dom)B= $dom
ABresult
1X1
011
000
Truth table0%0/3
A && B
A= $_->{'host'}B= _registrable($_->{'host'}) eq (_registrable($dom) // $dom)
ABresult
0X0
100
111
Truth table0%0/3
A // B
A= _registrable($dom)B= $dom
ABresult
1X1
011
000
2099 } $self->embedded_urls());
2100 37 ▶ unless ($spoofable_only) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($spoofable_only) 27 7 34
unless ($spoofable_only) 0 0 0
2101 $add->(
2102 role => "Domain registrar for $dom",
2103 address => $d->{registrar_abuse},
2104 30 ▶ note => 'Registrar: ' . ($d->{registrar} // '(unknown)'),
Condition50%2/4
$d->{"registrar"} // "(unknown)"
l!l
1116
$d->{"registrar"} // "(unknown)"
l!l
00
MC/DC100%1/1
$d->{"registrar"} // "(unknown)"
$d->{"registrar"}
MC/DC0%0/1
$d->{"registrar"} // "(unknown)"
$d->{"registrar"}
Truth table100%2/2
A
A= $d->{"registrar"}
Aresult
11
00
Truth table0%0/2
A
A= $d->{"registrar"}
Aresult
11
00
2105 via => 'domain-whois',
2106 );
2107 }
2108 }
2109 }
2110  
2111 # Route 4 -- From:/Reply-To:/Return-Path:/Sender: account provider
2112 185 for my $hname (qw(from reply-to return-path sender)) {
2113 731 ▶ my $val = $self->_header_value($hname) // next;
Condition60%3/5
$self->_header_value($hname) // (next)
l!l
315413
$self->_header_value($hname) // (next)
l!l
100
MC/DC100%1/1
$self->_header_value($hname) // (next)
$self->_header_value($hname)
MC/DC0%0/1
$self->_header_value($hname) // (next)
$self->_header_value($hname)
Truth table100%2/2
A
A= $self->_header_value($hname)
Aresult
11
00
Truth table50%1/2
A
A= $self->_header_value($hname)
Aresult
11
00
2114  
2115 # Extract addr-spec from angle-bracket form to avoid display-name @-signs
2116 318 ▶ my $addr_spec = ($val =~ /<([^>]*)>\s*$/) ? $1 : $val;
Branch: 2 branches
BranchTrueFalseTotal
$val =~ /<([^>]*)>\s*$/ ? : 257 58 315
$val =~ /<([^>]*)>\s*$/ ? : 0 0 0
2117 318 my ($addr_domain) = $addr_spec =~ /\@([\w.-]+)/;
2118 318 ▶ next unless $addr_domain;
Branch: 2 branches
BranchTrueFalseTotal
unless $addr_domain 1 314 315
unless $addr_domain 0 0 0
2119  
2120 # Skip SRS-rewritten forwarder addresses (not the real sender)
2121 317 ▶ next if $addr_spec =~ /\+SRS[0-9]?=/i;
Branch: 2 branches
BranchTrueFalseTotal
if $addr_spec =~ /\+SRS[0-9]?=/i 2 312 314
if $addr_spec =~ /\+SRS[0-9]?=/i 0 0 0
2122  
2123 315 my $pa = $self->_provider_abuse_for_host($addr_domain);
2124 315 ▶ if ($pa) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pa) 40 272 312
if ($pa) 0 0 0
2125 43 ▶ my $role_addr = $addr_spec =~ /\@/ ? $addr_spec : $val;
Branch: 2 branches
BranchTrueFalseTotal
$addr_spec =~ /\@/ ? : 40 0 40
$addr_spec =~ /\@/ ? : 0 0 0
2126 43 $role_addr =~ s/^\s+|\s+$//g;
2127 $add->(
2128 role => "Account provider ($hname: $role_addr)",
2129 address => $pa->{email},
2130 note => $pa->{note},
2131 43 via => 'provider-table',
2132 );
2133 }
2134 }
2135  
2136 # Route 5 -- DKIM signing organisation
2137 185 my $auth = $self->_parse_auth_results_cached();
2138 185 ▶ if ($auth->{dkim_domain}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($auth->{"dkim_domain"}) 2 180 182
if ($auth->{"dkim_domain"}) 0 0 0
2139 5 my $pa = $self->_provider_abuse_for_host($auth->{dkim_domain});
2140 5 ▶ if ($pa) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pa) 1 1 2
if ($pa) 0 0 0
2141 $add->(
2142 role => "DKIM signer: $auth->{dkim_domain}",
2143 address => $pa->{email},
2144 note => $pa->{note},
2145 4 via => 'provider-table',
2146 );
2147 }
2148 }
2149  
2150 # Route 6 -- List-Unsubscribe ESP domain
2151 185 my $unsub = $self->_header_value('list-unsubscribe');
2152 185 ▶ if ($unsub) {
Branch: 2 branches
BranchTrueFalseTotal
if ($unsub) 0 182 182
if ($unsub) 0 0 0
2153 3 my @unsub_domains;
2154 3 ▶ while ($unsub =~ m{https?://([^/:?\s>]+)}gi) {
Branch: 2 branches
BranchTrueFalseTotal
if ($unsub =~ m[https?://([^/:?\s>]+)]gi) 0 0 0
if ($unsub =~ m[https?://([^/:?\s>]+)]gi) 0 0 0
2155 3 push @unsub_domains, lc $1;
2156 }
2157 3 ▶ while ($unsub =~ m{mailto:[^@\s>]+\@([\w.-]+)}gi) {
Branch: 2 branches
BranchTrueFalseTotal
if ($unsub =~ /mailto:[^\@\s>]+\@([\w.-]+)/gi) 0 0 0
if ($unsub =~ /mailto:[^\@\s>]+\@([\w.-]+)/gi) 0 0 0
2158 3 push @unsub_domains, lc $1;
2159 }
2160 3 my %unsub_seen;
2161 3 for my $dom (grep { !$unsub_seen{$_}++ } @unsub_domains) {
2162 3 my $pa = $self->_provider_abuse_for_host($dom);
2163 3 ▶ if ($pa) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pa) 0 0 0
if ($pa) 0 0 0
2164 $add->(
2165 role => "ESP / bulk sender (List-Unsubscribe: $dom)",
2166 address => $pa->{email},
2167 3 note => "$pa->{note} -- responsible for this bulk delivery",
2168 via => 'provider-table',
2169 );
2170 }
2171 }
2172 }
2173  
2174 # Route 7 -- Reply addresses embedded in the message body
2175 185 my %body_addr_seen;
2176 185 my $combined_body = $self->{_body_plain} . "\n" . $self->{_body_html};
2177 185 for my $addr_dom ($self->_domains_from_text($combined_body)) {
2178 22 ▶ next if $body_addr_seen{$addr_dom}++;
Branch: 2 branches
BranchTrueFalseTotal
if $body_addr_seen{$addr_dom}++ 0 19 19
if $body_addr_seen{$addr_dom}++ 0 0 0
2179 22 my $pa = $self->_provider_abuse_for_host($addr_dom);
2180 22 ▶ next unless $pa && $pa->{email};
Branch: 2 branches
BranchTrueFalseTotal
unless $pa and $pa->{"email"} 15 4 19
unless $pa and $pa->{"email"} 0 0 0
Condition33%2/6
$pa and $pa->{"email"}
!ll&&!rl&&r
1504
$pa and $pa->{"email"}
!ll&&!rl&&r
000
MC/DC50%1/2
$pa and $pa->{"email"}
$pa $pa->{"email"}
MC/DC0%0/2
$pa and $pa->{"email"}
$pa $pa->{"email"}
Truth table66%2/3
A and B
A= $paB= $pa->{"email"}
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $paB= $pa->{"email"}
ABresult
0X0
100
111
2181 7 my ($example_addr) = $combined_body =~ /(\S+\@\Q$addr_dom\E)/i;
2182 7 ▶ $example_addr //= "\@$addr_dom";
Condition25%1/4
$example_addr //= "\@$addr_dom"
l!l
40
$example_addr //= "\@$addr_dom"
l!l
00
MC/DC0%0/1
$example_addr //= "\@$addr_dom"
$example_addr
MC/DC0%0/1
$example_addr //= "\@$addr_dom"
$example_addr
Truth table50%1/2
A
A= $example_addr
Aresult
11
00
Truth table0%0/2
A
A= $example_addr
Aresult
11
00
2183 $add->(
2184 role => "Reply address in body ($example_addr)",
2185 address => $pa->{email},
2186 note => $pa->{note},
2187 7 via => 'provider-table',
2188 );
2189 }
2190  
2191 185 return @contacts;
2192 24 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
2193  
2194 # -----------------------------------------------------------------------
2195 # Public: form contacts (providers that require web-form submission)
2196 # -----------------------------------------------------------------------
2197  
2198 =head2 form_contacts()
2199  
2200 Returns the list of parties that require abuse reports via a web form
2201 rather than email. These are providers whose C<%PROVIDER_ABUSE> entry
2202 has a C<form> key. Each hashref includes the form URL, paste
2203 instructions, upload instructions, and the discovery role.
2204  
2205 =head3 Usage
2206  
2207 my @forms = $analyser->form_contacts();
2208 for my $c (@forms) {
2209 printf "Open: %s\n", $c->{form};
2210 }
2211  
2212 =head3 Arguments
2213  
2214 None. C<parse_email()> must have been called first.
2215  
2216 =head3 Returns
2217  
2218 A list of hashrefs, one per unique form contact. Each hashref has keys
2219 C<form>, C<role>, C<note>, C<form_paste> (optional), C<form_upload>
2220 (optional), and C<via>. Returns an empty list if no form contacts are found.
2221  
2222 =head3 Side Effects
2223  
2224 Triggers C<originating_ip()>, C<embedded_urls()>, and C<mailto_domains()>
2225 if not already cached.
2226  
2227 =head3 Notes
2228  
2229 Deduplication is by form URL.
2230  
2231 =head3 API Specification
2232  
2233 =head4 Input
2234  
2235 []
2236  
2237 =head4 Output
2238  
2239 (
2240 {
2241 type => 'hashref',
2242 keys => {
2243 form => { type => 'string', regex => qr{^https?://} },
2244 role => { type => 'string' },
2245 note => { type => 'string' },
2246 form_paste => { type => 'string', optional => 1 },
2247 form_upload => { type => 'string', optional => 1 },
2248 via => { type => 'string' },
2249 },
2250 },
2251 ...
2252 )
2253  
2254 =cut
2255  
2256 169 ▶sub form_contacts {
Subroutine form_contacts: called CC=53 SCAR=61.2
Subroutine form_contacts: called CC=53 SCAR=61.2
Pod documented
2257 167 my $self = $_[0];
2258  
2259 167 my (@contacts, %seen);
2260  
2261 # Inner closure: add one form-contact entry, deduplicating by form URL
2262 43 ▶ my $add = sub {
Subroutine __ANON__: called CC=4 SCAR=16.9
Subroutine __ANON__: called CC=4 SCAR=16.9
2263 42 my (%args) = @_;
2264 42 ▶ my $form = $args{form} // '';
Condition20%1/5
$args{"form"} // ""
l!l
3900
$args{"form"} // ""
l!l
00
MC/DC100%1/1
$args{"form"} // ""
$args{"form"}
MC/DC0%0/1
$args{"form"} // ""
$args{"form"}
Truth table100%2/2
A
A= $args{"form"}
Aresult
11
00
Truth table0%0/2
A
A= $args{"form"}
Aresult
11
00
2265 42 ▶ return unless $form;
Branch: 2 branches
BranchTrueFalseTotal
unless $form 1 38 39
unless $form 0 0 0
2266 42 ▶ return if $seen{$form}++;
Branch: 2 branches
BranchTrueFalseTotal
if $seen{$form}++ 10 29 39
if $seen{$form}++ 0 0 0
2267 32 push @contacts, \%args;
2268 167 };
2269  
2270 # Route 1 -- Sending ISP
2271 167 my $orig = $self->originating_ip();
2272 167 ▶ if ($orig) {
Branch: 2 branches
BranchTrueFalseTotal
if ($orig) 94 70 164
if ($orig) 0 0 0
2273 97 my $pa = $self->_provider_abuse_for_ip($orig->{ip}, $orig->{rdns});
2274 97 ▶ if ($pa && $pa->{form}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pa and $pa->{"form"}) 0 94 94
if ($pa and $pa->{"form"}) 0 0 0
Condition33%2/6
$pa and $pa->{"form"}
!ll&&!rl&&r
8860
$pa and $pa->{"form"}
!ll&&!rl&&r
000
MC/DC0%0/2
$pa and $pa->{"form"}
$pa $pa->{"form"}
MC/DC0%0/2
$pa and $pa->{"form"}
$pa $pa->{"form"}
Truth table66%2/3
A and B
A= $paB= $pa->{"form"}
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $paB= $pa->{"form"}
ABresult
0X0
100
111
2275 $add->(
2276 role => 'Sending ISP',
2277 form => $pa->{form},
2278 note => $pa->{note} // '',
2279 form_paste => $pa->{form_paste} // '',
2280 3 ▶ form_upload => $pa->{form_upload} // '',
Condition6%1/16
$pa->{"note"} // ""
l!l
000
$pa->{"form_paste"} // ""
l!l
000
$pa->{"form_upload"} // ""
l!l
000
$pa->{"note"} // ""
l!l
200
$pa->{"form_paste"} // ""
l!l
00
$pa->{"form_upload"} // ""
l!l
00
MC/DC0%0/1
$pa->{"note"} // ""
$pa->{"note"}
MC/DC0%0/1
$pa->{"form_paste"} // ""
$pa->{"form_paste"}
MC/DC0%0/1
$pa->{"form_upload"} // ""
$pa->{"form_upload"}
MC/DC0%0/1
$pa->{"note"} // ""
$pa->{"note"}
MC/DC0%0/1
$pa->{"form_paste"} // ""
$pa->{"form_paste"}
MC/DC0%0/1
$pa->{"form_upload"} // ""
$pa->{"form_upload"}
Truth table0%0/2
A
A= $pa->{"note"}
Aresult
11
00
Truth table0%0/2
A
A= $pa->{"form_paste"}
Aresult
11
00
Truth table0%0/2
A
A= $pa->{"form_upload"}
Aresult
11
00
Truth table50%1/2
A
A= $pa->{"note"}
Aresult
11
00
Truth table0%0/2
A
A= $pa->{"form_paste"}
Aresult
11
00
Truth table0%0/2
A
A= $pa->{"form_upload"}
Aresult
11
00
2281 via => 'provider-table',
2282 );
2283 }
2284 }
2285  
2286 # Route 2 -- URL hosts
2287 167 my %url_host_seen;
2288 167 for my $u ($self->embedded_urls()) {
2289 73 ▶ next if $url_host_seen{ $u->{host} }++;
Branch: 2 branches
BranchTrueFalseTotal
if $url_host_seen{$u->{"host"}}++ 11 59 70
if $url_host_seen{$u->{"host"}}++ 0 0 0
2290 62 my $pa = $self->_provider_abuse_for_host($u->{host});
2291 62 ▶ if ($pa && $pa->{form}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pa and $pa->{"form"}) 3 56 59
if ($pa and $pa->{"form"}) 0 0 0
Condition50%3/6
$pa and $pa->{"form"}
!ll&&!rl&&r
5153
$pa and $pa->{"form"}
!ll&&!rl&&r
000
MC/DC100%2/2
$pa and $pa->{"form"}
$pa $pa->{"form"}
MC/DC0%0/2
$pa and $pa->{"form"}
$pa $pa->{"form"}
Truth table100%3/3
A and B
A= $paB= $pa->{"form"}
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $paB= $pa->{"form"}
ABresult
0X0
100
111
2292 $add->(
2293 role => "URL host: $u->{host}",
2294 form => $pa->{form},
2295 form_domain => $u->{host},
2296 note => $pa->{note} // '',
2297 form_paste => $pa->{form_paste} // '',
2298 6 ▶ form_upload => $pa->{form_upload} // '',
Condition40%6/15
$pa->{"note"} // ""
l!l
30
$pa->{"form_paste"} // ""
l!l
30
$pa->{"form_upload"} // ""
l!l
300
$pa->{"note"} // ""
l!l
101
$pa->{"form_paste"} // ""
l!l
100
$pa->{"form_upload"} // ""
l!l
00
MC/DC0%0/1
$pa->{"note"} // ""
$pa->{"note"}
MC/DC0%0/1
$pa->{"form_paste"} // ""
$pa->{"form_paste"}
MC/DC0%0/1
$pa->{"form_upload"} // ""
$pa->{"form_upload"}
MC/DC100%1/1
$pa->{"note"} // ""
$pa->{"note"}
MC/DC0%0/1
$pa->{"form_paste"} // ""
$pa->{"form_paste"}
MC/DC0%0/1
$pa->{"form_upload"} // ""
$pa->{"form_upload"}
Truth table50%1/2
A
A= $pa->{"note"}
Aresult
11
00
Truth table50%1/2
A
A= $pa->{"form_paste"}
Aresult
11
00
Truth table50%1/2
A
A= $pa->{"form_upload"}
Aresult
11
00
Truth table100%2/2
A
A= $pa->{"note"}
Aresult
11
00
Truth table50%1/2
A
A= $pa->{"form_paste"}
Aresult
11
00
Truth table0%0/2
A
A= $pa->{"form_upload"}
Aresult
11
00
2299 via => 'provider-table',
2300 );
2301 }
2302 }
2303  
2304 # Route 3 -- Contact domains (web host + registrar)
2305 167 for my $d ($self->mailto_domains()) {
2306 165 my $dom = $d->{domain};
2307 165 my $pa = $self->_provider_abuse_for_host($dom);
2308 165 ▶ if ($pa && $pa->{form}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pa and $pa->{"form"}) 7 155 162
if ($pa and $pa->{"form"}) 0 0 0
Condition33%2/6
$pa and $pa->{"form"}
!ll&&!rl&&r
15507
$pa and $pa->{"form"}
!ll&&!rl&&r
000
MC/DC50%1/2
$pa and $pa->{"form"}
$pa $pa->{"form"}
MC/DC0%0/2
$pa and $pa->{"form"}
$pa $pa->{"form"}
Truth table66%2/3
A and B
A= $paB= $pa->{"form"}
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $paB= $pa->{"form"}
ABresult
0X0
100
111
2309 $add->(
2310 role => "Web host of $dom",
2311 form => $pa->{form},
2312 form_domain => $dom,
2313 note => $pa->{note} // '',
2314 form_paste => $pa->{form_paste} // '',
2315 10 ▶ form_upload => $pa->{form_upload} // '',
Condition42%6/14
$pa->{"note"} // ""
l!l
700
$pa->{"form_paste"} // ""
l!l
70
$pa->{"form_upload"} // ""
l!l
70
$pa->{"note"} // ""
l!l
00
$pa->{"form_paste"} // ""
l!l
31
$pa->{"form_upload"} // ""
l!l
300
MC/DC0%0/1
$pa->{"note"} // ""
$pa->{"note"}
MC/DC0%0/1
$pa->{"form_paste"} // ""
$pa->{"form_paste"}
MC/DC0%0/1
$pa->{"form_upload"} // ""
$pa->{"form_upload"}
MC/DC0%0/1
$pa->{"note"} // ""
$pa->{"note"}
MC/DC100%1/1
$pa->{"form_paste"} // ""
$pa->{"form_paste"}
MC/DC0%0/1
$pa->{"form_upload"} // ""
$pa->{"form_upload"}
Truth table50%1/2
A
A= $pa->{"note"}
Aresult
11
00
Truth table50%1/2
A
A= $pa->{"form_paste"}
Aresult
11
00
Truth table50%1/2
A
A= $pa->{"form_upload"}
Aresult
11
00
Truth table0%0/2
A
A= $pa->{"note"}
Aresult
11
00
Truth table100%2/2
A
A= $pa->{"form_paste"}
Aresult
11
00
Truth table50%1/2
A
A= $pa->{"form_upload"}
Aresult
11
00
2316 via => 'provider-table',
2317 );
2318 }
2319  
2320 # Registrar identified via WHOIS -- check for form-only registrar
2321 165 ▶ if ($d->{registrar_abuse} && $d->{registrar_abuse} =~ /\@([\w.-]+)/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($d->{"registrar_abuse"} and $d->{"registrar_abuse"} =~ /\@([\w.-]+)/) 22 140 162
if ($d->{"registrar_abuse"} and $d->{"registrar_abuse"} =~ /\@([\w.-]+)/) 0 0 0
Condition33%2/6
$d->{"registrar_abuse"} and $d->{"registrar_abuse"} =~ /\@([\w.-]+)/
!ll&&!rl&&r
140022
$d->{"registrar_abuse"} and $d->{"registrar_abuse"} =~ /\@([\w.-]+)/
!ll&&!rl&&r
000
MC/DC50%1/2
$d->{"registrar_abuse"} and $d->{"registrar_abuse"} =~ /\@([\w.-]+)/
$d->{"registrar_abuse"} $d->{"registrar_abuse"} =~ /\@([\w.-]+)/
MC/DC0%0/2
$d->{"registrar_abuse"} and $d->{"registrar_abuse"} =~ /\@([\w.-]+)/
$d->{"registrar_abuse"} $d->{"registrar_abuse"} =~ /\@([\w.-]+)/
Truth table66%2/3
A and B
A= $d->{"registrar_abuse"}B= $d->{"registrar_abuse"} =~ /\@([\w.-]+)/
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $d->{"registrar_abuse"}B= $d->{"registrar_abuse"} =~ /\@([\w.-]+)/
ABresult
0X0
100
111
2322 25 my $reg_domain = lc $1;
2323 25 my $rpa = $self->_provider_abuse_for_host($reg_domain);
2324 25 ▶ if ($rpa && $rpa->{form}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rpa and $rpa->{"form"}) 14 8 22
if ($rpa and $rpa->{"form"}) 0 0 0
Condition50%3/6
$rpa and $rpa->{"form"}
!ll&&!rl&&r
6214
$rpa and $rpa->{"form"}
!ll&&!rl&&r
000
MC/DC100%2/2
$rpa and $rpa->{"form"}
$rpa $rpa->{"form"}
MC/DC0%0/2
$rpa and $rpa->{"form"}
$rpa $rpa->{"form"}
Truth table100%3/3
A and B
A= $rpaB= $rpa->{"form"}
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $rpaB= $rpa->{"form"}
ABresult
0X0
100
111
2325 $add->(
2326 role => "Domain registrar for $dom (web form only)",
2327 form => $rpa->{form},
2328 form_domain => $dom,
2329 note => $rpa->{note} // '',
2330 form_paste => $rpa->{form_paste} // '',
2331 17 ▶ form_upload => $rpa->{form_upload} // '',
Condition23%3/13
$rpa->{"note"} // ""
l!l
140
$rpa->{"form_paste"} // ""
l!l
140
$rpa->{"form_upload"} // ""
l!l
140
$rpa->{"note"} // ""
l!l
00
$rpa->{"form_paste"} // ""
l!l
000
$rpa->{"form_upload"} // ""
l!l
00
MC/DC0%0/1
$rpa->{"note"} // ""
$rpa->{"note"}
MC/DC0%0/1
$rpa->{"form_paste"} // ""
$rpa->{"form_paste"}
MC/DC0%0/1
$rpa->{"form_upload"} // ""
$rpa->{"form_upload"}
MC/DC0%0/1
$rpa->{"note"} // ""
$rpa->{"note"}
MC/DC0%0/1
$rpa->{"form_paste"} // ""
$rpa->{"form_paste"}
MC/DC0%0/1
$rpa->{"form_upload"} // ""
$rpa->{"form_upload"}
Truth table50%1/2
A
A= $rpa->{"note"}
Aresult
11
00
Truth table50%1/2
A
A= $rpa->{"form_paste"}
Aresult
11
00
Truth table50%1/2
A
A= $rpa->{"form_upload"}
Aresult
11
00
Truth table0%0/2
A
A= $rpa->{"note"}
Aresult
11
00
Truth table0%0/2
A
A= $rpa->{"form_paste"}
Aresult
11
00
Truth table0%0/2
A
A= $rpa->{"form_upload"}
Aresult
11
00
2332 via => 'provider-table',
2333 );
2334 }
2335 }
2336 }
2337  
2338 # Route 4 -- Account provider headers
2339 167 for my $hname (qw(from reply-to return-path sender)) {
2340 659 ▶ my $val = $self->_header_value($hname) // next;
Condition50%2/4
$self->_header_value($hname) // (next)
l!l
276380
$self->_header_value($hname) // (next)
l!l
00
MC/DC100%1/1
$self->_header_value($hname) // (next)
$self->_header_value($hname)
MC/DC0%0/1
$self->_header_value($hname) // (next)
$self->_header_value($hname)
Truth table100%2/2
A
A= $self->_header_value($hname)
Aresult
11
00
Truth table0%0/2
A
A= $self->_header_value($hname)
Aresult
11
00
2341 279 ▶ my $addr_spec = ($val =~ /<([^>]*)>\s*$/) ? $1 : $val;
Branch: 2 branches
BranchTrueFalseTotal
$val =~ /<([^>]*)>\s*$/ ? : 240 36 276
$val =~ /<([^>]*)>\s*$/ ? : 0 0 0
2342 279 my ($addr_domain) = $addr_spec =~ /\@([\w.-]+)/;
2343 279 ▶ next unless $addr_domain;
Branch: 2 branches
BranchTrueFalseTotal
unless $addr_domain 0 276 276
unless $addr_domain 0 0 0
2344 # Skip SRS forwarder rewrite addresses
2345 279 ▶ next if $addr_spec =~ /\+SRS[0-9]?=/i;
Branch: 2 branches
BranchTrueFalseTotal
if $addr_spec =~ /\+SRS[0-9]?=/i 0 276 276
if $addr_spec =~ /\+SRS[0-9]?=/i 0 0 0
2346 279 my $pa = $self->_provider_abuse_for_host($addr_domain);
2347 279 ▶ if ($pa && $pa->{form}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pa and $pa->{"form"}) 11 265 276
if ($pa and $pa->{"form"}) 0 0 0
Condition50%3/6
$pa and $pa->{"form"}
!ll&&!rl&&r
2442111
$pa and $pa->{"form"}
!ll&&!rl&&r
000
MC/DC100%2/2
$pa and $pa->{"form"}
$pa $pa->{"form"}
MC/DC0%0/2
$pa and $pa->{"form"}
$pa $pa->{"form"}
Truth table100%3/3
A and B
A= $paB= $pa->{"form"}
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $paB= $pa->{"form"}
ABresult
0X0
100
111
2348 14 ▶ my $role_addr = $addr_spec =~ /@/ ? $addr_spec : $val;
Branch: 2 branches
BranchTrueFalseTotal
$addr_spec =~ /@/ ? : 11 0 11
$addr_spec =~ /@/ ? : 0 0 0
2349 14 $role_addr =~ s/^\s+|\s+$//g;
2350 $add->(
2351 role => "Account provider ($hname: $role_addr)",
2352 form => $pa->{form},
2353 note => $pa->{note} // '',
2354 form_paste => $pa->{form_paste} // '',
2355 14 ▶ form_upload => $pa->{form_upload} // '',
Condition23%3/13
$pa->{"note"} // ""
l!l
110
$pa->{"form_paste"} // ""
l!l
110
$pa->{"form_upload"} // ""
l!l
110
$pa->{"note"} // ""
l!l
000
$pa->{"form_paste"} // ""
l!l
00
$pa->{"form_upload"} // ""
l!l
00
MC/DC0%0/1
$pa->{"note"} // ""
$pa->{"note"}
MC/DC0%0/1
$pa->{"form_paste"} // ""
$pa->{"form_paste"}
MC/DC0%0/1
$pa->{"form_upload"} // ""
$pa->{"form_upload"}
MC/DC0%0/1
$pa->{"note"} // ""
$pa->{"note"}
MC/DC0%0/1
$pa->{"form_paste"} // ""
$pa->{"form_paste"}
MC/DC0%0/1
$pa->{"form_upload"} // ""
$pa->{"form_upload"}
Truth table50%1/2
A
A= $pa->{"note"}
Aresult
11
00
Truth table50%1/2
A
A= $pa->{"form_paste"}
Aresult
11
00
Truth table50%1/2
A
A= $pa->{"form_upload"}
Aresult
11
00
Truth table0%0/2
A
A= $pa->{"note"}
Aresult
11
00
Truth table0%0/2
A
A= $pa->{"form_paste"}
Aresult
11
00
Truth table0%0/2
A
A= $pa->{"form_upload"}
Aresult
11
00
2356 via => 'provider-table',
2357 );
2358 }
2359 }
2360  
2361 # Route 5 -- DKIM signer
2362 167 my $auth = $self->_parse_auth_results_cached();
2363 167 ▶ if ($auth->{dkim_domain}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($auth->{"dkim_domain"}) 2 162 164
if ($auth->{"dkim_domain"}) 0 0 0
2364 5 my $pa = $self->_provider_abuse_for_host($auth->{dkim_domain});
2365 5 ▶ if ($pa && $pa->{form}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pa and $pa->{"form"}) 1 1 2
if ($pa and $pa->{"form"}) 0 0 0
Condition33%2/6
$pa and $pa->{"form"}
!ll&&!rl&&r
101
$pa and $pa->{"form"}
!ll&&!rl&&r
000
MC/DC50%1/2
$pa and $pa->{"form"}
$pa $pa->{"form"}
MC/DC0%0/2
$pa and $pa->{"form"}
$pa $pa->{"form"}
Truth table66%2/3
A and B
A= $paB= $pa->{"form"}
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $paB= $pa->{"form"}
ABresult
0X0
100
111
2366 $add->(
2367 role => "DKIM signer: $auth->{dkim_domain}",
2368 form => $pa->{form},
2369 note => $pa->{note} // '',
2370 form_paste => $pa->{form_paste} // '',
2371 4 ▶ form_upload => $pa->{form_upload} // '',
Condition23%3/13
$pa->{"note"} // ""
l!l
10
$pa->{"form_paste"} // ""
l!l
10
$pa->{"form_upload"} // ""
l!l
10
$pa->{"note"} // ""
l!l
000
$pa->{"form_paste"} // ""
l!l
00
$pa->{"form_upload"} // ""
l!l
00
MC/DC0%0/1
$pa->{"note"} // ""
$pa->{"note"}
MC/DC0%0/1
$pa->{"form_paste"} // ""
$pa->{"form_paste"}
MC/DC0%0/1
$pa->{"form_upload"} // ""
$pa->{"form_upload"}
MC/DC0%0/1
$pa->{"note"} // ""
$pa->{"note"}
MC/DC0%0/1
$pa->{"form_paste"} // ""
$pa->{"form_paste"}
MC/DC0%0/1
$pa->{"form_upload"} // ""
$pa->{"form_upload"}
Truth table50%1/2
A
A= $pa->{"note"}
Aresult
11
00
Truth table50%1/2
A
A= $pa->{"form_paste"}
Aresult
11
00
Truth table50%1/2
A
A= $pa->{"form_upload"}
Aresult
11
00
Truth table0%0/2
A
A= $pa->{"note"}
Aresult
11
00
Truth table0%0/2
A
A= $pa->{"form_paste"}
Aresult
11
00
Truth table0%0/2
A
A= $pa->{"form_upload"}
Aresult
11
00
2372 via => 'provider-table',
2373 );
2374 }
2375 }
2376  
2377 # Route 6 -- List-Unsubscribe ESP domains
2378 167 my $unsub = $self->_header_value('list-unsubscribe');
2379 167 ▶ if ($unsub) {
Branch: 2 branches
BranchTrueFalseTotal
if ($unsub) 2 162 164
if ($unsub) 0 0 0
2380 5 my @unsub_domains;
2381 5 ▶ while ($unsub =~ m{https?://([^/:?\s>]+)}gi) { push @unsub_domains, lc $1 }
Branch: 2 branches
BranchTrueFalseTotal
if ($unsub =~ m[https?://([^/:?\s>]+)]gi) 1 2 3
if ($unsub =~ m[https?://([^/:?\s>]+)]gi) 0 0 0
2382 5 ▶ while ($unsub =~ m{mailto:[^@\s>]+\@([\w.-]+)}gi) { push @unsub_domains, lc $1 }
Branch: 2 branches
BranchTrueFalseTotal
if ($unsub =~ /mailto:[^\@\s>]+\@([\w.-]+)/gi) 1 2 3
if ($unsub =~ /mailto:[^\@\s>]+\@([\w.-]+)/gi) 0 0 0
2383 5 my %useen;
2384 5 for my $dom (grep { !$useen{$_}++ } @unsub_domains) {
2385 5 my $pa = $self->_provider_abuse_for_host($dom);
2386 5 ▶ if ($pa && $pa->{form}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pa and $pa->{"form"}) 2 0 2
if ($pa and $pa->{"form"}) 0 0 0
Condition16%1/6
$pa and $pa->{"form"}
!ll&&!rl&&r
002
$pa and $pa->{"form"}
!ll&&!rl&&r
000
MC/DC0%0/2
$pa and $pa->{"form"}
$pa $pa->{"form"}
MC/DC0%0/2
$pa and $pa->{"form"}
$pa $pa->{"form"}
Truth table33%1/3
A and B
A= $paB= $pa->{"form"}
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $paB= $pa->{"form"}
ABresult
0X0
100
111
2387 $add->(
2388 role => "ESP / bulk sender (List-Unsubscribe: $dom)",
2389 form => $pa->{form},
2390 note => $pa->{note} // '',
2391 form_paste => $pa->{form_paste} // '',
2392 5 ▶ form_upload => $pa->{form_upload} // '',
Condition23%3/13
$pa->{"note"} // ""
l!l
20
$pa->{"form_paste"} // ""
l!l
20
$pa->{"form_upload"} // ""
l!l
20
$pa->{"note"} // ""
l!l
000
$pa->{"form_paste"} // ""
l!l
00
$pa->{"form_upload"} // ""
l!l
00
MC/DC0%0/1
$pa->{"note"} // ""
$pa->{"note"}
MC/DC0%0/1
$pa->{"form_paste"} // ""
$pa->{"form_paste"}
MC/DC0%0/1
$pa->{"form_upload"} // ""
$pa->{"form_upload"}
MC/DC0%0/1
$pa->{"note"} // ""
$pa->{"note"}
MC/DC0%0/1
$pa->{"form_paste"} // ""
$pa->{"form_paste"}
MC/DC0%0/1
$pa->{"form_upload"} // ""
$pa->{"form_upload"}
Truth table50%1/2
A
A= $pa->{"note"}
Aresult
11
00
Truth table50%1/2
A
A= $pa->{"form_paste"}
Aresult
11
00
Truth table50%1/2
A
A= $pa->{"form_upload"}
Aresult
11
00
Truth table0%0/2
A
A= $pa->{"note"}
Aresult
11
00
Truth table0%0/2
A
A= $pa->{"form_paste"}
Aresult
11
00
Truth table0%0/2
A
A= $pa->{"form_upload"}
Aresult
11
00
2393 via => 'provider-table',
2394 );
2395 }
2396 }
2397 }
2398  
2399 167 return @contacts;
2400 }
2401  
2402 # -----------------------------------------------------------------------
2403 # Public: full analyst report
2404 # -----------------------------------------------------------------------
2405  
2406 =head2 report()
2407  
2408 Produces a comprehensive, analyst-facing plain-text report covering all
2409 findings: envelope fields, risk assessment, originating host, sending
2410 software, received chain tracking IDs, embedded URLs, contact domain
2411 intelligence, and recommended abuse contacts.
2412  
2413 Use C<report()> for human review or ticketing systems. Use
2414 C<abuse_report_text()> for sending to ISP abuse desks.
2415  
2416 =head3 Usage
2417  
2418 print $analyser->report();
2419  
2420 open my $fh, '>', 'report.txt' or croak "Cannot open: $!";
2421 print $fh $analyser->report();
2422 close $fh;
2423  
2424 =head3 Arguments
2425  
2426 None. C<parse_email()> must have been called first.
2427  
2428 =head3 Returns
2429  
2430 A plain scalar string, newline-terminated, Unix line endings. Never empty
2431 or undef.
2432  
2433 =head3 Side Effects
2434  
2435 Triggers all analysis methods if not already cached.
2436  
2437 =head3 Notes
2438  
2439 The report is idempotent: calling it multiple times on the same object
2440 always returns an identical string. All user-derived content is sanitised
2441 before output.
2442  
2443 =head3 API Specification
2444  
2445 =head4 Input
2446  
2447 []
2448  
2449 =head4 Output
2450  
2451 { type => 'string' }
2452  
2453 =cut
2454  
2455 81 ▶sub report {
Subroutine report: called CC=66 SCAR=42.4
Subroutine report: called CC=66 SCAR=42.4
Pod documented
2456 80 my $self = $_[0];
2457  
2458 80 my @out;
2459  
2460 # Banner header
2461 79 push @out, '=' x 72;
2462 80 push @out, " Email::Abuse::Investigator Report (v$VERSION)";
2463 80 push @out, '=' x 72;
2464 79 push @out, '';
2465  
2466 # Envelope summary -- decode MIME encoded-words for readability
2467 79 for my $f (qw(from reply-to return-path subject date message-id)) {
2468 459 my $v = $self->_header_value($f);
2469 459 ▶ next unless defined $v;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $v 132 324 456
unless defined $v 0 0 0
2470 328 my $decoded = $self->_decode_mime_words($v);
2471 329 my $label = ucfirst($f);
2472 329 ▶ push @out, sprintf(' %-14s : %s', $label,
Branch: 2 branches
BranchTrueFalseTotal
$decoded ne $v ? : 9 315 324
$decoded ne $v ? : 0 0 0
2473 _sanitise_output($decoded ne $v ? "$decoded [encoded: $v]" : $v));
2474 }
2475 81 push @out, '';
2476  
2477 # Risk assessment section
2478 79 my $risk = $self->risk_assessment();
2479 81 push @out, "[ RISK ASSESSMENT: $risk->{level} (score: $risk->{score}) ]";
2480 80 ▶ if (@{ $risk->{flags} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$risk->{'flags'};}) { } 43 35 78
2481 47 for my $f (@{ $risk->{flags} }) {
2482 82 push @out, " [$f->{severity}] " . _sanitise_output($f->{detail});
2483 }
2484 } else {
2485 40 push @out, ' (no specific red flags detected)';
2486 }
2487 81 push @out, '';
2488  
2489 # Originating host section
2490 81 push @out, '[ ORIGINATING HOST ]';
2491 81 ▶ if(my $orig = $self->originating_ip()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $orig = $self->originating_ip) { } 47 31 78
2492 51 push @out, ' IP : ' . _sanitise_output($orig->{ip});
2493 51 ▶ push @out, ' Reverse DNS : ' . _sanitise_output($orig->{rdns}) if $orig->{rdns};
Branch: 1 branch
BranchTrueFalseTotal
if $orig->{"rdns"} 46 1 47
2494 51 ▶ push @out, ' Country : ' . _sanitise_output($orig->{country}) if $orig->{country};
Branch: 1 branch
BranchTrueFalseTotal
if $orig->{"country"} 14 34 48
2495 49 ▶ push @out, ' Organisation : ' . _sanitise_output($orig->{org}) if $orig->{org};
Branch: 1 branch
BranchTrueFalseTotal
if $orig->{"org"} 46 2 48
2496 49 ▶ push @out, ' Abuse addr : ' . _sanitise_output($orig->{abuse}) if $orig->{abuse};
Branch: 1 branch
BranchTrueFalseTotal
if $orig->{"abuse"} 46 2 48
2497 49 push @out, " Confidence : $orig->{confidence}";
2498 50 ▶ push @out, ' Note : ' . _sanitise_output($orig->{note}) if $orig->{note};
Branch: 1 branch
BranchTrueFalseTotal
if $orig->{"note"} 38 10 48
2499 } else {
2500 34 push @out, ' (could not determine originating IP)';
2501 }
2502 79 push @out, '';
2503  
2504 # Sending software section (omitted if none found)
2505 79 my @sw = $self->sending_software();
2506 79 ▶ if (@sw) {
Branch: 1 branch
BranchTrueFalseTotal
if (@sw) 3 73 76
2507 7 push @out, '[ SENDING SOFTWARE / INFRASTRUCTURE CLUES ]';
2508 7 for my $s (@sw) {
2509 7 push @out, sprintf(' %-14s : %s', $s->{header}, _sanitise_output($s->{value}));
2510 7 push @out, " Note : $s->{note}";
2511 7 push @out, '';
2512 }
2513 }
2514  
2515 # Received chain tracking IDs (only hops with id or for are shown)
2516 79 ▶ my @trail = grep { defined $_->{id} || defined $_->{for} }
Condition33%2/6
defined $_->{'id'} || defined $_->{'for'}
l!l&&r!l&&!r
2063
defined $_->{'id'} || defined $_->{'for'}
l!l&&r!l&&!r
000
MC/DC50%1/2
defined $_->{'id'} || defined $_->{'for'}
defined $_->{'id'} defined $_->{'for'}
MC/DC0%0/2
defined $_->{'id'} || defined $_->{'for'}
defined $_->{'id'} defined $_->{'for'}
Truth table66%2/3
A || B
A= defined $_->{'id'}B= defined $_->{'for'}
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= defined $_->{'id'}B= defined $_->{'for'}
ABresult
1X1
011
000
2517 $self->received_trail();
2518 79 ▶ if (@trail) {
Branch: 1 branch
BranchTrueFalseTotal
if (@trail) 2 75 77
2519 5 push @out, '[ RECEIVED CHAIN TRACKING IDs ]';
2520 5 push @out, ' (Supply these to the relevant ISP abuse team to trace the session)';
2521 5 push @out, '';
2522 5 for my $hop (@trail) {
2523 5 ▶ push @out, ' IP : ' . (_sanitise_output($hop->{ip}) // '(unknown)');
Condition25%1/4
_sanitise_output($hop->{"ip"}) // "(unknown)"
l!l
20
_sanitise_output($hop->{"ip"}) // "(unknown)"
l!l
00
MC/DC0%0/1
_sanitise_output($hop->{"ip"}) // "(unknown)"
_sanitise_output($hop->{"ip"})
MC/DC0%0/1
_sanitise_output($hop->{"ip"}) // "(unknown)"
_sanitise_output($hop->{"ip"})
Truth table50%1/2
A
A= _sanitise_output($hop->{"ip"})
Aresult
11
00
Truth table0%0/2
A
A= _sanitise_output($hop->{"ip"})
Aresult
11
00
2524 6 ▶ push @out, ' Envelope for : ' . _sanitise_output($hop->{for}) if $hop->{for};
Branch: 1 branch
BranchTrueFalseTotal
if $hop->{"for"} 2 0 2
2525 6 ▶ push @out, ' Server ID : ' . _sanitise_output($hop->{id}) if $hop->{id};
Branch: 1 branch
BranchTrueFalseTotal
if $hop->{"id"} 2 1 3
2526 6 push @out, '';
2527 }
2528 }
2529  
2530 # Embedded URLs section -- grouped by hostname
2531 80 push @out, '[ EMBEDDED HTTP/HTTPS URLs ]';
2532 80 my @urls = $self->embedded_urls();
2533 80 ▶ if (@urls) {
Branch: 1 branch
BranchTrueFalseTotal
if (@urls) { } 25 51 76
2534 29 my (%host_order, %host_meta, %host_paths);
2535 29 my $seq = 0;
2536 28 for my $u (@urls) {
2537 43 my $h = $u->{host};
2538 43 ▶ unless (exists $host_order{$h}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $host_order{$h}) 29 10 39
2539 30 $host_order{$h} = $seq++;
2540 $host_meta{$h} = {
2541 ip => $u->{ip},
2542 org => $u->{org},
2543 abuse => $u->{abuse},
2544 country => $u->{country},
2545 33 };
2546 }
2547 43 push @{ $host_paths{$h} }, $u->{url};
2548 }
2549  
2550 # Output each host group in first-seen order
2551 29 for my $h (sort { $host_order{$a} <=> $host_order{$b} } keys %host_order) {
2552 33 my $m = $host_meta{$h};
2553 33 my $bare = lc $h; $bare =~ s/^www\.//;
2554 push @out, ' Host : ' . _sanitise_output($h) .
2555 33 ▶ (($URL_SHORTENERS{$bare} || $self->{url_shorteners}->{$bare})
Branch: 1 branch
BranchTrueFalseTotal
$URL_SHORTENERS{$bare} || $self->{'url_shorteners'}{$bare} ? : 4 25 29
Condition33%2/6
$URL_SHORTENERS{$bare} or $self->{"url_shorteners"}{$bare}
l!l&&r!l&&!r
4025
$URL_SHORTENERS{$bare} or $self->{"url_shorteners"}{$bare}
l!l&&r!l&&!r
000
MC/DC50%1/2
$URL_SHORTENERS{$bare} or $self->{"url_shorteners"}{$bare}
$URL_SHORTENERS{$bare} $self->{"url_shorteners"}{$bare}
MC/DC0%0/2
$URL_SHORTENERS{$bare} or $self->{"url_shorteners"}{$bare}
$URL_SHORTENERS{$bare} $self->{"url_shorteners"}{$bare}
Truth table66%2/3
A or B
A= $URL_SHORTENERS{$bare}B= $self->{"url_shorteners"}{$bare}
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= $URL_SHORTENERS{$bare}B= $self->{"url_shorteners"}{$bare}
ABresult
1X1
011
000
2556 ? ' *** URL SHORTENER -- real destination hidden ***' : '');
2557 32 ▶ push @out, ' IP : ' . _sanitise_output($m->{ip}) if $m->{ip};
Branch: 1 branch
BranchTrueFalseTotal
if $m->{"ip"} 30 0 30
2558 32 ▶ push @out, ' Country : ' . _sanitise_output($m->{country}) if $m->{country};
Branch: 1 branch
BranchTrueFalseTotal
if $m->{"country"} 6 24 30
2559 32 ▶ push @out, ' Organisation : ' . _sanitise_output($m->{org}) if $m->{org};
Branch: 1 branch
BranchTrueFalseTotal
if $m->{"org"} 29 1 30
2560 32 ▶ push @out, ' Abuse addr : ' . _sanitise_output($m->{abuse}) if $m->{abuse};
Branch: 1 branch
BranchTrueFalseTotal
if $m->{"abuse"} 29 1 30
2561 33 my @paths = @{ $host_paths{$h} };
2562 34 ▶ if (@paths == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@paths == 1) { } 22 8 30
2563 27 push @out, ' URL : ' . _sanitise_output($paths[0]);
2564 } else {
2565 12 push @out, ' URLs (' . scalar(@paths) . ') :';
2566 12 push @out, ' ' . _sanitise_output($_) for @paths;
2567 }
2568 34 push @out, '';
2569 }
2570 } else {
2571 56 push @out, ' (none found)';
2572 54 push @out, '';
2573 }
2574  
2575 # Contact / reply-to domains section
2576 79 push @out, '[ CONTACT / REPLY-TO DOMAINS ]';
2577 79 my @mdoms = $self->mailto_domains();
2578 80 ▶ if (@mdoms) {
Branch: 1 branch
BranchTrueFalseTotal
if (@mdoms) { } 62 15 77
2579 66 for my $d (@mdoms) {
2580 82 push @out, ' Domain : ' . _sanitise_output($d->{domain});
2581 82 push @out, ' Found in : ' . _sanitise_output($d->{source});
2582 82 ▶ if ($d->{recently_registered}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d->{"recently_registered"}) 4 75 79
2583 8 push @out, ' *** WARNING: RECENTLY REGISTERED - possible phishing domain ***';
2584 }
2585 83 ▶ push @out, ' Registered : ' . $d->{registered} if $d->{registered};
Branch: 1 branch
BranchTrueFalseTotal
if $d->{"registered"} 7 74 81
2586 82 ▶ push @out, ' Expires : ' . $d->{expires} if $d->{expires};
Branch: 1 branch
BranchTrueFalseTotal
if $d->{"expires"} 5 76 81
2587 82 ▶ push @out, ' Registrar : ' . _sanitise_output($d->{registrar}) if $d->{registrar};
Branch: 1 branch
BranchTrueFalseTotal
if $d->{"registrar"} 9 72 81
2588 82 ▶ push @out, ' Reg. abuse : ' . _sanitise_output($d->{registrar_abuse}) if $d->{registrar_abuse};
Branch: 1 branch
BranchTrueFalseTotal
if $d->{"registrar_abuse"} 9 72 81
2589 82 ▶ if ($d->{web_ip}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d->{'web_ip'}) { } 28 51 79
2590 31 push @out, ' Web host IP : ' . _sanitise_output($d->{web_ip});
2591 31 ▶ push @out, ' Web host org : ' . _sanitise_output($d->{web_org}) if $d->{web_org};
Branch: 1 branch
BranchTrueFalseTotal
if $d->{"web_org"} 27 2 29
2592 31 ▶ push @out, ' Web abuse : ' . _sanitise_output($d->{web_abuse}) if $d->{web_abuse};
Branch: 1 branch
BranchTrueFalseTotal
if $d->{"web_abuse"} 27 1 28
2593 } else {
2594 54 push @out, ' Web host : (no A record / unreachable)';
2595 }
2596 82 ▶ if ($d->{mx_host}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d->{'mx_host'}) { } 4 76 80
2597 7 push @out, ' MX host : ' . _sanitise_output($d->{mx_host});
2598 7 ▶ push @out, ' MX IP : ' . _sanitise_output($d->{mx_ip}) if $d->{mx_ip};
Branch: 1 branch
BranchTrueFalseTotal
if $d->{"mx_ip"} 4 0 4
2599 8 ▶ push @out, ' MX org : ' . _sanitise_output($d->{mx_org}) if $d->{mx_org};
Branch: 1 branch
BranchTrueFalseTotal
if $d->{"mx_org"} 4 0 4
2600 9 ▶ push @out, ' MX abuse : ' . _sanitise_output($d->{mx_abuse}) if $d->{mx_abuse};
Branch: 1 branch
BranchTrueFalseTotal
if $d->{"mx_abuse"} 4 0 4
2601 } else {
2602 80 push @out, ' MX host : (none found)';
2603 }
2604 84 ▶ if ($d->{ns_host}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d->{"ns_host"}) 5 76 81
2605 8 push @out, ' NS host : ' . _sanitise_output($d->{ns_host});
2606 8 ▶ push @out, ' NS IP : ' . _sanitise_output($d->{ns_ip}) if $d->{ns_ip};
Branch: 1 branch
BranchTrueFalseTotal
if $d->{"ns_ip"} 3 2 5
2607 8 ▶ push @out, ' NS org : ' . _sanitise_output($d->{ns_org}) if $d->{ns_org};
Branch: 1 branch
BranchTrueFalseTotal
if $d->{"ns_org"} 3 2 5
2608 8 ▶ push @out, ' NS abuse : ' . _sanitise_output($d->{ns_abuse}) if $d->{ns_abuse};
Branch: 1 branch
BranchTrueFalseTotal
if $d->{"ns_abuse"} 3 2 5
2609 }
2610 82 push @out, '';
2611 }
2612 } else {
2613 19 push @out, ' (none found)';
2614 19 push @out, '';
2615 }
2616  
2617 # Abuse contacts summary
2618 81 push @out, '[ WHERE TO SEND ABUSE REPORTS ]';
2619 81 my @contacts = $self->abuse_contacts();
2620 81 ▶ if (@contacts) {
Branch: 1 branch
BranchTrueFalseTotal
if (@contacts) { } 49 29 78
2621 52 for my $c (@contacts) {
2622 81 push @out, ' Role : ' . _sanitise_output($c->{role});
2623 81 push @out, ' Send to : ' . _sanitise_output($c->{address});
2624 81 ▶ push @out, ' Note : ' . _sanitise_output($c->{note}) if $c->{note};
Branch: 1 branch
BranchTrueFalseTotal
if $c->{"note"} 76 2 78
2625 81 push @out, " Discovered : $c->{via}";
2626 79 push @out, '';
2627 }
2628 } else {
2629 32 push @out, ' (no abuse contacts could be determined)';
2630 30 push @out, '';
2631 }
2632  
2633 # Web-form contacts (providers that require manual form submission)
2634 79 my @form_cs = $self->form_contacts();
2635 79 ▶ if (@form_cs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@form_cs) 4 72 76
2636 7 push @out, '[ WHERE TO FILE WEB-FORM REPORTS ]';
2637 9 push @out, ' The following parties require manual submission via a web form.';
2638 15 push @out, ' Open each URL in a browser, then follow the instructions below it.';
2639 11 push @out, '';
2640 11 for my $c (@form_cs) {
2641 11 push @out, ' Role : ' . _sanitise_output($c->{role});
2642 11 push @out, ' Form URL : ' . _sanitise_output($c->{form});
2643 11 ▶ push @out, ' Domain/URL : ' . _sanitise_output($c->{form_domain}) if $c->{form_domain};
Branch: 1 branch
BranchTrueFalseTotal
if $c->{"form_domain"} 8 0 8
2644 11 ▶ push @out, ' Note : ' . _sanitise_output($c->{note}) if $c->{note};
Branch: 1 branch
BranchTrueFalseTotal
if $c->{"note"} 4 4 8
2645 7 ▶ if ($c->{form_paste}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->{"form_paste"}) 4 4 8
2646 # Word-wrap the paste hint at ROLE_WRAP_LEN characters
2647 7 my $hint = $c->{form_paste};
2648 7 my @words = split /\s+/, $hint;
2649 9 my (@lines, $line);
2650 9 for my $w (@words) {
2651 124 ▶ if (defined $line && length("$line $w") > $ROLE_WRAP_LEN) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $line and length "$line $w" > $ROLE_WRAP_LEN) { } 8 117 125
Condition50%3/6
defined $line and length "$line $w" > $ROLE_WRAP_LEN
!ll&&!rl&&r
41098
defined $line and length "$line $w" > $ROLE_WRAP_LEN
!ll&&!rl&&r
000
MC/DC100%2/2
defined $line and length "$line $w" > $ROLE_WRAP_LEN
defined $line length "$line $w" > $ROLE_WRAP_LEN
MC/DC0%0/2
defined $line and length "$line $w" > $ROLE_WRAP_LEN
defined $line length "$line $w" > $ROLE_WRAP_LEN
Truth table100%3/3
A and B
A= defined $lineB= length "$line $w" > $ROLE_WRAP_LEN
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $lineB= length "$line $w" > $ROLE_WRAP_LEN
ABresult
0X0
100
111
2652 11 push @lines, $line;
2653 11 $line = $w;
2654 } else {
2655 118 ▶ $line = defined $line ? "$line $w" : $w;
Branch: 1 branch
BranchTrueFalseTotal
defined $line ? : 109 4 113
2656 }
2657 }
2658 9 ▶ push @lines, $line if defined $line;
Branch: 1 branch
BranchTrueFalseTotal
if defined $line 4 2 6
2659 7 ▶ push @out, ' Paste : ' . shift @lines if @lines;
Branch: 1 branch
BranchTrueFalseTotal
if @lines 4 0 4
2660 7 push @out, ' ' . $_ for @lines;
2661 }
2662 7 ▶ push @out, ' Upload : ' . _sanitise_output($c->{form_upload}) if $c->{form_upload};
Branch: 1 branch
BranchTrueFalseTotal
if $c->{"form_upload"} 4 2 6
2663 7 push @out, '';
2664 }
2665 }
2666  
2667 79 push @out, '=' x 72;
2668 79 return join("\n", @out) . "\n";
2669 }
2670  
2671 # -----------------------------------------------------------------------
2672 # Private: output sanitisation
2673 # -----------------------------------------------------------------------
2674  
2675 # _sanitise_output( $str ) -> $str
2676 #
2677 # Purpose:
2678 # Strip control characters that could affect terminal rendering or HTML
2679 # injection from any string that will appear in a report or abuse email.
2680 # Preserves printable ASCII, high-bytes (for UTF-8 content), tabs, and
2681 # line endings.
2682 #
2683 # Entry criteria:
2684 # $str -- a defined or undef scalar.
2685 #
2686 # Exit status:
2687 # Returns the sanitised string, or the empty string if $str is undef.
2688 #
2689 # Notes:
2690 # Only strips C0 control characters below 0x20 (except \t) and the DEL
2691 # character (0x7F). High bytes (0x80-0xFF) are preserved because they
2692 # form valid UTF-8 multi-byte sequences in headers and body text.
2693  
2694 1693 ▶sub _sanitise_output :Private {
Subroutine _sanitise_output: called CC=2 SCAR=6.9
Subroutine _sanitise_output: called CC=2 SCAR=6.9
2695 1689 my $str = $_[0];
2696 1689 ▶ return '' unless defined $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 6 1685 1691
2697 # Remove C0 controls (except tab) and DEL
2698 1684 $str =~ s/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]//g;
2699 1684 return $str;
2700 24 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
2701  
2702 # -----------------------------------------------------------------------
2703 # Private: message parsing
2704 # -----------------------------------------------------------------------
2705  
2706 # _split_message( $text )
2707 #
2708 # Purpose:
2709 # Split a raw RFC 2822 email into headers and body, parse all headers,
2710 # decode the body (including multipart), extract sending-software
2711 # fingerprints, and populate per-hop tracking data.
2712 #
2713 # Entry criteria:
2714 # $text -- defined scalar, already dereferenced by parse_email().
2715 # $self->{_sending_sw} and $self->{_rcvd_tracking} reset to [] by caller.
2716 #
2717 # Exit status:
2718 # Returns undef silently if the header block is empty/whitespace-only.
2719 # Otherwise all results are communicated via side effects on $self.
2720 #
2721 # Side effects:
2722 # Populates _headers, _received, _body_plain, _body_html, _sending_sw,
2723 # and _rcvd_tracking.
2724 #
2725 # Notes:
2726 # Delegates to _decode_multipart() for multipart/* content types.
2727 # Lines not matching the header pattern are silently discarded.
2728 # Boundary extraction uses a simple regex; missing boundary causes the
2729 # body to be skipped silently.
2730  
2731 717 ▶sub _split_message :Private {
Subroutine _split_message: called CC=17 SCAR=29.7
Subroutine _split_message: called CC=17 SCAR=29.7
2732 716 my ($self, $text) = @_;
2733  
2734 # Split at the first blank line (RFC 2822 header/body separator)
2735 718 my ($header_block, $body_raw) = split /\r?\n\r?\n/, $text, 2;
2736  
2737 717 ▶ return unless defined $header_block && $header_block =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $header_block and $header_block =~ /\S/ 0 717 717
Condition16%1/6
defined $header_block and $header_block =~ /\S/
!ll&&!rl&&r
00713
defined $header_block and $header_block =~ /\S/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $header_block and $header_block =~ /\S/
defined $header_block $header_block =~ /\S/
MC/DC0%0/2
defined $header_block and $header_block =~ /\S/
defined $header_block $header_block =~ /\S/
Truth table33%1/3
A and B
A= defined $header_blockB= $header_block =~ /\S/
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $header_blockB= $header_block =~ /\S/
ABresult
0X0
100
111
2738 722 ▶ $body_raw //= '';
Condition40%2/5
$body_raw //= ""
l!l
7102
$body_raw //= ""
l!l
000
MC/DC100%1/1
$body_raw //= ""
$body_raw
MC/DC0%0/1
$body_raw //= ""
$body_raw
Truth table100%2/2
A
A= $body_raw
Aresult
11
00
Truth table0%0/2
A
A= $body_raw
Aresult
11
00
2739  
2740 # Unfold RFC 2822 continuation lines (s2.2.3)
2741 722 $header_block =~ s/\r?\n([ \t]+)/ $1/g;
2742  
2743 # Parse each header line into a { name, value } pair
2744 721 my @headers;
2745 721 for my $line (split /\r?\n/, $header_block) {
2746 5649 ▶ if ($line =~ /^([\w-]+)\s*:\s*(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^([\w-]+)\s*:\s*(.*)/) 5637 4 5641
2747 5642 push @headers, { name => lc($1), value => $2 };
2748 }
2749 }
2750 716 $self->{_headers} = \@headers;
2751  
2752 # Collect all Received: header values (most-recent first, as in message)
2753 $self->{_received} = [
2754 1622 map { $_->{value} }
2755 717 grep { $_->{name} eq 'received' } @headers
2756 ];
2757  
2758 # Determine content type and transfer encoding from top-level headers
2759 716 my ($ct_h) = grep { $_->{name} eq 'content-type' } @headers;
2760 715 my ($cte_h) = grep { $_->{name} eq 'content-transfer-encoding' } @headers;
2761 717 ▶ my $ct = defined $ct_h ? $ct_h->{value} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $ct_h ? : 390 324 714
2762 717 ▶ my $cte = defined $cte_h ? $cte_h->{value} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $cte_h ? : 353 361 714
2763  
2764 # Decode multipart or single-part body as appropriate
2765 717 ▶ if ($ct =~ /multipart/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ct =~ /multipart/i) { } 18 696 714
2766 21 my ($boundary) = $ct =~ /boundary="?([^";]+)"?/i;
2767 # Pass depth=0 to enforce the MAX_MULTIPART_DEPTH recursion guard
2768 22 ▶ $self->_decode_multipart($body_raw, $boundary, 0) if $boundary;
Branch: 1 branch
BranchTrueFalseTotal
if $boundary 18 1 19
2769 } else {
2770 699 my $decoded = $self->_decode_body($body_raw, $cte);
2771 699 ▶ if ($ct =~ /html/i) { $self->{_body_html} = $decoded }
Branch: 1 branch
BranchTrueFalseTotal
if ($ct =~ /html/i) { } 9 688 697
2772 691 else { $self->{_body_plain} = $decoded }
2773 }
2774  
2775 $self->_debug(sprintf 'Parsed %d headers, %d Received lines',
2776 717 scalar @headers, scalar @{ $self->{_received} });
2777  
2778 # --- Sending software fingerprints ---
2779 # These headers identify the mailer or shared-hosting script that sent
2780 # the message; invaluable for shared-hosting abuse reports.
2781 718 my %sw_notes = (
2782 'x-php-originating-script' => 'PHP script on shared hosting -- report to hosting abuse team',
2783 'x-source' => 'Source file on shared hosting -- report to hosting abuse team',
2784 'x-source-host' => 'Sending hostname injected by shared hosting provider',
2785 'x-source-args' => 'Command-line args injected by shared hosting provider',
2786 'x-mailer' => 'Email client or bulk-mailer identifier',
2787 'user-agent' => 'Email client identifier',
2788 );
2789 718 for my $sw_hdr (sort keys %sw_notes) {
2790 4278 my ($h) = grep { $_->{name} eq $sw_hdr } @headers;
2791 4277 ▶ next unless $h;
Branch: 1 branch
BranchTrueFalseTotal
unless $h 4250 24 4274
2792 26 push @{ $self->{_sending_sw} }, {
2793 header => $sw_hdr,
2794 value => $h->{value},
2795 28 note => $sw_notes{$sw_hdr},
2796 };
2797 }
2798  
2799 # --- Per-hop tracking IDs from Received: chain ---
2800 # Walk oldest-first (reverse) so _rcvd_tracking is oldest-first
2801 715 for my $rcvd (reverse @{ $self->{_received} }) {
2802 1620 my $ip = $self->_extract_ip_from_received($rcvd);
2803 1620 my ($for_addr) = $rcvd =~ /\bfor\s+<?([^\s>]+\@[\w.-]+\.[\w]+)>?/i;
2804 1618 my ($srv_id) = $rcvd =~ /\bid\s+([\w.-]+)/i;
2805 # Skip hops with no actionable tracking data
2806 1618 ▶ next unless defined $ip || defined $for_addr || defined $srv_id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ip or defined $for_addr or defined $srv_id 1 1617 1618
Condition33%4/12
defined $ip or defined $for_addr or defined $srv_id
l!l&&r!l&&!r
161601
defined $ip or defined $for_addr
l!l&&r!l&&!r
161801
defined $ip or defined $for_addr or defined $srv_id
l!l&&r!l&&!r
000
defined $ip or defined $for_addr
l!l&&r!l&&!r
000
MC/DC33%1/3
defined $ip or defined $for_addr or defined $srv_id
defined $ip defined $for_addr defined $srv_id
MC/DC0%0/3
defined $ip or defined $for_addr or defined $srv_id
defined $ip defined $for_addr defined $srv_id
Truth table50%2/4
A or B or C
A= defined $ipB= defined $for_addrC= defined $srv_id
ABCresult
1XX1
01X1
0011
0000
Truth table0%0/4
A or B or C
A= defined $ipB= defined $for_addrC= defined $srv_id
ABCresult
1XX1
01X1
0011
0000
2807 1617 push @{ $self->{_rcvd_tracking} }, {
2808 received => $rcvd,
2809 ip => $ip,
2810 for => $for_addr,
2811 id => $srv_id,
2812 };
2813 }
2814 24 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
2815  
2816 # _decode_multipart( $body, $boundary, $depth )
2817 #
2818 # Purpose:
2819 # Recursively split a MIME multipart body on its boundary and decode each
2820 # text/plain and text/html part. Nested multipart/* containers are
2821 # recursed into up to MAX_MULTIPART_DEPTH levels deep.
2822 #
2823 # Entry criteria:
2824 # $body -- the raw body text of the multipart container.
2825 # $boundary -- the boundary string from the Content-Type header.
2826 # $depth -- current recursion depth (starts at 0 from _split_message).
2827 #
2828 # Exit status:
2829 # Returns undef if $depth >= MAX_MULTIPART_DEPTH (recursion guard).
2830 # Otherwise all results via side effects.
2831 #
2832 # Side effects:
2833 # Appends decoded text to $self->{_body_plain} and $self->{_body_html}.
2834 #
2835 # Notes:
2836 # Whitespace-only MIME segments between boundaries are silently skipped.
2837 # Decoding errors are silenced; raw bytes are used as fallback.
2838  
2839 55 ▶sub _decode_multipart :Private {
Subroutine _decode_multipart: called CC=15 SCAR=27.8
Subroutine _decode_multipart: called CC=15 SCAR=27.8
2840 51 my ($self, $body, $boundary, $depth) = @_;
2841 50 ▶ $depth //= 0;
Condition40%2/5
$depth //= 0
l!l
4901
$depth //= 0
l!l
00
MC/DC0%0/1
$depth //= 0
$depth
MC/DC0%0/1
$depth //= 0
$depth
Truth table50%1/2
A
A= $depth
Aresult
11
00
Truth table0%0/2
A
A= $depth
Aresult
11
00
2842  
2843 # Enforce the recursion depth limit to prevent stack exhaustion on
2844 # pathological crafted messages with deeply nested multipart structures.
2845 50 ▶ if ($depth >= $MAX_MULTIPART_DEPTH) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth >= $MAX_MULTIPART_DEPTH) 9 43 52
2846 8 Carp::carp 'Email::Abuse::Investigator: multipart nesting depth limit',
2847 "($MAX_MULTIPART_DEPTH) exceeded; stopping recursion";
2848 9 return;
2849 }
2850  
2851 # Split on the boundary marker; the (?:--)? suffix handles closing boundary
2852 44 my @parts = split /--\Q$boundary\E(?:--)?/, $body;
2853  
2854 44 for my $part (@parts) {
2855 # Skip whitespace-only segments between boundaries
2856 136 ▶ next unless $part =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $part =~ /\S/ 83 54 137
2857  
2858 53 $part =~ s/^\r?\n//;
2859  
2860 # Each MIME part has its own headers separated from body by a blank line
2861 53 my ($phdr_block, $pbody) = split /\r?\n\r?\n/, $part, 2;
2862 53 ▶ next unless defined $pbody;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pbody 1 53 54
2863  
2864 # Unfold continuation header lines within this part
2865 52 $phdr_block =~ s/\r?\n([ \t]+)/ $1/g;
2866  
2867 # Parse this part's headers into a simple hash
2868 53 my %phdr;
2869 53 for my $line (split /\r?\n/, $phdr_block) {
2870 56 ▶ $phdr{ lc($1) } = $2 if $line =~ /^([\w-]+)\s*:\s*(.*)/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^([\w-]+)\s*:\s*(.*)/ 54 3 57
2871 }
2872  
2873 53 ▶ my $pct = $phdr{'content-type'} // '';
Condition50%2/4
$phdr{"content-type"} // ""
l!l
483
$phdr{"content-type"} // ""
l!l
00
MC/DC100%1/1
$phdr{"content-type"} // ""
$phdr{"content-type"}
MC/DC0%0/1
$phdr{"content-type"} // ""
$phdr{"content-type"}
Truth table100%2/2
A
A= $phdr{"content-type"}
Aresult
11
00
Truth table0%0/2
A
A= $phdr{"content-type"}
Aresult
11
00
2874 53 ▶ my $pcte = $phdr{'content-transfer-encoding'} // '';
Condition50%3/6
$phdr{"content-transfer-encoding"} // ""
l!l
2491
$phdr{"content-transfer-encoding"} // ""
l!l
000
MC/DC100%1/1
$phdr{"content-transfer-encoding"} // ""
$phdr{"content-transfer-encoding"}
MC/DC0%0/1
$phdr{"content-transfer-encoding"} // ""
$phdr{"content-transfer-encoding"}
Truth table100%2/2
A
A= $phdr{"content-transfer-encoding"}
Aresult
11
00
Truth table0%0/2
A
A= $phdr{"content-transfer-encoding"}
Aresult
11
00
2875  
2876 # Nested multipart/* must be recursed into; without this URLs in
2877 # multipart/alternative inside multipart/mixed would be missed.
2878 53 ▶ if ($pct =~ /multipart/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pct =~ /multipart/i) 24 27 51
2879 26 my ($inner_boundary) = $pct =~ /boundary\s*=\s*"?([^";]+)"?/i;
2880 26 ▶ if ($inner_boundary) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inner_boundary) 24 1 25
2881 25 $inner_boundary =~ s/\s+$//;
2882 # Increment depth counter for the recursion guard
2883 26 $self->_decode_multipart($pbody, $inner_boundary, $depth + 1);
2884 }
2885 26 next;
2886 }
2887  
2888 # Decode transfer encoding and accumulate by content type
2889 29 my $decoded = $self->_decode_body($pbody, $pcte);
2890 29 ▶ if ($pct =~ /text\/html/i) { $self->{_body_html} .= $decoded }
Branch: 2 branches
BranchTrueFalseTotal
if ($pct =~ m[text/html]i) { } 10 17 27
elsif ($pct =~ /text/i or not $pct) { } 16 2 18
2891 17 ▶ elsif ($pct =~ /text/i || !$pct) { $self->{_body_plain} .= $decoded }
Condition50%3/6
$pct =~ /text/i or not $pct
l!l&&r!l&&!r
1432
$pct =~ /text/i or not $pct
l!l&&r!l&&!r
000
MC/DC100%2/2
$pct =~ /text/i or not $pct
$pct =~ /text/i not $pct
MC/DC0%0/2
$pct =~ /text/i or not $pct
$pct =~ /text/i not $pct
Truth table100%3/3
A or not(B)
A= $pct =~ /text/iB= not $pct
ABresult
1X1
011
000
Truth table0%0/3
A or not(B)
A= $pct =~ /text/iB= not $pct
ABresult
1X1
011
000
2892 }
2893 24 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
2894  
2895 # _decode_body( $body, $cte ) -> string
2896 #
2897 # Purpose:
2898 # Decode a MIME body part according to its Content-Transfer-Encoding.
2899 #
2900 # Entry criteria:
2901 # $body -- raw body string (may be undef).
2902 # $cte -- Content-Transfer-Encoding value string (may be undef).
2903 #
2904 # Exit status:
2905 # Returns the decoded string, or the original string if the encoding is
2906 # 7bit/8bit/binary or unrecognised.
2907 #
2908 # Notes:
2909 # decode_qp and decode_base64 are imported from MIME:: modules; errors
2910 # from malformed content are silenced by the eval wrappers they provide.
2911  
2912 733 ▶sub _decode_body :Private {
Subroutine _decode_body: called CC=5 SCAR=16.6
Subroutine _decode_body: called CC=5 SCAR=16.6
2913 730 my ($self, $body, $cte) = @_;
2914 731 ▶ $cte //= '';
Condition60%3/5
$cte //= ""
l!l
72812
$cte //= ""
l!l
00
MC/DC0%0/1
$cte //= ""
$cte
MC/DC0%0/1
$cte //= ""
$cte
Truth table50%1/2
A
A= $cte
Aresult
11
00
Truth table0%0/2
A
A= $cte
Aresult
11
00
2915 731 ▶ return decode_qp($body) if $cte =~ /quoted-printable/i;
Branch: 1 branch
BranchTrueFalseTotal
if $cte =~ /quoted-printable/i 6 722 728
2916 725 ▶ return decode_base64($body) if $cte =~ /base64/i;
Branch: 1 branch
BranchTrueFalseTotal
if $cte =~ /base64/i 3 719 722
2917 720 ▶ return $body // '';
Condition50%2/4
$body // ""
l!l
7181
$body // ""
l!l
00
MC/DC100%1/1
$body // ""
$body
MC/DC0%0/1
$body // ""
$body
Truth table100%2/2
A
A= $body
Aresult
11
00
Truth table0%0/2
A
A= $body
Aresult
11
00
2918 24 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
2919  
2920 # -----------------------------------------------------------------------
2921 # Private: Received-chain -> originating IP
2922 # -----------------------------------------------------------------------
2923  
2924 # _find_origin()
2925 #
2926 # Purpose:
2927 # Walk the Received: chain (oldest-first) to find the first external IP,
2928 # or fall back to X-Originating-IP. Enrich with rDNS and WHOIS.
2929 #
2930 # Entry criteria:
2931 # $self->{_received} populated by _split_message().
2932 # $self->{trusted_relays} set by new().
2933 #
2934 # Exit status:
2935 # Returns { ip, rdns, org, abuse, country, confidence, note } on success.
2936 # Returns undef if no usable IP can be identified.
2937 #
2938 # Side effects:
2939 # Network I/O via _enrich_ip(): one PTR lookup, one RDAP/WHOIS query.
2940 # Results are also stored in the CHI cross-message cache if available.
2941 #
2942 # Notes:
2943 # confidence 'high' = 2+ distinct external IPs;
2944 # 'medium' = exactly one external IP;
2945 # 'low' = taken from X-Originating-IP.
2946  
2947 428 ▶sub _find_origin :Private {
Subroutine _find_origin: called CC=9 SCAR=22.0
Subroutine _find_origin: called CC=9 SCAR=22.0
2948 427 my $self = $_[0];
2949  
2950 426 my @candidates;
2951  
2952 # Walk oldest-first (reverse) to collect external IPs
2953 426 for my $hdr (reverse @{ $self->{_received} }) {
2954 1349 ▶ my $ip = $self->_extract_ip_from_received($hdr) // next;
Condition50%3/6
$self->_extract_ip_from_received($hdr) // (next)
l!l
134612
$self->_extract_ip_from_received($hdr) // (next)
l!l
000
MC/DC100%1/1
$self->_extract_ip_from_received($hdr) // (next)
$self->_extract_ip_from_received($hdr)
MC/DC0%0/1
$self->_extract_ip_from_received($hdr) // (next)
$self->_extract_ip_from_received($hdr)
Truth table100%2/2
A
A= $self->_extract_ip_from_received($hdr)
Aresult
11
00
Truth table0%0/2
A
A= $self->_extract_ip_from_received($hdr)
Aresult
11
00
2955 1349 ▶ next if $self->_is_private($ip);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_private($ip) 1208 138 1346
2956 144 ▶ next if $self->_is_trusted($ip);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_trusted($ip) 7 133 140
2957 138 push @candidates, $ip;
2958 }
2959  
2960 # Fall back to X-Originating-IP if no external IPs in Received: chain
2961 429 ▶ unless (@candidates) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@candidates) 295 132 427
2962 300 my $xoip = $self->_header_value('x-originating-ip');
2963 298 ▶ if ($xoip) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xoip) 6 292 298
2964 11 $xoip =~ s/[\[\]\s]//g;
2965 11 ▶ return $self->_enrich_ip($xoip, 'low',
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_private($xoip) 5 4 9
2966 'Taken from X-Originating-IP (webmail, unverified)')
2967 unless $self->_is_private($xoip);
2968 }
2969 293 return;
2970 }
2971  
2972 # Report the oldest (first) external IP; confidence depends on count
2973 132 ▶ return $self->_enrich_ip(
Branch: 1 branch
BranchTrueFalseTotal
@candidates > 1 ? : 5 128 133
2974 $candidates[0],
2975 @candidates > 1 ? 'high' : 'medium',
2976 'First external hop in Received: chain',
2977 );
2978 24 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
2979  
2980 # _extract_ip_from_received( $hdr ) -> ipv4_or_ipv6_string | undef
2981 #
2982 # Purpose:
2983 # Extract the most-significant IP address from a raw Received: header
2984 # value, trying patterns in priority order. Supports both IPv4 dotted-
2985 # quad and IPv6 bracket notation.
2986 #
2987 # Entry criteria:
2988 # $hdr -- a defined Received: header value string.
2989 #
2990 # Exit status:
2991 # Returns the IP string on success, undef if no IP can be extracted.
2992 #
2993 # Notes:
2994 # IPv4 addresses are validated (all octets <= 255).
2995 # IPv6 addresses are returned as-is if they contain colons.
2996  
2997 2983 ▶sub _extract_ip_from_received :Private {
Subroutine _extract_ip_from_received: called CC=6 SCAR=17.9
Subroutine _extract_ip_from_received: called CC=6 SCAR=17.9
2998 2982 my ($self, $hdr) = @_;
2999 2981 for my $re (@RECEIVED_IP_RE) {
3000 3128 ▶ if ($hdr =~ $re) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hdr =~ /$re/) 3006 123 3129
3001 3007 my $ip = $1;
3002  
3003 # Accept IPv6 addresses (contain colons) without further validation
3004 3007 ▶ return $ip if $ip =~ /:/;
Branch: 1 branch
BranchTrueFalseTotal
if $ip =~ /:/ 4 3003 3007
3005  
3006 # Validate IPv4 format and octet range
3007 3005 ▶ next unless $ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/ 29 2976 3005
3008 2978 ▶ next if grep { $_ > 255 } split /\./, $ip;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ > 255;} split(/\./, $ip, 0) 9 2970 2979
3009 2969 return $ip;
3010 }
3011 }
3012 9 return;
3013 24 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
3014  
3015 # _is_private( $ip ) -> bool
3016 #
3017 # Purpose:
3018 # Test whether an IP address falls in any private, reserved, or special-
3019 # use range (IPv4 or IPv6) that should never be reported as a spam origin.
3020 #
3021 # Entry criteria:
3022 # $ip -- a scalar IP string (IPv4 or IPv6); may be undef.
3023 #
3024 # Exit status:
3025 # Returns 1 (true) if the IP is private/reserved, 0 (false) otherwise.
3026 # Returns 1 for undef or empty strings.
3027 #
3028 # Notes:
3029 # Uses the module-level @PRIVATE_RANGES array of pre-compiled regexes.
3030 # Covers all ranges listed in RFC 1122, 1918, 5737, 6598, and RFC 4193.
3031  
3032 1391 ▶sub _is_private :Private {
Subroutine _is_private: called CC=5 SCAR=16.5
Subroutine _is_private: called CC=5 SCAR=16.5
3033 1391 my ($self, $ip) = @_;
3034 1391 ▶ return 1 if !defined($ip) || $ip eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $ip or $ip eq "" 2 1390 1392
Condition50%3/6
not defined $ip or $ip eq ""
l!l&&r!l&&!r
111386
not defined $ip or $ip eq ""
l!l&&r!l&&!r
000
MC/DC100%2/2
not defined $ip or $ip eq ""
not defined $ip $ip eq ""
MC/DC0%0/2
not defined $ip or $ip eq ""
not defined $ip $ip eq ""
Truth table100%3/3
not(A) or B
A= not defined $ipB= $ip eq ""
ABresult
1X1
011
000
Truth table0%0/3
not(A) or B
A= not defined $ipB= $ip eq ""
ABresult
1X1
011
000
3035 1387 ▶ for my $re (@PRIVATE_RANGES) { return 1 if $ip =~ $re }
Branch: 1 branch
BranchTrueFalseTotal
if $ip =~ /$re/ 1236 6704 7940
3036 151 return 0;
3037 24 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
3038  
3039 # _is_trusted( $ip ) -> bool
3040 #
3041 # Purpose:
3042 # Test whether an IP address matches any entry in the caller-supplied
3043 # trusted_relays list (exact IP or CIDR block).
3044 #
3045 # Entry criteria:
3046 # $ip -- a defined IPv4 address string.
3047 # $self->{trusted_relays} -- arrayref of exact IPs or CIDR strings.
3048 #
3049 # Exit status:
3050 # Returns 1 (true) if the IP matches any trusted relay, 0 otherwise.
3051  
3052 147 ▶sub _is_trusted :Private {
Subroutine _is_trusted: called CC=3 SCAR=11.0
Subroutine _is_trusted: called CC=3 SCAR=11.0
3053 145 my ($self, $ip) = @_;
3054 147 for my $cidr (@{ $self->{trusted_relays} }) {
3055 20 ▶ return 1 if $self->_ip_in_cidr($ip, $cidr);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_ip_in_cidr($ip, $cidr) 8 11 19
3056 }
3057 138 return 0;
3058 24 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
3059  
3060 # -----------------------------------------------------------------------
3061 # Private: HTTP/HTTPS URL extraction and resolution
3062 # -----------------------------------------------------------------------
3063  
3064 # _extract_and_resolve_urls() -> arrayref of url hashrefs
3065 #
3066 # Purpose:
3067 # Extract all HTTP/HTTPS URLs from the decoded body, resolve each unique
3068 # hostname to an IP, and enrich with WHOIS/RDAP data. Optionally uses
3069 # AnyEvent::DNS to parallelise the DNS resolution step.
3070 #
3071 # Entry criteria:
3072 # $self->{_body_plain} and $self->{_body_html} populated by _split_message().
3073 #
3074 # Exit status:
3075 # Returns an arrayref of url hashrefs (possibly empty).
3076 #
3077 # Side effects:
3078 # Network I/O per unique hostname: one A/AAAA lookup, one RDAP/WHOIS.
3079 # Results stored in the CHI cross-message cache if available.
3080  
3081 230 ▶sub _extract_and_resolve_urls :Private {
Subroutine _extract_and_resolve_urls: called CC=27 SCAR=36.4
Subroutine _extract_and_resolve_urls: called CC=27 SCAR=36.4
3082 230 my $self = $_[0];
3083 230 my (%url_seen, %host_cache);
3084 229 my @results;
3085 230 my $combined = $self->{_body_plain} . "\n" . $self->{_body_html};
3086  
3087 # Collect unique URLs from body
3088 230 my @urls = grep { !$url_seen{$_}++ } $self->_extract_http_urls($combined);
3089  
3090 # For URL-shortener and redirect-cloaker hosts, follow the redirect chain to
3091 # discover the real destination (e.g. GCS bucket → phishing landing page).
3092 # Done before DNS resolution so that destination hostnames can be parallelised.
3093 # _follow_redirect_chain() is a :Protected seam that handles LWP availability
3094 # internally — do not guard this block with $HAS_LWP so the seam can be
3095 # stubbed unconditionally in tests regardless of whether LWP is installed.
3096 230 for my $url (@urls) {
3097 644 my ($host) = $url =~ m{https?://([^/:?\s#]+)}i;
3098 644 ▶ next unless $host;
Branch: 1 branch
BranchTrueFalseTotal
unless $host 0 643 643
3099 644 my $bare = lc $host;
3100 645 $bare =~ s/^www\.//;
3101 next unless $URL_SHORTENERS{$bare}
3102 645 ▶ || ($self->{url_shorteners} && $self->{url_shorteners}{$bare})
Branch: 1 branch
BranchTrueFalseTotal
unless $URL_SHORTENERS{$bare} or $self->{"url_shorteners"} and $self->{"url_shorteners"}{$bare} or $self->_is_redirect_cloaker($bare) 634 10 644
Condition38%7/18
$URL_SHORTENERS{$bare} or $self->{"url_shorteners"} and $self->{"url_shorteners"}{$bare} or $self->_is_redirect_cloaker($bare)
l!l&&r!l&&!r
93635
$URL_SHORTENERS{$bare} or $self->{"url_shorteners"} and $self->{"url_shorteners"}{$bare}
l!l&&r!l&&!r
90635
$self->{"url_shorteners"} and $self->{"url_shorteners"}{$bare}
!ll&&!rl&&r
63501
$URL_SHORTENERS{$bare} or $self->{"url_shorteners"} and $self->{"url_shorteners"}{$bare} or $self->_is_redirect_cloaker($bare)
l!l&&r!l&&!r
000
$URL_SHORTENERS{$bare} or $self->{"url_shorteners"} and $self->{"url_shorteners"}{$bare}
l!l&&r!l&&!r
000
$self->{"url_shorteners"} and $self->{"url_shorteners"}{$bare}
!ll&&!rl&&r
000
MC/DC50%2/4
$URL_SHORTENERS{$bare} or $self->{"url_shorteners"} and $self->{"url_shorteners"}{$bare} or $self->_is_redirect_cloaker($bare)
$URL_SHORTENERS{$bare} $self->{"url_shorteners"} $self->{"url_shorteners"}{$bare} $self->_is_redirect_cloaker($bare)
MC/DC0%0/4
$URL_SHORTENERS{$bare} or $self->{"url_shorteners"} and $self->{"url_shorteners"}{$bare} or $self->_is_redirect_cloaker($bare)
$URL_SHORTENERS{$bare} $self->{"url_shorteners"} $self->{"url_shorteners"}{$bare} $self->_is_redirect_cloaker($bare)
Truth table50%3/6
A or B and C or D
A= $URL_SHORTENERS{$bare}B= $self->{"url_shorteners"}C= $self->{"url_shorteners"}{$bare}D= $self->_is_redirect_cloaker($bare)
ABCDresult
1XXX1
011X1
00X11
01011
00X00
01000
Truth table0%0/6
A or B and C or D
A= $URL_SHORTENERS{$bare}B= $self->{"url_shorteners"}C= $self->{"url_shorteners"}{$bare}D= $self->_is_redirect_cloaker($bare)
ABCDresult
1XXX1
011X1
00X11
01011
00X00
01000
3103 || $self->_is_redirect_cloaker($bare);
3104 10 my $dest = $self->_follow_redirect_chain($url);
3105 9 ▶ next unless defined $dest && !$url_seen{$dest}++;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dest and not $url_seen{$dest}++ 4 5 9
Condition33%2/6
defined $dest and not $url_seen{$dest}++
!ll&&!rl&&r
404
defined $dest and not $url_seen{$dest}++
!ll&&!rl&&r
000
MC/DC50%1/2
defined $dest and not $url_seen{$dest}++
defined $dest not $url_seen{$dest}++
MC/DC0%0/2
defined $dest and not $url_seen{$dest}++
defined $dest not $url_seen{$dest}++
Truth table66%2/3
A and not(B)
A= defined $destB= not $url_seen{$dest}++
ABresult
0X0
100
111
Truth table0%0/3
A and not(B)
A= defined $destB= not $url_seen{$dest}++
ABresult
0X0
100
111
3106 6 push @urls, $dest;
3107 }
3108  
3109 # Extract unique hostnames for parallel DNS resolution (including redirect destinations)
3110 230 my %hostname_needed;
3111 230 for my $url (@urls) {
3112 645 my ($host) = $url =~ m{https?://([^/:?\s#]+)}i;
3113 643 ▶ $hostname_needed{$host}++ if $host;
Branch: 1 branch
BranchTrueFalseTotal
if $host 642 0 642
3114 }
3115  
3116 # Parallelise DNS lookups if AnyEvent::DNS is available
3117 230 ▶ if ($HAS_ANYEVENT_DNS && scalar(keys %hostname_needed) > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($HAS_ANYEVENT_DNS and scalar keys %hostname_needed > 1) 0 227 227
Condition33%2/6
$HAS_ANYEVENT_DNS and scalar keys %hostname_needed > 1
!ll&&!rl&&r
22701
$HAS_ANYEVENT_DNS and scalar keys %hostname_needed > 1
!ll&&!rl&&r
000
MC/DC0%0/2
$HAS_ANYEVENT_DNS and scalar keys %hostname_needed > 1
$HAS_ANYEVENT_DNS scalar keys %hostname_needed > 1
MC/DC0%0/2
$HAS_ANYEVENT_DNS and scalar keys %hostname_needed > 1
$HAS_ANYEVENT_DNS scalar keys %hostname_needed > 1
Truth table33%1/3
A and B
A= $HAS_ANYEVENT_DNSB= scalar keys %hostname_needed > 1
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $HAS_ANYEVENT_DNSB= scalar keys %hostname_needed > 1
ABresult
0X0
100
111
3118 2 $self->_parallel_resolve_hosts(\%hostname_needed, \%host_cache);
3119 }
3120  
3121 # Process each URL: resolve hostname and WHOIS-enrich
3122 229 for my $url (@urls) {
3123 644 my ($host) = $url =~ m{https?://([^/:?\s#]+)}i;
3124 643 ▶ next unless $host;
Branch: 1 branch
BranchTrueFalseTotal
unless $host 1 642 643
3125  
3126 # Resolve and WHOIS once per unique hostname, then cache the result
3127 644 ▶ unless (exists $host_cache{$host}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $host_cache{$host}) 127 517 644
3128 # Check the cross-message CHI cache first
3129 130 ▶ my $cached = $_cache ? $_cache->get("url:$host") : undef;
Branch: 1 branch
BranchTrueFalseTotal
$_cache ? : 125 2 127
3130 129 ▶ if ($cached) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cached) { } 27 99 126
3131 31 $host_cache{$host} = $cached;
3132 } else {
3133 102 ▶ my $ip = $self->_resolve_host($host) // '(unresolved)';
Condition40%2/5
$self->_resolve_host($host) // "(unresolved)"
l!l
60380
$self->_resolve_host($host) // "(unresolved)"
l!l
00
MC/DC100%1/1
$self->_resolve_host($host) // "(unresolved)"
$self->_resolve_host($host)
MC/DC0%0/1
$self->_resolve_host($host) // "(unresolved)"
$self->_resolve_host($host)
Truth table100%2/2
A
A= $self->_resolve_host($host)
Aresult
11
00
Truth table0%0/2
A
A= $self->_resolve_host($host)
Aresult
11
00
3134 102 ▶ my $whois = $ip ne '(unresolved)'
Branch: 1 branch
BranchTrueFalseTotal
$ip ne '(unresolved)' ? : 60 39 99
3135 ? $self->_whois_ip($ip)
3136 : {};
3137  
3138 # Fall back to domain WHOIS if IP lookup returned nothing
3139 102 ▶ if (!$whois->{abuse}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($whois->{"abuse"}) 47 53 100
3140 51 ▶ my $reg = _registrable($host) // $host;
Condition66%2/3
_registrable($host) // $host
l!l&&r!l&&!r
4701
MC/DC50%1/2
_registrable($host) // $host
_registrable($host) $host
Truth table66%2/3
A // B
A= _registrable($host)B= $host
ABresult
1X1
011
000
3141 48 my $dw = $self->_parse_domain_whois_abuse($reg);
3142 48 ▶ $whois = $dw if $dw->{abuse};
Branch: 1 branch
BranchTrueFalseTotal
if $dw->{"abuse"} 7 42 49
3143 }
3144  
3145 my $entry = {
3146 ip => $ip,
3147 org => $whois->{org} // '(unknown)',
3148 abuse => $whois->{abuse} // '(unknown)',
3149 country => $whois->{country} // undef,
3150 99 ▶ };
Condition77%7/9
$whois->{"org"} // "(unknown)"
l!l
58400
$whois->{"abuse"} // "(unknown)"
l!l
57411
$whois->{"country"} // undef
l!l
6920
MC/DC100%1/1
$whois->{"org"} // "(unknown)"
$whois->{"org"}
MC/DC100%1/1
$whois->{"abuse"} // "(unknown)"
$whois->{"abuse"}
MC/DC100%1/1
$whois->{"country"} // undef
$whois->{"country"}
Truth table100%2/2
A
A= $whois->{"org"}
Aresult
11
00
Truth table100%2/2
A
A= $whois->{"abuse"}
Aresult
11
00
Truth table100%2/2
A
A= $whois->{"country"}
Aresult
11
00
3151 101 $host_cache{$host} = $entry;
3152  
3153 # Store in cross-message cache for reuse across messages
3154 101 ▶ $_cache->set("url:$host", $entry) if $_cache;
Branch: 1 branch
BranchTrueFalseTotal
if $_cache 98 1 99
3155 }
3156 }
3157  
3158 644 push @results, { url => $url, host => $host, %{ $host_cache{$host} } };
3159 }
3160 229 return \@results;
3161 24 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
3162  
3163 # _is_redirect_cloaker( $bare_host ) -> bool
3164 #
3165 # Purpose:
3166 # Return true if $bare_host is a known cloud-storage or CDN host that is
3167 # commonly abused to serve client-side redirect pages hiding the real
3168 # phishing destination. Checks the exact-match %REDIRECT_HOSTS table and
3169 # the suffix patterns in @REDIRECT_HOST_SUFFIXES.
3170 #
3171 # Entry criteria:
3172 # $bare_host -- lowercase hostname with any leading "www." already stripped.
3173 #
3174 # Exit status:
3175 # Returns 1 (true) or empty string (false).
3176  
3177 701 ▶sub _is_redirect_cloaker :Private {
Subroutine _is_redirect_cloaker: called CC=5 SCAR=16.1
Subroutine _is_redirect_cloaker: called CC=5 SCAR=16.1
3178 701 my (undef, $host) = @_;
3179 701 ▶ return 1 if $REDIRECT_HOSTS{$host};
Branch: 1 branch
BranchTrueFalseTotal
if $REDIRECT_HOSTS{$host} 4 697 701
3180 698 for my $suffix (@REDIRECT_HOST_SUFFIXES) {
3181 4874 ▶ return 1 if length($host) > length($suffix)
Branch: 1 branch
BranchTrueFalseTotal
if length $host > length $suffix and substr($host, -length($suffix)) eq $suffix 0 4875 4875
Condition100%3/3
length $host > length $suffix and substr($host, -length($suffix)) eq $suffix
!ll&&!rl&&r
319816741
MC/DC0%0/2
length $host > length $suffix and substr($host, -length($suffix)) eq $suffix
length $host > length $suffix substr($host, -length($suffix)) eq $suffix
Truth table66%2/3
A and B
A= length $host > length $suffixB= substr($host, -length($suffix)) eq $suffix
ABresult
0X0
100
111
3182 && substr($host, -length($suffix)) eq $suffix;
3183 }
3184 703 return '';
3185 24 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
3186  
3187 # _follow_redirect_chain( $url ) -> $final_url | undef
3188 #
3189 # Purpose:
3190 # Follow up to $REDIRECT_MAX_HOPS HTTP hops for a given URL and return the
3191 # final destination. Detects HTTP 3xx Location: redirects, HTML
3192 # <meta http-equiv="refresh" content="...url=..."> tags, and
3193 # window.location.replace() / window.location.href JavaScript patterns.
3194 # Used to expose the real phishing landing page hidden behind a cloud
3195 # object-store redirect page (e.g. a GCS bucket containing only a
3196 # meta-refresh pointing to the attacker-controlled domain).
3197 #
3198 # Entry criteria:
3199 # $url -- an https?:// URL string whose host has already been identified
3200 # as a URL shortener or redirect cloaker.
3201 # LWP::UserAgent must be installed ($HAS_LWP must be true).
3202 #
3203 # Exit status:
3204 # Returns the first URL that differs from the input after following the
3205 # chain, or undef if no redirect was found or LWP is unavailable.
3206 # Never returns the input $url unchanged.
3207 #
3208 # Side effects:
3209 # Up to $REDIRECT_MAX_HOPS HTTP GET requests.
3210 # Successful result cached in the cross-message CHI cache keyed
3211 # "redirect:<url>" to avoid re-fetching across messages.
3212  
3213 7 ▶sub _follow_redirect_chain :Protected {
Subroutine _follow_redirect_chain: called CC=18 SCAR=29.5
Subroutine _follow_redirect_chain: called CC=18 SCAR=29.5
3214 11 my ($self, $url) = @_;
3215 10 ▶ return undef unless $HAS_LWP;
Branch: 1 branch
BranchTrueFalseTotal
unless $HAS_LWP 0 7 7
3216  
3217 # Serve from cache when available
3218 10 ▶ if ($_cache) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_cache) 5 8 13
3219 10 my $cached = $_cache->get("redirect:$url");
3220 6 ▶ return $cached if defined $cached;
Branch: 1 branch
BranchTrueFalseTotal
if defined $cached 0 9 9
3221 }
3222  
3223 # Dedicated no-follow UA so we can inspect each redirect hop manually.
3224 # Stored separately from the RDAP UA ($self->{ua}) which needs auto-follow.
3225 6 ▶ unless (defined $self->{_ua_nofollow}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"_ua_nofollow"}) 5 1 6
3226 my $ua = LWP::UserAgent->new(
3227 timeout => $self->{timeout},
3228 6 agent => "Email-Abuse-Investigator/$VERSION",
3229 max_redirect => 0,
3230 );
3231 6 ▶ if ($HAS_CONN_CACHE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($HAS_CONN_CACHE) 5 0 5
3232 7 my $cc = LWP::ConnCache->new();
3233 6 $cc->total_capacity(4);
3234 6 $ua->conn_cache($cc);
3235 }
3236 6 $ua->env_proxy(1);
3237 5 $self->{_ua_nofollow} = $ua;
3238 }
3239 6 my $ua = $self->{_ua_nofollow};
3240  
3241 6 my $current = $url;
3242 6 my $final;
3243 6 for my $hop (1 .. $REDIRECT_MAX_HOPS) {
3244 11 my $res = eval { $ua->get($current) };
3245 11 ▶ last unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 1 10 11
3246  
3247 11 ▶ if ($res->is_redirect()) {
Branch: 2 branches
BranchTrueFalseTotal
if ($res->is_redirect) { } 5 5 10
elsif ($res->is_success) { } 1 4 5
3248 # HTTP 3xx: extract Location header
3249 7 my $loc = $res->header('Location');
3250 7 ▶ last unless defined $loc;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $loc 1 5 6
3251  
3252 # Resolve relative Location URLs against the current base
3253 6 ▶ if ($loc !~ m{^https?://}i) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($loc =~ m[^https?://]i) 1 5 6
3254 2 require URI;
3255 2 $loc = URI->new_abs($loc, $current)->as_string();
3256 }
3257 7 $final = $loc;
3258 6 $current = $loc;
3259 } elsif ($res->is_success()) {
3260 # 2xx: inspect body for client-side redirect patterns
3261 1 ▶ my $body = $res->decoded_content() // '';
Condition0%0/3
$res->decoded_content // ""
l!l
000
MC/DC0%0/1
$res->decoded_content // ""
$res->decoded_content
Truth table0%0/2
A
A= $res->decoded_content
Aresult
11
00
3262 1 my $dest;
3263  
3264 # <meta http-equiv="refresh" content="N; url=https://...">
3265 1 ▶ if ($body =~ m{<meta[^>]+http-equiv\s*=\s*["']?refresh["']?[^>]+
Branch: 2 branches
BranchTrueFalseTotal
if ($body =~ m[<meta[^>]+http-equiv\s*=\s*["']?refresh["']?[^>]+ content\s*=\s*["'][^"']*url\s*=\s*(https?://[^"'\s>]+)]xi) { } 0 1 1
elsif ($body =~ m[window\.location(?:\.replace\s*\(\s*|\.href\s*=\s*) ["'](https?://[^"']+)["']]xi) { } 0 1 1
3266 content\s*=\s*["'][^"']*url\s*=\s*(https?://[^"'\s>]+)}xi) {
3267 1 $dest = $1;
3268 }
3269 # window.location.replace("...") or window.location.href = "..."
3270 elsif ($body =~ m{window\.location(?:\.replace\s*\(\s*|\.href\s*=\s*)
3271 ["'](https?://[^"']+)["']}xi) {
3272 2 $dest = $1;
3273 }
3274  
3275 2 ▶ last unless defined $dest;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dest 0 0 0
3276 2 $final = $dest;
3277 1 $current = $dest;
3278 } else {
3279 5 last;
3280 }
3281 }
3282  
3283 # Cache for reuse across messages in this session
3284 5 ▶ $_cache->set("redirect:$url", $final) if $_cache && defined $final;
Branch: 1 branch
BranchTrueFalseTotal
if $_cache and defined $final 3 1 4
Condition66%2/3
$_cache and defined $final
!ll&&!rl&&r
013
MC/DC50%1/2
$_cache and defined $final
$_cache defined $final
Truth table66%2/3
A and B
A= $_cacheB= defined $final
ABresult
0X0
100
111
3285  
3286 5 return $final;
3287 24 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
3288  
3289 # _parallel_resolve_hosts( \%hostnames, \%cache )
3290 #
3291 # Purpose:
3292 # Resolve multiple hostnames to IPs in parallel using AnyEvent::DNS.
3293 # Populates the cache with resolved IPs so the sequential loop in
3294 # _extract_and_resolve_urls() can skip the DNS step for pre-resolved hosts.
3295 #
3296 # Entry criteria:
3297 # $hostnames_ref -- hashref keyed by hostname (values ignored).
3298 # $cache_ref -- hashref to populate with { ip => '...' } results.
3299 # AnyEvent::DNS must be installed ($HAS_ANYEVENT_DNS is true).
3300 #
3301 # Exit status:
3302 # Returns undef; all results written to %$cache_ref via side effects.
3303 #
3304 # Notes:
3305 # Errors (NXDOMAIN, timeout) are silently swallowed; the sequential
3306 # resolution loop will return '(unresolved)' for those hosts.
3307  
3308 3 ▶sub _parallel_resolve_hosts :Private {
Subroutine _parallel_resolve_hosts: called CC=4 SCAR=14.4
Subroutine _parallel_resolve_hosts: called CC=4 SCAR=14.4
3309 1 my ($self, $hostnames_ref, $cache_ref) = @_;
3310 # Guard both conditions together: an empty hash must never reach condvar
3311 # creation because $cv->recv would block forever with $pending == 0.
3312 1 ▶ return unless $HAS_ANYEVENT_DNS && %$hostnames_ref;
Branch: 1 branch
BranchTrueFalseTotal
unless $HAS_ANYEVENT_DNS and %$hostnames_ref 1 0 1
Condition33%1/3
$HAS_ANYEVENT_DNS and %$hostnames_ref
!ll&&!rl&&r
100
MC/DC0%0/2
$HAS_ANYEVENT_DNS and %$hostnames_ref
$HAS_ANYEVENT_DNS %$hostnames_ref
Truth table33%1/3
A and B
A= $HAS_ANYEVENT_DNSB= %$hostnames_ref
ABresult
0X0
100
111
3313  
3314 # Build an AnyEvent condvar to wait for all lookups to complete
3315 1 my $cv = AnyEvent->condvar;
3316 1 my $pending = scalar keys %$hostnames_ref;
3317  
3318 # AnyEvent::DNS::resolve is a method, not a standalone function.
3319 # Use the global resolver singleton; passing a bare string as the
3320 # first arg would make Perl treat the hostname as the invocant.
3321 2 my $resolver = AnyEvent::DNS::resolver();
3322  
3323 2 for my $host (keys %$hostnames_ref) {
3324 # Fire an async A query for each hostname
3325 $resolver->resolve(
3326 $host, 'a',
3327 3 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.4
Subroutine __ANON__: called CC=3 SCAR=11.4
3328 2 my @answers = @_;
3329 2 ▶ if (@answers) {
Branch: 1 branch
BranchTrueFalseTotal
if (@answers) 1 0 1
3330 # Cache the first A record result
3331 2 $cache_ref->{$host} = { ip => $answers[0][4] };
3332 }
3333 # Decrement the pending counter; signal when all done
3334 2 ▶ $cv->send if --$pending <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if --$pending <= 0 0 1 1
3335 },
3336 2 );
3337 }
3338  
3339 # Block until all DNS queries complete (subject to AnyEvent's own timeouts)
3340 2 $cv->recv;
3341 24 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
3342  
3343 # _extract_http_urls( $body ) -> list of url strings
3344 #
3345 # Purpose:
3346 # Extract all HTTP and HTTPS URLs from a body string, using both
3347 # structural HTML parsing (if HTML::LinkExtor is available) and a
3348 # plain-text regex pass. Deduplicates and strips trailing punctuation.
3349 #
3350 # Entry criteria:
3351 # $body -- combined plain+HTML body string.
3352 #
3353 # Exit status:
3354 # Returns a list of URL strings (possibly empty), deduplicated.
3355  
3356 233 ▶sub _extract_http_urls :Private {
Subroutine _extract_http_urls: called CC=5 SCAR=16.1
Subroutine _extract_http_urls: called CC=5 SCAR=16.1
3357 232 my ($self, $body) = @_;
3358 232 my @urls;
3359  
3360 # Structural HTML link extraction (handles quoted attributes correctly)
3361 232 ▶ if ($HAS_HTML_LINKEXTOR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($HAS_HTML_LINKEXTOR) 231 0 231
3362 19 ▶ my $p = HTML::LinkExtor->new(sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
Subroutine __ANON__: called CC=5 SCAR=16.1
3363 18 my ($tag, %attrs) = @_;
3364 18 for my $attr (qw(href src action)) {
3365 50 ▶ my $val = $attrs{$attr} // '';
Condition66%2/3
$attrs{$attr} // ""
l!l
16320
MC/DC100%1/1
$attrs{$attr} // ""
$attrs{$attr}
Truth table100%2/2
A
A= $attrs{$attr}
Aresult
11
00
3366 50 ▶ if ($val =~ m{^https?://}i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($val =~ m[^https?://]i) { } 13 36 49
elsif ($val =~ m[^//[\w.-]]) { } 3 34 37
3367 13 push @urls, $val;
3368 } elsif ($val =~ m{^//[\w.-]}) {
3369 # Protocol-relative -- assume https
3370 3 push @urls, 'https:' . $val;
3371 }
3372 }
3373 232 });
3374 232 $p->parse($body);
3375 }
3376  
3377 # Plain-text regex pass for bare URLs not in HTML attributes
3378 231 ▶ while ($body =~ m{(https?://[^\s<>"'\)\]]+)}gi) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body =~ m<(https?://[^\s<>"'\)\]]+)>gi) 1140 231 1371
3379 1142 push @urls, $1;
3380 }
3381  
3382 # Protocol-relative URLs not caught above
3383 232 ▶ while ($body =~ m{(?:^|[\s"'=])(//[\w.-][^\s<>"'\)\]]*)}gim) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body =~ m<(?:^|[\s"'=])(//[\w.-][^\s<>"'\)\]]*)>mig) 3 230 233
3384 4 push @urls, 'https:' . $1;
3385 }
3386  
3387 # Deduplicate and strip trailing punctuation
3388 232 my %seen;
3389 232 my @all = grep { !$seen{$_}++ } @urls;
3390 232 s/[.,;:!?\)>\]]+$// for @all;
3391 232 return @all;
3392 24 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
3393  
3394 # -----------------------------------------------------------------------
3395 # Private: domain extraction and full analysis
3396 # -----------------------------------------------------------------------
3397  
3398 # _extract_and_analyse_domains() -> arrayref of domain hashrefs
3399 #
3400 # Purpose:
3401 # Collect all non-infrastructure contact domains from headers and body,
3402 # run the full domain intelligence pipeline on each, and return an arrayref
3403 # suitable for storage in $self->{_mailto_domains}.
3404 #
3405 # Entry criteria:
3406 # _split_message() must have been called.
3407 #
3408 # Exit status:
3409 # Always returns an arrayref; never undef.
3410 #
3411 # Side effects:
3412 # Network I/O per domain via _analyse_domain().
3413 # Results stored in $self->{_domain_info} and CHI cache.
3414  
3415 216 ▶sub _extract_and_analyse_domains :Private {
Subroutine _extract_and_analyse_domains: called CC=26 SCAR=33.0
Subroutine _extract_and_analyse_domains: called CC=26 SCAR=33.0
3416 215 my $self = $_[0];
3417 214 my (%seen, @domains_with_source);
3418  
3419 # Build a set of recipient domains to exclude (victims, not senders)
3420 214 my %recipient_domains;
3421 215 for my $hname (qw(to cc)) {
3422 428 ▶ my $val = $self->_header_value($hname) // next;
Condition66%2/3
$self->_header_value($hname) // (next)
l!l
1962300
MC/DC100%1/1
$self->_header_value($hname) // (next)
$self->_header_value($hname)
Truth table100%2/2
A
A= $self->_header_value($hname)
Aresult
11
00
3423 198 for my $dom ($self->_domains_from_text($val)) {
3424 195 ▶ my $reg = _registrable($dom) // $dom;
Condition33%1/3
_registrable($dom) // $dom
l!l&&r!l&&!r
19300
MC/DC0%0/2
_registrable($dom) // $dom
_registrable($dom) $dom
Truth table33%1/3
A // B
A= _registrable($dom)B= $dom
ABresult
1X1
011
000
3425 195 $recipient_domains{$dom}++;
3426 195 $recipient_domains{$reg}++;
3427 }
3428 }
3429  
3430 # Also exclude domains from Received: "for" envelope recipients
3431 215 for my $hop (@{ $self->{_rcvd_tracking} }) {
3432 204 ▶ next unless $hop->{for} && $hop->{for} =~ /\@([\w.-]+)/;
Branch: 1 branch
BranchTrueFalseTotal
unless $hop->{"for"} and $hop->{"for"} =~ /\@([\w.-]+)/ 201 2 203
Condition100%3/3
$hop->{"for"} and $hop->{"for"} =~ /\@([\w.-]+)/
!ll&&!rl&&r
20012
MC/DC100%2/2
$hop->{"for"} and $hop->{"for"} =~ /\@([\w.-]+)/
$hop->{"for"} $hop->{"for"} =~ /\@([\w.-]+)/
Truth table100%3/3
A and B
A= $hop->{"for"}B= $hop->{"for"} =~ /\@([\w.-]+)/
ABresult
0X0
100
111
3433 4 my $dom = lc $1;
3434 4 ▶ my $reg = _registrable($dom) // $dom;
Condition33%1/3
_registrable($dom) // $dom
l!l&&r!l&&!r
200
MC/DC0%0/2
_registrable($dom) // $dom
_registrable($dom) $dom
Truth table33%1/3
A // B
A= _registrable($dom)B= $dom
ABresult
1X1
011
000
3435 4 $recipient_domains{$dom}++;
3436 4 $recipient_domains{$reg}++;
3437 }
3438  
3439 # Inner closure: record a domain if it passes all filters
3440 814 ▶ my $record = sub {
Subroutine __ANON__: called CC=9 SCAR=22.1
Subroutine __ANON__: called CC=9 SCAR=22.1
3441 812 my ($dom, $source) = @_;
3442 812 $dom = lc $dom;
3443 813 $dom =~ s/\.$//;
3444 813 ▶ next if $self->{trusted_domains}->{$dom};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"trusted_domains"}{$dom} 0 812 812
3445 812 ▶ return if $TRUSTED_DOMAINS{$dom};
Branch: 1 branch
BranchTrueFalseTotal
if $TRUSTED_DOMAINS{$dom} 44 767 811
3446 768 ▶ return if $recipient_domains{$dom};
Branch: 1 branch
BranchTrueFalseTotal
if $recipient_domains{$dom} 2 765 767
3447 766 ▶ return if $recipient_domains{ _registrable($dom) // $dom };
Branch: 1 branch
BranchTrueFalseTotal
if $recipient_domains{_registrable($dom) // $dom} 0 766 766
Condition66%2/3
_registrable($dom) // $dom
l!l&&r!l&&!r
667980
MC/DC0%0/2
_registrable($dom) // $dom
_registrable($dom) $dom
Truth table66%2/3
A // B
A= _registrable($dom)B= $dom
ABresult
1X1
011
000
3448 # Discard non-routable hostnames (single-label, pseudo-TLDs, etc.)
3449 766 ▶ return unless $dom =~ /\.[a-zA-Z]{2,}$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $dom =~ /\.[a-zA-Z]{2,}$/ 98 667 765
3450 668 ▶ return if $dom =~ /\.(?:local|internal|lan|localdomain|arpa)$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $dom =~ /\.(?:local|internal|lan|localdomain|arpa)$/i 2 665 667
3451 666 ▶ return if $seen{$dom}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$dom}++ 191 474 665
3452 475 push @domains_with_source, { domain => $dom, source => $source };
3453 214 };
3454  
3455 # Collect from standard sender/reply headers
3456 214 my %header_sources = (
3457 'from' => 'From: header',
3458 'reply-to' => 'Reply-To: header',
3459 'return-path' => 'Return-Path: header',
3460 'sender' => 'Sender: header',
3461 );
3462 214 for my $hname (sort keys %header_sources) {
3463 854 ▶ my $val = $self->_header_value($hname) // next;
Condition100%2/2
$self->_header_value($hname) // (next)
l!l
375477
MC/DC100%1/1
$self->_header_value($hname) // (next)
$self->_header_value($hname)
Truth table100%2/2
A
A= $self->_header_value($hname)
Aresult
11
00
3464 $record->($_, $header_sources{$hname})
3465 377 for $self->_domains_from_text($val);
3466 }
3467  
3468 # Message-ID domain often reveals the real bulk-sending platform
3469 214 my $mid = $self->_header_value('message-id');
3470 214 ▶ if ($mid && $mid =~ /\@([\w.-]+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mid and $mid =~ /\@([\w.-]+)/) 195 19 214
Condition66%2/3
$mid and $mid =~ /\@([\w.-]+)/
!ll&&!rl&&r
180195
MC/DC50%1/2
$mid and $mid =~ /\@([\w.-]+)/
$mid $mid =~ /\@([\w.-]+)/
Truth table66%2/3
A and B
A= $midB= $mid =~ /\@([\w.-]+)/
ABresult
0X0
100
111
3471 196 my $mid_dom = lc $1;
3472 196 ▶ my $mid_reg = _registrable($mid_dom) // $mid_dom;
Condition66%2/3
_registrable($mid_dom) // $mid_dom
l!l&&r!l&&!r
98980
MC/DC0%0/2
_registrable($mid_dom) // $mid_dom
_registrable($mid_dom) $mid_dom
Truth table66%2/3
A // B
A= _registrable($mid_dom)B= $mid_dom
ABresult
1X1
011
000
3473 $record->($mid_dom, 'Message-ID: header')
3474 197 ▶ unless $TRUSTED_DOMAINS{$mid_dom} || $TRUSTED_DOMAINS{$mid_reg} || $self->{trusted_domains}->{$mid_dom} || $self->{trusted_domains}->{$mid_reg};
Branch: 1 branch
BranchTrueFalseTotal
unless $TRUSTED_DOMAINS{$mid_dom} or $TRUSTED_DOMAINS{$mid_reg} or $self->{"trusted_domains"}{$mid_dom} or $self->{"trusted_domains"}{$mid_reg} 193 3 196
Condition66%6/9
$TRUSTED_DOMAINS{$mid_dom} or $TRUSTED_DOMAINS{$mid_reg} or $self->{"trusted_domains"}{$mid_dom} or $self->{"trusted_domains"}{$mid_reg}
l!l&&r!l&&!r
20193
$TRUSTED_DOMAINS{$mid_dom} or $TRUSTED_DOMAINS{$mid_reg} or $self->{"trusted_domains"}{$mid_dom}
l!l&&r!l&&!r
20193
$TRUSTED_DOMAINS{$mid_dom} or $TRUSTED_DOMAINS{$mid_reg}
l!l&&r!l&&!r
02193
MC/DC25%1/4
$TRUSTED_DOMAINS{$mid_dom} or $TRUSTED_DOMAINS{$mid_reg} or $self->{"trusted_domains"}{$mid_dom} or $self->{"trusted_domains"}{$mid_reg}
$TRUSTED_DOMAINS{$mid_dom} $TRUSTED_DOMAINS{$mid_reg} $self->{"trusted_domains"}{$mid_dom} $self->{"trusted_domains"}{$mid_reg}
Truth table40%2/5
A or B or C or D
A= $TRUSTED_DOMAINS{$mid_dom}B= $TRUSTED_DOMAINS{$mid_reg}C= $self->{"trusted_domains"}{$mid_dom}D= $self->{"trusted_domains"}{$mid_reg}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
3475 }
3476  
3477 # DKIM signing domain(s) -- the organisation that vouches for the message
3478 215 my $auth = $self->_parse_auth_results_cached();
3479 214 ▶ for my $dkim_d (@{ $auth->{dkim_domains} // [] }) {
Condition66%2/3
$auth->{"dkim_domains"} // []
l!l
72070
MC/DC100%1/1
$auth->{"dkim_domains"} // []
$auth->{"dkim_domains"}
Truth table100%2/2
A
A= $auth->{"dkim_domains"}
Aresult
11
00
3480 9 $record->($dkim_d, 'DKIM-Signature: d= (signing domain)');
3481 }
3482  
3483 # List-Unsubscribe identifies the ESP or bulk sender
3484 214 my $unsub = $self->_header_value('list-unsubscribe');
3485 215 ▶ if ($unsub) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unsub) 2 212 214
3486 3 ▶ while ($unsub =~ m{https?://([^/:?\s>]+)}gi) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unsub =~ m[https?://([^/:?\s>]+)]gi) 1 3 4
3487 2 $record->(lc $1, 'List-Unsubscribe: header');
3488 }
3489 4 ▶ while ($unsub =~ m{mailto:[^@\s>]+\@([\w.-]+)}gi) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unsub =~ /mailto:[^\@\s>]+\@([\w.-]+)/gi) 1 3 4
3490 3 $record->(lc $1, 'List-Unsubscribe: header');
3491 }
3492 }
3493  
3494 # Body email addresses (mailto: and bare user@domain forms)
3495 215 my $combined = $self->{_body_plain} . "\n" . $self->{_body_html};
3496 $record->($_, 'email address / mailto in body')
3497 215 for $self->_domains_from_text($combined);
3498  
3499 # Run the full intelligence pipeline on each collected domain
3500 215 my @results;
3501 215 for my $entry (@domains_with_source) {
3502 476 my $info = $self->_analyse_domain($entry->{domain});
3503 476 push @results, { %$entry, %$info };
3504 }
3505 215 return \@results;
3506 24 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
3507  
3508 # _domains_from_text( $text ) -> list of domain strings
3509 #
3510 # Purpose:
3511 # Extract unique domain names from mailto: links and bare user@domain
3512 # addresses in a block of text.
3513 #
3514 # Entry criteria:
3515 # $text -- a defined scalar of decoded body or header text.
3516 #
3517 # Exit status:
3518 # Returns a list of lower-cased domain strings (possibly empty).
3519  
3520 978 ▶sub _domains_from_text :Private {
Subroutine _domains_from_text: called CC=5 SCAR=16.1
Subroutine _domains_from_text: called CC=5 SCAR=16.1
3521 976 my ($self, $text) = @_;
3522 976 my (%seen, @out);
3523  
3524 # mailto: links (including HTML-entity-encoded @ signs from QP)
3525 976 ▶ while ($text =~ /mailto:(?:[^@\s<>"]+)@([\w.-]+)/gi) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /mailto:(?:[^\@\s<>"]+)@([\w.-]+)/gi) 13 976 989
3526 14 my $dom = lc $1; $dom =~ s/\.$//;
3527 14 ▶ push @out, $dom unless $seen{$dom}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $seen{$dom}++ 12 1 13
3528 }
3529  
3530 # Bare user@domain patterns
3531 976 ▶ while ($text =~ /\b[\w.+%-]+@([\w.-]+\.[a-zA-Z]{2,})\b/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /\b[\w.+%-]+@([\w.-]+\.[a-zA-Z]{2,})\b/g) 841 975 1816
3532 842 my $dom = lc $1; $dom =~ s/\.$//;
3533 842 ▶ push @out, $dom unless $seen{$dom}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $seen{$dom}++ 819 22 841
3534 }
3535 976 return @out;
3536 24 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
3537  
3538 # _analyse_domain( $domain ) -> hashref
3539 #
3540 # Purpose:
3541 # Run the complete intelligence pipeline for a single domain: A record
3542 # (web hosting), MX record (mail hosting), NS record (DNS hosting),
3543 # and WHOIS (registrar, creation/expiry dates, abuse contact).
3544 # Each IP is enriched via RDAP/WHOIS. Results are cached per domain
3545 # in $self->{_domain_info} and in the CHI cross-message cache.
3546 #
3547 # Entry criteria:
3548 # $domain -- lower-cased, no trailing dot, not in TRUSTED_DOMAINS.
3549 # $self->{timeout} used for all network operations.
3550 #
3551 # Exit status:
3552 # Always returns a hashref reference; never undef; may be empty ({}).
3553 # Possible keys: web_ip, web_org, web_abuse, mx_host, mx_ip, mx_org,
3554 # mx_abuse, ns_host, ns_ip, ns_org, ns_abuse, registrar,
3555 # registrar_abuse, registered, expires, recently_registered, whois_raw.
3556 #
3557 # Side effects:
3558 # Network I/O; writes result to $self->{_domain_info}{$domain} and CHI.
3559 #
3560 # Notes:
3561 # MX/NS lookups require Net::DNS; absent without it.
3562 # recently_registered is set to 1 (not 0) when the threshold is met.
3563 # whois_raw is truncated to WHOIS_RAW_MAX bytes.
3564  
3565 480 ▶sub _analyse_domain :Private {
Subroutine _analyse_domain: called CC=33 SCAR=35.8
Subroutine _analyse_domain: called CC=33 SCAR=35.8
3566 478 my ($self, $domain) = @_;
3567  
3568 # Return the per-message cached result if already analysed
3569 return $self->{_domain_info}{$domain}
3570 478 ▶ if $self->{_domain_info}{$domain};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_domain_info"}{$domain} 5 473 478
3571  
3572 # Check the cross-message CHI cache before hitting the network
3573 474 ▶ if ($_cache) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_cache) 474 0 474
3574 474 my $cached = $_cache->get("dom:$domain");
3575 474 ▶ if ($cached) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cached) 133 341 474
3576 134 $self->{_domain_info}{$domain} = $cached;
3577 135 return $cached;
3578 }
3579 }
3580  
3581 343 $self->_debug("Analysing domain: $domain");
3582 343 my %info;
3583  
3584 # --- A record -> web hosting IP ---
3585 343 my $web_ip = $self->_resolve_host($domain);
3586 343 ▶ if ($web_ip) {
Branch: 1 branch
BranchTrueFalseTotal
if ($web_ip) 27 313 340
3587 30 $info{web_ip} = $web_ip;
3588 30 my $w = $self->_whois_ip($web_ip);
3589 30 ▶ $info{web_org} = $w->{org} if $w->{org};
Branch: 1 branch
BranchTrueFalseTotal
if $w->{"org"} 24 3 27
3590 40 ▶ $info{web_abuse} = $w->{abuse} if $w->{abuse};
Branch: 1 branch
BranchTrueFalseTotal
if $w->{"abuse"} 23 4 27
3591 }
3592  
3593 # MX and NS lookups require Net::DNS
3594 353 ▶ if ($HAS_NET_DNS) {
Branch: 1 branch
BranchTrueFalseTotal
if ($HAS_NET_DNS) 0 340 340
3595 my $res = Net::DNS::Resolver->new(
3596 tcp_timeout => $self->{timeout},
3597 udp_timeout => $self->{timeout},
3598 11 );
3599  
3600 # --- MX record -> mail hosting ---
3601 11 ▶ if(my $mxq = $res->search($domain, 'MX')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $mxq = $res->search($domain, "MX")) 0 0 0
3602 3 my ($best) = sort { $a->preference <=> $b->preference }
3603 11 grep { $_->type eq 'MX' } $mxq->answer;
3604 3 ▶ if ($best) {
Branch: 1 branch
BranchTrueFalseTotal
if ($best) 0 0 0
3605 3 (my $mx_host = lc $best->exchange) =~ s/\.$//;
3606 2 $info{mx_host} = $mx_host;
3607 1 my $mx_ip = $self->_resolve_host($mx_host);
3608 1 ▶ if ($mx_ip) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mx_ip) 0 0 0
3609 0 $info{mx_ip} = $mx_ip;
3610 3 my $mw = $self->_whois_ip($mx_ip);
3611 3 ▶ $info{mx_org} = $mw->{org} if $mw->{org};
Branch: 1 branch
BranchTrueFalseTotal
if $mw->{"org"} 0 0 0
3612 3 ▶ $info{mx_abuse} = $mw->{abuse} if $mw->{abuse};
Branch: 1 branch
BranchTrueFalseTotal
if $mw->{"abuse"} 2 10 12
3613 }
3614 }
3615 }
3616  
3617 # --- NS record -> DNS hosting ---
3618 3 ▶ if(my $nsq = $res->search($domain, 'NS')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $nsq = $res->search($domain, "NS")) 0 10 10
3619 # Sort alphabetically so DNS round-robin ordering never changes which
3620 # nameserver we record -- both runs of the same domain always agree.
3621 3 my ($first) = sort { $a->nsdname cmp $b->nsdname } grep { $_->type eq 'NS' } $nsq->answer;
3622 3 ▶ if ($first) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first) 0 2 2
3623 3 (my $ns_host = lc $first->nsdname) =~ s/\.$//;
3624 3 $info{ns_host} = $ns_host;
3625 3 my $ns_ip = $self->_resolve_host($ns_host);
3626 1 ▶ if ($ns_ip) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ns_ip) 2 0 2
3627 3 $info{ns_ip} = $ns_ip;
3628 3 my $nw = $self->_whois_ip($ns_ip);
3629 3 ▶ $info{ns_org} = $nw->{org} if $nw->{org};
Branch: 1 branch
BranchTrueFalseTotal
if $nw->{"org"} 2 0 2
3630 1 ▶ $info{ns_abuse} = $nw->{abuse} if $nw->{abuse};
Branch: 1 branch
BranchTrueFalseTotal
if $nw->{"abuse"} 0 2 2
3631 }
3632 }
3633 }
3634 }
3635  
3636 # --- Domain WHOIS -> registrar + dates ---
3637 341 ▶ if(my $domain_whois = $self->_domain_whois($domain)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $domain_whois = $self->_domain_whois($domain)) 23 319 342
3638 # Truncate raw WHOIS for storage but parse structured fields from full text
3639 22 $info{whois_raw} = substr($domain_whois, 0, $WHOIS_RAW_MAX);
3640  
3641 # Registrar name
3642 22 ▶ if ($domain_whois =~ /Registrar:\s*(.+)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($domain_whois =~ /Registrar:\s*(.+)/i) 19 4 23
3643 18 ($info{registrar} = $1) =~ s/\s+$//;
3644 }
3645  
3646 # Registrar abuse contact email (try multiple field names)
3647 24 for my $pat (
3648 qr/Registrar Abuse Contact Email:\s*(\S+@\S+)/i,
3649 qr/Abuse Contact Email:\s*(\S+@\S+)/i,
3650 qr/abuse-contact:\s*(\S+@\S+)/i,
3651 ) {
3652 66 ▶ if (!$info{registrar_abuse} && $domain_whois =~ $pat) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $info{"registrar_abuse"} and $domain_whois =~ /$pat/) 16 49 65
Condition100%3/3
not $info{"registrar_abuse"} and $domain_whois =~ /$pat/
!ll&&!rl&&r
252414
MC/DC100%2/2
not $info{"registrar_abuse"} and $domain_whois =~ /$pat/
not $info{"registrar_abuse"} $domain_whois =~ /$pat/
Truth table100%3/3
not(A) and B
A= not $info{"registrar_abuse"}B= $domain_whois =~ /$pat/
ABresult
0X0
100
111
3653 17 ($info{registrar_abuse} = $1) =~ s/\s+$//;
3654 }
3655 }
3656  
3657 # Domain creation date (multiple registrar field name variations)
3658 22 for my $pat (
3659 qr/Creation Date:\s*(\S+)/i,
3660 qr/Created(?:\s+On)?:\s*(\S+)/i,
3661 qr/Registration Time:\s*(\S+)/i,
3662 qr/^registered:\s*(\S+)/im,
3663 ) {
3664 85 ▶ if (!$info{registered} && $domain_whois =~ $pat) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $info{"registered"} and $domain_whois =~ /$pat/) 15 71 86
Condition100%3/3
not $info{"registered"} and $domain_whois =~ /$pat/
!ll&&!rl&&r
383115
MC/DC100%2/2
not $info{"registered"} and $domain_whois =~ /$pat/
not $info{"registered"} $domain_whois =~ /$pat/
Truth table100%3/3
not(A) and B
A= not $info{"registered"}B= $domain_whois =~ /$pat/
ABresult
0X0
100
111
3665 16 ($info{registered} = $1) =~ s/[TZ].*//;
3666 }
3667 }
3668  
3669 # Domain expiry date
3670 22 for my $pat (
3671 qr/Registry Expiry Date:\s*(\S+)/i,
3672 qr/Expir(?:y|ation)(?: Date)?:\s*(\S+)/i,
3673 qr/paid-till:\s*(\S+)/i,
3674 ) {
3675 64 ▶ if (!$info{expires} && $domain_whois =~ $pat) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $info{"expires"} and $domain_whois =~ /$pat/) 13 52 65
Condition100%3/3
not $info{"expires"} and $domain_whois =~ /$pat/
!ll&&!rl&&r
222813
MC/DC100%2/2
not $info{"expires"} and $domain_whois =~ /$pat/
not $info{"expires"} $domain_whois =~ /$pat/
Truth table100%3/3
not(A) and B
A= not $info{"expires"}B= $domain_whois =~ /$pat/
ABresult
0X0
100
111
3676 14 ($info{expires} = $1) =~ s/[TZ].*//;
3677 }
3678 }
3679  
3680 # Flag recently-registered domains (< RECENT_REG_DAYS old)
3681 22 ▶ if ($info{registered}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info{"registered"}) 15 6 21
3682 16 my $epoch = $self->_parse_date_to_epoch($info{registered});
3683 18 ▶ $info{recently_registered} = 1
Branch: 1 branch
BranchTrueFalseTotal
if $epoch and time - $epoch < $RECENT_REG_DAYS * $SECS_PER_DAY 6 9 15
Condition100%3/3
$epoch and time - $epoch < $RECENT_REG_DAYS * $SECS_PER_DAY
!ll&&!rl&&r
2116
MC/DC50%1/2
$epoch and time - $epoch < $RECENT_REG_DAYS * $SECS_PER_DAY
$epoch time - $epoch < $RECENT_REG_DAYS * $SECS_PER_DAY
Truth table66%2/3
A and B
A= $epochB= time - $epoch < $RECENT_REG_DAYS * $SECS_PER_DAY
ABresult
0X0
100
111
3684 if $epoch && (time() - $epoch) < $RECENT_REG_DAYS * $SECS_PER_DAY;
3685 }
3686 }
3687  
3688 # Store in per-message and cross-message caches
3689 343 $self->{_domain_info}{$domain} = \%info;
3690 343 ▶ $_cache->set("dom:$domain", \%info) if $_cache;
Branch: 1 branch
BranchTrueFalseTotal
if $_cache 342 0 342
3691  
3692 343 return \%info;
3693 24 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
3694  
3695 # -----------------------------------------------------------------------
3696 # Private: DNS helpers
3697 # -----------------------------------------------------------------------
3698  
3699 # _resolve_host( $host ) -> ip_string | undef
3700 #
3701 # Purpose:
3702 # Resolve a hostname to an IPv4 (or IPv6) address. Uses Net::DNS for
3703 # both A and AAAA queries when available; falls back to inet_aton for
3704 # pure IPv4 resolution.
3705 #
3706 # Entry criteria:
3707 # $host -- hostname string or already-numeric IP.
3708 #
3709 # Exit status:
3710 # Returns the first resolved IP string, or undef on failure.
3711 #
3712 # Notes:
3713 # When the input is already a dotted-quad IPv4 it is returned immediately.
3714 # AAAA records are tried if the A query fails and Net::DNS is available.
3715  
3716 11 ▶sub _resolve_host :Protected {
Subroutine _resolve_host: called CC=14 SCAR=26.8
Subroutine _resolve_host: called CC=14 SCAR=26.8
3717 11 my ($self, $host) = @_;
3718 11 ▶ return $host if $host =~ /^\d{1,3}(?:\.\d{1,3}){3}$/;
Branch: 1 branch
BranchTrueFalseTotal
if $host =~ /^\d{1,3}(?:\.\d{1,3}){3}$/ 3 7 10
3719  
3720 # Check the CHI cache before hitting DNS
3721 10 ▶ if ($_cache) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_cache) 7 2 9
3722 10 my $cached_ip = $_cache->get("resolve:$host");
3723 10 ▶ return $cached_ip if defined $cached_ip;
Branch: 1 branch
BranchTrueFalseTotal
if defined $cached_ip 2 7 9
3724 }
3725  
3726 10 my $ip;
3727  
3728 10 ▶ if ($HAS_NET_DNS) {
Branch: 1 branch
BranchTrueFalseTotal
if ($HAS_NET_DNS) { } 0 9 9
3729 my $res = Net::DNS::Resolver->new(
3730 tcp_timeout => $self->{timeout},
3731 udp_timeout => $self->{timeout},
3732 3 );
3733  
3734 # Try A record first, then AAAA for IPv6
3735 3 for my $type (qw(A AAAA)) {
3736 1 my $query = $res->search($host, $type);
3737 3 ▶ if ($query) {
Branch: 1 branch
BranchTrueFalseTotal
if ($query) 2 0 2
3738 3 for my $rr ($query->answer) {
3739 3 ▶ if ($rr->type eq 'A') {
Branch: 2 branches
BranchTrueFalseTotal
if ($rr->type eq 'A') { } 2 0 2
elsif ($rr->type eq 'AAAA') { } 2 0 2
3740 3 $ip = $rr->address;
3741 3 last;
3742 } elsif ($rr->type eq 'AAAA') {
3743 3 $ip = $rr->address;
3744 3 last;
3745 }
3746 }
3747 }
3748 3 ▶ last if defined $ip;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ip 2 0 2
3749 }
3750 } else {
3751 # Fallback: gethostbyname (IPv4 only)
3752 10 my $packed = eval { inet_aton($host) };
3753 10 ▶ $ip = $packed ? inet_ntoa($packed) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$packed ? : 0 9 9
3754 }
3755  
3756 # Cache the result (including undef as '' to avoid repeated failed lookups)
3757 10 ▶ if ($_cache) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_cache) 7 2 9
3758 8 ▶ $_cache->set("resolve:$host", $ip // '');
Condition66%2/3
$ip // ""
l!l
270
MC/DC0%0/1
$ip // ""
$ip
Truth table50%1/2
A
A= $ip
Aresult
11
00
3759 }
3760  
3761 8 return $ip;
3762 24 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
3763  
3764 # _reverse_dns( $ip ) -> hostname | undef
3765 #
3766 # Purpose:
3767 # Perform a PTR (reverse DNS) lookup for an IP address. Supports both
3768 # IPv4 and IPv6 via Net::DNS when available; falls back to gethostbyaddr.
3769 #
3770 # Entry criteria:
3771 # $ip -- a defined IPv4 or IPv6 address string.
3772 #
3773 # Exit status:
3774 # Returns the PTR hostname string, or undef if no record exists.
3775  
3776 25 ▶sub _reverse_dns :Protected {
Subroutine _reverse_dns: called CC=6 SCAR=18.6
Subroutine _reverse_dns: called CC=6 SCAR=18.6
3777 25 my ($self, $ip) = @_;
3778 23 ▶ return unless $ip;
Branch: 1 branch
BranchTrueFalseTotal
unless $ip 0 24 24
3779  
3780 23 ▶ if ($HAS_NET_DNS) {
Branch: 1 branch
BranchTrueFalseTotal
if ($HAS_NET_DNS) 0 24 24
3781 3 my $res = Net::DNS::Resolver->new(tcp_timeout => $self->{timeout});
3782 3 my $query = $res->search($ip, 'PTR');
3783 3 ▶ if ($query) {
Branch: 1 branch
BranchTrueFalseTotal
if ($query) 0 2 2
3784 3 for my $rr ($query->answer) {
3785 3 ▶ return $rr->ptrdname if $rr->type eq 'PTR';
Branch: 1 branch
BranchTrueFalseTotal
if $rr->type eq "PTR" 0 2 2
3786 }
3787 }
3788 3 return;
3789 }
3790  
3791 # Fallback for IPv4 only
3792 25 return scalar gethostbyaddr(inet_aton($ip), AF_INET);
3793 24 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
3794  
3795 # -----------------------------------------------------------------------
3796 # Private: WHOIS / RDAP
3797 # -----------------------------------------------------------------------
3798  
3799 # _whois_ip( $ip ) -> hashref
3800 #
3801 # Purpose:
3802 # Enrich an IP address with organisation name, abuse contact, and country
3803 # code. Tries RDAP first (if LWP is available), then falls back to raw
3804 # WHOIS via IANA referral. Results are cached in CHI if available.
3805 #
3806 # Entry criteria:
3807 # $ip -- a defined IPv4 or IPv6 address string.
3808 #
3809 # Exit status:
3810 # Returns { org, abuse, country } hashref; keys absent when unknown.
3811  
3812 28 ▶sub _whois_ip :Protected {
Subroutine _whois_ip: called CC=10 SCAR=23.2
Subroutine _whois_ip: called CC=10 SCAR=23.2
3813 26 my ($self, $ip) = @_;
3814  
3815 # Check CHI cache before going to the network
3816 28 ▶ if ($_cache) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_cache) 25 0 25
3817 28 my $cached = $_cache->get("whois_ip:$ip");
3818 28 ▶ return $cached if $cached;
Branch: 1 branch
BranchTrueFalseTotal
if $cached 18 7 25
3819 }
3820  
3821 10 ▶ my $result = $HAS_LWP ? $self->_rdap_lookup($ip) : {};
Branch: 1 branch
BranchTrueFalseTotal
$HAS_LWP ? : 7 2 9
3822  
3823 # Fall back to raw WHOIS if RDAP returned no organisation
3824 10 ▶ unless ($result->{org}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result->{"org"}) 3 4 7
3825 4 my $raw = $self->_raw_whois($ip, 'whois.iana.org');
3826 4 ▶ if ($raw) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raw) 2 1 3
3827 3 my ($ref) = $raw =~ /whois:\s*([\w.-]+)/i;
3828 5 ▶ my $detail = $ref ? $self->_raw_whois($ip, $ref) : $raw;
Branch: 1 branch
BranchTrueFalseTotal
$ref ? : 1 1 2
3829 5 ▶ $result = $self->_parse_whois_text($detail) if $detail;
Branch: 1 branch
BranchTrueFalseTotal
if $detail 2 2 4
3830 }
3831 }
3832  
3833 # Cache the enrichment result
3834 8 ▶ $_cache->set("whois_ip:$ip", $result) if $_cache && $result;
Branch: 1 branch
BranchTrueFalseTotal
if $_cache and $result 7 0 7
Condition33%1/3
$_cache and $result
!ll&&!rl&&r
007
MC/DC0%0/2
$_cache and $result
$_cache $result
Truth table33%1/3
A and B
A= $_cacheB= $result
ABresult
0X0
100
111
3835  
3836 8 return $result;
3837 24 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
3838  
3839 # _domain_whois( $domain ) -> raw_whois_string | undef
3840 #
3841 # Purpose:
3842 # Perform a two-step WHOIS lookup for a domain: first ask IANA for the
3843 # TLD's authoritative WHOIS server, then query that server.
3844 #
3845 # Entry criteria:
3846 # $domain -- a lower-cased domain name string.
3847 #
3848 # Exit status:
3849 # Returns the raw WHOIS response string, or undef on failure.
3850  
3851 14 ▶sub _domain_whois :Protected {
Subroutine _domain_whois: called CC=3 SCAR=11.0
Subroutine _domain_whois: called CC=3 SCAR=11.0
3852 12 my ($self, $domain) = @_;
3853 12 ▶ my $iana = $self->_raw_whois($domain, 'whois.iana.org') // return;
Condition66%2/3
$self->_raw_whois($domain, "whois.iana.org") // (return)
l!l
1010
MC/DC100%1/1
$self->_raw_whois($domain, "whois.iana.org") // (return)
$self->_raw_whois($domain, "whois.iana.org")
Truth table100%2/2
A
A= $self->_raw_whois($domain, "whois.iana.org")
Aresult
11
00
3854 13 my ($server) = $iana =~ /whois:\s*([\w.-]+)/i;
3855 13 ▶ return unless $server;
Branch: 1 branch
BranchTrueFalseTotal
unless $server 9 1 10
3856 2 return $self->_raw_whois($domain, $server);
3857 24 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
3858  
3859 # _parse_domain_whois_abuse( $domain ) -> hashref
3860 #
3861 # Purpose:
3862 # Lightweight domain WHOIS lookup to extract only registrar name and
3863 # abuse contact. Used as a fallback in _extract_and_resolve_urls() when
3864 # a URL host cannot be resolved to an IP.
3865 #
3866 # Entry criteria:
3867 # $domain -- a registrable domain name string.
3868 #
3869 # Exit status:
3870 # Returns { org, abuse } hashref; empty hashref on failure.
3871  
3872 53 ▶sub _parse_domain_whois_abuse :Private {
Subroutine _parse_domain_whois_abuse: called CC=6 SCAR=18.0
Subroutine _parse_domain_whois_abuse: called CC=6 SCAR=18.0
3873 51 my ($self, $domain) = @_;
3874 51 ▶ my $raw = $self->_domain_whois($domain) // return {};
Condition66%2/3
$self->_domain_whois($domain) // (return {})
l!l
8420
MC/DC100%1/1
$self->_domain_whois($domain) // (return {})
$self->_domain_whois($domain)
Truth table100%2/2
A
A= $self->_domain_whois($domain)
Aresult
11
00
3875 9 my %info;
3876 11 ▶ if ($raw =~ /Registrar:\s*(.+)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raw =~ /Registrar:\s*(.+)/i) 8 0 8
3877 9 ($info{org} = $1) =~ s/\s+$//;
3878 }
3879 # Try multiple field name patterns for the abuse email
3880 9 for my $pat (
3881 qr/Registrar Abuse Contact Email:\s*(\S+\@\S+)/i,
3882 qr/Abuse Contact Email:\s*(\S+\@\S+)/i,
3883 qr/abuse-contact:\s*(\S+\@\S+)/i,
3884 ) {
3885 27 ▶ if (!$info{abuse} && $raw =~ $pat) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $info{"abuse"} and $raw =~ /$pat/) 10 16 26
Condition100%3/3
not $info{"abuse"} and $raw =~ /$pat/
!ll&&!rl&&r
1518
MC/DC100%2/2
not $info{"abuse"} and $raw =~ /$pat/
not $info{"abuse"} $raw =~ /$pat/
Truth table100%3/3
not(A) and B
A= not $info{"abuse"}B= $raw =~ /$pat/
ABresult
0X0
100
111
3886 11 ($info{abuse} = $1) =~ s/\s+$//;
3887 }
3888 }
3889 11 return \%info;
3890 24 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
3891  
3892 # _rdap_lookup( $ip ) -> hashref
3893 #
3894 # Purpose:
3895 # Query the ARIN RDAP API for IP block ownership information. RDAP is
3896 # preferred over raw WHOIS because it returns structured JSON.
3897 #
3898 # Entry criteria:
3899 # $ip -- a defined IPv4 or IPv6 address string. RFC 4007 zone
3900 # identifiers (e.g. %eth0 appended to link-local addresses)
3901 # are stripped before validation. The remaining value must
3902 # match dotted-quad IPv4 or bare hex-colon IPv6; malformed
3903 # inputs return {} immediately without a network call.
3904 # LWP::UserAgent must be installed.
3905 #
3906 # Exit status:
3907 # Returns { org, abuse, country } hashref; empty hashref on failure or
3908 # when $ip does not pass format validation.
3909 #
3910 # Security:
3911 # $ip is validated before being interpolated into the RDAP URL path to
3912 # prevent URL path manipulation. Zone IDs are stripped first because
3913 # a literal '%' in the path would corrupt the URL.
3914  
3915 10 ▶sub _rdap_lookup :Protected {
Subroutine _rdap_lookup: called CC=13 SCAR=25.9
Subroutine _rdap_lookup: called CC=13 SCAR=25.9
3916 10 my ($self, $ip) = @_;
3917 11 ▶ return {} unless $HAS_LWP;
Branch: 1 branch
BranchTrueFalseTotal
unless $HAS_LWP 3 7 10
3918  
3919 11 my $ua = $self->{ua};
3920 11 ▶ if(!defined($ua)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ua) 4 5 9
3921 $ua = LWP::UserAgent->new(
3922 timeout => $self->{timeout},
3923 8 agent => "Email-Abuse-Investigator/$VERSION",
3924 );
3925  
3926 8 ▶ if($HAS_CONN_CACHE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($HAS_CONN_CACHE) 4 0 4
3927 5 my $conn_cache = LWP::ConnCache->new();
3928 5 $conn_cache->total_capacity(10);
3929 7 $ua->conn_cache($conn_cache);
3930 }
3931  
3932 7 $ua->env_proxy(1);
3933 5 $self->{ua} = $ua;
3934 }
3935  
3936 # Validate and normalise the IP before interpolating into the URL path.
3937 # Strip RFC 4007 IPv6 zone IDs (%eth0 suffix) which would corrupt the URL,
3938 # then assert the result is a valid dotted-quad IPv4 or bare hex IPv6.
3939 8 (my $safe_ip = $ip) =~ s/%.*\z//;
3940 8 ▶ unless ($safe_ip =~ /\A\d{1,3}(?:\.\d{1,3}){3}\z/
Branch: 1 branch
BranchTrueFalseTotal
unless ($safe_ip =~ /\A\d{1,3}(?:\.\d{1,3}){3}\z/ or $safe_ip =~ /\A[0-9a-fA-F:]+\z/) 2 5 7
Condition100%3/3
$safe_ip =~ /\A\d{1,3}(?:\.\d{1,3}){3}\z/ or $safe_ip =~ /\A[0-9a-fA-F:]+\z/
l!l&&r!l&&!r
412
MC/DC100%2/2
$safe_ip =~ /\A\d{1,3}(?:\.\d{1,3}){3}\z/ or $safe_ip =~ /\A[0-9a-fA-F:]+\z/
$safe_ip =~ /\A\d{1,3}(?:\.\d{1,3}){3}\z/ $safe_ip =~ /\A[0-9a-fA-F:]+\z/
Truth table100%3/3
A or B
A= $safe_ip =~ /\A\d{1,3}(?:\.\d{1,3}){3}\z/B= $safe_ip =~ /\A[0-9a-fA-F:]+\z/
ABresult
1X1
011
000
3941 || $safe_ip =~ /\A[0-9a-fA-F:]+\z/) {
3942 3 $self->_debug("_rdap_lookup: malformed IP '$ip' -- skipping");
3943 3 return {};
3944 }
3945  
3946 # Use the ARIN RDAP endpoint; it covers the ARIN region and redirects
3947 # for RIPE/APNIC/LACNIC/AfriNIC allocations.
3948 6 my $res = eval { $ua->get("https://rdap.arin.net/registry/ip/$safe_ip") };
3949 6 ▶ return {} unless $res && $res->is_success();
Branch: 1 branch
BranchTrueFalseTotal
unless $res and $res->is_success 1 4 5
Condition66%2/3
$res and $res->is_success
!ll&&!rl&&r
014
MC/DC50%1/2
$res and $res->is_success
$res $res->is_success
Truth table66%2/3
A and B
A= $resB= $res->is_success
ABresult
0X0
100
111
3950  
3951 5 my $j = $res->decoded_content();
3952 5 my %info;
3953  
3954 # Extract organisation name from the JSON response
3955 5 ▶ if ($j =~ /"name"\s*:\s*"([^"]+)"/) { $info{org} = $1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($j =~ /"name"\s*:\s*"([^"]+)"/) 4 0 4
3956 5 ▶ if ($j =~ /"handle"\s*:\s*"([^"]+)"/) { $info{handle} = $1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($j =~ /"handle"\s*:\s*"([^"]+)"/) 4 0 4
3957  
3958 # Extract abuse email from the vcardArray contact block
3959 5 ▶ if ($j =~ /"abuse".*?"email"\s*:\s*"([^"]+)"/s) {
Branch: 2 branches
BranchTrueFalseTotal
if ($j =~ /"abuse".*?"email"\s*:\s*"([^"]+)"/s) { } 0 4 4
elsif ($j =~ /"email"\s*:\s*"([^\@"]+\@[^"]+)"/) { } 0 4 4
3960 1 $info{abuse} = $1;
3961 } elsif ($j =~ /"email"\s*:\s*"([^@"]+@[^"]+)"/) {
3962 1 $info{abuse} = $1;
3963 }
3964  
3965 # Country code from the network's country field
3966 5 ▶ if ($j =~ /"country"\s*:\s*"([A-Z]{2})"/) { $info{country} = $1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($j =~ /"country"\s*:\s*"([A-Z]{2})"/) 4 0 4
3967  
3968 5 return \%info;
3969 24 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
3970  
3971 # _raw_whois( $query, $server ) -> string | undef
3972 #
3973 # Purpose:
3974 # Open a TCP connection to a WHOIS server on port 43, send the query,
3975 # and return the full response as a string. Uses IO::Select for read
3976 # timeouts so that alarm() is never needed (alarm() is unreliable on
3977 # Windows and in threaded Perl). Supports IPv6 WHOIS servers via
3978 # IO::Socket::IP when that module is available.
3979 #
3980 # Entry criteria:
3981 # $query -- the domain name or IP to query (defined, non-empty after
3982 # stripping control characters; croaks if it becomes empty).
3983 # $server -- the WHOIS server hostname (default: 'whois.iana.org').
3984 # $self->{timeout} -- seconds used for connect and per-read waits.
3985 #
3986 # Exit status:
3987 # Returns the raw WHOIS response string, or undef on connection/write failure.
3988 # Croaks if $query is empty or contains only control characters.
3989 #
3990 # Security:
3991 # All ASCII control characters (C0 range 0x00-0x1F and DEL 0x7F) are
3992 # stripped from $query before it is sent to the socket. This prevents
3993 # WHOIS protocol injection via CRLF sequences that could smuggle a second
3994 # query into the same TCP stream. The guard is enforced at this
3995 # :Protected boundary so subclass callers are also protected.
3996 #
3997 # Notes:
3998 # Uses IO::Socket::IP (dual-stack) when available, falling back to
3999 # IO::Socket::INET (IPv4 only) otherwise. The IO::Select loop reads
4000 # until the server closes the connection or the per-read timeout expires.
4001  
4002 15 ▶sub _raw_whois :Protected {
Subroutine _raw_whois: called CC=14 SCAR=27.2
Subroutine _raw_whois: called CC=14 SCAR=27.2
4003 13 my ($self, $query, $server) = @_;
4004 13 ▶ $server //= 'whois.iana.org';
Condition33%1/3
$server //= "whois.iana.org"
l!l
1200
MC/DC0%0/1
$server //= "whois.iana.org"
$server
Truth table50%1/2
A
A= $server
Aresult
11
00
4005  
4006 # Strip all C0/C1 control characters to prevent WHOIS protocol injection.
4007 # Legitimate domain names and IP addresses never contain CR, LF, NUL, or
4008 # any other control character; their presence indicates either a caller bug
4009 # or hostile input embedded in a malicious email. This guard is placed at
4010 # the :Protected boundary so subclass callers are also protected.
4011 13 (my $safe_query = $query) =~ s/[\x00-\x1F\x7F]+//g;
4012 15 ▶ Carp::croak(__PACKAGE__ . '::_raw_whois: empty query after stripping control characters')
Branch: 1 branch
BranchTrueFalseTotal
unless length $safe_query 2 16 18
4013 unless length $safe_query;
4014  
4015 14 $self->_debug("WHOIS $server -> $safe_query");
4016  
4017 # Choose the socket class based on what is installed.
4018 # IO::Socket::IP supports both IPv4 and IPv6 WHOIS servers.
4019 13 ▶ my $sock_class = $HAS_IO_SOCKET_IP ? 'IO::Socket::IP' : 'IO::Socket::INET';
Branch: 1 branch
BranchTrueFalseTotal
$HAS_IO_SOCKET_IP ? : 11 5 16
4020  
4021 # Attempt TCP connection to port 43 on the WHOIS server
4022 13 my $sock = eval {
4023 $sock_class->new(
4024 PeerAddr => $server,
4025 PeerPort => $WHOIS_PORT,
4026 Proto => 'tcp',
4027 Timeout => $self->{timeout},
4028 13 );
4029 };
4030 13 ▶ return unless $sock;
Branch: 1 branch
BranchTrueFalseTotal
unless $sock 2 10 12
4031  
4032 # Send the WHOIS query in wire format (CRLF-terminated per RFC 3912)
4033 11 ▶ $sock->print("$safe_query\r\n") or do { $sock->close(); return };
Branch: 1 branch
BranchTrueFalseTotal
$sock->print("$safe_query\r\n") or do { $sock->close; return } 10 0 10
4034  
4035 # Use IO::Select to implement per-read timeouts without alarm()
4036 16 my $sel = IO::Select->new($sock);
4037 16 my $response = '';
4038 15 my $buf = '';
4039  
4040 # Read until EOF (server closes) or timeout
4041 15 ▶ while ($sel->can_read($self->{timeout})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sel->can_read($self->{"timeout"})) 19 0 19
4042 # Wrap in eval to catch 'Connection reset by peer' thrown by Fatal/autodie
4043 24 my $n = eval { sysread($sock, $buf, $WHOIS_READ_CHUNK) };
4044  
4045 20 ▶ if ($@ || !defined $n || $n <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or not defined $n or $n <= 0) 9 10 19
Condition50%3/6
$@ or not defined $n or $n <= 0
l!l&&r!l&&!r
099
$@ or not defined $n
l!l&&r!l&&!r
0018
MC/DC33%1/3
$@ or not defined $n or $n <= 0
$@ not defined $n $n <= 0
Truth table50%2/4
A or not(B) or C
A= $@B= not defined $nC= $n <= 0
ABCresult
1XX1
01X1
0011
0000
4046 11 ▶ $self->_debug("WHOIS read failed: $@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 9 10
4047 11 last;
4048 }
4049 11 ▶ last if !defined($n) || $n <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $n or $n <= 0 1 9 10
Condition33%1/3
not defined $n or $n <= 0
l!l&&r!l&&!r
009
MC/DC0%0/2
not defined $n or $n <= 0
not defined $n $n <= 0
Truth table33%1/3
not(A) or B
A= not defined $nB= $n <= 0
ABresult
1X1
011
000
4050 10 $response .= $buf;
4051 }
4052  
4053 10 $sock->close();
4054 10 ▶ return $response || undef;
Condition50%1/2
$response || undef
l!l
90
MC/DC0%0/1
$response || undef
$response
Truth table50%1/2
A
A= $response
Aresult
11
00
4055 24 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
4056  
4057 # _parse_whois_text( $text ) -> hashref
4058 #
4059 # Purpose:
4060 # Parse a raw WHOIS IP block response to extract organisation name,
4061 # abuse contact email, and country code.
4062 #
4063 # Entry criteria:
4064 # $text -- a defined WHOIS response string.
4065 #
4066 # Exit status:
4067 # Returns { org, abuse, country } hashref; keys absent when not found.
4068  
4069 38 ▶sub _parse_whois_text :Private {
Subroutine _parse_whois_text: called CC=11 SCAR=24.0
Subroutine _parse_whois_text: called CC=11 SCAR=24.0
4070 37 my ($self, $text) = @_;
4071 37 ▶ return {} unless $text;
Branch: 1 branch
BranchTrueFalseTotal
unless $text 3 33 36
4072 35 my %info;
4073  
4074 # Try multiple field names for the organisation name
4075 35 for my $pat (
4076 qr/^OrgName:\s*(.+)/mi, qr/^org-name:\s*(.+)/mi,
4077 qr/^owner:\s*(.+)/mi, qr/^descr:\s*(.+)/mi,
4078 ) {
4079 134 ▶ if (!$info{org} && $text =~ $pat) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $info{"org"} and $text =~ /$pat/) 18 115 133
Condition100%3/3
not $info{"org"} and $text =~ /$pat/
!ll&&!rl&&r
447218
MC/DC100%2/2
not $info{"org"} and $text =~ /$pat/
not $info{"org"} $text =~ /$pat/
Truth table100%3/3
not(A) and B
A= not $info{"org"}B= $text =~ /$pat/
ABresult
0X0
100
111
4080 20 ($info{org} = $1) =~ s/\s+$//;
4081 }
4082 }
4083  
4084 # Try multiple field names for the abuse email
4085 35 for my $pat (
4086 qr/OrgAbuseEmail:\s*(\S+@\S+)/mi,
4087 qr/abuse-mailbox:\s*(\S+@\S+)/mi,
4088 ) {
4089 68 ▶ if (!$info{abuse} && $text =~ $pat) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $info{"abuse"} and $text =~ /$pat/) 7 60 67
Condition100%3/3
not $info{"abuse"} and $text =~ /$pat/
!ll&&!rl&&r
5547
MC/DC100%2/2
not $info{"abuse"} and $text =~ /$pat/
not $info{"abuse"} $text =~ /$pat/
Truth table100%3/3
not(A) and B
A= not $info{"abuse"}B= $text =~ /$pat/
ABresult
0X0
100
111
4090 9 ($info{abuse} = $1) =~ s/\s+$//;
4091 }
4092 }
4093  
4094 # Last-resort: any abuse@ address in the response
4095 35 ▶ if (!$info{abuse} && $text =~ /(abuse\@[\w.-]+)/i) { $info{abuse} = $1 }
Branch: 1 branch
BranchTrueFalseTotal
if (not $info{"abuse"} and $text =~ /(abuse\@[\w.-]+)/i) 3 30 33
Condition100%3/3
not $info{"abuse"} and $text =~ /(abuse\@[\w.-]+)/i
!ll&&!rl&&r
7233
MC/DC100%2/2
not $info{"abuse"} and $text =~ /(abuse\@[\w.-]+)/i
not $info{"abuse"} $text =~ /(abuse\@[\w.-]+)/i
Truth table100%3/3
not(A) and B
A= not $info{"abuse"}B= $text =~ /(abuse\@[\w.-]+)/i
ABresult
0X0
100
111
4096  
4097 # Country code (case-insensitive match, normalised to uppercase)
4098 34 ▶ if ($text =~ /^country:\s*([A-Za-z]{2})\s*$/m) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /^country:\s*([A-Za-z]{2})\s*$/m) 6 27 33
4099 8 $info{country} = uc $1;
4100 }
4101 35 return \%info;
4102 24 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
4103  
4104 # -----------------------------------------------------------------------
4105 # Private: authentication results parsing
4106 # -----------------------------------------------------------------------
4107  
4108 # _parse_auth_results_cached() -> hashref
4109 #
4110 # Purpose:
4111 # Parse the Authentication-Results: header(s) from the message once,
4112 # cache the result, and return it. Extracts SPF, DKIM, DMARC, ARC
4113 # results and the DKIM signing domain(s).
4114 #
4115 # Entry criteria:
4116 # $self->{_headers} populated by _split_message().
4117 #
4118 # Exit status:
4119 # Returns { spf, dkim, dmarc, arc, dkim_domain, dkim_domains } hashref.
4120 # Keys absent when the corresponding header or field is not present.
4121  
4122 948 ▶sub _parse_auth_results_cached :Private {
Subroutine _parse_auth_results_cached: called CC=14 SCAR=26.4
Subroutine _parse_auth_results_cached: called CC=14 SCAR=26.4
4123 947 my $self = $_[0];
4124 946 ▶ return $self->{_auth_results} if $self->{_auth_results};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_auth_results"} 422 524 946
4125  
4126 525 my %auth;
4127  
4128 # Concatenate all Authentication-Results: header values
4129 my $raw = join('; ',
4130 59 map { $_->{value} }
4131 3785 grep { $_->{name} eq 'authentication-results' }
4132 525 @{ $self->{_headers} }
4133 );
4134  
4135 # Extract individual authentication mechanism results
4136 526 ▶ if ($raw =~ /\bspf=(\S+)/i) { $auth{spf} = $1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($raw =~ /\bspf=(\S+)/i) 43 482 525
4137 525 ▶ if ($raw =~ /\bdkim=(\S+)/i) { $auth{dkim} = $1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($raw =~ /\bdkim=(\S+)/i) 38 487 525
4138 525 ▶ if ($raw =~ /\bdmarc=(\S+)/i) { $auth{dmarc} = $1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($raw =~ /\bdmarc=(\S+)/i) 25 500 525
4139 525 ▶ if ($raw =~ /\barc=(\S+)/i) { $auth{arc} = $1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($raw =~ /\barc=(\S+)/i) 2 523 525
4140  
4141 # Strip trailing punctuation captured by the greedy \S+
4142 525 for my $k (qw(spf dkim dmarc arc)) {
4143 2097 ▶ $auth{$k} =~ s/[;,\s]+$// if defined $auth{$k};
Branch: 1 branch
BranchTrueFalseTotal
if defined $auth{$k} 107 1990 2097
4144 }
4145  
4146 # Extract DKIM signing domains from all DKIM-Signature: d= tags.
4147 # Prefer the first domain that matches the provider table (identifies ESP).
4148 526 my @dkim_domains;
4149 525 for my $h (grep { $_->{name} eq 'dkim-signature' } @{ $self->{_headers} }) {
4150 22 ▶ if ($h->{value} =~ /\bd=([^;,\s]+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h->{"value"} =~ /\bd=([^;,\s]+)/) 21 0 21
4151 22 push @dkim_domains, lc $1;
4152 }
4153 }
4154  
4155 526 ▶ if (@dkim_domains) {
Branch: 1 branch
BranchTrueFalseTotal
if (@dkim_domains) 16 509 525
4156 # Check if any signing domain matches a known provider
4157 17 my $preferred;
4158 17 for my $d (@dkim_domains) {
4159 22 ▶ if ($self->_provider_abuse_for_host($d)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_provider_abuse_for_host($d)) 7 14 21
4160 8 $preferred = $d;
4161 8 last;
4162 }
4163 }
4164 17 ▶ $auth{dkim_domain} = $preferred // $dkim_domains[0];
Condition66%2/3
$preferred // $dkim_domains[0]
l!l&&r!l&&!r
690
MC/DC0%0/2
$preferred // $dkim_domains[0]
$preferred $dkim_domains[0]
Truth table66%2/3
A // B
A= $preferredB= $dkim_domains[0]
ABresult
1X1
011
000
4165 16 $auth{dkim_domains} = \@dkim_domains;
4166 }
4167  
4168 526 $self->{_auth_results} = \%auth;
4169 526 return \%auth;
4170 24 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
4171  
4172 # -----------------------------------------------------------------------
4173 # Private: provider-table lookups
4174 # -----------------------------------------------------------------------
4175  
4176 # _provider_abuse_for_host( $host ) -> hashref | undef
4177 #
4178 # Purpose:
4179 # Look up a hostname (and each of its parent domains, stripping one label
4180 # at a time from the left) in the %PROVIDER_ABUSE table.
4181 #
4182 # Entry criteria:
4183 # $host -- a defined hostname or domain string.
4184 #
4185 # Exit status:
4186 # Returns the %PROVIDER_ABUSE entry hashref on match, undef otherwise.
4187  
4188 1524 ▶sub _provider_abuse_for_host :Private {
Subroutine _provider_abuse_for_host: called CC=4 SCAR=13.9
Subroutine _provider_abuse_for_host: called CC=4 SCAR=13.9
4189 1523 my ($self, $host) = @_;
4190 1523 $host = lc $host;
4191 # Strip successive subdomains until we find a match or exhaust labels
4192 1523 ▶ while ($host =~ /\./) {
Branch: 1 branch
BranchTrueFalseTotal
if ($host =~ /\./) 1635 1235 2870
4193 1636 ▶ return $self->{provider_abuse}->{$host} if $self->{provider_abuse}->{$host};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"provider_abuse"}{$host} 0 1636 1636
4194 1637 ▶ return $PROVIDER_ABUSE{$host} if $PROVIDER_ABUSE{$host};
Branch: 1 branch
BranchTrueFalseTotal
if $PROVIDER_ABUSE{$host} 287 1349 1636
4195 1350 $host =~ s/^[^.]+\.//;
4196 }
4197 1235 return;
4198 24 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
4199  
4200 # _provider_abuse_for_ip( $ip, $rdns ) -> hashref | undef
4201 #
4202 # Purpose:
4203 # Look up an IP's reverse-DNS hostname in the %PROVIDER_ABUSE table to
4204 # identify well-known provider networks by rDNS pattern.
4205 #
4206 # Entry criteria:
4207 # $ip -- IPv4 or IPv6 address string (used as fallback if $rdns absent).
4208 # $rdns -- optional rDNS hostname string.
4209 #
4210 # Exit status:
4211 # Returns the %PROVIDER_ABUSE entry on match, undef otherwise.
4212  
4213 189 ▶sub _provider_abuse_for_ip :Private {
Subroutine _provider_abuse_for_ip: called CC=2 SCAR=6.9
Subroutine _provider_abuse_for_ip: called CC=2 SCAR=6.9
4214 188 my ($self, $ip, $rdns) = @_;
4215 188 ▶ return $self->_provider_abuse_for_host($rdns) if $rdns;
Branch: 1 branch
BranchTrueFalseTotal
if $rdns 184 3 187
4216 4 return;
4217 24 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
4218  
4219 # -----------------------------------------------------------------------
4220 # Private: eTLD+1 normalisation
4221 # -----------------------------------------------------------------------
4222  
4223 # _registrable( $host ) -> string | undef
4224 #
4225 # Purpose:
4226 # Return the registrable eTLD+1 form of a hostname. Uses
4227 # Domain::PublicSuffix when installed for accurate results; falls back
4228 # to a built-in heuristic for the common two-letter ccTLD+2 pattern.
4229 #
4230 # Entry criteria:
4231 # $host -- a hostname string (may include subdomains).
4232 #
4233 # Exit status:
4234 # Returns the registrable domain string, or undef for single-label
4235 # hostnames (e.g. 'localhost').
4236 #
4237 # Notes:
4238 # The heuristic handles co.uk, com.au, net.jp, org.nz etc. but not
4239 # uncommon second-level delegations like ltd.uk or plc.uk.
4240  
4241 1315 ▶sub _registrable :Private {
Subroutine _registrable: called CC=8 SCAR=20.9
Subroutine _registrable: called CC=8 SCAR=20.9
4242 1313 my $host = $_[0];
4243 1313 ▶ return unless $host && $host =~ /\./;
Branch: 1 branch
BranchTrueFalseTotal
unless $host and $host =~ /\./ 205 1108 1313
Condition100%3/3
$host and $host =~ /\./
!ll&&!rl&&r
52021107
MC/DC100%2/2
$host and $host =~ /\./
$host $host =~ /\./
Truth table100%3/3
A and B
A= $hostB= $host =~ /\./
ABresult
0X0
100
111
4244  
4245 # Use Domain::PublicSuffix for accurate PSL-based normalisation
4246 1109 ▶ if ($HAS_PUBLIC_SUFFIX) {
Branch: 1 branch
BranchTrueFalseTotal
if ($HAS_PUBLIC_SUFFIX) 0 1108 1108
4247 1 my $psl = Domain::PublicSuffix->new();
4248 1 my $root = $psl->get_root_domain(lc $host);
4249 2 ▶ return $root if $root;
Branch: 1 branch
BranchTrueFalseTotal
if $root 0 0 0
4250 }
4251  
4252 # Built-in heuristic fallback
4253 1109 my @labels = split /\./, lc $host;
4254 1109 ▶ return $host if @labels <= 2;
Branch: 1 branch
BranchTrueFalseTotal
if @labels <= 2 995 112 1107
4255  
4256 # Detect common ccTLD second-level patterns (e.g. co.uk, com.au)
4257 114 ▶ if ($labels[-1] =~ /^[a-z]{2}$/ &&
Branch: 1 branch
BranchTrueFalseTotal
if ($labels[-1] =~ /^[a-z]{2}$/ and $labels[-2] =~ /^(?:co|com|net|org|gov|edu|ac|me)$/) 65 48 113
Condition100%3/3
$labels[-1] =~ /^[a-z]{2}$/ and $labels[-2] =~ /^(?:co|com|net|org|gov|edu|ac|me)$/
!ll&&!rl&&r
44365
MC/DC100%2/2
$labels[-1] =~ /^[a-z]{2}$/ and $labels[-2] =~ /^(?:co|com|net|org|gov|edu|ac|me)$/
$labels[-1] =~ /^[a-z]{2}$/ $labels[-2] =~ /^(?:co|com|net|org|gov|edu|ac|me)$/
Truth table100%3/3
A and B
A= $labels[-1] =~ /^[a-z]{2}$/B= $labels[-2] =~ /^(?:co|com|net|org|gov|edu|ac|me)$/
ABresult
0X0
100
111
4258 $labels[-2] =~ /^(?:co|com|net|org|gov|edu|ac|me)$/) {
4259 67 return join('.', @labels[-3..-1]);
4260 }
4261 49 return join('.', @labels[-2..-1]);
4262 24 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
4263  
4264 # -----------------------------------------------------------------------
4265 # Private: utilities
4266 # -----------------------------------------------------------------------
4267  
4268 # _enrich_ip( $ip, $confidence, $note ) -> origin hashref
4269 #
4270 # Purpose:
4271 # Perform rDNS and WHOIS/RDAP for a single IP and package the results
4272 # into the standard origin hashref returned by originating_ip().
4273 #
4274 # Entry criteria:
4275 # $ip -- a defined, non-private IPv4 or IPv6 address string.
4276 # $confidence -- 'high', 'medium', or 'low'.
4277 # $note -- human-readable explanation of why this IP was chosen.
4278 #
4279 # Exit status:
4280 # Returns { ip, rdns, org, abuse, country, confidence, note } hashref.
4281  
4282 141 ▶sub _enrich_ip :Private {
Subroutine _enrich_ip: called CC=5 SCAR=16.5
Subroutine _enrich_ip: called CC=5 SCAR=16.5
4283 140 my ($self, $ip, $confidence, $note) = @_;
4284 139 my $rdns = $self->_reverse_dns($ip);
4285 140 my $whois = $self->_whois_ip($ip);
4286 return {
4287 ip => $ip,
4288 rdns => $rdns // '(no reverse DNS)',
4289 org => $whois->{org} // '(unknown)',
4290 abuse => $whois->{abuse} // '(unknown)',
4291 country => $whois->{country} // undef,
4292 140 ▶ confidence => $confidence,
Condition72%8/11
$rdns // "(no reverse DNS)"
l!l
89490
$whois->{"org"} // "(unknown)"
l!l
99390
$whois->{"abuse"} // "(unknown)"
l!l
78620
$whois->{"country"} // undef
l!l
35103
MC/DC100%1/1
$rdns // "(no reverse DNS)"
$rdns
MC/DC100%1/1
$whois->{"org"} // "(unknown)"
$whois->{"org"}
MC/DC100%1/1
$whois->{"abuse"} // "(unknown)"
$whois->{"abuse"}
MC/DC100%1/1
$whois->{"country"} // undef
$whois->{"country"}
Truth table100%2/2
A
A= $rdns
Aresult
11
00
Truth table100%2/2
A
A= $whois->{"org"}
Aresult
11
00
Truth table100%2/2
A
A= $whois->{"abuse"}
Aresult
11
00
Truth table100%2/2
A
A= $whois->{"country"}
Aresult
11
00
4293 note => $note,
4294 };
4295 24 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
4296  
4297 # _header_value( $name ) -> value_string | undef
4298 #
4299 # Purpose:
4300 # Return the value of the first header matching the given lower-cased
4301 # header name.
4302 =head2 header_value( $name )
4303  
4304 Returns the value of the first occurrence of a named header field, or
4305 C<undef> if the header is absent. The name comparison is case-insensitive.
4306  
4307 =head3 Usage
4308  
4309 my $subj = $analyser->header_value('Subject');
4310 my $from = $analyser->header_value('From');
4311 my $msgid = $analyser->header_value('Message-ID');
4312  
4313 =head3 Arguments
4314  
4315 =over 4
4316  
4317 =item C<$name> (string, required)
4318  
4319 The header field name, e.g. C<'Subject'>, C<'From'>, C<'X-Mailer'>.
4320 Comparison is case-insensitive.
4321  
4322 =back
4323  
4324 =head3 Returns
4325  
4326 The raw header value string (not decoded), or C<undef> if the named header
4327 is not present. When the same header appears more than once, only the value
4328 of the first occurrence is returned.
4329  
4330 =head3 Side Effects
4331  
4332 None. Header data is pre-parsed during C<parse_email()>.
4333  
4334 =head3 Notes
4335  
4336 Header values are returned verbatim, including any MIME encoded-word sequences
4337 (C<=?charset?B/Q?...?=>). Pass the result through C<_decode_mime_words()>
4338 internally if human-readable output is needed.
4339  
4340 =head3 API Specification
4341  
4342 =head4 Input
4343  
4344 {
4345 name => { type => 'string', required => 1 },
4346 }
4347  
4348 =head4 Output
4349  
4350 { type => [ 'string', 'undef' ] }
4351  
4352 =head3 Messages
4353  
4354 None -- returns C<undef> on a missing header, never throws.
4355  
4356 =cut
4357  
4358 40 ▶sub header_value {
Subroutine header_value: called CC=5 SCAR=18.1
Subroutine header_value: called CC=5 SCAR=18.1
Pod documented
4359 39 my $self = shift;
4360  
4361 39 ▶ my $params = Params::Validate::Strict::validate_strict({
Condition50%1/2
Params::Get::get_params('name', \@_) || {}
l!l
370
MC/DC0%0/1
Params::Get::get_params('name', \@_) || {}
Params::Get::get_params('name', \@_)
Truth table50%1/2
A
A= Params::Get::get_params('name', \@_)
Aresult
11
00
4362 args => Params::Get::get_params('name', \@_) || {},
4363 schema => {
4364 name => {
4365 'type' => 'string',
4366 'optional' => 0,
4367 }
4368 }
4369 });
4370  
4371 39 ▶ return if((!defined($params)) || !defined($params->{name}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params and defined $params->{"name"} 0 37 37
Condition33%1/3
defined $params and defined $params->{"name"}
!ll&&!rl&&r
0037
MC/DC0%0/2
defined $params and defined $params->{"name"}
defined $params defined $params->{"name"}
Truth table33%1/3
A and B
A= defined $paramsB= defined $params->{"name"}
ABresult
0X0
100
111
4372 38 ▶ return if ref($params->{name});
Branch: 1 branch
BranchTrueFalseTotal
if ref $params->{"name"} 0 37 37
4373 38 return $self->_header_value($params->{name});
4374 }
4375  
4376 #
4377 # _header_value( $name ) -> string | undef
4378 #
4379 # Purpose:
4380 # Internal implementation for header_value(). Walks _headers list and
4381 # returns the value of the first matching header.
4382 #
4383 # Entry criteria:
4384 # $name -- a lower-cased header name string.
4385 # $self->{_headers} populated by _split_message().
4386 #
4387 # Exit status:
4388 # Returns the value string, or undef if the header is not present.
4389  
4390 6108 ▶sub _header_value :Private {
Subroutine _header_value: called CC=3 SCAR=11.0
Subroutine _header_value: called CC=3 SCAR=11.0
4391 6106 my ($self, $name) = @_;
4392 6107 for my $h (@{ $self->{_headers} }) {
4393 33306 ▶ return $h->{value} if $h->{name} eq lc($name);
Branch: 1 branch
BranchTrueFalseTotal
if $h->{"name"} eq lc $name 3112 30193 33305
4394 }
4395 2994 return;
4396 24 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
4397  
4398 # _ip_in_cidr( $ip, $cidr ) -> bool
4399 #
4400 # Purpose:
4401 # Test whether an IPv4 address falls within a CIDR block or is an exact
4402 # match (when $cidr contains no '/' separator).
4403 #
4404 # Entry criteria:
4405 # $ip -- a defined dotted-quad IPv4 address string.
4406 # $cidr -- a CIDR string like '10.0.0.0/8' or an exact IP.
4407 #
4408 # Exit status:
4409 # Returns 1 (true) if the IP is within the CIDR block, 0 otherwise.
4410  
4411 54 ▶sub _ip_in_cidr :Private {
Subroutine _ip_in_cidr: called CC=7 SCAR=19.9
Subroutine _ip_in_cidr: called CC=7 SCAR=19.9
4412 52 my ($self, $ip, $cidr) = @_;
4413 52 ▶ return $ip eq $cidr unless $cidr =~ m{/};
Branch: 1 branch
BranchTrueFalseTotal
unless $cidr =~ m[/] 6 46 52
4414 47 my ($net_addr, $prefix) = split m{/}, $cidr;
4415 47 ▶ return 0 if !defined($prefix) || $prefix !~ /^\d+$/ || $prefix > 32;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $prefix && $prefix =~ /^\d+$/ or $prefix > 32 1 44 45
Condition66%4/6
not defined $prefix && $prefix =~ /^\d+$/ or $prefix > 32
l!l&&r!l&&!r
2144
defined $prefix && $prefix =~ /^\d+$/
!ll&&!rl&&r
0045
MC/DC33%1/3
not defined $prefix && $prefix =~ /^\d+$/ or $prefix > 32
defined $prefix $prefix =~ /^\d+$/ $prefix > 32
Truth table50%2/4
not(A && B) or C
A= defined $prefixB= $prefix =~ /^\d+$/C= $prefix > 32
ABCresult
0XX1
10X1
1111
1100
4416  
4417 # Compute the network mask and compare masked network addresses
4418 45 my $mask = ~0 << (32 - $prefix);
4419 45 ▶ my $net_n = unpack 'N', (inet_aton($net_addr) // return 0);
Condition50%1/2
inet_aton($net_addr) // (return 0)
l!l
440
MC/DC0%0/1
inet_aton($net_addr) // (return 0)
inet_aton($net_addr)
Truth table50%1/2
A
A= inet_aton($net_addr)
Aresult
11
00
4420 45 ▶ my $ip_n = unpack 'N', (inet_aton($ip) // return 0);
Condition50%1/2
inet_aton($ip) // (return 0)
l!l
440
MC/DC0%0/1
inet_aton($ip) // (return 0)
inet_aton($ip)
Truth table50%1/2
A
A= inet_aton($ip)
Aresult
11
00
4421 45 return ($ip_n & $mask) == ($net_n & $mask);
4422 24 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
4423  
4424 # _decode_mime_words( $str ) -> decoded_string
4425 #
4426 # Purpose:
4427 # Decode MIME encoded-words (=?charset?B/Q?...?=) in a header value
4428 # string for human-readable display in reports.
4429 #
4430 # Entry criteria:
4431 # $str -- a defined header value string; may be undef.
4432 #
4433 # Exit status:
4434 # Returns the decoded string, or '' if $str is undef.
4435  
4436 729 ▶sub _decode_mime_words :Private {
Subroutine _decode_mime_words: called CC=2 SCAR=6.9
Subroutine _decode_mime_words: called CC=2 SCAR=6.9
4437 728 my ($self, $str) = @_;
4438 727 ▶ return '' unless defined $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 2 724 726
4439 # Replace each encoded-word with its decoded equivalent
4440 725 $str =~ s/=\?([^?]+)\?([BbQq])\?([^?]*)\?=/_decode_ew($1,$2,$3)/ge;
4441 726 return $str;
4442 24 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
4443  
4444 # _decode_ew( $charset, $enc, $text ) -> decoded_bytes
4445 #
4446 # Purpose:
4447 # Decode a single MIME encoded-word component (base64 or quoted-printable).
4448 #
4449 # Notes:
4450 # Non-UTF-8 charsets return raw bytes; good enough for display-name spoof
4451 # detection which only needs ASCII matching.
4452  
4453 46 ▶sub _decode_ew :Private {
Subroutine _decode_ew: called CC=2 SCAR=6.9
Subroutine _decode_ew: called CC=2 SCAR=6.9
4454 45 my ($charset, $enc, $text) = @_;
4455 45 my $raw;
4456 45 ▶ if (uc($enc) eq 'B') {
Branch: 1 branch
BranchTrueFalseTotal
if (uc $enc eq 'B') { } 39 4 43
4457 41 $raw = decode_base64($text);
4458 } else {
4459 # Quoted-printable encoded-word uses underscore for space
4460 6 $text =~ s/_/ /g;
4461 6 $raw = decode_qp($text);
4462 }
4463 45 return $raw;
4464 24 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
4465  
4466 # _parse_date_to_epoch( $str ) -> epoch_int | undef
4467 #
4468 # Purpose:
4469 # Parse common WHOIS date strings to a Unix epoch integer.
4470 # Handles YYYY-MM-DD, YYYY-MM-DDThh:mm:ssZ, and DD-Mon-YYYY formats.
4471 #
4472 # Entry criteria:
4473 # $str -- a defined date string; may be undef.
4474 #
4475 # Exit status:
4476 # Returns epoch integer on success, undef if the string cannot be parsed.
4477  
4478 68 ▶sub _parse_date_to_epoch :Private {
Subroutine _parse_date_to_epoch: called CC=12 SCAR=24.9
Subroutine _parse_date_to_epoch: called CC=12 SCAR=24.9
4479 66 my ($self, $str) = @_;
4480 66 ▶ return unless $str;
Branch: 1 branch
BranchTrueFalseTotal
unless $str 2 64 66
4481  
4482 # Clean the string of trailing whitespace/newlines
4483 66 $str =~ s/^\s+|\s+$//g;
4484  
4485 # Guard Regex: Validates the strict YYYY-MM-DDThh:mm:ssZ format
4486 66 ▶ if ($str =~ /^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2})(?:\.\d+)?Z$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2})(?:\.\d+)?Z$/) 3 62 65
4487 # Parse the string
4488 # We use 'strptime' to create a Time::Piece object.
4489 # The 'Z' indicates UTC (Zulu time).
4490 3 my $epoch = eval {
4491 3 my $t = Time::Piece->strptime($1, '%Y-%m-%dT%H:%M:%S');
4492  
4493 # Return seconds since the epoch
4494 # Time::Piece handles the timezone offset internally when calling ->epoch
4495  
4496 # strptime returns a local time object.
4497 # We must subtract the local timezone offset to get the true UTC epoch.
4498 3 return $t->epoch - $t->tzoffset->seconds;
4499 };
4500 3 ▶ return $epoch if defined $epoch;
Branch: 1 branch
BranchTrueFalseTotal
if defined $epoch 2 1 3
4501 }
4502 63 my ($y, $m, $d);
4503  
4504 63 ▶ if ($str =~ /^(\d{4})-(\d{2})-(\d{2})/) { ($y,$m,$d)=($1,$2,$3) }
Branch: 3 branches
BranchTrueFalseTotal
if ($str =~ /^(\d{4})-(\d{2})-(\d{2})/) { } 43 19 62
elsif ($str =~ /^(\d{2})-([A-Za-z]{3})-(\d{4})/) { } 17 2 19
elsif ($str =~ m[^(\d{2})/(\d{2})/(\d{4})]) { } 1 1 2
4505 18 ▶ elsif ($str =~ /^(\d{2})-([A-Za-z]{3})-(\d{4})/) { ($d,$m,$y)=($1,$Readonly::Values::Months::months{lc$2}//0,$3) }
Condition66%2/3
$Readonly::Values::Months::months{lc $2} // 0
l!l
1610
MC/DC100%1/1
$Readonly::Values::Months::months{lc $2} // 0
$Readonly::Values::Months::months{lc $2}
Truth table100%2/2
A
A= $Readonly::Values::Months::months{lc $2}
Aresult
11
00
4506 2 elsif ($str =~ /^(\d{2})\/(\d{2})\/(\d{4})/) { ($m,$d,$y)=($1,$2,$3) }
4507  
4508 63 ▶ return unless $y && $m && $d;
Branch: 1 branch
BranchTrueFalseTotal
unless $y and $m and $d 2 60 62
Condition100%6/6
$y and $m and $d
!ll&&!rl&&r
6460
$y and $m
!ll&&!rl&&r
5160
MC/DC66%2/3
$y and $m and $d
$y $m $d
Truth table75%3/4
A and B and C
A= $yB= $mC= $d
ABCresult
0XX0
10X0
1100
1111
4509  
4510 61 ▶ if (eval { require Time::Local; 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require Time::Local; 1 } }) 60 0 60
4511 61 return eval { Time::Local::timegm(0,0,0,$d,$m-1,$y-1900) };
4512 }
4513 # Approximate fallback without Time::Local
4514 1 return ($y-1970)*365.25*$SECS_PER_DAY + ($m-1)*30.5*$SECS_PER_DAY + ($d-1)*$SECS_PER_DAY;
4515 24 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
4516  
4517 # _parse_rfc2822_date( $str ) -> epoch_int | undef
4518 #
4519 # Purpose:
4520 # Parse an RFC 2822 Date: header value to a Unix epoch integer.
4521 # Timezone offsets are intentionally ignored; the function returns a
4522 # UTC-equivalent value. For the 7-day suspicious_date window the
4523 # maximum error is ~14 hours, well within the tolerance.
4524 #
4525 # Entry criteria:
4526 # $str -- a defined Date: header value string.
4527 #
4528 # Exit status:
4529 # Returns epoch integer on success, undef if the string cannot be parsed.
4530  
4531 366 ▶sub _parse_rfc2822_date :Private {
Subroutine _parse_rfc2822_date: called CC=6 SCAR=17.9
Subroutine _parse_rfc2822_date: called CC=6 SCAR=17.9
4532 364 my $str = $_[0];
4533 364 ▶ return unless $str;
Branch: 1 branch
BranchTrueFalseTotal
unless $str 2 361 363
4534  
4535 # Match: DD Mon YYYY HH:MM:SS (timezone offset ignored)
4536 362 ▶ if ($str =~ /(\d{1,2})\s+([A-Za-z]{3})\s+(\d{4})\s+(\d{2}):(\d{2}):(\d{2})/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /(\d{1,2})\s+([A-Za-z]{3})\s+(\d{4})\s+(\d{2}):(\d{2}):(\d{2})/) 358 3 361
4537 my ($d, $m, $y, $H, $M, $S) =
4538 359 ▶ ($1, $Readonly::Values::Months::months{ lc $2 } // 0, $3, $4, $5, $6);
Condition100%2/2
$Readonly::Values::Months::months{lc $2} // 0
l!l
3571
MC/DC100%1/1
$Readonly::Values::Months::months{lc $2} // 0
$Readonly::Values::Months::months{lc $2}
Truth table100%2/2
A
A= $Readonly::Values::Months::months{lc $2}
Aresult
11
00
4539 359 ▶ return unless $m;
Branch: 1 branch
BranchTrueFalseTotal
unless $m 1 357 358
4540 358 ▶ if (eval { require Time::Local; 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require Time::Local; 1 } }) 358 5 363
4541 359 return eval { Time::Local::timegm($S, $M, $H, $d, $m - 1, $y - 1900) };
4542 }
4543 }
4544 5 return;
4545 24 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
4546  
4547 # _country_name( $cc ) -> country_name_string
4548 #
4549 # Purpose:
4550 # Return a human-readable country name for a two-letter ISO 3166-1
4551 # alpha-2 country code. Only the small set of statistically high-volume
4552 # spam-originating countries is covered; other codes are returned as-is.
4553 #
4554 # Entry criteria:
4555 # $cc -- a two-letter uppercase country code string.
4556 #
4557 # Exit status:
4558 # Returns the country name string, or the code itself if not in the table.
4559  
4560 62 ▶sub _country_name :Private {
Subroutine _country_name: called CC=2 SCAR=7.0
Subroutine _country_name: called CC=2 SCAR=7.0
4561 61 my $cc = $_[0];
4562 61 my %names = (
4563 CN => 'China', RU => 'Russia', NG => 'Nigeria',
4564 VN => 'Vietnam', IN => 'India', PK => 'Pakistan',
4565 BD => 'Bangladesh',
4566 );
4567 61 ▶ return $names{$cc} // $cc;
Condition66%2/3
$names{$cc} // $cc
l!l&&r!l&&!r
5360
MC/DC0%0/2
$names{$cc} // $cc
$names{$cc} $cc
Truth table66%2/3
A // B
A= $names{$cc}B= $cc
ABresult
1X1
011
000
4568 24 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
4569  
4570 # _debug( $msg )
4571 #
4572 # Purpose:
4573 # Write a diagnostic message to STDERR when verbose mode is enabled.
4574 #
4575 # Entry criteria:
4576 # $msg -- a defined message string.
4577 #
4578 # Notes:
4579 # Messages are prefixed with the class name for easy grepping.
4580  
4581 1070 ▶sub _debug :Private {
Subroutine _debug: called CC=3 SCAR=11.0
Subroutine _debug: called CC=3 SCAR=11.0
4582 1069 my ($self, $msg) = @_;
4583  
4584 1069 ▶ if($self->{verbose}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"verbose"}) 3 1069 1072
4585 10 ▶ if (my $logger = $self->{logger}) { # Set via Object::Configure
Branch: 1 branch
BranchTrueFalseTotal
if (my $logger = $self->{'logger'}) { } 2 2 4
4586 8 $logger->debug("[Email::Abuse::Investigator] $msg");
4587 } else {
4588 8 print STDERR "[Email::Abuse::Investigator] $msg\n";
4589 }
4590 }
4591 24 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
4592  
4593 22 1;
4594  
4595 __END__
↑ Summary bin/submit_abuse_report »