Skip to content

Commit 4ecb80e

Browse files
author
nitzanpx
authored
Merge pull request #17 from PerimeterX/dev
Releasing version 1.3.0
2 parents 8c6539a + 2c74fab commit 4ecb80e

30 files changed

+824
-98
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
*.rbc
2+
*.iml
23
capybara-*.html
34
.rspec
45
/log

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
language: ruby
2+
rvm:
3+
- 2.3

Gemfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
perimeter_x (1.2.0)
4+
perimeter_x (1.3.0)
55
activesupport (>= 4.2.0)
66
concurrent-ruby (~> 1.0, >= 1.0.5)
77
mustache (~> 1.0, >= 1.0.3)
@@ -20,7 +20,7 @@ GEM
2020
ethon (0.10.1)
2121
ffi (>= 1.3.0)
2222
ffi (1.9.18)
23-
i18n (0.8.1)
23+
i18n (0.8.6)
2424
metaclass (0.0.4)
2525
minitest (5.10.1)
2626
mocha (1.2.1)

Rakefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ begin
33

44
RSpec::Core::RakeTask.new(:spec)
55

6+
task :default => :spec
67
task :test => :spec
78
rescue LoadError
89
# no rspec available

changelog.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

8+
## [1.3.0] - 2017-06-04
9+
### Added
10+
- Sending client_uuid on page_requested activities
11+
- Supporting mobile sdk
12+
### Fixed
13+
- Using `request.env` instead of `env`
14+
815
## [1.2.0] - 2017-06-04
916
### Fixed
1017
- Default timeouts for post api requests
@@ -27,3 +34,4 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2734
- Constants on px_constants
2835
- Cookie Validation flow when cookie score was over the configured threshold
2936
- Using symbols instead of strings for requests body
37+

examples/app/controllers/home_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class HomeController < ApplicationController
22
include PxModule
33

4-
before_filter :px_verify_request
4+
before_action :px_verify_request
55

66
def index
77
end

lib/perimeter_x.rb

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
require 'concurrent'
2+
require 'json'
3+
require 'base64'
14
require 'perimeterx/configuration'
25
require 'perimeterx/utils/px_logger'
36
require 'perimeterx/utils/px_constants'
@@ -10,29 +13,46 @@
1013
require 'perimeterx/internal/validators/perimeter_x_captcha_validator'
1114

1215
module PxModule
13-
1416
# Module expose API
1517
def px_verify_request
16-
verified, px_ctx = PerimeterX.instance.verify(env)
18+
verified, px_ctx = PerimeterX.instance.verify(request.env)
1719

1820
# Invalidate _pxCaptcha, can be done only on the controller level
19-
cookies[:_pxCaptcha] = { value: "", expires: -1.minutes.from_now }
21+
cookies[:_pxCaptcha] = {value: "", expires: -1.minutes.from_now}
2022

21-
if (!verified)
23+
unless verified
2224
# In case custon block handler exists
2325
if (PerimeterX.instance.px_config.key?(:custom_block_handler))
24-
PerimeterX.instance.px_config[:logger].debug("PxModule[px_verify_request]: custom_block_handler triggered")
26+
PerimeterX.instance.px_config[:logger].debug('PxModule[px_verify_request]: custom_block_handler triggered')
2527
return instance_exec(px_ctx, &PerimeterX.instance.px_config[:custom_block_handler])
2628
else
2729
# Generate template
28-
PerimeterX.instance.px_config[:logger].debug("PxModule[px_verify_request]: sending default block page")
30+
PerimeterX.instance.px_config[:logger].debug('PxModule[px_verify_request]: sending default block page')
2931
html = PxTemplateFactory.get_template(px_ctx, PerimeterX.instance.px_config)
30-
response.headers["Content-Type"] = "text/html"
32+
response.headers['Content-Type'] = 'text/html'
3133
response.status = 403
32-
render :html => html
34+
# Web handler
35+
if px_ctx.context[:cookie_origin] == 'cookie'
36+
PerimeterX.instance.px_config[:logger].debug('PxModule[px_verify_request]: web block')
37+
response.headers['Content-Type'] = 'text/html'
38+
render :html => html
39+
else # Mobile SDK
40+
PerimeterX.instance.px_config[:logger].debug('PxModule[px_verify_request]: mobile sdk block')
41+
response.headers['Content-Type'] = 'application/json'
42+
hash_json = {
43+
:action => px_ctx.context[:block_action],
44+
:uuid => px_ctx.context[:uuid],
45+
:vid => px_ctx.context[:vid],
46+
:appId => PerimeterX.instance.px_config[:app_id],
47+
:page => Base64.strict_encode64(html),
48+
:collectorUrl => "https://collector-#{PerimeterX.instance.px_config[:app_id]}.perimeterx.net"
49+
}
50+
render :json => hash_json
51+
end
3352
end
3453
end
3554

55+
# Request was verified
3656
return verified
3757
end
3858

@@ -41,7 +61,7 @@ def self.configure(params)
4161
end
4262

4363

44-
# PerimtereX Module
64+
# PerimeterX Module
4565
class PerimeterX
4666
@@__instance = nil
4767
@@mutex = Mutex.new
@@ -62,19 +82,19 @@ def self.configure(params)
6282

