Class: TDiary::Filter::SpamFilter

Inherits:
Filter
  • Object
show all
Defined in:
lib/tdiary/filter/spam.rb

Constant Summary collapse

TLD =
%w(com net org edu gov mil int info biz name pro museum aero coop [a-z]{2})

Constants inherited from Filter

Filter::DEBUG_FULL, Filter::DEBUG_NONE, Filter::DEBUG_SPAM

Instance Method Summary collapse

Methods inherited from Filter

#debug

Methods included from ViewHelper

#base_url, #bot?

Constructor Details

#initialize(*args) ⇒ SpamFilter

Returns a new instance of SpamFilter.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/tdiary/filter/spam.rb', line 14

def initialize( *args )
  super( *args )
  @filter_mode = true
  @max_uris = nil
  @max_rate = nil
  @bad_uri_patts_for_mails = false

  @bad_uri_patts = nil
  @bad_mail_patts = nil
  @bad_comment_patts = nil
  @bad_ip_addrs = nil

  @bad_uris = []
  @bad_mails_ext = []
  @bad_mails = []
  @bad_comments = []
  @bad_ips = []

  @date_limit = nil
end

Instance Method Details

#black_url?(body) ⇒ Boolean

Returns:

  • (Boolean)


181
182
183
184
185
186
187
188
189
190
# File 'lib/tdiary/filter/spam.rb', line 181

