Class: PxModule::PerimeterxCookieValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/perimeterx/internal/validators/perimeter_x_cookie_validator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(px_config) ⇒ PerimeterxCookieValidator

Returns a new instance of PerimeterxCookieValidator.



13
14
15
16
# File 'lib/perimeterx/internal/validators/perimeter_x_cookie_validator.rb', line 13

def initialize(px_config)
  @px_config = px_config
  @logger = px_config[:logger]
end

Instance Attribute Details

#px_configObject

Returns the value of attribute px_config.



11
12
13
# File 'lib/perimeterx/internal/validators/perimeter_x_cookie_validator.rb', line 11

def px_config
  @px_config
end

Instance Method Details

#verify(px_ctx) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/perimeterx/internal/validators/perimeter_x_cookie_validator.rb', line 19

def verify(px_ctx)
  begin
    if px_ctx.context[:px_cookie].empty?
      @logger.warn("PerimeterxCookieValidator:[verify]: no cookie")
      px_ctx.context[:s2s_call_reason] = PxModule::NO_COOKIE
      return false, px_ctx
    end
    
    # Deserialize cookie start
    cookie = PerimeterxPayload.px_cookie_factory(px_ctx, @px_config)
    if !cookie.deserialize()
      @logger.warn("PerimeterxCookieValidator:[verify]: invalid cookie")
      px_ctx.context[:px_orig_cookie] = px_ctx.get_px_cookie
      px_ctx.context[:s2s_call_reason] =  PxModule::COOKIE_DECRYPTION_FAILED
      return false, px_ctx
    end
    px_ctx.context[:decoded_cookie] = cookie.decoded_cookie
    px_ctx.context[:score] = cookie.cookie_score()
    px_ctx.context[:uuid] = cookie.decoded_cookie[:u]
    px_ctx.context[:block_action] = px_ctx.set_block_action_type(cookie.cookie_block_action())
    px_ctx.context[:cookie_hmac] = cookie.cookie_hmac()

    vid = cookie.decoded_cookie[:v]
    if vid.is_a?(String) && vid.match(PxModule::VID_REGEX)
      px_ctx.context[:vid_source] = "risk_cookie"
      px_ctx.context[:vid] = vid
    end

    if cookie.expired?
      @logger.warn("PerimeterxCookieValidator:[verify]: cookie expired")
      px_ctx.context[:s2s_call_reason] = PxModule::EXPIRED_COOKIE
      return false, px_ctx
    end

    if cookie.high_score?
      @logger.warn("PerimeterxCookieValidator:[verify]: cookie high score")
      px_ctx.context[:blocking_reason] = 'cookie_high_score'
      return true, px_ctx
    end

    if !cookie.secured?
      @logger.warn("PerimeterxCookieValidator:[verify]: cookie invalid hmac")
      px_ctx.context[:s2s_call_reason] = PxModule:: COOKIE_VALIDATION_FAILED
      return false, px_ctx
    end

    if px_ctx.context[:sensitive_route]
      @logger.info("PerimeterxCookieValidator:[verify]: cookie was verified but route is sensitive")
      px_ctx.context[:s2s_call_reason] = PxModule::SENSITIVE_ROUTE
      return false, px_ctx
    end

    @logger.debug("PerimeterxCookieValidator:[verify]: cookie validation passed succesfully")

    px_ctx.context[:pass_reason] = 'cookie'
    return true, px_ctx
  rescue Exception => e
    @logger.error("PerimeterxCookieValidator:[verify]: exception while verifying cookie => #{e.message}")
    px_ctx.context[:px_orig_cookie] = px_ctx.context[:px_cookie]
    px_ctx.context[:s2s_call_reason] = PxModule::COOKIE_DECRYPTION_FAILED
    return false, px_ctx
  end
end