Note that there are some explanatory texts on larger screens.

plurals
  1. POGem-idea: Automatic spam protection with captcha in before_filter when HTTP-method is post,put or delete
    primarykey
    data
    text
    <p>I'm thinking about writing an automatic spam protection system (maybe I will write a public gem) for rails.</p> <p>My concept is to include a helper method in application_controller f.e.:</p> <pre><code>class ApplicationController &lt; ActionController::Base automatic_captcha_redirect(:min_time =&gt; 30.seconds :limit =&gt; 50) ... end </code></pre> <p>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.</p> <p>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).</p> <pre><code># before_filter :check_spam def check_spam if !request.get? &amp;&amp; session[:last_manipulation_at] &amp;&amp; session[:last_manipulation_at] &gt;= 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 </code></pre> <p>And in captcha.haml</p> <pre><code>=form_tag -request.params.each do |key, value| =hidden_field_tag key, value =captcha_image =submit_button_tag </code></pre> <p>If the user submits the right captcha-word, his data will be posted to the right action.</p> <p>Do you think thats realizable? Any critics or suggestions? Or an idea how to realize this behaviour?</p> <p><strong>EDIT:</strong></p> <ul> <li>this should not pass through all the ActiveRecord stack; can't it be implemented as a middleware hook (Rails Rack)? <ul> <li>Yes, would be a good idea - but I'm not very familiar with rails rack :/</li> </ul></li> <li>what about file uploads? (you can not store it in a hidden file) <ul> <li>Hm... maybe a check if there is a file in the post? (How could that be realized?)</li> </ul></li> <li>what about Ajax posting? <ul> <li>Maybe sending back http-status codes (f.e. 503 Service temporary unavailable)</li> </ul></li> <li>why only POST and not also PUT and DELETE? <ul> <li>corrected this in my question</li> </ul></li> </ul> <p><strong>EDIT:</strong></p> <p>First structure of processing (as non rack-app - I dont know how to write rack apps):</p> <p>0) Settings in environment.rb</p> <pre><code>auto_recaptcha[:limit] = 10 auto_recaptcha[:min_time] = 1.minute </code></pre> <p>1) User posts data </p> <p>Check last_manipulation and max. amount of allowed manipultations in application_controller.rb</p> <pre><code>class ApplicationController &lt; 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? &amp;&amp; session[:last_manipulation_at][:date] &gt; DateTime.now - auto_recaptcha[:min_time] &amp;&amp; session[:last_manipulation_at][:manipultation].count &lt; 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] &lt;&lt; DateTime.now session[:last_manipulation_at][:date] = DateTime.now end end end end </code></pre> <p>captcha.haml</p> <pre><code>-form_tag @url do -request.params.each do |key, value| =hidden_field_tag key, value =captcha_image =submit_button_tag </code></pre> <p>2) ... ... ...</p> <p>last) Post userdata to the right location</p> <pre><code>post(params) =&gt; users_path # path "/users" with method: post </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload