From 7ab0f052f335777ec4d3b0e4e71a72c4eef228c5 Mon Sep 17 00:00:00 2001 From: Rick Okin Date: Tue, 9 Aug 2005 01:58:16 +0000 Subject: [PATCH] Primitive spam filter --- app/controllers/wiki_controller.rb | 24 +++++++++++++++++++++++- config/environments/production.rb | 11 +++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/app/controllers/wiki_controller.rb b/app/controllers/wiki_controller.rb index 43d55b2e..8fcf3060 100644 --- a/app/controllers/wiki_controller.rb +++ b/app/controllers/wiki_controller.rb @@ -176,6 +176,9 @@ class WikiController < ApplicationController cookies['author'] = @params['author'] begin + check_for_spam(@params['content'], remote_ip) + check_blocked_ips(remote_ip) + if @page wiki.revise_page(@web_name, @page_name, @params['content'], Time.now, Author.new(@params['author'], remote_ip)) @@ -227,7 +230,26 @@ class WikiController < ApplicationController private - + + def check_blocked_ips(ip) + if defined? BLOCKED_IPS and not BLOCKED_IPS.nil? + BLOCKED_IPS.each do |blocked_ip| + raise Instiki::ValidationError.new('Revision rejected by spam filter') if ip == blocked_ip + end + end + end + + def check_for_spam(new_content, ip) + if defined? SPAM_PATTERNS and not SPAM_PATTERNS.nil? + SPAM_PATTERNS.each do |pattern| + if new_content =~ pattern + logger.info "Spam attempt from IP address #{ip}" + raise Instiki::ValidationError.new('Revision rejected by spam filter') + end + end + end + end + def convert_tex_to_pdf(tex_path) # TODO remove earlier PDF files with the same prefix # TODO handle gracefully situation where pdflatex is not available diff --git a/config/environments/production.rb b/config/environments/production.rb index e5b18783..02d9dcb9 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -2,3 +2,14 @@ Dependencies.mechanism = :require ActionController::Base.consider_all_requests_local = false ActionController::Base.perform_caching = false + +spam_patterns_filename = RAILS_ROOT + '/config/spam_patterns.txt' +if File.exists? spam_patterns_filename + SPAM_PATTERNS = File.readlines(spam_patterns_filename).select { |line| line != '' }.map { + |line| Regexp.new(line) } +end + +blocked_ips_filename = RAILS_ROOT + '/config/blocked_ips.txt' +if File.exists? blocked_ips_filename + BLOCKED_IPS = File.readlines(blocked_ips_filename).select { |line| line != '' } +end