perimeter_x 1.0.4.pre.alpha → 1.0.4
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 +12 -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/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 +66 -58
- 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 +216 -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
- /data/examples/{routes.rb → config/routes.rb} +0 -0
data/readme.md
CHANGED
@@ -10,6 +10,20 @@ Table of Contents
|
|
10
10
|
* [Installation](#installation)
|
11
11
|
* [Basic Usage Example](#basic-usage)
|
12
12
|
- [Configuration](#configuration)
|
13
|
+
* [Configuring Required Parameters](#requireied-params)
|
14
|
+
* [Blocking Score](#blocking-score)
|
15
|
+
* [Custom Block Page](#custom-block-page)
|
16
|
+
* [Custom Block Action](#custom-block-action)
|
17
|
+
* [Enable/Disable Captcha](#captcha-support)
|
18
|
+
* [Extracting Real IP Address](#real-ip)
|
19
|
+
* [Custom URI](#custom-uri)
|
20
|
+
* [Filter Sensitive Headers](#sensitive-headers)
|
21
|
+
* [API Timeouts](#api-timeout)
|
22
|
+
* [Send Page Activities](#send-page-activities)
|
23
|
+
* [Additional Page Activity Handler](#additional-page-activity-handler)
|
24
|
+
* [Monitor Only](#logging)
|
25
|
+
* [Debug Mode](#debug-mode)
|
26
|
+
- [Contributing](#contributing)
|
13
27
|
|
14
28
|
<a name="Usage"></a>
|
15
29
|
<a name="dependencies"></a> Dependencies
|
@@ -17,66 +31,85 @@ Table of Contents
|
|
17
31
|
|
18
32
|
- Ruby version 2.3+
|
19
33
|
- Rails version 4.2
|
20
|
-
- [
|
34
|
+
- [httpclient](https://rubygems.org/gems/httpclient/versions/2.8.3)
|
35
|
+
- [mustache](https://rubygems.org/gems/mustache)
|
21
36
|
|
22
37
|
<a name="installation"></a> Installation
|
23
38
|
----------------------------------------
|
24
|
-
Install it through command line ```gem install perimeter_x
|
25
|
-
Or add it in Gemfile ```gem 'perimeter_x', '~> 1.0.3.pre.alpha'```
|
39
|
+
Install it through command line ```gem install perimeter_x```
|
26
40
|
|
27
41
|
|
28
42
|
<a name=basic-usage></a> Basic Usage Example
|
29
43
|
----------------------------------------
|
30
|
-
On the Rails controller include the PerimeterX SDK via the before_action which will call your defined middleware function. This function is a wrapper for the px_verify method which takes a request and processes it. The verify method can return true if verified, or false if not verified.
|
31
44
|
|
32
|
-
|
45
|
+
### Configuration & Initialization
|
46
|
+
Create a configuration file at `<rails_app>/config/initializers/perimeterx.rb` and initialize PerimeterX instance on the rails application startup
|
47
|
+
```ruby
|
48
|
+
params = {
|
49
|
+
:app_id => "APP_ID",
|
50
|
+
:cookie_key => "COOKIE_KEY",
|
51
|
+
:auth_token => "AUTH_TOKEN"
|
52
|
+
}
|
33
53
|
|
54
|
+
PxModule.configure(params)
|
34
55
|
```
|
56
|
+
|
57
|
+
On the Rails controller include the PerimeterX SDK via the before_action and call PerimterX middleware function.
|
58
|
+
|
59
|
+
```ruby
|
35
60
|
class HomeController < ApplicationController
|
36
|
-
include
|
37
|
-
|
38
|
-
|
39
|
-
...
|
40
|
-
before_action :px_middleware
|
61
|
+
include PxModule
|
62
|
+
|
63
|
+
before_filter :px_verify_request
|
41
64
|
...
|
42
65
|
...
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
66
|
+
end
|
67
|
+
```
|
68
|
+
|
69
|
+
<a name="configuration"></a> Configuration options
|
70
|
+
----------------------------------------
|
71
|
+
<a name="requireied-params"></a>**Configuring Required Parameters**
|
72
|
+
Configuration options are set on the ``params`` variable on the initializer file.
|
73
|
+
|
74
|
+
- ``app_id``
|
75
|
+
- ``cookie_key``
|
76
|
+
- ``auth_token``
|
77
|
+
|
78
|
+
All parameters are obtainable via the PerimeterX Portal. (Applications and Policies pages)
|
79
|
+
|
80
|
+
<a name="blocking-score"></a>**Changing the Minimum Score for Blocking**
|
81
|
+
|
82
|
+
>Note: Default blocking value: 70
|
83
|
+
|
84
|
+
```ruby
|
85
|
+
params = {
|
50
86
|
...
|
87
|
+
:blocking_score => 100
|
51
88
|
...
|
52
|
-
|
53
|
-
px.px_verify(request.env)
|
54
|
-
end
|
89
|
+
}
|
55
90
|
```
|
56
91
|
|
57
|
-
<a name="configuration"></a> Configuration
|
58
|
-
----------------------------------------
|
59
92
|
|
60
|
-
|
61
|
-
|
93
|
+
|
94
|
+
<a name="custom-block-action"></a>**Custom Verification Handler**
|
95
|
+
|
96
|
+
A custom verification handler is being executed inside ``px_verify_request`` instead of the the default behavior and allows a user to use a custom action based on the risk score returned by PerimeterX.
|
62
97
|
|
63
98
|
When implemented, this method receives a hash variable as input which represents data from the PerimeterX context of the request (px_ctx).
|
64
99
|
|
65
|
-
- `px_ctx[:score] ` contains the risk score
|
66
|
-
- `px_ctx[:uuid] ` contains the request UUID
|
100
|
+
- `px_ctx[:score] ` contains the risk score
|
101
|
+
- `px_ctx[:uuid] ` contains the request UUID
|
67
102
|
|
68
103
|
To replace the default verification behavior, add the configuration a lambda member as shown in the example below.
|
69
104
|
|
70
105
|
The method must return boolen value.
|
71
106
|
|
72
|
-
|
73
|
-
|
74
107
|
```ruby
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
if px_ctx[:score] >= 60
|
108
|
+
params = {
|
109
|
+
:app_id => <APP_ID>,
|
110
|
+
:auth_token => <AUTH_TOKEN>,
|
111
|
+
:custom_block_handler => -> (px_ctx) {
|
112
|
+
if px_ctx.context[:score] >= 60
|
80
113
|
# take your action and retun a message or JSON with a status code of 403 and option UUID of the request. Can return false and include action in the px_middleware method.
|
81
114
|
end
|
82
115
|
return true
|
@@ -84,7 +117,33 @@ configuration = {
|
|
84
117
|
}
|
85
118
|
```
|
86
119
|
|
87
|
-
**
|
120
|
+
**Example**
|
121
|
+
### Serving a Custom HTML Page ###
|
122
|
+
```ruby
|
123
|
+
|
124
|
+
params[:custom_block_handler] = -> (px_ctx)
|
125
|
+
{
|
126
|
+
block_score = px_ctx.context[:score];
|
127
|
+
block_uuid = px_ctx.context[:uuid];
|
128
|
+
full_url = px_ctx.context[:full_url];
|
129
|
+
|
130
|
+
html = "<html>
|
131
|
+
<body>
|
132
|
+
<div>Access to #{full_url} has been blocked.</div>
|
133
|
+
<div>Block reference - #{block_uuid} </div>
|
134
|
+
<div>Block score - #{block_score} </div>
|
135
|
+
</body>
|
136
|
+
</html>".html_safe
|
137
|
+
response.headers["Content-Type"] = "text/html"
|
138
|
+
response.status = 403
|
139
|
+
render :html => html
|
140
|
+
return false
|
141
|
+
};
|
142
|
+
|
143
|
+
PxModule.configure(params)
|
144
|
+
```
|
145
|
+
|
146
|
+
<a name="real-ip"></a>** Custom User IP **
|
88
147
|
|
89
148
|
> Note: IP extraction, according to your network setup, is very important. It is common to have a load balancer/proxy on top of your applications, in which case the PerimeterX module will send the system's internal IP as the user's. In order to properly perform processing and detection on server-to-server calls, PerimeterX module needs the real user's IP.
|
90
149
|
|
@@ -111,3 +170,126 @@ configuration = {
|
|
111
170
|
}
|
112
171
|
}
|
113
172
|
```
|
173
|
+
<a name="custom-block-page"></a>**Customizing Default Block Pages**
|
174
|
+
|
175
|
+
Adding a custom logo to the blocking page is by providing the `params` a key `custom_logo` , the logo will be displayed at the top div of the the block page The logo's `max-heigh` property would be `150px` and width would be set to `auto`
|
176
|
+
|
177
|
+
The key custom_logo expects a valid URL address such as https://s.perimeterx.net/logo.png
|
178
|
+
|
179
|
+
```ruby
|
180
|
+
params = [
|
181
|
+
:app_id => 'APP_ID',
|
182
|
+
:cookie_key => 'COOKIE_SECRET',
|
183
|
+
:auth_token => 'AUTH_TOKEN',
|
184
|
+
:custom_logo => 'LOGO_URL'
|
185
|
+
];
|
186
|
+
```
|
187
|
+
|
188
|
+
**Custom JS/CSS**
|
189
|
+
The block page can be modified with a custom CSS by adding to the `params` the key `css_ref` and providing a valid URL to the css In addition there is also the option to add a custom JS file by adding `js_ref` key to the pxConfig and providing the JS file that will be loaded with the block page, this key also expects a valid URL
|
190
|
+
|
191
|
+
```ruby
|
192
|
+
params = [
|
193
|
+
:app_id => 'APP_ID',
|
194
|
+
:cookie_key => 'COOKIE_SECRET',
|
195
|
+
:auth_token => 'AUTH_TOKEN',
|
196
|
+
:css_ref => 'CSS',
|
197
|
+
:js_ref => 'JS'
|
198
|
+
];
|
199
|
+
```
|
200
|
+
> Note: Custom logo/js/css can be added together
|
201
|
+
|
202
|
+
<a name="logging"></a>**No Blocking, Monitor Only**
|
203
|
+
Default mode: PxModule::ACTIVE_MODE
|
204
|
+
|
205
|
+
- PxModule::ACTIVE_MODE - Module blocks users crossing the predefined block threshold. Server-to-server requests are sent synchronously.
|
206
|
+
|
207
|
+
- PxModule::$MONITOR_MODE - Module does not block users crossing the predefined block threshold. The `custom_block_handler` function will be eval'd in case one is supplied, upon crossing the defined block threshold.
|
208
|
+
|
209
|
+
```ruby
|
210
|
+
params[:module_mode] = PxModule::MONITOR_MODE
|
211
|
+
```
|
212
|
+
|
213
|
+
<a name="captcha-support"></a>**Enable/Disable CAPTCHA on the block page**
|
214
|
+
Default mode: enabled
|
215
|
+
|
216
|
+
By enabling CAPTCHA support, a CAPTCHA will be served as part of the block page, giving real users the ability to identify as a human. By solving the CAPTCHA, the user's score is then cleaned up and the user is allowed to continue normal use.
|
217
|
+
|
218
|
+
```ruby
|
219
|
+
params[:captcha_enabled] = false
|
220
|
+
```
|
221
|
+
|
222
|
+
<a name="custom-uri"></a>**Custom URI**
|
223
|
+
|
224
|
+
Default: 'REQUEST_URI'
|
225
|
+
|
226
|
+
The URI can be returned to the PerimeterX module, using a custom user function, defined on the ``params`` variable
|
227
|
+
|
228
|
+
```ruby
|
229
|
+
params[:custom_uri] = -> (request) {
|
230
|
+
return request.headers['HTTP_X_CUSTOM_URI']
|
231
|
+
}
|
232
|
+
```
|
233
|
+
|
234
|
+
<a name="sensitive-headers"></a>**Filter sensitive headers**
|
235
|
+
A list of sensitive headers can be configured to prevent specific headers from being sent to PerimeterX servers (lower case header names). Filtering cookie headers for privacy is set by default, and can be overridden on the `params` variable.
|
236
|
+
|
237
|
+
Default: cookie, cookies
|
238
|
+
|
239
|
+
```ruby
|
240
|
+
params[:sensitive_headers] = ['cookie', 'cookies', 'secret-header']
|
241
|
+
|
242
|
+
```
|
243
|
+
|
244
|
+
<a name="api-timeout"></a>**API Timeouts**
|
245
|
+
>Note: Controls the timeouts for PerimeterX requests. The API is called when a Risk Cookie does not exist, or is expired or invalid
|
246
|
+
|
247
|
+
The API Timeout, in seconds (int), to wait for the PerimeterX server API response.
|
248
|
+
|
249
|
+
Default: 1
|
250
|
+
|
251
|
+
```ruby
|
252
|
+
params[:api_timeout] = 4
|
253
|
+
```
|
254
|
+
|
255
|
+
<a name="send-page-activities"></a>**Send Page Activities**
|
256
|
+
Default: true
|
257
|
+
A boolean flag to enable or disable sending of activities and metrics to PerimeterX on each page request. Enabling this feature will provide data that populates the PerimeterX portal with valuable information, such as the amount of requests blocked and additional API usage statistics.
|
258
|
+
|
259
|
+
```ruby
|
260
|
+
params[:send_page_activities] = false
|
261
|
+
```
|
262
|
+
|
263
|
+
<a name="additional-page-activity-handler"></a>**Additional Page Activity Handler**
|
264
|
+
|
265
|
+
Adding an additional activity handler is done by setting `additional_activity_handler` with a user defined function on the `params` variable. The `additional_activity_handler` function will be executed before sending the data to the PerimeterX portal.
|
266
|
+
|
267
|
+
Default: Only send activity to PerimeterX as controlled by `params`.
|
268
|
+
|
269
|
+
|
270
|
+
|
271
|
+
```ruby
|
272
|
+
params[:additional_activity_handler] = -> (activity_type, px_ctx, details){
|
273
|
+
// user defined logic comes here
|
274
|
+
};
|
275
|
+
```
|
276
|
+
|
277
|
+
<a name="debug-mode"></a>**Debug Mode**
|
278
|
+
Default: false
|
279
|
+
|
280
|
+
Enables debug logging mode to STDOUT
|
281
|
+
```ruby
|
282
|
+
params[:debug] = true
|
283
|
+
```
|
284
|
+
|
285
|
+
<a name="contributing"></a># Contributing #
|
286
|
+
------------------------------
|
287
|
+
The following steps are welcome when contributing to our project.
|
288
|
+
###Fork/Clone
|
289
|
+
First and foremost, [Create a fork](https://guides.github.com/activities/forking/) of the repository, and clone it locally.
|
290
|
+
Create a branch on your fork, preferably using a self descriptive branch name.
|
291
|
+
|
292
|
+
###Code/Run
|
293
|
+
Help improve our project by implementing missing features, adding capabilities or fixing bugs.
|
294
|
+
|
295
|
+
To run the code, simply follow the steps in the [installation guide](#installation). Grab the keys from the PerimeterX Portal, and try refreshing your page several times continously. If no default behaviours have been overriden, you should see the PerimeterX block page. Solve the CAPTCHA to clean yourself and start fresh again.
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: perimeter_x
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.4
|
4
|
+
version: 1.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nitzan Goldfeder
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-04-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -52,6 +52,74 @@ dependencies:
|
|
52
52
|
- - '='
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: 2.8.2.4
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: mustache
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.0'
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: 1.0.3
|
65
|
+
type: :runtime
|
66
|
+
prerelease: false
|
67
|
+
version_requirements: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - "~>"
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '1.0'
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: 1.0.3
|
75
|
+
- !ruby/object:Gem::Dependency
|
76
|
+
name: activesupport
|
77
|
+
requirement: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: 4.2.0
|
82
|
+
type: :runtime
|
83
|
+
prerelease: false
|
84
|
+
version_requirements: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: 4.2.0
|
89
|
+
- !ruby/object:Gem::Dependency
|
90
|
+
name: rspec
|
91
|
+
requirement: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - "~>"
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '3.0'
|
96
|
+
type: :development
|
97
|
+
prerelease: false
|
98
|
+
version_requirements: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - "~>"
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '3.0'
|
103
|
+
- !ruby/object:Gem::Dependency
|
104
|
+
name: mocha
|
105
|
+
requirement: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - "~>"
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '1.2'
|
110
|
+
- - ">="
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: 1.2.1
|
113
|
+
type: :development
|
114
|
+
prerelease: false
|
115
|
+
version_requirements: !ruby/object:Gem::Requirement
|
116
|
+
requirements:
|
117
|
+
- - "~>"
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '1.2'
|
120
|
+
- - ">="
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: 1.2.1
|
55
123
|
description: PerimeterX ruby module to monitor and block traffic according to PerimeterX
|
56
124
|
risk score
|
57
125
|
email: nitzan@perimeterx.com
|
@@ -67,18 +135,29 @@ files:
|
|
67
135
|
- Gemfile.lock
|
68
136
|
- LICENSE.txt
|
69
137
|
- Rakefile
|
70
|
-
- bin/console
|
71
|
-
- bin/setup
|
72
138
|
- changelog.md
|
73
|
-
- examples/home_controller.rb
|
74
|
-
- examples/
|
139
|
+
- examples/app/controllers/home_controller.rb
|
140
|
+
- examples/app/views/home/index.html.erb.dist
|
141
|
+
- examples/config/initializers/perimeterx.rb.dist
|
142
|
+
- examples/config/routes.rb
|
75
143
|
- lib/perimeter_x.rb
|
76
144
|
- lib/perimeterx/configuration.rb
|
145
|
+
- lib/perimeterx/internal/clients/perimeter_x_activity_client.rb
|
146
|
+
- lib/perimeterx/internal/clients/perimeter_x_risk_client.rb
|
147
|
+
- lib/perimeterx/internal/exceptions/px_cookie_decryption_exception.rb
|
77
148
|
- lib/perimeterx/internal/perimeter_x_context.rb
|
78
|
-
- lib/perimeterx/internal/
|
79
|
-
- lib/perimeterx/internal/
|
149
|
+
- lib/perimeterx/internal/perimeter_x_cookie.rb
|
150
|
+
- lib/perimeterx/internal/perimeter_x_cookie_v1.rb
|
151
|
+
- lib/perimeterx/internal/perimeter_x_cookie_v3.rb
|
152
|
+
- lib/perimeterx/internal/validators/perimeter_x_captcha_validator.rb
|
153
|
+
- lib/perimeterx/internal/validators/perimeter_x_cookie_validator.rb
|
154
|
+
- lib/perimeterx/internal/validators/perimeter_x_s2s_validator.rb
|
155
|
+
- lib/perimeterx/utils/px_constants.rb
|
80
156
|
- lib/perimeterx/utils/px_http_client.rb
|
81
157
|
- lib/perimeterx/utils/px_logger.rb
|
158
|
+
- lib/perimeterx/utils/px_template_factory.rb
|
159
|
+
- lib/perimeterx/utils/templates/block.mustache
|
160
|
+
- lib/perimeterx/utils/templates/captcha.mustache
|
82
161
|
- lib/perimeterx/version.rb
|
83
162
|
- perimeter_x.gemspec
|
84
163
|
- readme.md
|
@@ -101,9 +180,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
101
180
|
version: '2.3'
|
102
181
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
182
|
requirements:
|
104
|
-
- - "
|
183
|
+
- - ">="
|
105
184
|
- !ruby/object:Gem::Version
|
106
|
-
version:
|
185
|
+
version: '0'
|
107
186
|
requirements: []
|
108
187
|
rubyforge_project:
|
109
188
|
rubygems_version: 2.6.11
|
data/bin/console
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
require "bundler/setup"
|
4
|
-
require "perimeter_x"
|
5
|
-
|
6
|
-
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
-
# with your gem easier. You can also use a different console, if you like.
|
8
|
-
|
9
|
-
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
-
# require "pry"
|
11
|
-
# Pry.start
|
12
|
-
|
13
|
-
require "irb"
|
14
|
-
IRB.start(__FILE__)
|
data/bin/setup
DELETED
@@ -1,23 +0,0 @@
|
|
1
|
-
class HomeController < ApplicationController
|
2
|
-
include PerimeterX
|
3
|
-
|
4
|
-
before_action :px_middleware
|
5
|
-
|
6
|
-
attr_accessor :px
|
7
|
-
|
8
|
-
def initialize()
|
9
|
-
params = {
|
10
|
-
"app_id" => <APP_ID>,
|
11
|
-
"cookie_key" => <COOKIE_KEY>,
|
12
|
-
"auth_token" => <AUTH_TOKEN>
|
13
|
-
}
|
14
|
-
@px = PxModule.instance(params)
|
15
|
-
end
|
16
|
-
|
17
|
-
def index
|
18
|
-
end
|
19
|
-
|
20
|
-
def px_middleware
|
21
|
-
px.px_verify(request.env)
|
22
|
-
end
|
23
|
-
end
|
@@ -1,29 +0,0 @@
|
|
1
|
-
require 'perimeterx/utils/px_logger'
|
2
|
-
|
3
|
-
class PerimeterxRiskClient
|
4
|
-
L = PxLogger.instance
|
5
|
-
|
6
|
-
attr_accessor :px_ctx
|
7
|
-
attr_accessor :px_config
|
8
|
-
attr_accessor :http_client
|
9
|
-
|
10
|
-
def initialize(px_ctx, px_config, http_client)
|
11
|
-
@px_ctx = px_ctx
|
12
|
-
@px_config = px_config
|
13
|
-
@http_client = http_client;
|
14
|
-
end
|
15
|
-
|
16
|
-
def format_headers()
|
17
|
-
formated_headers = []
|
18
|
-
@px_ctx.context[:headers].each do |k,v|
|
19
|
-
if (!@px_config["sensitive_headers"].include? k.to_s)
|
20
|
-
formated_headers.push({
|
21
|
-
:name => k.to_s,
|
22
|
-
:value => v
|
23
|
-
})
|
24
|
-
end #end if
|
25
|
-
end #end forech
|
26
|
-
return formated_headers
|
27
|
-
end #end method
|
28
|
-
|
29
|
-
end #end class
|
@@ -1,68 +0,0 @@
|
|
1
|
-
require 'perimeterx/internal/perimeter_x_risk_client'
|
2
|
-
|
3
|
-
class PerimeterxS2SValidator < PerimeterxRiskClient
|
4
|
-
|
5
|
-
attr_accessor :risk_mode
|
6
|
-
attr_accessor :response
|
7
|
-
|
8
|
-
def initialize(px_ctx, px_config, http_client)
|
9
|
-
L.info("PerimeterxS2SValidator: initialize")
|
10
|
-
@px_ctx = px_ctx
|
11
|
-
@px_config = px_config
|
12
|
-
@http_client = http_client
|
13
|
-
end
|
14
|
-
|
15
|
-
def send_risk_request
|
16
|
-
L.info("PerimeterxS2SValidator[send_risk_request]: send_risk_request")
|
17
|
-
request_body = {
|
18
|
-
'request' => {
|
19
|
-
'ip' => @px_ctx.context[:ip],
|
20
|
-
'headers' => format_headers(),
|
21
|
-
'uri' => @px_ctx.context[:uri],
|
22
|
-
'url' => @px_ctx.context[:full_url]
|
23
|
-
},
|
24
|
-
'additional' => {
|
25
|
-
's2s_call_reason' => @px_ctx.context[:s2s_call_reason],
|
26
|
-
'module_version' => @px_config["sdk_name"],
|
27
|
-
'http_method' => @px_ctx.context[:http_method],
|
28
|
-
'http_version' => @px_ctx.context[:http_version],
|
29
|
-
}
|
30
|
-
}
|
31
|
-
|
32
|
-
headers = {
|
33
|
-
"Authorization" => "Bearer #{@px_config['auth_token']}" ,
|
34
|
-
"Content-Type" => "application/json"
|
35
|
-
};
|
36
|
-
|
37
|
-
return @http_client.post("/api/v2/risk", request_body, headers)
|
38
|
-
end
|
39
|
-
|
40
|
-
def verify
|
41
|
-
L.info("PerimeterxS2SValidator[verify]: started")
|
42
|
-
response = send_risk_request()
|
43
|
-
if (!response)
|
44
|
-
return @px_ctx
|
45
|
-
end
|
46
|
-
@px_ctx.context[:made_s2s_risk_api_call] = true
|
47
|
-
response_body = eval(response.content);
|
48
|
-
# When success
|
49
|
-
if (response.status == 200 && response_body.key?(:score) && response_body.key?(:action))
|
50
|
-
L.info("PerimeterxS2SValidator[verify]: response ok")
|
51
|
-
score = response_body[:score]
|
52
|
-
@px_ctx.context[:score] = score
|
53
|
-
@px_ctx.context[:uuid] = response_body[:uuid]
|
54
|
-
@px_ctx.context[:block_action] = response_body[:action]
|
55
|
-
end #end success response
|
56
|
-
|
57
|
-
# When error
|
58
|
-
if(response.status != 200)
|
59
|
-
L.warn("PerimeterxS2SValidator[verify]: bad response, return code #{response.code}")
|
60
|
-
@px_ctx.context[:uuid] = ""
|
61
|
-
@px_ctx.context[:s2s_error_msg] = response_body[:message]
|
62
|
-
end
|
63
|
-
|
64
|
-
L.info("PerimeterxS2SValidator[verify]: done")
|
65
|
-
return @px_ctx
|
66
|
-
end #end method
|
67
|
-
|
68
|
-
end
|
File without changes
|