diff --git a/Gemfile b/Gemfile index 14b23e16..fdf97428 100644 --- a/Gemfile +++ b/Gemfile @@ -45,6 +45,7 @@ gem 'htmlentities' gem 'rinku' gem 'image_optim' gem 'image_optim_pack' +gem 'ipaddress' platform :mri, :rbx do gem 'magic' # sudo apt-get install file, For OSX: brew install libmagic diff --git a/Gemfile.lock b/Gemfile.lock index 69308e6a..0bf1e9b7 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -109,6 +109,7 @@ GEM image_size (1.5.0) in_threads (1.3.1) io-extra (1.2.8) + ipaddress (0.8.3) jimson-temp (0.9.5) blankslate (>= 3.1.2) multi_json (~> 1.0) @@ -294,6 +295,7 @@ DEPENDENCIES image_optim image_optim_pack io-extra + ipaddress jdbc-postgres jruby-openssl json @@ -346,4 +348,4 @@ DEPENDENCIES zipruby BUNDLED WITH - 1.12.5 + 1.13.7 diff --git a/Rakefile b/Rakefile index 120f015d..76b28d86 100644 --- a/Rakefile +++ b/Rakefile @@ -266,15 +266,6 @@ task :prime_site_updated_at => [:environment] do end end -desc 'hash_ips' -task :hash_ips => [:environment] do - Site.select(:id,:ip).order(:id).all.each do |s| - next if s.ip.nil? || s.ip.match(/#{$config['ip_hash_salt']}/) - s.ip = s.ip - s.save_changes validate: false - end -end - desc 'prime_site_files' task :prime_site_files => [:environment] do Site.where(is_banned: false).where(is_deleted: false).select(:id, :username).all.each do |site| diff --git a/app/admin.rb b/app/admin.rb index 2eda7fa6..0ef3cc27 100644 --- a/app/admin.rb +++ b/app/admin.rb @@ -236,7 +236,7 @@ post '/admin/banhammer' do site.ban! deleted_count += 1 - if !params[:ban_using_ips].empty? && !site.ip.empty? + if !params[:ban_using_ips].empty? && IPAddress.valid?(site.ip) sites = Site.filter(ip: site.ip, is_banned: false).all sites.each do |s| next if usernames.include?(s.username) @@ -244,6 +244,16 @@ post '/admin/banhammer' do end ip_deleted_count += 1 end + + if params[:classifier] == 'spam' || params[:classifier] == 'phishing' + next unless IPAddress.valid?(site.ip) + StopForumSpamWorker.perform_async( + username: site.username, + email: site.email, + ip: site.ip, + evidence: "#{params[:classifier]}\n#{site.screenshot_url(Site::SCREENSHOT_RESOLUTIONS.first)}" + ) + end end flash[:success] = "#{ip_deleted_count + deleted_count} sites have been banned, including #{ip_deleted_count} matching IPs." diff --git a/app/site.rb b/app/site.rb index 0a151cd8..4dbd395f 100644 --- a/app/site.rb +++ b/app/site.rb @@ -257,7 +257,7 @@ post '/site/:username/report' do |username| redirect request.referer if current_site.id == site.id report.reporting_site_id = current_site.id else - report.ip = Site.hash_ip request.ip + report.ip = request.ip end report.save diff --git a/config.yml.template b/config.yml.template index 3c55d28e..11804688 100644 --- a/config.yml.template +++ b/config.yml.template @@ -24,6 +24,7 @@ development: education_tag_whitelist: - mrteacher screenshots_url: http://screenshots:derp@127.0.0.1:12345 + stop_forum_spam_api_key: testkey test: database: 'postgres://localhost/neocities_test' database_pool: 1 @@ -47,3 +48,4 @@ test: - 10.0.0.2 education_tag_whitelist: - mrteacher + stop_forum_spam_api_key: testkey diff --git a/config.yml.travis b/config.yml.travis index 4eb05aa9..a5e3445b 100644 --- a/config.yml.travis +++ b/config.yml.travis @@ -19,3 +19,4 @@ proxy_ips: - 10.0.0.2 education_tag_whitelist: - mrteacher +stop_forum_spam_api_key: testkey diff --git a/models/site.rb b/models/site.rb index 004f4bc3..52357b70 100644 --- a/models/site.rb +++ b/models/site.rb @@ -296,19 +296,15 @@ class Site < Sequel::Model end def ip_create_limit?(ip) - hashed_ip = hash_ip ip - Site.where('created_at > ?', Date.today.to_time).where(ip: hashed_ip).count > IP_CREATE_LIMIT || - Site.where(ip: hashed_ip).count > TOTAL_IP_CREATE_LIMIT - end - - def hash_ip(ip) - SCrypt::Engine.hash_secret ip, $config['ip_hash_salt'] + Site.where('created_at > ?', Date.today.to_time).where(ip: ip).count > IP_CREATE_LIMIT || + Site.where(ip: ip).count > TOTAL_IP_CREATE_LIMIT end def banned_ip?(ip) return false if ENV['RACK_ENV'] == 'production' && ip == '127.0.0.1' + return false if ip.blank? return true if Site.where(is_banned: true). - where(ip: hash_ip(ip)). + where(Sequel.or(ip: ip, ip: hash_ip(ip))). where(['updated_at > ?', Time.now-BANNED_TIME]). first @@ -317,6 +313,10 @@ class Site < Sequel::Model false end + def hash_ip(ip) + SCrypt::Engine.hash_secret ip, $config['ip_hash_salt'] + end + def ssl_sites select(:id, :username, :domain, :ssl_key, :ssl_cert). exclude(domain: nil). @@ -326,10 +326,6 @@ class Site < Sequel::Model end end - def ip=(ip) - super self.class.hash_ip(ip) - end - def is_following?(site) followings_dataset.select(:follows__id).filter(site_id: site.id).first ? true : false end diff --git a/tests/acceptance/signup_tests.rb b/tests/acceptance/signup_tests.rb index cf25ffba..e8e123ed 100644 --- a/tests/acceptance/signup_tests.rb +++ b/tests/acceptance/signup_tests.rb @@ -60,7 +60,7 @@ describe 'signup' do site.site_updated_at.must_equal nil site.is_education.must_equal false - site.ip.must_equal Site.hash_ip('127.0.0.1') + site.ip.must_equal '127.0.0.1' end it 'fails if site with same ip has been banned' do diff --git a/workers/stop_forum_spam_worker.rb b/workers/stop_forum_spam_worker.rb new file mode 100644 index 00000000..f0f41b77 --- /dev/null +++ b/workers/stop_forum_spam_worker.rb @@ -0,0 +1,10 @@ +class StopForumSpamWorker + include Sidekiq::Worker + sidekiq_options queue: :stop_forum_spam, retry: 1, backtrace: true + + def perform(opts) + opts.merge! api_key: $config['stop_forum_spam_api_key'] + res = HTTP.post 'https://stopforumspam.com/add', opts + puts res.inspect + end +end