cuba-csrf 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE +1 -1
- data/README.md +17 -4
- data/cuba-csrf.gemspec +1 -1
- data/lib/cuba/csrf.rb +9 -2
- data/makefile +1 -1
- data/test/csrf.rb +17 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e90068b1138f718ffb414569573232216f4a5f87
|
4
|
+
data.tar.gz: aa11d64ba6805e470a61650b7cf0165c49235a26
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cf0209f12ee25accc870ad7e8fb05830e9d46d7c65d7755851154f7d5930ef079cf31f4c75b558646cc108e9250d7051d2215b0dbf02ba678450c08079526bac
|
7
|
+
data.tar.gz: 85537a9e17c131a9afa899d8dd7b1d65f02c30e157f02cc63e6ae7f5a1a74f43dd2b35500de439aee0f3d9e1672371dac5a63e35640697e23bfbe2625b6d30a5
|
data/LICENSE
CHANGED
data/README.md
CHANGED
@@ -31,7 +31,7 @@ require "cuba/csrf"
|
|
31
31
|
Cuba.plugin(Cuba::CSRF)
|
32
32
|
|
33
33
|
Cuba.define do
|
34
|
-
|
34
|
+
on !csrf_safe? do
|
35
35
|
session.clear
|
36
36
|
|
37
37
|
res.status = 403
|
@@ -64,9 +64,22 @@ HTTP compression or randomize secrets per request.
|
|
64
64
|
If it's possible, disable HTTP compression. In Nginx, you can use
|
65
65
|
the `gzip off` directive.
|
66
66
|
|
67
|
-
|
68
|
-
that if you plan to use HTTP compression, your application might
|
69
|
-
to BREACH.
|
67
|
+
By default, this plugin doesn't generate or mask CSRF tokens per request.
|
68
|
+
This means that if you plan to use HTTP compression, your application might
|
69
|
+
be vulnerable to BREACH. However, generation of new secrets per request
|
70
|
+
can be done with:
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
Cuba.define do
|
74
|
+
on !csrf_safe? do
|
75
|
+
...
|
76
|
+
end
|
77
|
+
|
78
|
+
session.delete(:csrf_token)
|
79
|
+
|
80
|
+
...
|
81
|
+
end
|
82
|
+
```
|
70
83
|
|
71
84
|
We designed this library to fit our use case. We don't have HTTP compression
|
72
85
|
enabled because we also have other sensitive information apart from the CSRF
|
data/cuba-csrf.gemspec
CHANGED
data/lib/cuba/csrf.rb
CHANGED
@@ -3,14 +3,21 @@ require "securerandom"
|
|
3
3
|
|
4
4
|
module Cuba::CSRF
|
5
5
|
def csrf_safe?
|
6
|
-
req.get? || req.head? ||
|
6
|
+
req.get? || req.head? ||
|
7
|
+
req[:csrf_token] == csrf_token ||
|
8
|
+
env["HTTP_X_CSRF_TOKEN"] == csrf_token
|
7
9
|
end
|
8
10
|
|
9
11
|
def csrf_token
|
10
12
|
session[:csrf_token] ||= SecureRandom.base64(32)
|
11
13
|
end
|
12
14
|
|
13
|
-
def
|
15
|
+
def csrf_form_tag
|
14
16
|
%Q(<input type="hidden" name="csrf_token" value="#{csrf_token}">)
|
15
17
|
end
|
18
|
+
alias :csrf_tag :csrf_form_tag
|
19
|
+
|
20
|
+
def csrf_meta_tag
|
21
|
+
%Q(<meta name="csrf_token" content="#{csrf_token}">)
|
22
|
+
end
|
16
23
|
end
|
data/makefile
CHANGED
data/test/csrf.rb
CHANGED
@@ -69,6 +69,17 @@ scope do
|
|
69
69
|
end
|
70
70
|
end
|
71
71
|
|
72
|
+
test "http header" do
|
73
|
+
csrf_token = SecureRandom.hex(32)
|
74
|
+
|
75
|
+
Cuba.define do
|
76
|
+
session[:csrf_token] = csrf_token
|
77
|
+
raise unless csrf_safe?
|
78
|
+
end
|
79
|
+
|
80
|
+
post "/", {}, { "HTTP_X_CSRF_TOKEN" => csrf_token }
|
81
|
+
end
|
82
|
+
|
72
83
|
test "sub app raises too" do
|
73
84
|
class App < Cuba
|
74
85
|
define do
|
@@ -131,8 +142,12 @@ scope do
|
|
131
142
|
end
|
132
143
|
|
133
144
|
api = Api.new
|
134
|
-
csrf_tag = %Q(<input type="hidden" name="csrf_token" value="#{api.csrf_token}">)
|
135
145
|
|
136
|
-
|
146
|
+
csrf_form_tag = %Q(<input type="hidden" name="csrf_token" value="#{api.csrf_token}">)
|
147
|
+
csrf_meta_tag = %Q(<meta name="csrf_token" content="#{api.csrf_token}">)
|
148
|
+
|
149
|
+
assert_equal(csrf_form_tag, api.csrf_tag)
|
150
|
+
assert_equal(csrf_form_tag, api.csrf_form_tag)
|
151
|
+
assert_equal(csrf_meta_tag, api.csrf_meta_tag)
|
137
152
|
end
|
138
153
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cuba-csrf
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Francesco Rodríguez
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-
|
12
|
+
date: 2015-02-07 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: cuba
|