File Coverage

blib/lib/Mail/SpamAssassin/Plugin/URIDetail.pm
Criterion Covered Total %
statement 31 122 25.4
branch 0 46 0.0
condition 1 48 2.0
subroutine 8 10 80.0
pod 1 3 33.3
total 41 229 17.9


line stmt bran cond sub pod time code
1             # <@LICENSE>
2             # Licensed to the Apache Software Foundation (ASF) under one or more
3             # contributor license agreements. See the NOTICE file distributed with
4             # this work for additional information regarding copyright ownership.
5             # The ASF licenses this file to you under the Apache License, Version 2.0
6             # (the "License"); you may not use this file except in compliance with
7             # the License. You may obtain a copy of the License at:
8             #
9             # http://www.apache.org/licenses/LICENSE-2.0
10             #
11             # Unless required by applicable law or agreed to in writing, software
12             # distributed under the License is distributed on an "AS IS" BASIS,
13             # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14             # See the License for the specific language governing permissions and
15             # limitations under the License.
16             # </@LICENSE>
17             #
18             # TODO: where are the tests?
19              
20             =head1 NAME
21              
22             URIDetail - test URIs using detailed URI information
23              
24             =head1 SYNOPSIS
25              
26             This plugin creates a new rule test type, known as "uri_detail". These
27             rules apply to all URIs found in the message.
28              
29             loadplugin Mail::SpamAssassin::Plugin::URIDetail
30              
31             =head1 RULE DEFINITIONS AND PRIVILEGED SETTINGS
32              
33             The format for defining a rule is as follows:
34              
35             uri_detail SYMBOLIC_TEST_NAME key1 =~ /value1/ key2 !~ /value2/ ...
36              
37             Supported keys are:
38              
39             C<raw> is the raw URI prior to any cleaning
40             (e.g. "http://spamassassin.apache%2Eorg/").
41              
42             C<type> is the tag(s) which referenced the raw_uri. I<parsed> is a
43             faked type which specifies that the raw_uri was parsed from the
44             rendered text.
45              
46             C<cleaned> is a list including the raw URI and various cleaned
47             versions of the raw URI (http://spamassassin.apache%2Eorg/,
48             https://spamassassin.apache.org/).
49              
50             C<text> is the anchor text(s) (text between <a> and </a>) that
51             linked to the raw URI.
52              
53             C<domain> is the domain(s) found in the cleaned URIs.
54              
55             Example rule for matching a URI where the raw URI matches "%2Ebar",
56             the domain "bar.com" is found, and the type is "a" (an anchor tag).
57              
58             uri_detail TEST1 raw =~ /%2Ebar/ domain =~ /^bar\.com$/ type =~ /^a$/
59              
60             Example rule to look for suspicious "https" links:
61              
62             uri_detail FAKE_HTTPS text =~ /\bhttps:/ cleaned !~ /\bhttps:/
63              
64             Regular expressions should be delimited by slashes.
65              
66             =cut
67              
68             package Mail::SpamAssassin::Plugin::URIDetail;
69 21     21   153 use Mail::SpamAssassin::Plugin;
  21         49  
  21         680  
70 21     21   120 use Mail::SpamAssassin::Logger;
  21         40  
  21         1379  
71 21     21   152 use Mail::SpamAssassin::Util qw(untaint_var compile_regexp);
  21         37  
  21         1157  
72              
73 21     21   144 use strict;
  21         61  
  21         783  
74 21     21   125 use warnings;
  21         42  
  21         662  
75             # use bytes;
76 21     21   119 use re 'taint';
  21         40  
  21         24065  
77              
78             our @ISA = qw(Mail::SpamAssassin::Plugin);
79              
80             # constructor
81             sub new {
82 62     62 1 281 my $class = shift;
83 62         175 my $mailsaobject = shift;
84              
85             # some boilerplate...
86 62   33     486 $class = ref($class) || $class;
87 62         995 my $self = $class->SUPER::new($mailsaobject);
88 62         178 bless ($self, $class);
89              
90 62         320 $self->register_eval_rule("check_uri_detail");
91              
92 62         346 $self->set_config($mailsaobject->{conf});
93              
94 62         613 return $self;
95             }
96              
97             sub set_config {
98 62     62 0 177 my ($self, $conf) = @_;
99 62         127 my @cmds;
100              
101 62         140 my $pluginobj = $self; # allow use inside the closure below
102              
103             push (@cmds, {
104             setting => 'uri_detail',
105             is_priv => 1,
106             code => sub {
107 0     0   0 my ($self, $key, $value, $line) = @_;
108              
109 0 0       0 if ($value !~ /^(\S+)\s+(.+)$/) {
110 0         0 return $Mail::SpamAssassin::Conf::INVALID_VALUE;
111             }
112 0         0 my $name = $1;
113 0         0 my $def = $2;
114 0         0 my $added_criteria = 0;
115              
116             # if this matches a regex, it strips slashes
117 0         0 while ($def =~ m{\b(\w+)\b\s*([\=\!]\~)\s*((?:/.*?/|m(\W).*?\4)[imsx]*)(?=\s|$)}g) {
118 0         0 my $target = $1;
119 0         0 my $op = $2;
120 0         0 my $pattern = $3;
121              
122 0 0       0 if ($target !~ /^(?:raw|type|cleaned|text|domain)$/) {
123 0         0 return $Mail::SpamAssassin::Conf::INVALID_VALUE;
124             }
125              
126 0         0 my ($rec, $err) = compile_regexp($pattern, 1);
127 0 0       0 if (!$rec) {
128 0         0 dbg("config: uri_detail invalid regexp '$pattern': $err");
129 0         0 return $Mail::SpamAssassin::Conf::INVALID_VALUE;
130             }
131              
132 0         0 dbg("config: uri_detail adding ($target $op /$rec/) to $name");
133 0         0 $conf->{parser}->{conf}->{uri_detail}->{$name}->{$target} =
134             [$op, $rec];
135 0         0 $added_criteria = 1;
136             }
137              
138 0 0       0 if ($added_criteria) {
139 0         0 dbg("config: uri_detail added $name\n");
140 0         0 $conf->{parser}->add_test($name, 'check_uri_detail()',
141             $Mail::SpamAssassin::Conf::TYPE_BODY_EVALS);
142             }
143             else {
144 0         0 warn "config: failed to add invalid rule $name";
145 0         0 return $Mail::SpamAssassin::Conf::INVALID_VALUE;
146             }
147             }
148 62         716 });
149            
150 62         395 $conf->{parser}->register_commands(\@cmds);
151             }
152              
153             sub check_uri_detail {
154 0     0 0   my ($self, $permsg) = @_;
155              
156 0           my %uri_detail = %{ $permsg->get_uri_detail_list() };
  0            
157              
158 0           while (my ($raw, $info) = each %uri_detail) {
159 0           my $test = $permsg->{current_rule_name};
160              
161 0           dbg("uri: running $test\n");
162              
163 0           my $rule = $permsg->{conf}->{uri_detail}->{$test};
164              
165 0 0         if (exists $rule->{raw}) {
166 0           my($op,$patt) = @{$rule->{raw}};
  0            
167 0 0 0       if ( ($op eq '=~' && $raw =~ $patt) ||
      0        
      0        
168             ($op eq '!~' && $raw !~ $patt) ) {
169 0           dbg("uri: raw matched: '%s' %s /%s/", $raw,$op,$patt);
170             } else {
171 0           next;
172             }
173             }
174              
175 0 0         if (exists $rule->{type}) {
176 0 0         next unless $info->{types};
177 0           my($op,$patt) = @{$rule->{type}};
  0            
178 0           my $match;
179 0           for my $text (keys %{ $info->{types} }) {
  0            
180 0 0 0       if ( ($op eq '=~' && $text =~ $patt) ||
      0        
      0        
181 0           ($op eq '!~' && $text !~ $patt) ) { $match = $text; last }
  0            
182             }
183 0 0         next unless defined $match;
184 0           dbg("uri: type matched: '%s' %s /%s/", $match,$op,$patt);
185             }
186              
187 0 0         if (exists $rule->{cleaned}) {
188 0 0         next unless $info->{cleaned};
189 0           my($op,$patt) = @{$rule->{cleaned}};
  0            
190 0           my $match;
191 0           for my $text (@{ $info->{cleaned} }) {
  0            
192 0 0 0       if ( ($op eq '=~' && $text =~ $patt) ||
      0        
      0        
193 0           ($op eq '!~' && $text !~ $patt) ) { $match = $text; last }
  0            
194             }
195 0 0         next unless defined $match;
196 0           dbg("uri: cleaned matched: '%s' %s /%s/", $match,$op,$patt);
197             }
198              
199 0 0         if (exists $rule->{text}) {
200 0 0         next unless $info->{anchor_text};
201 0           my($op,$patt) = @{$rule->{text}};
  0            
202 0           my $match;
203 0           for my $text (@{ $info->{anchor_text} }) {
  0            
204 0 0 0       if ( ($op eq '=~' && $text =~ $patt) ||
      0        
      0        
205 0           ($op eq '!~' && $text !~ $patt) ) { $match = $text; last }
  0            
206             }
207 0 0         next unless defined $match;
208 0           dbg("uri: text matched: '%s' %s /%s/", $match,$op,$patt);
209             }
210              
211 0 0         if (exists $rule->{domain}) {
212 0 0         next unless $info->{domains};
213 0           my($op,$patt) = @{$rule->{domain}};
  0            
214 0           my $match;
215 0           for my $text (keys %{ $info->{domains} }) {
  0            
216 0 0 0       if ( ($op eq '=~' && $text =~ $patt) ||
      0        
      0        
217 0           ($op eq '!~' && $text !~ $patt) ) { $match = $text; last }
  0            
218             }
219 0 0         next unless defined $match;
220 0           dbg("uri: domain matched: '%s' %s /%s/", $match,$op,$patt);
221             }
222              
223 0 0         if (would_log('dbg', 'rules') > 1) {
224 0           dbg("uri: criteria for $test met");
225             }
226            
227 0           $permsg->got_hit($test);
228              
229             # reset hash
230 0           keys %uri_detail;
231              
232 0           return 0;
233             }
234              
235 0           return 0;
236             }
237              
238             # ---------------------------------------------------------------------------
239              
240             1;