6383
def self.instance
6484
return @@__instance if !@@__instance.nil?
65-
raise Exception.new("Please initialize perimeter x first")
85+
raise Exception.new('Please initialize perimeter x first')
6686
end
6787

6888

6989
#Instance Methods
7090
def verify(env)
7191
begin
72-
@logger.debug("PerimeterX[pxVerify]")
73-
req = ActionDispatch::Request.new(env)
92+
@logger.debug('PerimeterX[pxVerify]')
7493
if (!@px_config[:module_enabled])
75-
@logger.warn("Module is disabled")
94+
@logger.warn('Module is disabled')
7695
return true
7796
end
97+
req = ActionDispatch::Request.new(env)
7898
px_ctx = PerimeterXContext.new(@px_config, req)
7999

80100
# Captcha phase
@@ -96,7 +116,7 @@ def verify(env)
96116
end
97117
rescue Exception => e
98118
@logger.error("#{e.backtrace.first}: #{e.message} (#{e.class})")
99-
e.backtrace.drop(1).map { |s| @logger.error("\t#{s}") }
119+
e.backtrace.drop(1).map {|s| @logger.error("\t#{s}")}
100120
return true
101121
end
102122
end
@@ -111,11 +131,11 @@ def verify(env)
111131
@px_cookie_validator = PerimeterxCookieValidator.new(@px_config)
112132
@px_s2s_validator = PerimeterxS2SValidator.new(@px_config, @px_http_client)
113133
@px_captcha_validator = PerimeterxCaptchaValidator.new(@px_config, @px_http_client)
114-
@logger.debug("PerimeterX[initialize]")
134+
@logger.debug('PerimeterX[initialize]Z')
115135
end
116136

117137
private def handle_verification(px_ctx)
118-
@logger.debug("PerimeterX[handle_verification]")
138+
@logger.debug('PerimeterX[handle_verification]')
119139
@logger.debug("PerimeterX[handle_verification]: processing ended - score:#{px_ctx.context[:score]}, uuid:#{px_ctx.context[:uuid]}")
120140

121141
score = px_ctx.context[:score]
@@ -130,12 +150,12 @@ def verify(env)
130150
@px_activity_client.send_block_activity(px_ctx)
131151

132152
# 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")
153+
if (@px_config[:module_mode] == PxModule::MONITOR_MODE)
154+
@logger.debug('PerimeterX[handle_verification]: monitor mode is on, passing request')
135155
return true
136156
end
137157

138-
@logger.debug("PerimeterX[handle_verification]: verification ended, the request should be blocked")
158+
@logger.debug('PerimeterX[handle_verification]: verification ended, the request should be blocked')
139159

140160
return false, px_ctx
141161
end

lib/perimeterx/configuration.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Configuration
2020
:api_connect_timeout => 1,
2121
:api_timeout => 1,
2222
:max_buffer_len => 10,
23-
:send_page_activities => false,
23+
:send_page_activities => true,
2424
:send_block_activities => true,
2525
:sdk_name => PxModule::SDK_NAME,
2626
:debug => false,
@@ -31,7 +31,7 @@ class Configuration
3131

3232
def initialize(params)
3333
PX_DEFAULT[:perimeterx_server_host] = "https://sapi-#{params[:app_id].downcase}.perimeterx.net"
34-
@configuration = PX_DEFAULT.merge(params);
34+
@configuration = PX_DEFAULT.merge(params)
3535
@configuration[:logger] = PxLogger.new(@configuration[:debug])
3636
end
3737
end

lib/perimeterx/internal/clients/perimeter_x_activity_client.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ def send_to_perimeterx(activity_type, px_ctx, details = [])
1818
end
1919

2020
details[:module_version] = @px_config[:sdk_name]
21+
details[:cookie_origin] = px_ctx.context[:cookie_origin]
22+
2123
px_data = {
2224
:type => activity_type,
2325
:headers => format_headers(px_ctx),
@@ -53,9 +55,9 @@ def send_block_activity(px_ctx)
5355
end
5456

5557
details = {
56-
:block_uuid => px_ctx.context[:uuid],
57-
:block_score => px_ctx.context[:score],
58-
:block_reason => px_ctx.context[:block_reason]
58+
:block_uuid => px_ctx.context[:uuid],
59+
:block_score => px_ctx.context[:score],
60+
:block_reason => px_ctx.context[:blocking_reason]
5961
}
6062

6163
send_to_perimeterx(PxModule::BLOCK_ACTIVITY, px_ctx, details)
@@ -70,7 +72,8 @@ def send_page_requested_activity(px_ctx)
7072

7173
details = {
7274
:http_version => px_ctx.context[:http_version],
73-
:http_method => px_ctx.context[:http_method]
75+
:http_method => px_ctx.context[:http_method],
76+
:client_uuid => px_ctx.context[:uuid]
7477
}
7578

7679
if (px_ctx.context.key?(:decoded_cookie))

lib/perimeterx/internal/perimeter_x_cookie_v1.rb renamed to lib/perimeterx/internal/payload/perimeter_x_cookie_v1.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module PxModule
2-
class PerimeterxCookieV1 < PerimeterxCookie
2+
class PerimeterxCookieV1 < PerimeterxPayload
33

44
attr_accessor :px_config, :px_ctx
55

0 commit comments

Comments
 (0)