File: HTMLEval.pm

package info (click to toggle)
spamassassin 4.0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 22,988 kB
  • sloc: perl: 88,863; ansic: 5,193; sh: 3,737; javascript: 339; sql: 295; makefile: 209; python: 49
file content (268 lines) | stat: -rw-r--r-- 7,195 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# <@LICENSE>
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to you under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at:
# 
#     http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# </@LICENSE>

package Mail::SpamAssassin::Plugin::HTMLEval;

use strict;
use warnings;
# use bytes;
use re 'taint';

use Mail::SpamAssassin::Plugin;
use Mail::SpamAssassin::Locales;
use Mail::SpamAssassin::Util qw(untaint_var compile_regexp);

our @ISA = qw(Mail::SpamAssassin::Plugin);

# constructor: register the eval rule
sub new {
  my $class = shift;
  my $mailsaobject = shift;

  # some boilerplate...
  $class = ref($class) || $class;
  my $self = $class->SUPER::new($mailsaobject);
  bless ($self, $class);

  # the important bit!
  $self->register_eval_rule("html_tag_balance", $Mail::SpamAssassin::Conf::TYPE_BODY_EVALS);
  $self->register_eval_rule("html_image_only", $Mail::SpamAssassin::Conf::TYPE_BODY_EVALS);
  $self->register_eval_rule("html_image_ratio", $Mail::SpamAssassin::Conf::TYPE_BODY_EVALS);
  $self->register_eval_rule("html_charset_faraway", $Mail::SpamAssassin::Conf::TYPE_BODY_EVALS);
  $self->register_eval_rule("html_tag_exists", $Mail::SpamAssassin::Conf::TYPE_BODY_EVALS);
  $self->register_eval_rule("html_test", $Mail::SpamAssassin::Conf::TYPE_BODY_EVALS);
  $self->register_eval_rule("html_eval", $Mail::SpamAssassin::Conf::TYPE_BODY_EVALS);
  $self->register_eval_rule("html_text_match", $Mail::SpamAssassin::Conf::TYPE_BODY_EVALS);
  $self->register_eval_rule("html_title_subject_ratio", $Mail::SpamAssassin::Conf::TYPE_BODY_EVALS);
  $self->register_eval_rule("html_text_not_match", $Mail::SpamAssassin::Conf::TYPE_BODY_EVALS);
  $self->register_eval_rule("html_range", $Mail::SpamAssassin::Conf::TYPE_BODY_EVALS);
  $self->register_eval_rule("check_iframe_src", $Mail::SpamAssassin::Conf::TYPE_BODY_EVALS);

  return $self;
}

sub html_tag_balance {
  my ($self, $pms, undef, $rawtag, $rawexpr) = @_;

  return 0 if $rawtag !~ /^([a-zA-Z0-9]+)$/;
  my $tag = $1;

  return 0 if $rawexpr !~ /^([\<\>\=\!\-\+ 0-9]+)$/;
  my $expr = untaint_var($1);

  foreach my $html (@{$pms->{html_all}}) {
    next unless exists $html->{inside}{$tag};
    $html->{inside}{$tag} =~ /^([\<\>\=\!\-\+ 0-9]+)$/;
    my $val = untaint_var($1);
    return 1 if eval "\$val $expr";
  }

  return 0;
}

sub html_image_only {
  my ($self, $pms, undef, $min, $max) = @_;

  foreach my $html (@{$pms->{html_all}}) {
    if (exists $html->{inside}{img} && exists $html->{length} &&
        $html->{length} > $min && $html->{length} <= $max)
    {
      return 1;
    }
  }

  return 0;
}

sub html_image_ratio {
  my ($self, $pms, undef, $min, $max) = @_;

  foreach my $html (@{$pms->{html_all}}) {
    next unless (exists $html->{non_space_len} &&
                 exists $html->{image_area} &&
                 $html->{image_area} > 0);
    my $ratio = $html->{non_space_len} / $html->{image_area};
    return 1 if $ratio > $min && $ratio <= $max;
  }

  return 0;
}

