Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 309137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:42:52+00:00 2026-05-12T07:42:52+00:00

I’m thinking about writing an automatic spam protection system (maybe I will write a

  • 0

I’m thinking about writing an automatic spam protection system (maybe I will write a public gem) for rails.

My concept is to include a helper method in application_controller f.e.:

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, which checks, if the current request is via post, put or delete-method.

If the user’s last post-request is smaller than :min_time, then the request should be redirected to an captcha-input-page (the posted user-data resides 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

If the user submits the right captcha-word, his data will be posted to the right action.

Do you think thats realizable?
Any critics or suggestions? Or an idea how to realize this behaviour?

EDIT:

  • this should not pass through all the ActiveRecord stack; can’t it be implemented as a middleware hook (Rails Rack)?
    • Yes, would be a good idea – but I’m not very familiar with rails rack :/
  • what about file uploads? (you can not store it in a hidden file)
    • Hm… maybe a check if there is a file in the post? (How could that be realized?)
  • what about Ajax posting?
    • Maybe sending back http-status codes (f.e. 503 Service temporary unavailable)
  • why only POST and not also PUT and DELETE?
    • corrected this in my question

EDIT:

First structure of processing (as non rack-app – I dont know how to write rack apps):

0) Settings in environment.rb

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

1) User posts data

Check last_manipulation and max. amount of allowed manipultations in 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 userdata to the right location

post(params) => users_path # path "/users" with method: post
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T07:42:52+00:00Added an answer on May 12, 2026 at 7:42 am

    One way this could be put together:

    • Middleware/rails metal component that
      monitors the requests and adds the
      information to the rack session.

    • Controller helpers for before_filters
      on things that might need captchas

    • View helpers for displaying the
      captchas

    You could make the captcha rate adjustable through the args passing mechanism of use

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

    Also, this should not rely on hidden form fields because a determined bot writer could just change the value they are posting to your server code.

    Simple middleware example code(slightly better than a stab in the dark, but still)

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 235k
  • Answers 235k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Make sure that a ViewResult is returned Make sure the… May 13, 2026 at 6:11 am
  • Editorial Team
    Editorial Team added an answer You can either have something higher up in the responder… May 13, 2026 at 6:11 am
  • Editorial Team
    Editorial Team added an answer Yes, all files necessary to build and run the project… May 13, 2026 at 6:11 am

Related Questions

I want use html5's new tag to play a wav file (currently only supported
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I've got a string that has curly quotes in it. I'd like to replace
In order to apply a triggered animation to all ToolTip s in my app,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.