def black_url?( body )
  body.scan( %r|https?://([^/:\s]+)| ) do |s|
    if @spamlookup_safe_domain_list.include?( s[0] )
      debug("#{s[0]} is safe host.", DEBUG_FULL)
      next
    end
    return true if name_base_black_domain?( s[0] ) || ip_base_black_domain?( s[0] )
  end
  return false
end

#comment_filter(diary, comment) ⇒ Object



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
# File 'lib/tdiary/filter/spam.rb', line 192

def comment_filter( diary, comment )
  update_config

  return false if black_url?( comment.body )

  if %r{/\.\/} =~ ENV['REQUEST_URI']
    debug( "REQUEST_URI contains %r{/\./}: #{ENV['REQUEST_URI']}" )
    comment.show = false
    return @filter_mode
  end

  if /^[\x20-\x7f]*$/io !~ comment.mail
    # mail address include not ASCII charactor
    debug( "invalid mail address: #{comment.mail.dump}" )
    comment.show = false
    return @filter_mode
  end

  if !comment.mail.empty? &&
      %r<@[^@]+\.(?:#{TLD.join("|")})$>i !~ comment.mail
    debug( "invalid domain name of mail address: #{comment.mail.dump}" )
    comment.show = false
    return @filter_mode
  end

  p = nil
  if @bad_mails.detect {|p| p =~ comment.mail} ||
      @bad_mails_ext.detect {|p| p =~ comment.mail}
    debug( "mail address blacklisted: /#{p}/ =~ #{comment.mail.dump}" )
    comment.show = false
    return @filter_mode
  end

  if @bad_comments.detect {|p| p =~ comment.body}
    debug( "comment contains bad words: /#{p}/" )
    comment.show = false
    return @filter_mode
  end

  if @bad_ips.detect {|p| p =~ @cgi.remote_addr}
    debug( "ip address blacklisted: /#{p}/ =~ #{@cgi.remote_addr}" )
    comment.show = false
    return @filter_mode
  end

  uris = URI.extract( comment.body, %w(http https ftp mailto) )
  unless uris.empty?
    if @max_uris && @max_uris >= 0 && uris.size > @max_uris
      debug( "too many URIs" )
      comment.show = false
      return @filter_mode
    end

    if @max_rate && @max_rate > 0 &&
        uris.join('').size * 100 / comment.body.gsub(/\s+/, '').size > @max_rate
      debug( "too many URI-chars" )
      comment.show = false
      return @filter_mode
    end

    uris.each do |uri|
      uri = uri.sub(/^ur[il]:/io, '')
      @bad_uris.each do |bad_uri|
        if bad_uri =~ uri
          debug( "comment contains bad words: #{uri}: #{bad_uri}" )
          comment.show = false
          return @filter_mode
        end
      end
    end
  end

  return true
end

#ip_base_black_domain?(domain) ⇒ Boolean

Returns:

  • (Boolean)


160
161
162
163
164
# File 'lib/tdiary/filter/spam.rb', line 160

def ip_base_black_domain?( domain )
  @spamlookup_ip_list.split(/[\n\r]+/).inject(false) do |r, dnsbl|
    r || lookup(domain, dnsbl, true)
  end
end

#lookup(domain, dnsbl, iplookup = false) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/tdiary/filter/spam.rb', line 166

def lookup(domain, dnsbl, iplookup = false)
  Timeout::timeout(1) do
    domain = IPSocket::getaddress( domain ).split(/\./).reverse.join(".") if iplookup
    address = Resolv.getaddress( "#{domain}.#{dnsbl}" )
    debug("lookup:#{domain}.#{dnsbl} address:#{address}: spam host.")
    return true
  end
rescue Timeout::Error, Resolv::ResolvTimeout
  debug("lookup:#{domain}.#{dnsbl}: safe host.")
  return false
rescue Resolv::ResolvError, Exception
  debug("unknown error:#{domain}.#{dnsbl}", DEBUG_FULL)
  return false
end

#name_base_black_domain?(domain) ⇒ Boolean

Returns:

  • (Boolean)


154
155
156
157
158
# File 'lib/tdiary/filter/spam.rb', line 154

def name_base_black_domain?( domain )
  @spamlookup_domain_list.split(/[\n\r]+/).inject(false) do |r, dnsbl|
    r || lookup(domain, dnsbl)
  end
end

#referer_filter(referer) ⇒ Object



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/tdiary/filter/spam.rb', line 267

def referer_filter( referer )
  return true unless referer

  update_config

  return false if black_url?( referer )

  if /#/ =~ referer then
    debug( "referer has a fragment: #{referer}" )
    return false
  end

  if %r{\A[^:]+://[^/]*\z} =~ referer
    debug( "referer has no path: #{referer}" )
    return false
  end

  @bad_uris.each do |bad_uri|
    if bad_uri =~ referer
      debug( "referer contains bad words: #{referer}: #{bad_uri}" )
      return false
    end
  end

  return true
end

#update_configObject



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
# File 'lib/tdiary/filter/spam.rb', line 35

def update_config
  if @conf.options.include?('spamfilter.filter_mode')
    if @conf.options['spamfilter.filter_mode']
      @filter_mode = true # invisible
    else
      @filter_mode = false # drop
    end
  else
    @filter_mode = true # invisible
  end

  if @conf.options.include?('spamfilter.max_uris')
    @max_uris = @conf.options['spamfilter.max_uris'].to_i
  else
    @max_uris = nil
  end

  if @conf.options.include?('spamfilter.max_rate')
    @max_rate = @conf.options['spamfilter.max_rate'].to_i
  else
    @max_rate = nil
  end

  if @conf.options.include?('spamlookup.ip.list')
    @spamlookup_ip_list = @conf.options['spamlookup.ip.list']
  else
    @spamlookup_ip_list = "bsb.spamlookup.net"
  end

  if @conf.options.include?('spamlookup.domain.list')
    @spamlookup_domain_list = @conf.options['spamlookup.domain.list']
  else
    @spamlookup_domain_list = "bsb.spamlookup.net\nmulti.surbl.org\nrbl.bulkfeeds.jp"
  end

  if @conf.options.include?('spamlookup.safe_domain.list')
    @spamlookup_safe_domain_list = @conf.options['spamlookup.safe_domain.list']
  else
    @spamlookup_safe_domain_list = "www.google.com\nwww.google.co.jp\nsearch.yahoo.co.jp\nwww.bing.com"
  end

  if @conf.options.include?('spamfilter.bad_uri_patts_for_mails')
    @bad_uri_patts_for_mails =
        @conf.options['spamfilter.bad_uri_patts_for_mails']
  else
    @bad_uri_patts_for_mails = false
  end
  unless @bad_uri_patts_for_mails
    @bad_mails_ext = []
  end

  unless @conf.options.include?('spamfilter.bad_uri_patts')
    @conf.options['spamfilter.bad_uri_patts'] = ''
  end
  if @bad_uri_patts != @conf.options['spamfilter.bad_uri_patts']
    @bad_uri_patts = @conf.options['spamfilter.bad_uri_patts']
    tmp = @bad_uri_patts.split(/[\r\n]+/)
    tmp.delete_if {|t| t.empty?}
    if tmp.empty?
      @bad_uris = []
      @bad_mails_ext = []
    else
      @bad_uris = [
        %r!^[a-z]*://(?:[^/]*(?:#{tmp.join('|')})){2}!i,
        %r!^[a-z]*://[^/]*\b(?:#{tmp.join('|')})!i,
        %r!^[a-z]*://[^/]*(?:#{tmp.join('|')})\b!i,
        %r!^[a-z]*://.*\b(?:#{tmp.join('|')})\b!i,
        %r!^[a-z]*://[^/]*?[^./]{20,}[^/]*/?$!i,
        %r!^[a-z]*://[^/.]+(?:/|$)!i,
        %r<^[a-z]*://[^/]+\.(?!(?:#{TLD.join("|")})\b\.?)[^.:/]+(?:\.?(?::\d+)?(?:/|$))>i,
      ]
      if @bad_uri_patts_for_mails
        @bad_mails_ext = [
          %r!\b(?:#{tmp.join('|')})!i,
          %r!(?:#{tmp.join('|')})\b!i,
        ]
      end
    end
  end

  unless @conf.options.include?('spamfilter.bad_mail_patts')
    @conf.options['spamfilter.bad_mail_patts'] = ''
  end
  if @bad_mail_patts != @conf.options['spamfilter.bad_mail_patts']
    @bad_mail_patts = @conf.options['spamfilter.bad_mail_patts']
    tmp = @bad_mail_patts.split(/[\r\n]+/)
    tmp.delete_if {|t| t.empty?}
    @bad_mails = tmp.collect {|t| %r!#{t}!i rescue nil}.compact
  end

  unless @conf.options.include?('spamfilter.bad_comment_patts')
    @conf.options['spamfilter.bad_comment_patts'] = ''
  end
  if @bad_comment_patts != @conf.options['spamfilter.bad_comment_patts']
    @bad_comment_patts = @conf.options['spamfilter.bad_comment_patts']
    tmp = @bad_comment_patts.split(/[\r\n]+/)
    tmp.delete_if {|t| t.empty?}
    @bad_comments = tmp.collect {|t| %r!#{t}!i rescue nil}.compact
  end

  unless @conf.options.include?('spamfilter.bad_ip_addrs')
    @conf.options['spamfilter.bad_ip_addrs'] = ''
  end
  if @bad_ip_addrs != @conf.options['spamfilter.bad_ip_addrs']
    @bad_ip_addrs = @conf.options['spamfilter.bad_ip_addrs']
    tmp = @bad_ip_addrs.split(/[\r\n]+/)
    tmp.delete_if {|t| t.empty?}
    @bad_ips = tmp.collect do |t|
      if /\.$/ =~ t
        %r!#{Regexp.quote(t[0..-2]) + '.*'}!i
      else
        %r!#{Regexp.quote(t)}!i
      end
    end
  end

  nil
end