perimeter_x 1.0.5.pre.alpha → 1.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +3 -1
- data/Dockerfile +4 -2
- data/Gemfile +1 -1
- data/Gemfile.lock +44 -2
- data/LICENSE.txt +9 -12
- data/Rakefile +9 -2
- data/changelog.md +16 -0
- data/examples/app/controllers/home_controller.rb +9 -0
- data/examples/app/views/home/index.html.erb.dist +20 -0
- data/examples/config/initializers/perimeterx.rb.dist +8 -0
- data/examples/{routes.rb → config/routes.rb} +0 -0
- data/lib/perimeter_x.rb +109 -33
- data/lib/perimeterx/configuration.rb +24 -17
- data/lib/perimeterx/internal/clients/perimeter_x_activity_client.rb +92 -0
- data/lib/perimeterx/internal/clients/perimeter_x_risk_client.rb +28 -0
- data/lib/perimeterx/internal/exceptions/px_cookie_decryption_exception.rb +5 -0
- data/lib/perimeterx/internal/perimeter_x_context.rb +67 -57
- data/lib/perimeterx/internal/perimeter_x_cookie.rb +140 -0
- data/lib/perimeterx/internal/perimeter_x_cookie_v1.rb +42 -0
- data/lib/perimeterx/internal/perimeter_x_cookie_v3.rb +37 -0
- data/lib/perimeterx/internal/validators/perimeter_x_captcha_validator.rb +65 -0
- data/lib/perimeterx/internal/validators/perimeter_x_cookie_validator.rb +70 -0
- data/lib/perimeterx/internal/validators/perimeter_x_s2s_validator.rb +114 -0
- data/lib/perimeterx/utils/px_constants.rb +44 -0
- data/lib/perimeterx/utils/px_http_client.rb +47 -26
- data/lib/perimeterx/utils/px_logger.rb +12 -6
- data/lib/perimeterx/utils/px_template_factory.rb +31 -0
- data/lib/perimeterx/utils/templates/block.mustache +146 -0
- data/lib/perimeterx/utils/templates/captcha.mustache +185 -0
- data/lib/perimeterx/version.rb +2 -2
- data/perimeter_x.gemspec +6 -1
- data/readme.md +218 -34
- metadata +89 -10
- data/bin/console +0 -14
- data/bin/setup +0 -8
- data/examples/home_controller.rb.dist +0 -23
- data/lib/perimeterx/internal/perimeter_x_risk_client.rb +0 -29
- data/lib/perimeterx/internal/perimeter_x_s2s_validator.rb +0 -68
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b524739e4d2df489ebc30c582912eadb7eb26681
|
4
|
+
data.tar.gz: 6f70604be366db4cb4520d6b1da6ac8bd5d8069f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 30fe7157555b9d62a8f0e66237fd2c6bce3a2ecf448d9f81ef98d178570e1df0ab5941c4a26323b3bf279390c3016d58c0f6a9414db625b33057960d3aada3f9
|
7
|
+
data.tar.gz: 2fd1be8ddc39fe6544a5f6b1d62043480c22a6b8a4eed2b684031a963b61ec89301958f2d4bffa00506251ee50ffa65f5cd0483b0441d092d8367528be114e5e
|
data/.gitignore
CHANGED
@@ -3,13 +3,15 @@ capybara-*.html
|
|
3
3
|
.rspec
|
4
4
|
/log
|
5
5
|
/tmp
|
6
|
+
/bin
|
6
7
|
/dev
|
7
8
|
/db/*.sqlite3
|
8
9
|
/db/*.sqlite3-journal
|
9
10
|
/public/system
|
10
11
|
/coverage/
|
11
12
|
/spec/tmp
|
12
|
-
examples/
|
13
|
+
examples/config/initializers/perimeterx.rb
|
14
|
+
examples/app/views/home/index.html.erb
|
13
15
|
**.orig
|
14
16
|
*.gem
|
15
17
|
rerun.txt
|
data/Dockerfile
CHANGED
@@ -37,12 +37,14 @@ RUN /bin/bash -l -c "gem install bundler"
|
|
37
37
|
RUN /bin/bash -l -c "gem install rails -v 4.2.0"
|
38
38
|
RUN mkdir -p /tmp/ruby_sandbox
|
39
39
|
WORKDIR /tmp/ruby_sandbox
|
40
|
+
RUN git clone https://github.com/PerimeterX/perimeterx-ruby-sdk.git
|
40
41
|
RUN /bin/bash -l -c "rails new webapp"
|
41
42
|
WORKDIR /tmp/ruby_sandbox/webapp
|
42
43
|
RUN /bin/bash -l -c "rails generate controller home index"
|
43
44
|
WORKDIR /tmp/ruby_sandbox/webapp
|
44
45
|
EXPOSE 3000
|
45
|
-
|
46
|
+
# TODO: make it take the files from git
|
47
|
+
RUN sed -i '2i gem "perimeter_x", :path => "/tmp/ruby_sandbox/perimeterx-ruby-sdk"' /tmp/ruby_sandbox/webapp/Gemfile
|
46
48
|
RUN /bin/bash -l -c "bundler update"
|
47
|
-
|
49
|
+
COPY ./examples/ /tmp/ruby_sandbox/webapp
|
48
50
|
CMD ["/bin/bash", "-l", "-c", "rails server -b 0.0.0.0;"]
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,13 +1,55 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
perimeter_x (1.0.3)
|
5
|
+
activesupport (>= 4.2.0)
|
6
|
+
httpclient (= 2.8.2.4)
|
7
|
+
mustache (~> 1.0, >= 1.0.3)
|
8
|
+
|
1
9
|
GEM
|
2
10
|
remote: https://rubygems.org/
|
3
11
|
specs:
|
4
|
-
|
12
|
+
activesupport (5.0.2)
|
13
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
14
|
+
i18n (~> 0.7)
|
15
|
+
minitest (~> 5.1)
|
16
|
+
tzinfo (~> 1.1)
|
17
|
+
concurrent-ruby (1.0.5)
|
18
|
+
diff-lcs (1.3)
|
19
|
+
httpclient (2.8.2.4)
|
20
|
+
i18n (0.8.1)
|
21
|
+
metaclass (0.0.4)
|
22
|
+
minitest (5.10.1)
|
23
|
+
mocha (1.2.1)
|
24
|
+
metaclass (~> 0.0.1)
|
25
|
+
mustache (1.0.4)
|
26
|
+
rake (10.4.2)
|
27
|
+
rspec (3.5.0)
|
28
|
+
rspec-core (~> 3.5.0)
|
29
|
+
rspec-expectations (~> 3.5.0)
|
30
|
+
rspec-mocks (~> 3.5.0)
|
31
|
+
rspec-core (3.5.4)
|
32
|
+
rspec-support (~> 3.5.0)
|
33
|
+
rspec-expectations (3.5.0)
|
34
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
35
|
+
rspec-support (~> 3.5.0)
|
36
|
+
rspec-mocks (3.5.0)
|
37
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
38
|
+
rspec-support (~> 3.5.0)
|
39
|
+
rspec-support (3.5.0)
|
40
|
+
thread_safe (0.3.6)
|
41
|
+
tzinfo (1.2.3)
|
42
|
+
thread_safe (~> 0.1)
|
5
43
|
|
6
44
|
PLATFORMS
|
7
45
|
ruby
|
8
46
|
|
9
47
|
DEPENDENCIES
|
10
|
-
|
48
|
+
bundler (~> 1.14)
|
49
|
+
mocha (~> 1.2, >= 1.2.1)
|
50
|
+
perimeter_x!
|
51
|
+
rake (~> 10.0)
|
52
|
+
rspec (~> 3.0)
|
11
53
|
|
12
54
|
BUNDLED WITH
|
13
55
|
1.14.6
|
data/LICENSE.txt
CHANGED
@@ -1,6 +1,4 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
Copyright (c) 2017 nitzanpx
|
1
|
+
Copyright © 2016 PerimeterX, Inc.
|
4
2
|
|
5
3
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
4
|
of this software and associated documentation files (the "Software"), to deal
|
@@ -9,13 +7,12 @@ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
7
|
copies of the Software, and to permit persons to whom the Software is
|
10
8
|
furnished to do so, subject to the following conditions:
|
11
9
|
|
12
|
-
The above copyright notice and this permission notice shall be included in
|
13
|
-
all copies or substantial portions of the Software.
|
10
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
14
11
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
21
|
-
THE SOFTWARE.
|
12
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
13
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
14
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
15
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
16
|
+
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
17
|
+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
18
|
+
USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
CHANGED
data/changelog.md
CHANGED
@@ -0,0 +1,16 @@
|
|
1
|
+
# Change Log
|
2
|
+
|
3
|
+
All notable changes to this project will be documented in this file.
|
4
|
+
|
5
|
+
The format is based on [Keep a Changelog](http://keepachangelog.com/)
|
6
|
+
and this project adheres to [Semantic Versioning](http://semver.org/).
|
7
|
+
|
8
|
+
## [1.0.5] - 2017-05-07
|
9
|
+
### Fixed
|
10
|
+
- Added request format into context for custom callbacks
|
11
|
+
|
12
|
+
## [1.0.4] - 2017-04-27
|
13
|
+
### Fixed
|
14
|
+
- Constants on px_constants
|
15
|
+
- Cookie Validation flow when cookie score was over the configured threshold
|
16
|
+
- Using symbols instead of strings for requests body
|
@@ -0,0 +1,20 @@
|
|
1
|
+
<h1>Home#index</h1>
|
2
|
+
<p>Find me in app/views/home/index.html.erb</p>
|
3
|
+
|
4
|
+
<script type="text/javascript">
|
5
|
+
(function(){
|
6
|
+
window._pxAppId ='APP_ID';
|
7
|
+
// Custom parameters
|
8
|
+
// window._pxParam1 = "<param1>";
|
9
|
+
var p = document.getElementsByTagName('script')[0],
|
10
|
+
s = document.createElement('script');
|
11
|
+
s.async = 1;
|
12
|
+
s.src = '//client.perimeterx.net/APP_ID/main.min.js';
|
13
|
+
p.parentNode.insertBefore(s,p);
|
14
|
+
}());
|
15
|
+
</script>
|
16
|
+
<noscript>
|
17
|
+
<div style="position:fixed; top:0; left:0; display:none" width="1" height="1">
|
18
|
+
<img src="//collector-APP_ID.perimeterx.net/api/v1/collector/noScript.gif?appId=APP_ID">
|
19
|
+
</div>
|
20
|
+
</noscript>
|
File without changes
|
data/lib/perimeter_x.rb
CHANGED
@@ -1,69 +1,145 @@
|
|
1
1
|
require 'perimeterx/configuration'
|
2
2
|
require 'perimeterx/utils/px_logger'
|
3
|
+
require 'perimeterx/utils/px_constants'
|
3
4
|
require 'perimeterx/utils/px_http_client'
|
5
|
+
require 'perimeterx/utils/px_template_factory'
|
4
6
|
require 'perimeterx/internal/perimeter_x_context'
|
5
|
-
require 'perimeterx/internal/
|
7
|
+
require 'perimeterx/internal/clients/perimeter_x_activity_client'
|
8
|
+
require 'perimeterx/internal/validators/perimeter_x_s2s_validator'
|
9
|
+
require 'perimeterx/internal/validators/perimeter_x_cookie_validator'
|
10
|
+
require 'perimeterx/internal/validators/perimeter_x_captcha_validator'
|
6
11
|
|
7
|
-
module
|
8
|
-
class PxModule
|
9
|
-
L = PxLogger.instance
|
12
|
+
module PxModule
|
10
13
|
|
11
|
-
|
12
|
-
|
14
|
+
# Module expose API
|
15
|
+
def px_verify_request
|
16
|
+
verified, px_ctx = PerimeterX.instance.verify(env)
|
17
|
+
|
18
|
+
# Invalidate _pxCaptcha, can be done only on the controller level
|
19
|
+
cookies[:_pxCaptcha] = { value: "", expires: -1.minutes.from_now }
|
20
|
+
|
21
|
+
if (!verified)
|
22
|
+
# In case custon block handler exists
|
23
|
+
if (PerimeterX.instance.px_config.key?(:custom_block_handler))
|
24
|
+
PerimeterX.instance.px_config[:logger].debug("PxModule[px_verify_request]: custom_block_handler triggered")
|
25
|
+
return instance_exec(px_ctx, &PerimeterX.instance.px_config[:custom_block_handler])
|
26
|
+
else
|
27
|
+
# Generate template
|
28
|
+
PerimeterX.instance.px_config[:logger].debug("PxModule[px_verify_request]: sending default block page")
|
29
|
+
html = PxTemplateFactory.get_template(px_ctx, PerimeterX.instance.px_config)
|
30
|
+
response.headers["Content-Type"] = "text/html"
|
31
|
+
response.status = 403
|
32
|
+
render :html => html
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
return verified
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.configure(params)
|
40
|
+
@px_instance = PerimeterX.configure(params)
|
41
|
+
end
|
42
|
+
|
43
|
+
|
44
|
+
# PerimtereX Module
|
45
|
+
class PerimeterX
|
46
|
+
@@__instance = nil
|
47
|
+
@@mutex = Mutex.new
|
13
48
|
|
14
49
|
attr_reader :px_config
|
15
50
|
attr_accessor :px_http_client
|
51
|
+
attr_accessor :px_activity_client
|
16
52
|
|
17
|
-
|
18
|
-
|
19
|
-
@@
|
20
|
-
|
21
|
-
@@
|
53
|
+
#Static methods
|
54
|
+
def self.configure(params)
|
55
|
+
return true if @@__instance
|
56
|
+
@@mutex.synchronize {
|
57
|
+
return @@__instance if @@__instance
|
58
|
+
@@__instance = new(params)
|
22
59
|
}
|
23
|
-
|
60
|
+
return true
|
24
61
|
end
|
25
62
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
@px_config = Configuration.new(params).configuration
|
30
|
-
@px_http_client = PxHttpClient.new(@px_config)
|
63
|
+
def self.instance
|
64
|
+
return @@__instance if !@@__instance.nil?
|
65
|
+
raise Exception.new("Please initialize perimeter x first")
|
31
66
|
end
|
32
67
|
|
33
|
-
|
68
|
+
|
69
|
+
#Instance Methods
|
70
|
+
def verify(env)
|
34
71
|
begin
|
35
|
-
|
72
|
+
@logger.debug("PerimeterX[pxVerify]")
|
36
73
|
req = ActionDispatch::Request.new(env)
|
37
|
-
|
38
|
-
|
39
|
-
L.warn("Module is disabled")
|
74
|
+
if (!@px_config[:module_enabled])
|
75
|
+
@logger.warn("Module is disabled")
|
40
76
|
return true
|
41
77
|
end
|
42
|
-
|
43
78
|
px_ctx = PerimeterXContext.new(@px_config, req)
|
44
|
-
px_ctx.context[:s2s_call_reason] = "no_cookie"
|
45
79
|
|
46
|
-
|
47
|
-
px_ctx =
|
80
|
+
# Captcha phase
|
81
|
+
captcha_verified, px_ctx = @px_captcha_validator.verify(px_ctx)
|
82
|
+
if (captcha_verified)
|
83
|
+
return handle_verification(px_ctx)
|
84
|
+
end
|
85
|
+
|
86
|
+
# Cookie phase
|
87
|
+
cookie_verified, px_ctx = @px_cookie_validator.verify(px_ctx)
|
88
|
+
if (!cookie_verified)
|
89
|
+
@px_s2s_validator.verify(px_ctx)
|
90
|
+
end
|
48
91
|
|
49
|
-
if (px_config.key?(
|
50
|
-
return px_config[
|
92
|
+
if (@px_config.key?(:custom_verification_handler))
|
93
|
+
return @px_config[:custom_verification_handler].call(px_ctx.context)
|
51
94
|
else
|
52
95
|
return handle_verification(px_ctx)
|
53
96
|
end
|
54
97
|
rescue Exception => e
|
55
|
-
|
98
|
+
@logger.error("#{e.backtrace.first}: #{e.message} (#{e.class})")
|
99
|
+
e.backtrace.drop(1).map { |s| @logger.error("\t#{s}") }
|
56
100
|
return true
|
57
101
|
end
|
58
102
|
end
|
59
103
|
|
60
|
-
|
104
|
+
private def initialize(params)
|
105
|
+
@px_config = Configuration.new(params).configuration
|
106
|
+
@logger = @px_config[:logger]
|
107
|
+
@px_http_client = PxHttpClient.new(@px_config)
|
108
|
+
|
109
|
+
@px_activity_client = PerimeterxActivitiesClient.new(@px_config, @px_http_client)
|
110
|
+
|
111
|
+
@px_cookie_validator = PerimeterxCookieValidator.new(@px_config)
|
112
|
+
@px_s2s_validator = PerimeterxS2SValidator.new(@px_config, @px_http_client)
|
113
|
+
@px_captcha_validator = PerimeterxCaptchaValidator.new(@px_config, @px_http_client)
|
114
|
+
@logger.debug("PerimeterX[initialize]")
|
115
|
+
end
|
116
|
+
|
61
117
|
private def handle_verification(px_ctx)
|
62
|
-
|
63
|
-
|
118
|
+
@logger.debug("PerimeterX[handle_verification]")
|
119
|
+
@logger.debug("PerimeterX[handle_verification]: processing ended - score:#{px_ctx.context[:score]}, uuid:#{px_ctx.context[:uuid]}")
|
120
|
+
|
121
|
+
score = px_ctx.context[:score]
|
122
|
+
# Case PASS request
|
123
|
+
if (score < @px_config[:blocking_score])
|
124
|
+
@logger.debug("PerimeterX[handle_verification]: score:#{score} < blocking score, passing request")
|
125
|
+
@px_activity_client.send_page_requested_activity(px_ctx)
|
126
|
+
return true
|
127
|
+
end
|
128
|
+
|
129
|
+
# Case blocking activity
|
130
|
+
@px_activity_client.send_block_activity(px_ctx)
|
131
|
+
|
132
|
+
# In case were in monitor mode, end here
|
133
|
+
if(@px_config[:module_mode] == PxModule::MONITOR_MODE)
|
134
|
+
@logger.debug("PerimeterX[handle_verification]: monitor mode is on, passing request")
|
135
|
+
return true
|
136
|
+
end
|
137
|
+
|
138
|
+
@logger.debug("PerimeterX[handle_verification]: verification ended, the request should be blocked")
|
139
|
+
|
140
|
+
return false, px_ctx
|
64
141
|
end
|
65
142
|
|
66
143
|
private_class_method :new
|
67
144
|
end
|
68
|
-
|
69
145
|
end
|
@@ -1,30 +1,37 @@
|
|
1
|
-
|
1
|
+
require 'perimeterx/utils/px_logger'
|
2
|
+
require 'perimeterx/utils/px_constants'
|
3
|
+
|
4
|
+
module PxModule
|
2
5
|
class Configuration
|
3
6
|
|
4
7
|
attr_accessor :configuration
|
5
8
|
attr_accessor :PX_DEFAULT
|
6
|
-
attr_accessor :MONITOR_MODE
|
7
|
-
attr_accessor :ACTIVE_MODE
|
8
|
-
|
9
|
-
MONITOR_MODE = 1
|
10
|
-
ACTIVE_MODE = 2
|
11
9
|
|
12
10
|
PX_DEFAULT = {
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
"
|
22
|
-
|
11
|
+
:app_id => nil,
|
12
|
+
:cookie_key => nil,
|
13
|
+
:auth_token => nil,
|
14
|
+
:module_enabled => true,
|
15
|
+
:captcha_enabled => true,
|
16
|
+
:challenge_enabled => true,
|
17
|
+
:encryption_enabled => true,
|
18
|
+
:blocking_score => 70,
|
19
|
+
:sensitive_headers => ["http-cookie", "http-cookies"],
|
20
|
+
:api_connect_timeout => 0,
|
21
|
+
:api_timeout => 0,
|
22
|
+
:max_buffer_len => 30,
|
23
|
+
:send_page_activities => false,
|
24
|
+
:send_block_activities => true,
|
25
|
+
:sdk_name => PxModule::SDK_NAME,
|
26
|
+
:debug => false,
|
27
|
+
:module_mode => PxModule::ACTIVE_MODE,
|
28
|
+
:local_proxy => false
|
23
29
|
}
|
24
30
|
|
25
31
|
def initialize(params)
|
26
|
-
PX_DEFAULT[
|
32
|
+
PX_DEFAULT[:perimeterx_server_host] = "https://sapi-#{params[:app_id].downcase}.perimeterx.net"
|
27
33
|
@configuration = PX_DEFAULT.merge(params);
|
34
|
+
@configuration[:logger] = PxLogger.new(@configuration[:debug])
|
28
35
|
end
|
29
36
|
end
|
30
37
|
end
|