Gem-idea: automatic spam protection with captcha in before_filter when an HTTP method is sent, placed or deleted

I’m thinking about creating an automatic anti-spam system (maybe I’ll write a public stone) for rails.

My concept is to include a helper method in application_controller fe:

class ApplicationController < ActionController::Base
  automatic_captcha_redirect(:min_time => 30.seconds :limit => 50)
...
end

Then I want to include automatical a before_filter in every controller that checks if the current request is being executed via post, put or delete-method.

If the last last user request is less than: min_time, then the request should be redirected to the captcha I / O page (the posted user data is in hidden html fields).

# before_filter :check_spam
def check_spam
  if !request.get? && session[:last_manipulation_at] 
      && session[:last_manipulation_at] >= DateTime.now - 30.seconds
    redirect_to captcha_path 
      # (doesn't know yet how to handle the post data to 
      # display in hidden fields in the spam-captcha-form)
  end
end

And in captcha.haml

=form_tag 
-request.params.each do |key, value|
  =hidden_field_tag key, value

=captcha_image
=submit_button_tag

, .

, ? ? , ?

EDIT:

  • ActiveRecord; (Rails Rack)?
    • , , :/
  • ? ( )
    • ... , , ? ( ?)
  • Ajax?
    • , http- (, 503 )
  • POST, PUT DELETE?

EDIT:

( -- - , ):

0) environment.rb

auto_recaptcha[:limit] = 10
auto_recaptcha[:min_time] = 1.minute

1)

last_manipulation . application_controller.rb

class ApplicationController < ActionController::Base
  before_filter :automatic_captcha_redirect

  def automatic_captcha_redirect
    session[:last_manipulation_at][:manipultation] = [] unless session[:last_manipulation_at][:manipultation]
    # Checks if requests are falling under the specifications for showing captcha


    if !request.get? 
       && session[:last_manipulation_at][:date] > DateTime.now - auto_recaptcha[:min_time] 
       && session[:last_manipulation_at][:manipultation].count < auto_recaptcha[:limit]

      # If user answered captcha, verify it
      if !verify_captcha(params)
        @url = request.url
        @params = request.params
        render "layouts/captcha.haml"
      else

        # Add successfull manipulation to counter
        session[:last_manipulation_at][:manipultation] << DateTime.now
        session[:last_manipulation_at][:date] = DateTime.now
      end
    end
  end
end

captcha.haml

-form_tag @url do 
  -request.params.each do |key, value|
    =hidden_field_tag key, value

  =captcha_image
  =submit_button_tag

2) ... ... ...

last)

post(params) => users_path # path "/users" with method: post
+2
2

:

  • Middleware/rails metal, .

  • before_filters , captchas

  • CAPTCHAs

use

#config/environment.rb
config.middleware.use 'CaptchaMiddleware',:period=>5.minutes,:limit=>50,:captcha_url=>'/captcha'

, , - , .

( , , )

class CaptchaMiddleware
  def initialize app,options
    @app = app
    @options=options
  end

  def update_stats!
    #session based,on account of laziness
    session[:reqs] ||= []
    session[:reqs].reject!{ |request| request < Time.now - @options[:period]}
    session[:reqs] << Time.now
  end

  def over_limit?
    session[:reqs].length > @options[:limit]
  end

  def call env
    @env = env
    if @env["REQUEST_METHOD"]!='GET'
      update_stats!
      if over_limit?
        return [302,{"Location: #{options[:captcha_url]}"},'']
      end
    end
    @app.call env
  end

  def session
    @env["rack.session"]
  end
end
+1

-, , .

/:

  • ActiveRecord; (Rails Rack)?
  • ? ( )
  • Ajax?
  • POST, PUT DELETE?

, 5 , , . , .

+2

Source: https://habr.com/ru/post/1717721/


All Articles