cuba-csrf 0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ae35f030c53ae173c8271c445993701b070d7565
4
+ data.tar.gz: 5b686325d6e707461182e3be78c57b4f41b9fa84
5
+ SHA512:
6
+ metadata.gz: 19fe159769620cb10305d0c2bd2c7b729ddd5d1bcea8ea8c8eaa2a5f4d1e7f583ecb69dd6abb5f45ded7fd45d8dff7031741825a00a90cbffa925d8936d5469c
7
+ data.tar.gz: 6e60f4477fcc17a4ab6ad57db483c5d1c241ff3a4eb59eb5b375157d3547fafa05a0b5751902ac0d8ac0597710dc47e94f6483cbb1fa491362491ba4a917afa6
data/.gems ADDED
@@ -0,0 +1,3 @@
1
+ cuba -v 3.3.0
2
+ cutest -v 1.2.2
3
+ rack-test -v 0.6.3
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2015-Present Francesco Rodríguez, Mayn Kjær
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,91 @@
1
+ cuba-csrf
2
+ =========
3
+
4
+ [Cross-Site Request Forgery][csrf] protection for Cuba applications.
5
+
6
+ Description
7
+ -----------
8
+
9
+ This library adopts the [Synchronizer Token][pattern] pattern.
10
+
11
+ This scheme protects the application by including a token in the
12
+ HTML forms of your application. This token is stored as a random
13
+ string in the user's current session. When a request reaches the
14
+ application, it verifies the received token with the token in the
15
+ session. This scheme ensures that the user actually intended to
16
+ submit the desired requests.
17
+
18
+ By default, `GET` and `HEAD` requests are not protected since they
19
+ don't have side effects like writing to the database and don't leak
20
+ sensitive information.
21
+
22
+ Usage
23
+ -----
24
+
25
+ To enable `Cuba::CSRF`, do:
26
+
27
+ ```ruby
28
+ require "cuba"
29
+ require "cuba/csrf"
30
+
31
+ Cuba.plugin(Cuba::CSRF)
32
+
33
+ Cuba.define do
34
+ begin
35
+ protect_from_forgery!
36
+ rescue Cuba::CSRF::InvalidToken
37
+ # In this case, if the verification fails
38
+ # we want to reset user's session.
39
+ session.clear
40
+
41
+ res.status = 403
42
+ res.write("Not Authorized")
43
+ halt(res.finish)
44
+ end
45
+ end
46
+ ```
47
+
48
+ Then, use the `csrf_tag` helper method to include
49
+ the security token in your HTML forms:
50
+
51
+ ```html
52
+ <form action="/account/delete" method="post">
53
+ {{ csrf_tag }}
54
+ </form>
55
+ ```
56
+
57
+ BREACH
58
+ ------
59
+
60
+ BREACH is a security exploit against HTTPS when using [HTTP compression][compression]
61
+ (GZIP/DEFLATE). This means that if your page is **served with HTTP compression
62
+ enabled** and **reflects user input**, an attacker can recover sensitive data
63
+ from an HTTP response body (e.g. a CSRF token).
64
+
65
+ There are two effective ways to mitigate BREACH: disable
66
+ HTTP compression or randomize secrets per request.
67
+
68
+ If it's possible, disable HTTP compression. In Nginx, you can use
69
+ the `gzip off` directive.
70
+
71
+ This plugin doesn't generate or mask CSRF tokens per request. This means
72
+ that if you plan to use HTTP compression, your application might be vulnerable
73
+ to BREACH.
74
+
75
+ We designed this library to fit our use case. We don't have HTTP compression
76
+ enabled because we also have other sensitive information apart from the CSRF
77
+ tokens that would require additional masking. Maybe we will add support for
78
+ masking tokens per request in the future.
79
+
80
+ For more information about BREACH, see <http://breachattack.com>.
81
+
82
+ Installation
83
+ ------------
84
+
85
+ ```
86
+ $ gem install cuba-csrf
87
+ ```
88
+
89
+ [compression]: https://en.wikipedia.org/wiki/HTTP_compression
90
+ [csrf]: https://www.owasp.org/index.php/Cross-Site_Request_Forgery_(CSRF)
91
+ [pattern]: https://www.owasp.org/index.php/Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet#General_Recommendation:_Synchronizer_Token_Pattern
data/cuba-csrf.gemspec ADDED
@@ -0,0 +1,15 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "cuba-csrf"
3
+ s.version = "0.0.1"
4
+ s.summary = "CSRF protection for Cuba applications."
5
+ s.description = s.summary
6
+ s.authors = ["Francesco Rodríguez", "Mayn Kjær"]
7
+ s.email = ["frodsan@me.com", "mayn.kjaer@gmail.com"]
8
+ s.homepage = "https://github.com/harmoni/cuba-csrf"
9
+ s.license = "MIT"
10
+
11
+ s.files = `git ls-files`.split("\n")
12
+
13
+ s.add_dependency "cuba"
14
+ s.add_development_dependency "cutest"
15
+ end
data/lib/cuba/csrf.rb ADDED
@@ -0,0 +1,22 @@
1
+ require "cuba"
2
+ require "securerandom"
3
+
4
+ module Cuba::CSRF
5
+ InvalidToken = Class.new(StandardError)
6
+
7
+ def protect_from_forgery!
8
+ raise InvalidToken unless _csrf_safe?
9
+ end
10
+
11
+ def csrf_token
12
+ session[:csrf_token] ||= SecureRandom.base64(32)
13
+ end
14
+
15
+ def csrf_tag
16
+ %Q(<input type="hidden" name="csrf_token" value="#{csrf_token}">)
17
+ end
18
+
19
+ def _csrf_safe?
20
+ req.get? || req.head? || req[:csrf_token] == csrf_token
21
+ end
22
+ end
data/makefile ADDED
@@ -0,0 +1,8 @@
1
+ DEFAULT_GOAL := test
2
+ .PHONY: test
3
+
4
+ gem:
5
+ gem build cuba-csrf.gemspec
6
+
7
+ test:
8
+ cutest test/*.rb
data/test/csrf.rb ADDED
@@ -0,0 +1,142 @@
1
+ require "cutest"
2
+ require "cuba"
3
+ require "cuba/test"
4
+ require_relative "../lib/cuba/csrf"
5
+
6
+ def assert_no_raise
7
+ yield
8
+ success
9
+ end
10
+
11
+ scope do
12
+ setup do
13
+ Cuba.reset!
14
+
15
+ Cuba.use(Rack::Session::Cookie, secret: "_this_must_be_secret")
16
+ Cuba.plugin(Cuba::CSRF)
17
+ end
18
+
19
+ test "safe http methods" do
20
+ Cuba.define do
21
+ protect_from_forgery!
22
+
23
+ on default do
24
+ res.write("safe")
25
+ end
26
+ end
27
+
28
+ assert_no_raise do
29
+ get "/"
30
+ head "/"
31
+ end
32
+ end
33
+
34
+ test "invalid csrf param" do
35
+ Cuba.define do
36
+ protect_from_forgery!
37
+ end
38
+
39
+ assert_raise(Cuba::CSRF::InvalidToken) do
40
+ post "/", "csrf_token" => nil
41
+ end
42
+
43
+ assert_raise(Cuba::CSRF::InvalidToken) do
44
+ post "/", "csrf_token" => ""
45
+ end
46
+
47
+ assert_raise(Cuba::CSRF::InvalidToken) do
48
+ post "/", "csrf_token" => "nonsense"
49
+ end
50
+ end
51
+
52
+ test "valid csrf param" do
53
+ Cuba.define do
54
+ protect_from_forgery!
55
+
56
+ on get do
57
+ res.write(csrf_token)
58
+ end
59
+
60
+ on post do
61
+ res.write("safe")
62
+ end
63
+ end
64
+
65
+ get "/"
66
+
67
+ csrf_token = last_response.body
68
+
69
+ assert(!csrf_token.empty?)
70
+
71
+ assert_no_raise do
72
+ post "/", "csrf_token" => csrf_token
73
+ end
74
+ end
75
+
76
+ test "sub app raises too" do
77
+ class App < Cuba
78
+ define do
79
+ on post do
80
+ res.write("unsafe")
81
+ end
82
+ end
83
+ end
84
+
85
+ Cuba.define do
86
+ protect_from_forgery!
87
+
88
+ on "app" do
89
+ run(App)
90
+ end
91
+ end
92
+
93
+ assert_raise(Cuba::CSRF::InvalidToken) do
94
+ post "/app"
95
+ end
96
+ end
97
+
98
+ test "only sub app" do
99
+ class App < Cuba
100
+ define do
101
+ protect_from_forgery!
102
+
103
+ on post do
104
+ res.write("unsafe")
105
+ end
106
+ end
107
+ end
108
+
109
+ Cuba.define do
110
+ on "app" do
111
+ run(App)
112
+ end
113
+
114
+ on default do
115
+ res.write("safe")
116
+ end
117
+ end
118
+
119
+ assert_no_raise do
120
+ post "/"
121
+ end
122
+
123
+ assert_raise(Cuba::CSRF::InvalidToken) do
124
+ post "/app"
125
+ end
126
+ end
127
+
128
+ test "html helpers" do
129
+ Cuba.plugin(Cuba::CSRF)
130
+
131
+ class Api < Cuba
132
+ def session
133
+ @session ||= {}
134
+ end
135
+ end
136
+
137
+ api = Api.new
138
+ csrf_tag = %Q(<input type="hidden" name="csrf_token" value="#{api.csrf_token}">)
139
+
140
+ assert_equal(csrf_tag, api.csrf_tag)
141
+ end
142
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cuba-csrf
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Francesco Rodríguez
8
+ - Mayn Kjær
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-01-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: cuba
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: cutest
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ description: CSRF protection for Cuba applications.
43
+ email:
44
+ - frodsan@me.com
45
+ - mayn.kjaer@gmail.com
46
+ executables: []
47
+ extensions: []
48
+ extra_rdoc_files: []
49
+ files:
50
+ - ".gems"
51
+ - LICENSE
52
+ - README.md
53
+ - cuba-csrf.gemspec
54
+ - lib/cuba/csrf.rb
55
+ - makefile
56
+ - test/csrf.rb
57
+ homepage: https://github.com/harmoni/cuba-csrf
58
+ licenses:
59
+ - MIT
60
+ metadata: {}
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubyforge_project:
77
+ rubygems_version: 2.4.5
78
+ signing_key:
79
+ specification_version: 4
80
+ summary: CSRF protection for Cuba applications.
81
+ test_files: []