百科.dev
全部条目AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
N

negative-captcha

> 编程语言
开源

一个插件,可以让在 Rails 中创建负面验证码的过程变得更加轻松

786 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

一个插件,可以让在 Rails 中创建负面验证码的过程变得更加轻松

This project is dead. So sorry.

Due to the way that browsers now interact with autocomplete and accessibility features it's not feasible to use this same style of captcha. Keeping this for historical purposes.

Negative Captcha

What is a Negative Captcha?

A negative captcha has the exact same purpose as your run-of-the-mill image captcha: To keep bots from submitting forms. Image ("positive") captchas do this by implementing a step which only humans can do, but bots cannot: read jumbled characters from an image. But this is bad. It creates usability problems, it hurts conversion rates, and it confuses the shit out of lots of people. Why not do it the other way around? Negative captchas create a form that has tasks that only bots can perform, but humans cannot. This has the exact same effect, with (anecdotally) a much lower false positive identification rate when compared with positive captchas. All of this comes without making humans go through any extra trouble to submit the form. It really is win-win.

How does it work?

In a negative captcha form there are two main parts and three ancillary parts. I'll explain them thusly.

Honeypots

Honeypots are form fields which look exactly like real form fields. Bots will see them and fill them out. Humans cannot see them and thusly will not fill them out. They are hidden indirectly- usually by positioning them off to the left of the browser. They look kind of like this:

Real fields

These fields are the ones humans will see, will subsequently fill out, and that you'll pull your real form data out of. The form name will be hashed so that bots will not know what it is. They look kind of like this:

Timestamp

This is a field that is used in the hash key to make the hash different on every GET request, and to prevent replayability.

Spinner

This is a rotating key that is used in the hash method to prevent replayability. I'm not sold on its usefulness.

Secret key

This is simply some key that is used in the hashing method to prevent bots from backing out the name of the field from the hashed field name.

How does Negative Captcha work?

Installation

You can let bundler install Negative Captcha by adding this line to your application’s Gemfile:

gem 'negative_captcha'

Controller Hooks

Place this before filter at the top of the controller you are protecting:

before_filter :setup_negative_captcha, :only => [:new, :create]

In the same controller include the following private method:

private
  def setup_negative_captcha
    @captcha = NegativeCaptcha.new(
      # A secret key entered in environment.rb. 'rake secret' will give you a good one.
      secret: NEGATIVE_CAPTCHA_SECRET,
      spinner: request.remote_ip,
      # Whatever fields are in your form
      fields: [:name, :email, :body],
      # If you wish to override the default CSS styles (position: absolute; left: -2000px;) used to position the fields off-screen
      css: "display: none",
      params: params
    )
  end

Modify your POST action(s) to check for the validity of the negative captcha form

def create
  # Decrypted params are stored in @captcha.values
  @comment = Comment.new(@captcha.values)

  # @captcha.valid? will return false if a bot submitted the form
  if @captcha.valid? && @comment.save
    redirect_to @comment
  else
    # @captcha.error will explain what went wrong
    flash[:notice] = @captcha.error if @captcha.error
    render :action => 'new'
  end
end

Automated tests

To make all field ids and names predictable for tests, simply add the following line in your spec helper.

NegativeCaptcha.test_mode = true

This will ensure that a field named email will not be referred to by a hash but by test-email instead. A tool like capybara can now bypass this security while still going through the captcha workflow.

Form Example

Modify your form to include the honeypots and other fields. You can probably leave any select, radio, and check box fields alone. The text field/text area helpers should be sufficient.


  
  
  

    

      
      
    

    

      
      
    

    

      
        Accepts a block.
      
      
    

    

      
    

  

Test and enjoy!

Possible Gotchas and other concerns

  • It is still possible for someone to write a bot to exploit a single site by closely examining the DOM. This means that if you are Yahoo, Google or Facebook, negative captchas will not be a complete solution. But if you have a small application, negative captchas will likely be a very, very good solution for you. There are no easy work-arounds to this quite yet. Let me know if you have one.
  • I'm not a genius. It is possible that a bot can figure out the hashed values and determine which forms are which. I don't know how, but I think they might be able to. I welcome people who have thought this out more thoroughly to criticize this method and help me find solutions. I like this idea a lot and want it to succeed.

Credit

The idea of a negative captcha is not mine. It originates (I think) from Damien Katz of CouchDB. I (Erik Peterson) wrote the plugin. Calvin Yu wrote the original class which I refactored quite a bit and made into the gem.

Issues· 14 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言