rucaptcha 3.1.4.pre-x86_64-linux-musl
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +175 -0
- data/Rakefile +66 -0
- data/app/controllers/ru_captcha/captcha_controller.rb +14 -0
- data/config/locales/rucaptcha.de-DE.yml +3 -0
- data/config/locales/rucaptcha.en.yml +3 -0
- data/config/locales/rucaptcha.pt-BR.yml +3 -0
- data/config/locales/rucaptcha.zh-CN.yml +3 -0
- data/config/locales/rucaptcha.zh-TW.yml +3 -0
- data/config/routes.rb +3 -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/fonts/FuzzyBubbles-Regular.ttf +0 -0
- data/ext/rucaptcha/fonts/Handlee-Regular.ttf +0 -0
- data/ext/rucaptcha/src/captcha.rs +257 -0
- data/ext/rucaptcha/src/lib.rs +18 -0
- data/lib/rucaptcha/3.1/rucaptcha.so +0 -0
- data/lib/rucaptcha/3.2/rucaptcha.so +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 +100 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 5268b05c9c23e17e4e0ec2d53ea8530d046377783e476da805b2ef86a15cb818
|
4
|
+
data.tar.gz: 40f35d26cd9dc7f71e006e4c0e12a872b1fc20b9878d54a7d8658aab1b23a9e8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 69bcdbf606d9bfcec5e13558e079b01c61cd6e2827aa10ab993dd440b65131ad20dd3ddd6865b486757bb416529c80ca6fa26c4540dd54166512cd3e4297f18f
|
7
|
+
data.tar.gz: 4967893d108cfa572b0a57e238545b000f06642ebe35a42ec61f3d13f253ba881bd8c451a02877e3611d090a8b87b23ae371802dbab7da7b5382129fe9dbbf0d
|
data/README.md
ADDED
@@ -0,0 +1,175 @@
|
|
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
|
+
<img src="https://user-images.githubusercontent.com/5518/196329734-fee49f62-050b-44c8-a5a8-7ffdd3c5a3f6.png" height="50" alt="0"> <img src="https://user-images.githubusercontent.com/5518/196329738-64b264a1-e3fb-4804-ac46-0df18fb31d1e.png" height="50" alt="1"> <img src="https://user-images.githubusercontent.com/5518/196329740-e10ded26-ba46-4e9b-93b8-ce30c198f880.png" height="50" alt="2"> <img src="https://user-images.githubusercontent.com/5518/196329743-c7b055b8-b309-4554-8c95-66c5caf4437d.png" height="50" alt="3"> <img src="https://user-images.githubusercontent.com/5518/196329745-eb68f0c3-ccac-4fa3-aa7a-cc4c2caeb41e.png" height="50" alt="4"> <img src="https://user-images.githubusercontent.com/5518/196329746-b15a9f71-262e-4699-87c7-a5561c6caf2c.png" height="50" alt="5"> <img src="https://user-images.githubusercontent.com/5518/196329747-d111a5d3-89a1-487b-989e-5be8059488c2.png" height="50" alt="6"> <img src="https://user-images.githubusercontent.com/5518/196329749-2cb44aa3-8b59-427c-91f3-59566d6de8a5.png" height="50" alt="7"> <img src="https://user-images.githubusercontent.com/5518/196329754-ae64374b-f2e5-44b8-a7f4-3aee1405c193.png" height="50" alt="8"> <img src="https://user-images.githubusercontent.com/5518/196329755-26b88705-bf34-4d32-a4dc-076530582a90.png" height="50" alt="9">
|
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
|
+
```
|
165
|
+
|
166
|
+
## Performance
|
167
|
+
|
168
|
+
`rake benchmark` to run benchmark test.
|
169
|
+
|
170
|
+
```
|
171
|
+
Warming up --------------------------------------
|
172
|
+
Generate image 51.000 i/100ms
|
173
|
+
Calculating -------------------------------------
|
174
|
+
Generate image 526.350 (± 2.5%) i/s - 2.652k in 5.041681s
|
175
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,66 @@
|
|
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
|
+
x86_64-linux-musl
|
14
|
+
]
|
15
|
+
|
16
|
+
spec = Bundler.load_gemspec("rucaptcha.gemspec")
|
17
|
+
|
18
|
+
Gem::PackageTask.new(spec).define
|
19
|
+
|
20
|
+
Rake::ExtensionTask.new("rucaptcha", spec) do |ext|
|
21
|
+
ext.lib_dir = "lib/rucaptcha"
|
22
|
+
ext.source_pattern = "*.{rs,toml}"
|
23
|
+
ext.cross_compile = true
|
24
|
+
ext.cross_platform = CROSS_PLATFORMS
|
25
|
+
end
|
26
|
+
|
27
|
+
RSpec::Core::RakeTask.new(:spec)
|
28
|
+
task default: :spec
|
29
|
+
|
30
|
+
task :preview do
|
31
|
+
require "rucaptcha"
|
32
|
+
res = RuCaptchaCore.create(5, 5)
|
33
|
+
warn "-------------------------\n#{res[0]}"
|
34
|
+
puts res[1].pack("c*")
|
35
|
+
end
|
36
|
+
|
37
|
+
task :memory do
|
38
|
+
require "rucaptcha"
|
39
|
+
require "memory_profiler"
|
40
|
+
|
41
|
+
RuCaptchaCore.create(5, 5)
|
42
|
+
|
43
|
+
report = MemoryProfiler.report do
|
44
|
+
1000.times do
|
45
|
+
RuCaptchaCore.create(5, 5)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
GC.start
|
50
|
+
report.pretty_print
|
51
|
+
|
52
|
+
puts "------------------------- Result Guide -------------------------"
|
53
|
+
puts "If [Total retained] have any bytes, there will have memory leak."
|
54
|
+
end
|
55
|
+
|
56
|
+
task :benchmark do
|
57
|
+
require "rucaptcha"
|
58
|
+
require "benchmark/ips"
|
59
|
+
|
60
|
+
RuCaptchaCore.create(5, 5)
|
61
|
+
|
62
|
+
Benchmark.ips do |x|
|
63
|
+
x.report("Generate image") { RuCaptchaCore.create(5, 5) }
|
64
|
+
x.compare!
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module RuCaptcha
|
2
|
+
class CaptchaController < ActionController::Base
|
3
|
+
def index
|
4
|
+
return head :ok if request.head?
|
5
|
+
|
6
|
+
headers["Cache-Control"] = "no-cache, no-store, max-age=0, must-revalidate"
|
7
|
+
headers["Pragma"] = "no-cache"
|
8
|
+
data = generate_rucaptcha
|
9
|
+
|
10
|
+
opts = { disposition: "inline", type: "image/jpeg" }
|
11
|
+
send_data data, opts
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
data/config/routes.rb
ADDED