rack-policy 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +5 -0
- data/CHANGELOG.md +8 -0
- data/README.md +24 -4
- data/lib/rack/policy.rb +4 -0
- data/lib/rack/policy/cookie_limiter.rb +22 -5
- data/lib/rack/policy/extensions.rb +18 -0
- data/lib/rack/policy/helpers.rb +16 -0
- data/lib/rack/policy/railtie.rb +11 -0
- data/lib/rack/policy/sinatra.rb +13 -0
- data/lib/rack/policy/version.rb +1 -1
- data/spec/cookie_limiter_spec.rb +60 -0
- data/spec/helpers_spec.rb +49 -0
- data/spec/spec_helper.rb +8 -3
- metadata +16 -10
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,11 @@
|
|
1
|
+
0.4.0 (July 8, 2012)
|
2
|
+
|
3
|
+
* Add helpers module to hold common rack apps extensions together with tests.
|
4
|
+
* Add extensions loader.
|
5
|
+
* Add view extensions for Rails 2, 3 & Sinatra.
|
6
|
+
* Add specs for finish method to verify headers removal.
|
7
|
+
* Add specs to test for consent token and environment variable setup.
|
8
|
+
|
1
9
|
0.3.0 (June 30, 2012)
|
2
10
|
|
3
11
|
* Add thread safety.
|
data/README.md
CHANGED
@@ -32,6 +32,8 @@ Rack::Policy::CookieLimiter, consent_token: 'allow_me'
|
|
32
32
|
|
33
33
|
The very same `consent_token` is used to toggle the limiter behaviour.
|
34
34
|
|
35
|
+
The `cookies_accpeted?` view helper method is automatically loaded for Rails, Sinatra & Padrino apps.
|
36
|
+
|
35
37
|
## Examples
|
36
38
|
|
37
39
|
Adding `Rack::Policy::CookieLimiter` to Rack applications
|
@@ -43,7 +45,7 @@ Adding `Rack::Policy::CookieLimiter` to Rack applications
|
|
43
45
|
require 'rack/policy'
|
44
46
|
|
45
47
|
class Application < Rails::Application
|
46
|
-
config.middleware.
|
48
|
+
config.middleware.insert_before ActionDispatch::Cookies, Rack::Policy::CookieLimiter, consent_token: 'rack.policy'
|
47
49
|
end
|
48
50
|
```
|
49
51
|
|
@@ -68,6 +70,22 @@ class CookiePolicyController < ApplicationController
|
|
68
70
|
end
|
69
71
|
```
|
70
72
|
|
73
|
+
Finally, in your view you can use helper method `cookies_accepted?` to display/toggle cookie information
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
<% cookies_accepted? do %>
|
77
|
+
Accepted Cookies!
|
78
|
+
<% end %>
|
79
|
+
|
80
|
+
or
|
81
|
+
|
82
|
+
<% if cookies_accepted? %>
|
83
|
+
Accepted Cookies!
|
84
|
+
<% else %>
|
85
|
+
Cookies Not Accepted!
|
86
|
+
<% end %>
|
87
|
+
```
|
88
|
+
|
71
89
|
### Rails 2.x
|
72
90
|
|
73
91
|
```ruby
|
@@ -79,7 +97,7 @@ Rails::Initializer.run do |config|
|
|
79
97
|
end
|
80
98
|
```
|
81
99
|
|
82
|
-
Set and unset cookie consent in similar way to Rails 3.x example.
|
100
|
+
Set and unset cookie consent in your controller and modify views logic in similar way to Rails 3.x example.
|
83
101
|
|
84
102
|
### Sinatra
|
85
103
|
|
@@ -101,6 +119,8 @@ get('/allow') { response.set_cookie 'rack.policy' }
|
|
101
119
|
get('/deny') { response.delete_cookie 'rack.policy' }
|
102
120
|
```
|
103
121
|
|
122
|
+
Similiar to Rails 3.x example you can use `cookies_accpeted?` helper to manage view logic related to cookie policy information.
|
123
|
+
|
104
124
|
### Padrino
|
105
125
|
|
106
126
|
```ruby
|
@@ -109,7 +129,7 @@ require 'padrino'
|
|
109
129
|
require 'rack/policy'
|
110
130
|
|
111
131
|
class MyApp < Padrino::Application
|
112
|
-
use Rack::Policy::CookieLimiter consent_token: 'rack.policy'
|
132
|
+
use Rack::Policy::CookieLimiter, consent_token: 'rack.policy'
|
113
133
|
end
|
114
134
|
```
|
115
135
|
|
@@ -119,7 +139,7 @@ end
|
|
119
139
|
#!/usr/bin/env rackup
|
120
140
|
require 'rack/policy'
|
121
141
|
|
122
|
-
use Rack::Policy::CookieLimiter consent_token: 'rack.policy'
|
142
|
+
use Rack::Policy::CookieLimiter, consent_token: 'rack.policy'
|
123
143
|
|
124
144
|
run lambda { |env| [200, {'Content-Type' => 'text/plain'}, "Hello, world!\n"] }
|
125
145
|
```
|
data/lib/rack/policy.rb
CHANGED
@@ -7,6 +7,10 @@ module Rack
|
|
7
7
|
|
8
8
|
autoload :CookieLimiter, 'rack/policy/cookie_limiter'
|
9
9
|
autoload :Version, 'rack/policy/version'
|
10
|
+
autoload :Helpers, 'rack/policy/helpers'
|
11
|
+
|
12
|
+
# Initialize Rack::Policy extensions within an application
|
13
|
+
require 'rack/policy/extensions'
|
10
14
|
|
11
15
|
end # Policy
|
12
16
|
end # Rack
|
@@ -12,7 +12,12 @@ module Rack
|
|
12
12
|
CONSENT_TOKEN = "cookie_limiter".freeze
|
13
13
|
|
14
14
|
attr_reader :app, :options
|
15
|
-
|
15
|
+
|
16
|
+
# The environment of the request
|
17
|
+
attr_reader :env
|
18
|
+
|
19
|
+
# HTTP message
|
20
|
+
attr_reader :status, :headers, :body
|
16
21
|
|
17
22
|
# @option options [String] :consent_token
|
18
23
|
#
|
@@ -33,11 +38,24 @@ module Rack
|
|
33
38
|
end
|
34
39
|
|
35
40
|
def call!(env)
|
36
|
-
|
41
|
+
@env = env
|
37
42
|
request = Rack::Request.new(env)
|
43
|
+
accepts?(request)
|
44
|
+
@status, @headers, @body = @app.call(env)
|
38
45
|
response = Rack::Response.new body, status, headers
|
39
46
|
clear_cookies!(request, response) unless allowed?(request)
|
40
|
-
finish
|
47
|
+
finish
|
48
|
+
end
|
49
|
+
|
50
|
+
# Identifies the approval of cookie policy inside rack app.
|
51
|
+
#
|
52
|
+
def accepts?(request)
|
53
|
+
if ( request.cookies.has_key?(consent_token.to_s) )
|
54
|
+
@env['rack-policy.consent'] = 'true'
|
55
|
+
else
|
56
|
+
@env.delete(HTTP_COOKIE) if @env[HTTP_COOKIE]
|
57
|
+
@env['rack-policy.consent'] = nil
|
58
|
+
end
|
41
59
|
end
|
42
60
|
|
43
61
|
# Returns `false` if the cookie policy disallows cookie storage
|
@@ -53,7 +71,7 @@ module Rack
|
|
53
71
|
end
|
54
72
|
|
55
73
|
# Finish http response with proper headers
|
56
|
-
def finish
|
74
|
+
def finish
|
57
75
|
if [204, 304].include?(status.to_i) || (status.to_i / 100 == 1)
|
58
76
|
headers.delete "Content-Length"
|
59
77
|
headers.delete "Content-Type"
|
@@ -86,7 +104,6 @@ module Rack
|
|
86
104
|
def clear_cookies!(request, response)
|
87
105
|
cookies = parse_cookies
|
88
106
|
headers.delete(SET_COOKIE)
|
89
|
-
request.env.delete(HTTP_COOKIE)
|
90
107
|
revalidate_cache!
|
91
108
|
|
92
109
|
cookies.merge(request.cookies).each do |key, value|
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
# Autoload Rails extensions
|
4
|
+
if defined?(Rails) && Rails.respond_to?(:application)
|
5
|
+
# Rails 3
|
6
|
+
require 'rack/policy/railtie'
|
7
|
+
|
8
|
+
elsif defined?(Rails::Initializer)
|
9
|
+
# Rails 2.3
|
10
|
+
require 'action/view/base'
|
11
|
+
|
12
|
+
ActionView::Base.send :include, Rack::Policy::Helpers
|
13
|
+
elsif defined?(Sinatra)
|
14
|
+
require 'rack/policy/sinatra'
|
15
|
+
|
16
|
+
elsif defined?(Padrino)
|
17
|
+
require 'padrino-core'
|
18
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
module Rack
|
4
|
+
module Policy
|
5
|
+
module Helpers
|
6
|
+
|
7
|
+
def cookies_accepted?
|
8
|
+
return false unless request.env.has_key? 'rack-policy.consent'
|
9
|
+
accepted = !request.env['rack-policy.consent'].nil?
|
10
|
+
yield if block_given? && accepted
|
11
|
+
accepted
|
12
|
+
end
|
13
|
+
|
14
|
+
end # Helpers
|
15
|
+
end # Policy
|
16
|
+
end # Rack
|
data/lib/rack/policy/version.rb
CHANGED
data/spec/cookie_limiter_spec.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
1
3
|
require File.expand_path('../spec_helper.rb', __FILE__)
|
2
4
|
|
3
5
|
describe Rack::Policy::CookieLimiter do
|
@@ -29,6 +31,16 @@ describe Rack::Policy::CookieLimiter do
|
|
29
31
|
last_response.headers['Set-Cookie'].should be_nil
|
30
32
|
end
|
31
33
|
|
34
|
+
it 'clears all the cookies' do
|
35
|
+
mock_app {
|
36
|
+
use Rack::Policy::CookieLimiter, :consent_token => 'consent'
|
37
|
+
run DummyApp
|
38
|
+
}
|
39
|
+
set_cookie ["foo=1", "bar=2"]
|
40
|
+
request '/'
|
41
|
+
last_request.cookies.should == {}
|
42
|
+
end
|
43
|
+
|
32
44
|
it 'revalidates caches' do
|
33
45
|
mock_app {
|
34
46
|
use Rack::Policy::CookieLimiter
|
@@ -59,6 +71,39 @@ describe Rack::Policy::CookieLimiter do
|
|
59
71
|
get '/'
|
60
72
|
last_response.headers['Set-Cookie'].should =~ /github.com=bot/
|
61
73
|
end
|
74
|
+
|
75
|
+
context 'token' do
|
76
|
+
it 'preserves all the cookies if custom consent token present' do
|
77
|
+
mock_app {
|
78
|
+
use Rack::Policy::CookieLimiter, :consent_token => 'consent'
|
79
|
+
run DummyApp
|
80
|
+
}
|
81
|
+
set_cookie ["foo=1", "bar=2", "consent=true"]
|
82
|
+
request '/'
|
83
|
+
last_request.cookies.should == {'foo'=>'1', 'bar'=>'2', 'consent'=>'true'}
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
context 'accepts?' do
|
89
|
+
it "sets environment consent variable" do
|
90
|
+
mock_app {
|
91
|
+
use Rack::Policy::CookieLimiter
|
92
|
+
run DummyApp
|
93
|
+
}
|
94
|
+
request '/'
|
95
|
+
last_request.env.should have_key('rack-policy.consent')
|
96
|
+
end
|
97
|
+
|
98
|
+
it "assigns value for the consent variable" do
|
99
|
+
mock_app {
|
100
|
+
use Rack::Policy::CookieLimiter, :consent_token => 'consent'
|
101
|
+
run DummyApp
|
102
|
+
}
|
103
|
+
set_cookie ["consent=true"]
|
104
|
+
request '/'
|
105
|
+
last_request.env['rack-policy.consent'].should == 'true'
|
106
|
+
end
|
62
107
|
end
|
63
108
|
|
64
109
|
context 'finish response' do
|
@@ -70,6 +115,21 @@ describe Rack::Policy::CookieLimiter do
|
|
70
115
|
head '/'
|
71
116
|
last_response.should be_ok
|
72
117
|
end
|
118
|
+
|
119
|
+
it "strips content headers for no content" do
|
120
|
+
mock_app with_status(204)
|
121
|
+
get '/'
|
122
|
+
last_response.headers['Content-Type'].should be_nil
|
123
|
+
last_response.headers['Content-Length'].should be_nil
|
124
|
+
last_response.body.should be_empty
|
125
|
+
end
|
126
|
+
|
127
|
+
it "strips headers for information request" do
|
128
|
+
mock_app with_status(102)
|
129
|
+
get '/'
|
130
|
+
last_response.headers['Content-Length'].should be_nil
|
131
|
+
last_response.body.should be_empty
|
132
|
+
end
|
73
133
|
end
|
74
134
|
|
75
135
|
end # Rack::Policy::CookieLimiter
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
require File.expand_path('../spec_helper.rb', __FILE__)
|
4
|
+
|
5
|
+
class HelperTest
|
6
|
+
attr_accessor :request
|
7
|
+
include Rack::Policy::Helpers
|
8
|
+
|
9
|
+
def initialize
|
10
|
+
@request = HelperTest::Request.new
|
11
|
+
end
|
12
|
+
|
13
|
+
class Request
|
14
|
+
attr_reader :env
|
15
|
+
def initialize; @env = {}; end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
describe Rack::Policy::Helpers do
|
20
|
+
|
21
|
+
let(:helper_test) { HelperTest.new }
|
22
|
+
|
23
|
+
before do
|
24
|
+
helper_test.request.env.stub(:has_key?).and_return true
|
25
|
+
end
|
26
|
+
|
27
|
+
it "guards against missing key" do
|
28
|
+
helper_test.request.env.stub(:has_key?).and_return false
|
29
|
+
helper_test.cookies_accepted?.should be_false
|
30
|
+
end
|
31
|
+
|
32
|
+
it "doesn't accept cookies" do
|
33
|
+
helper_test.request.env.stub(:[]).with('rack-policy.consent') { nil }
|
34
|
+
helper_test.cookies_accepted?.should be_false
|
35
|
+
end
|
36
|
+
|
37
|
+
it "accepts cookies" do
|
38
|
+
helper_test.request.env.stub(:[]).with('rack-policy.consent') { 'true' }
|
39
|
+
helper_test.cookies_accepted?.should be_true
|
40
|
+
end
|
41
|
+
|
42
|
+
it "yields to the block" do
|
43
|
+
helper_test.request.env.stub(:[]).with('rack-policy.consent') { 'true' }
|
44
|
+
block = Proc.new { 'Accepted'}
|
45
|
+
helper_test.should_receive(:cookies_accepted?).and_yield(&block)
|
46
|
+
helper_test.cookies_accepted?(&block)
|
47
|
+
end
|
48
|
+
|
49
|
+
end # Rack::Policy::Helpers
|
data/spec/spec_helper.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
1
3
|
require 'rubygems'
|
2
4
|
|
3
5
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
@@ -19,12 +21,11 @@ module TestHelpers
|
|
19
21
|
@app || mock_app(DummyApp)
|
20
22
|
end
|
21
23
|
|
22
|
-
def mock_app(app=nil, &block)
|
24
|
+
def mock_app(app=nil, opts={}, &block)
|
23
25
|
app = block if app.nil? and block.arity == 1
|
24
26
|
if app
|
25
|
-
klass = described_class
|
26
27
|
mock_app do
|
27
|
-
use
|
28
|
+
use Rack::Policy::CookieLimiter, opts
|
28
29
|
run app
|
29
30
|
end
|
30
31
|
else
|
@@ -35,6 +36,10 @@ module TestHelpers
|
|
35
36
|
def with_headers(headers)
|
36
37
|
proc { [200, {'Content-Type' => 'text/plain' }.merge(headers), ['ok']] }
|
37
38
|
end
|
39
|
+
|
40
|
+
def with_status(status=nil)
|
41
|
+
proc { [status || 200, {'Content-Type' => 'text/plain' }, ['ok']] }
|
42
|
+
end
|
38
43
|
end
|
39
44
|
|
40
45
|
RSpec.configure do |config|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-policy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-07-08 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rack
|
16
|
-
requirement: &
|
16
|
+
requirement: &2152743460 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '1.1'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *2152743460
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rack-test
|
27
|
-
requirement: &
|
27
|
+
requirement: &2152743000 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *2152743000
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rspec
|
38
|
-
requirement: &
|
38
|
+
requirement: &2152742320 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *2152742320
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: rake
|
49
|
-
requirement: &
|
49
|
+
requirement: &2152741600 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,7 +54,7 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *2152741600
|
58
58
|
description: This is Rack middleware that makes your app compliant with the 'EU ePrivacy
|
59
59
|
Directive'
|
60
60
|
email:
|
@@ -78,9 +78,14 @@ files:
|
|
78
78
|
- lib/rack-policy.rb
|
79
79
|
- lib/rack/policy.rb
|
80
80
|
- lib/rack/policy/cookie_limiter.rb
|
81
|
+
- lib/rack/policy/extensions.rb
|
82
|
+
- lib/rack/policy/helpers.rb
|
83
|
+
- lib/rack/policy/railtie.rb
|
84
|
+
- lib/rack/policy/sinatra.rb
|
81
85
|
- lib/rack/policy/version.rb
|
82
86
|
- rack-policy.gemspec
|
83
87
|
- spec/cookie_limiter_spec.rb
|
88
|
+
- spec/helpers_spec.rb
|
84
89
|
- spec/spec_helper.rb
|
85
90
|
homepage: https://github.com/peter-murach/rack-policy
|
86
91
|
licenses: []
|
@@ -110,4 +115,5 @@ summary: This is Rack middleware that makes your app compliant with the 'EU ePri
|
|
110
115
|
stored on his machine.
|
111
116
|
test_files:
|
112
117
|
- spec/cookie_limiter_spec.rb
|
118
|
+
- spec/helpers_spec.rb
|
113
119
|
- spec/spec_helper.rb
|