sub html_charset_faraway {
  my ($self, $pms) = @_;

  my @locales = Mail::SpamAssassin::Util::get_my_locales($pms->{conf}->{ok_locales});
  return 0 if grep { $_ eq "all" } @locales;

  foreach my $html (@{$pms->{html_all}}) {
    next unless exists $html->{charsets};
    my $okay = 0;
    my $bad = 0;
    foreach my $c (split(/\s+/, $html->{charsets})) {
      if (Mail::SpamAssassin::Locales::is_charset_ok_for_locales($c, @locales)) {
        $okay++;
      } else {
        $bad++;
      }
    }
    return 1 if $bad && $bad >= $okay;
  }

  return 0;
}

sub html_tag_exists {
  my ($self, $pms, undef, $tag) = @_;

  foreach my $html (@{$pms->{html_all}}) {
    return 1 if exists $html->{inside}{$tag};
  }

  return 0;
}

sub html_test {
  my ($self, $pms, undef, $test) = @_;

  foreach my $html (@{$pms->{html_all}}) {
    return 1 if $html->{$test};
  }

  return 0;
}

sub html_eval {
  my ($self, $pms, undef, $test, $rawexpr) = @_;

  return 0 if $rawexpr !~ /^([\<\>\=\!\-\+ 0-9]+)$/;
  my $expr = untaint_var($1);

  foreach my $html (@{$pms->{html_all}}) {
    # workaround bug 3320: weird perl bug where additional, very explicit
    # untainting into a new var is required.
    my $tainted = $html->{$test};
    next unless defined($tainted);
    my $val = $tainted;
    # just use the value in $val, don't copy it needlessly
    return 1 if eval "\$val $expr";
  }

  return 0;
}

sub html_text_match {
  my ($self, $pms, undef, $text, $regexp) = @_;

  my ($rec, $err) = compile_regexp($regexp, 0);
  if (!$rec) {
    warn "htmleval: html_text_match invalid regexp '$regexp': $err";
    return 0;
  }

  foreach my $html (@{$pms->{html_all}}) {
    next unless ref($html->{$text}) eq 'ARRAY';
    foreach my $string (@{$html->{$text}}) {
      next unless defined $string;
      if ($string =~ $rec) {
        return 1;
      }
    }
  }

  return 0;
}

sub html_title_subject_ratio {
  my ($self, $pms, undef, $ratio) = @_;

  my $subject = $pms->get('Subject');
  if ($subject eq '') {
    return 0;
  }

  foreach my $html (@{$pms->{html_all}}) {
    my $max = 0;
    foreach my $string (@{$html->{title}}) {
      if ($string) {
        my $ratio_s = length($string) / length($subject);
        $max = $ratio_s if $ratio_s > $max;
      }
    }
    return 1 if $max > $ratio;
  }

  return 0;
}

sub html_text_not_match {
  my ($self, $pms, undef, $text, $regexp) = @_;

  my ($rec, $err) = compile_regexp($regexp, 0);
  if (!$rec) {
    warn "htmleval: html_text_not_match invalid regexp '$regexp': $err";
    return 0;
  }

  foreach my $html (@{$pms->{html_all}}) {
    next unless ref($html->{$text}) eq 'ARRAY';
    foreach my $string (@{$html->{$text}}) {
      if (defined $string && $string !~ $rec) {
        return 1;
      }
    }
  }

  return 0;
}

sub html_range {
  my ($self, $pms, undef, $test, $min, $max) = @_;

  foreach my $html (@{$pms->{html_all}}) {
    next unless defined $html->{$test};
    my $value = $html->{$test};
    # not all perls understand what "inf" means, so we need to do
    # non-numeric tests!  urg!
    if (!defined $max || $max eq "inf") {
      return 1 if $value > $min;
    }
    elsif ($value eq "inf") {
      # $max < inf, so $value == inf means $value > $max
      next;
    }
    else {
      # if we get here everything should be a number
      return 1 if $value > $min && $value <= $max;
    }
  }

  return 0;
}

sub check_iframe_src {
  my ($self, $pms) = @_;

  foreach my $html (@{$pms->{html_all}}) {
    foreach my $v (values %{$html->{uri_detail}}) {
      return 1 if $v->{types}->{iframe};
    }
  }

  return 0;
}

1;