rucaptcha 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0b8fc53f553af82a9e3c1ea5ff203058d1c94220
4
- data.tar.gz: 675808db49191ce5c2396fa390fd764084557de6
3
+ metadata.gz: 4eeac0b904fbb23ad981f2a15be405045dd3ab36
4
+ data.tar.gz: 509df17c96e7b4c5e9a16d780a0140a57496c342
5
5
  SHA512:
6
- metadata.gz: 579416c175a280721232673ac4cc619fa76c9dd77b39d39cf4e0c203f31866b8476afe5ab00e3e53d711b0195b0c7e2080b77b6051aaa8bddb1efd5708a5080d
7
- data.tar.gz: 5c8f783af7b9ef4909bf5fab282e35727d2c879c48021a2d5ec7cf3cae5bfd67edc1e72dc619f8f61cfbf24c0e6487aedb6157a3f88286f9586039ec2cfa3730
6
+ metadata.gz: ee75335b1a9494f3180976808f7f6bf15722c4d3fac3bf1ccccc58f519f81fac797cc3460a034ae58dc8c0dd5a825a4b86346c1f1802407a031ecd6187d201f4
7
+ data.tar.gz: 01dc3cf718968f2823f168f368bff69e2df8c056d5de7d285de44bd7284f449d191c98582425c9e550d8837cc3b58b53b9e1b7a6dc088659b65c1a1f8883c36e
data/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ 1.0.1
2
+ -----
3
+
4
+ ## Security Notes
5
+
6
+ - Fix Session replay secure issue that when Rails application use CookieStore.
7
+
1
8
  1.0.0
2
9
  -----
3
10
 
data/README.md CHANGED
@@ -34,7 +34,7 @@ Idea by: https://ruby-china.org/topics/20558#reply4
34
34
  #### Ubuntu
35
35
 
36
36
  ```
37
- sudo apt-get install imagemagick
37
+ sudo apt-get install imagemagick ghostscript
38
38
  ```
39
39
 
40
40
  #### Mac OS X
@@ -11,7 +11,7 @@ module RuCaptcha
11
11
  attr_accessor :cache_limit
12
12
  # Color style, default: :colorful, allows: [:colorful, :black_white]
13
13
  attr_accessor :style
14
- # session[:_rucaptcha] expire time, default 2 minutes
14
+ # rucaptcha expire time, default 2 minutes
15
15
  attr_accessor :expires_in
16
16
  end
17
17
  end
@@ -6,28 +6,55 @@ module RuCaptcha
6
6
  helper_method :verify_rucaptcha?
7
7
  end
8
8
 
9
+ def rucaptcha_sesion_key_key
10
+ ['rucaptcha-session', session.id].join(':')
11
+ end
12
+
9
13
  def generate_rucaptcha
10
- session[:_rucaptcha] = RuCaptcha::Captcha.random_chars
11
- session[:_rucaptcha_at] = Time.now.to_i
14
+ code = RuCaptcha::Captcha.random_chars
15
+ Rails.cache.write(rucaptcha_sesion_key_key, {
16
+ code: code,
17
+ time: Time.now.to_i
18
+ })
12
19
 
13
- RuCaptcha::Captcha.create(session[:_rucaptcha])
20
+ RuCaptcha::Captcha.create(code)
14
21
  end
15
22
 
16
23
  def verify_rucaptcha?(resource = nil)
17
- rucaptcha_at = session[:_rucaptcha_at].to_i
18
- captcha = (params[:_rucaptcha] || '').downcase.strip
24
+ store_info = Rails.cache.read(rucaptcha_sesion_key_key)
25
+ # make sure move used key
26
+ Rails.cache.delete(rucaptcha_sesion_key_key)
27
+
28
+ # Make sure session exist
29
+ if store_info.blank?
30
+ return add_rucaptcha_validation_error
31
+ end
32
+
33
+ # Make sure not expire
34
+ if (Time.now.to_i - store_info[:time]) > RuCaptcha.config.expires_in
35
+ return add_rucaptcha_validation_error
36
+ end
19
37
 
20
- # Captcha chars in Session expire in 2 minutes
21
- valid = false
22
- if (Time.now.to_i - rucaptcha_at) <= RuCaptcha.config.expires_in
23
- valid = captcha.present? && captcha == session.delete(:_rucaptcha)
38
+ # Make sure parama have captcha
39
+ captcha = (params[:_rucaptcha] || '').downcase.strip
40
+ if captcha.blank?
41
+ return add_rucaptcha_validation_error
24
42
  end
25
43
 
26
- if resource && resource.respond_to?(:errors)
27
- resource.errors.add(:base, t('rucaptcha.invalid')) unless valid
44
+ if captcha != store_info[:code]
45
+ return add_rucaptcha_validation_error
28
46
  end
29
47
 
30
- valid
48
+ true
49
+ end
50
+
51
+ private
52
+
53
+ def add_rucaptcha_validation_error
54
+ if defined?(resource) && resource && resource.respond_to?(:errors)
55
+ resource.errors.add(:base, t('rucaptcha.invalid'))
56
+ end
57
+ false
31
58
  end
32
59
  end
33
60
  end
@@ -1,3 +1,3 @@
1
1
  module RuCaptcha
2
- VERSION = '1.0.0'
2
+ VERSION = '1.0.1'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rucaptcha
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Lee
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-09-01 00:00:00.000000000 Z
11
+ date: 2016-10-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties