rucaptcha 3.0.0.beta1-x86_64-darwin
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +164 -0
- data/Rakefile +34 -0
- data/ext/rucaptcha/Cargo.lock +1226 -0
- data/ext/rucaptcha/Cargo.toml +14 -0
- data/ext/rucaptcha/extconf.rb +4 -0
- data/ext/rucaptcha/src/captcha.rs +250 -0
- data/ext/rucaptcha/src/lib.rs +18 -0
- data/lib/rucaptcha/2.7/rucaptcha.bundle +0 -0
- data/lib/rucaptcha/3.0/rucaptcha.bundle +0 -0
- data/lib/rucaptcha/3.1/rucaptcha.bundle +0 -0
- data/lib/rucaptcha/cache.rb +12 -0
- data/lib/rucaptcha/configuration.rb +15 -0
- data/lib/rucaptcha/controller_helpers.rb +100 -0
- data/lib/rucaptcha/engine.rb +15 -0
- data/lib/rucaptcha/errors/configuration.rb +5 -0
- data/lib/rucaptcha/version.rb +3 -0
- data/lib/rucaptcha/view_helpers.rb +22 -0
- data/lib/rucaptcha.rb +83 -0
- metadata +91 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b0a186b93a922de9876ed0b49faee0b7235cc731c982c4acc22c63f10e6df7cd
|
4
|
+
data.tar.gz: 5d372dc495e52108fc8edcfe92e3491e04a8938c5407737f703592e78bf95fc8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 79ffdd1c63d46e430946fbefc0ffcba3c63adef4499fe6a0c8a565ee4236ecfd5e6be6c1b5b5663ba33d8bcc55fb3b3d0e6982eaa738ac6b14ba9e4e6aafeedb
|
7
|
+
data.tar.gz: ab5fe4ce86abd8261aed45805a409780415b752ec3cbc6da30fb7c9778903ac1cfa4b75fb79a7ce05f3e6548f999df02f2b7008d8b2a6b605bc6ce7d04c93db4
|
data/README.md
ADDED
@@ -0,0 +1,164 @@
|
|
1
|
+
# RuCaptcha
|
2
|
+
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/rucaptcha.svg)](https://badge.fury.io/rb/rucaptcha)
|
4
|
+
[![build](https://github.com/huacnlee/rucaptcha/workflows/build/badge.svg)](https://github.com/huacnlee/rucaptcha/actions?query=workflow%3Abuild)
|
5
|
+
|
6
|
+
Captcha Gem for Rails, which generates captcha image by Rust.
|
7
|
+
|
8
|
+
> NOTE: According to the use of Ruby China, the verification code looks like has a lower than 5% probability of being parsed by OCR and the verification code is cracked (All Image Captcha libs are has same problem). It is recommended that you use the IP rate limit to enhance the protection.
|
9
|
+
> NOTE: 以 Ruby China 的使用来看,验证码似乎有低于 5% 的概率被 OCR 读取解析 (图片验证码都有这个问题) 导致验证码被破解(我们从日志分析绝大多数是成功的,但偶尔一个成功,配合大量机器攻击,导致注册了很多的垃圾账号),建议你额外配合 IP 频率限制的功能来加强保护。
|
10
|
+
|
11
|
+
> 如果你需要更高强度的验证,建议选择商用服务。
|
12
|
+
|
13
|
+
[中文介绍和使用说明](https://ruby-china.org/topics/27832)
|
14
|
+
|
15
|
+
## Example
|
16
|
+
|
17
|
+
![1](https://user-images.githubusercontent.com/5518/195587367-6b579046-1d75-4a64-8e8f-4475da8932fa.png) ![2](https://user-images.githubusercontent.com/5518/195587377-08065df4-80ad-4c3f-baf4-2da7f9919b19.png) ![3](https://user-images.githubusercontent.com/5518/195587383-de8f73ea-e934-4c9d-b278-c78fb69cad58.png) ![4](https://user-images.githubusercontent.com/5518/195587381-a67e586d-0b54-4d6f-89c8-a431b085e16c.png)
|
18
|
+
|
19
|
+
## Feature
|
20
|
+
|
21
|
+
- Native Gem base on Rust.
|
22
|
+
- For Rails Application;
|
23
|
+
- Simple, Easy to use;
|
24
|
+
- High performance.
|
25
|
+
|
26
|
+
## Usage
|
27
|
+
|
28
|
+
Put rucaptcha in your `Gemfile`:
|
29
|
+
|
30
|
+
```
|
31
|
+
gem 'rucaptcha'
|
32
|
+
```
|
33
|
+
|
34
|
+
Create `config/initializers/rucaptcha.rb`
|
35
|
+
|
36
|
+
```rb
|
37
|
+
RuCaptcha.configure do
|
38
|
+
# Custom captcha code expire time if you need, default: 2 minutes
|
39
|
+
# self.expires_in = 120
|
40
|
+
# [Requirement / 重要]
|
41
|
+
# Store Captcha code where, this config more like Rails config.cache_store
|
42
|
+
# default: Read config info from `Rails.application.config.cache_store`
|
43
|
+
# But RuCaptcha requirements cache_store not in [:null_store, :memory_store, :file_store]
|
44
|
+
# 默认:会从 Rails 配置的 cache_store 里面读取相同的配置信息,并尝试用可以运行的方式,用于存储验证码字符
|
45
|
+
# 但如果是 [:null_store, :memory_store, :file_store] 之类的,你可以通过下面的配置项单独给 RuCaptcha 配置 cache_store
|
46
|
+
self.cache_store = :mem_cache_store
|
47
|
+
# 如果想要 disable cache_store 的 warning,就设置为 true,default false
|
48
|
+
# self.skip_cache_store_check = true
|
49
|
+
# Chars length, default: 5, allows: [3 - 7]
|
50
|
+
# self.length = 5
|
51
|
+
end
|
52
|
+
```
|
53
|
+
|
54
|
+
RuCaptcha 没有使用 Rails Session 来存储验证码信息,因为 Rails 的默认 Session 是存储在 Cookie 里面,如果验证码存在里面会存在 [Replay attack](https://en.wikipedia.org/wiki/Replay_attack) 漏洞,导致验证码关卡被攻破。
|
55
|
+
|
56
|
+
所以我在设计上要求 RuCaptcha 得配置一个可以支持分布式的后端存储方案例如:Memcached 或 Redis 以及其他可以支持分布式的 cache_store 方案。
|
57
|
+
|
58
|
+
同时,为了保障易用性,默认会尝试使用 `:file_store` 的方式,将验证码存在应用程序的 `tmp/cache/rucaptcha/session` 目录(但请注意,多机器部署这样是无法正常运作的)。
|
59
|
+
|
60
|
+
所以,我建议大家使用的时候,配置上 `cache_store` (详见 [Rails Guides 缓存配置部分](https://ruby-china.github.io/rails-guides/caching_with_rails.html#%E9%85%8D%E7%BD%AE)的文档)到一个 Memcached 或 Redis,这才是最佳实践。
|
61
|
+
|
62
|
+
#
|
63
|
+
|
64
|
+
(RuCaptha do not use Rails Session to store captcha information. As the default session is stored in Cookie in Rails, there's a [Replay attack](https://en.wikipedia.org/wiki/Replay_attack) bug which may causes capthcha being destroyed if we store captcha in Rails Session.
|
65
|
+
|
66
|
+
So in my design I require RuCaptcha to configure a distributed backend storage scheme, such as Memcached, Redis or other cache_store schemes which support distribution.
|
67
|
+
|
68
|
+
Meanwhile, for the ease of use, RuCapthca would try to use `:file_store` by default and store the capthca in `tmp/cache/rucaptcha/session` directory (kindly note that it's not working if deploy on multiple machine).
|
69
|
+
|
70
|
+
For recommendation, configure the `cache_store`(more details on [Rails Guides Configuration of Cache Stores](http://guides.rubyonrails.org/caching_with_rails.html#configuration)) to Memcached or Redis, that would be the best practice.)
|
71
|
+
|
72
|
+
#
|
73
|
+
|
74
|
+
Controller `app/controller/account_controller.rb`
|
75
|
+
|
76
|
+
When you called `verify_rucaptcha?`, it uses value from `params[:_rucaptcha]` to validate.
|
77
|
+
|
78
|
+
```rb
|
79
|
+
class AccountController < ApplicationController
|
80
|
+
def create
|
81
|
+
@user = User.new(params[:user])
|
82
|
+
if verify_rucaptcha?(@user) && @user.save
|
83
|
+
redirect_to root_path, notice: 'Sign up successed.'
|
84
|
+
else
|
85
|
+
render 'account/new'
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
class ForgotPasswordController < ApplicationController
|
91
|
+
def create
|
92
|
+
# without any args
|
93
|
+
if verify_rucaptcha?
|
94
|
+
to_send_email
|
95
|
+
else
|
96
|
+
redirect_to '/forgot-password', alert: 'Invalid captcha code.'
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
```
|
101
|
+
|
102
|
+
> TIP: Sometimes you may need to keep last verified captcha code in session on `verify_rucaptcha?` method call, you can use `keep_session: true`. For example: `verify_rucaptcha? @user, keep_session: true`.
|
103
|
+
|
104
|
+
View `app/views/account/new.html.erb`
|
105
|
+
|
106
|
+
```erb
|
107
|
+
<form method="POST">
|
108
|
+
...
|
109
|
+
<div class="form-group">
|
110
|
+
<%= rucaptcha_input_tag(class: 'form-control', placeholder: 'Input Captcha') %>
|
111
|
+
<%= rucaptcha_image_tag(alt: 'Captcha') %>
|
112
|
+
</div>
|
113
|
+
...
|
114
|
+
|
115
|
+
<div class="form-group">
|
116
|
+
<button type="submit" class="btn btn-primary">Submit</button>
|
117
|
+
</div>
|
118
|
+
</form>
|
119
|
+
```
|
120
|
+
|
121
|
+
And if you are using [Devise](https://github.com/plataformatec/devise), you can read this reference to add validation: [RuCaptcha with Devise](https://github.com/huacnlee/rucaptcha/wiki/Working-with-Devise).
|
122
|
+
|
123
|
+
### Write your test skip captcha validation
|
124
|
+
|
125
|
+
for RSpec
|
126
|
+
|
127
|
+
```rb
|
128
|
+
describe 'sign up and login', type: :feature do
|
129
|
+
before do
|
130
|
+
allow_any_instance_of(ActionController::Base).to receive(:verify_rucaptcha?).and_return(true)
|
131
|
+
end
|
132
|
+
|
133
|
+
it { ... }
|
134
|
+
end
|
135
|
+
```
|
136
|
+
|
137
|
+
for MiniTest
|
138
|
+
|
139
|
+
```rb
|
140
|
+
class ActionDispatch::IntegrationTest
|
141
|
+
def sign_in(user)
|
142
|
+
ActionController::Base.any_instance.stubs(:verify_rucaptcha?).returns(true)
|
143
|
+
post user_session_path \
|
144
|
+
'user[email]' => user.email,
|
145
|
+
'user[password]' => user.password
|
146
|
+
end
|
147
|
+
end
|
148
|
+
```
|
149
|
+
|
150
|
+
### Invalid message without Devise
|
151
|
+
|
152
|
+
When you are using this gem without Devise, you may find out that the invalid message is missing.
|
153
|
+
For this case, use the trick below to add your i18n invalid message manually.
|
154
|
+
|
155
|
+
```rb
|
156
|
+
if verify_rucaptcha?(@user) && @user.save
|
157
|
+
do_whatever_you_want
|
158
|
+
redirect_to someplace_you_want
|
159
|
+
else
|
160
|
+
# this is the trick
|
161
|
+
@user.errors.add(:base, t('rucaptcha.invalid'))
|
162
|
+
render :new
|
163
|
+
end
|
164
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "rspec/core/rake_task"
|
3
|
+
require "rake/extensiontask"
|
4
|
+
require "rubygems/package_task"
|
5
|
+
require "bundler"
|
6
|
+
|
7
|
+
CROSS_PLATFORMS = %w[
|
8
|
+
aarch64-linux
|
9
|
+
arm64-darwin
|
10
|
+
x64-mingw32
|
11
|
+
x86_64-darwin
|
12
|
+
x86_64-linux
|
13
|
+
]
|
14
|
+
|
15
|
+
spec = Bundler.load_gemspec("rucaptcha.gemspec")
|
16
|
+
|
17
|
+
Gem::PackageTask.new(spec).define
|
18
|
+
|
19
|
+
Rake::ExtensionTask.new("rucaptcha", spec) do |ext|
|
20
|
+
ext.lib_dir = "lib/rucaptcha"
|
21
|
+
ext.source_pattern = "*.{rs,toml}"
|
22
|
+
ext.cross_compile = true
|
23
|
+
ext.cross_platform = CROSS_PLATFORMS
|
24
|
+
end
|
25
|
+
|
26
|
+
RSpec::Core::RakeTask.new(:spec)
|
27
|
+
task default: :spec
|
28
|
+
|
29
|
+
task :preview do
|
30
|
+
require "rucaptcha"
|
31
|
+
res = RuCaptchaCore.create(5, 5)
|
32
|
+
warn res[0]
|
33
|
+
puts res[1].pack("c*")
|
34
|
+
end
|