Class: PxModule::PerimeterXContext

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(px_config, req) ⇒ PerimeterXContext

instance methods



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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/perimeterx/internal/perimeter_x_context.rb', line 32

def initialize(px_config, req)
  @logger = px_config[:logger]
  @logger.debug('PerimeterXContext[initialize]')
  @context = Hash.new

  @context[:px_cookie] = Hash.new
  @context[:headers] = Hash.new
  @context[:cookie_origin] = 'cookie'
  @context[:made_s2s_risk_api_call] = false
  @context[:first_party_enabled] = px_config[:first_party_enabled]
  
  cookies = req.cookies

  @context[:ip] = PerimeterXContext.extract_ip(req, px_config)

  # Get token from header
  if req.headers[PxModule::TOKEN_HEADER]
    @context[:cookie_origin] = 'header'
    token = PerimeterXContext.force_utf8(req.headers[PxModule::TOKEN_HEADER])
    if token.match(PxModule::MOBILE_TOKEN_V3_REGEX)
      @context[:px_cookie][:v3] = token[2..-1]
    elsif token.match(PxModule::MOBILE_ERROR_REGEX)
      @context[:mobile_error] = token
      if req.headers[PxModule::ORIGINAL_TOKEN_HEADER]
        token = PerimeterXContext.force_utf8(req.headers[PxModule::ORIGINAL_TOKEN_HEADER])
        if token.match(PxModule::MOBILE_TOKEN_V3_REGEX)
          @context[:px_cookie][:v3] = token[2..-1]
        end
      end
    end
  elsif !cookies.empty? # Get cookie from jar
    # Prepare hashed cookies
    cookies.each do |k, v|
      case k.to_s
        when '_px3'
          @context[:px_cookie][:v3] = PerimeterXContext.force_utf8(v)
        when '_px'
          @context[:px_cookie][:v1] = PerimeterXContext.force_utf8(v)
        when '_pxvid'
          if v.is_a?(String) && v.match(PxModule::VID_REGEX)
            @context[:vid_source] = "vid_cookie"
            @context[:vid] = PerimeterXContext.force_utf8(v)
          end
      end
    end #end case
  end #end empty cookies

  req.headers.each do |k, v|
    if (k.start_with? 'HTTP_')
      header = k.to_s.gsub('HTTP_', '')
      header = header.gsub('_', '-').downcase
      @context[:headers][header.to_sym] = PerimeterXContext.force_utf8(v)
    end
  end #end headers foreach
  
  @context[:hostname]= req.server_name
  @context[:user_agent] = req.user_agent ? req.user_agent : ''
  @context[:uri] = px_config[:custom_uri] ? px_config[:custom_uri].call(req)  : req.fullpath 
  @context[:full_url] = req.original_url
  @context[:format] = req.format.symbol
  @context[:score] = 0

  if req.server_protocol
      httpVer = req.server_protocol.split('/')
      if httpVer.size > 0
          @context[:http_version] = httpVer[1]
      end
  end
  @context[:http_method] = req.method
	  @context[:sensitive_route] = check_sensitive_route(px_config[:sensitive_routes], @context[:uri]) 
end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



7
8
9
# File 'lib/perimeterx/internal/perimeter_x_context.rb', line 7

def context
  @context
end

#px_configObject

Returns the value of attribute px_config.



8
9
10
# File 'lib/perimeterx/internal/perimeter_x_context.rb', line 8

def px_config
  @px_config
end

Class Method Details

.extract_ip(req, px_config) ⇒ Object

class methods



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/perimeterx/internal/perimeter_x_context.rb', line 12

def self.extract_ip(req, px_config)
  # Get IP from header/custom function
  if px_config[:ip_headers].length() > 0
    px_config[:ip_headers].each do |ip_header|
      if req.headers[ip_header]
        return PerimeterXContext.force_utf8(req.headers[ip_header])
      end
    end
  elsif px_config[:ip_header_function] != nil
    return px_config[:ip_header_function].call(req) 
  end      
  return req.ip
end

.force_utf8(str) ⇒ Object



26
27
28
# File 'lib/perimeterx/internal/perimeter_x_context.rb', line 26

def self.force_utf8(str)
  return str.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '')
end

Instance Method Details

#check_sensitive_route(sensitive_routes, uri) ⇒ Object

end init



104
105
106
107
108
109
# File 'lib/perimeterx/internal/perimeter_x_context.rb', line 104

def check_sensitive_route(sensitive_routes, uri)
  sensitive_routes.each do |sensitive_route|
    return true if uri.start_with? sensitive_route
  end
  false
end


126
127
128
129
# File 'lib/perimeterx/internal/perimeter_x_context.rb', line 126

def get_px_cookie
  cookie = @context[:px_cookie].key?(:v3) ? @context[:px_cookie][:v3] : @context[:px_cookie][:v1]
  return cookie.tr(' ','+') if !cookie.nil?
end

#set_block_action_type(action) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/perimeterx/internal/perimeter_x_context.rb', line 111

def set_block_action_type(action)
  @context[:block_action] = case action
    when 'c'
      return 'captcha'
    when 'b'
      return 'block'
    when 'j'
      return 'challenge'
    when 'r'
      return 'rate_limit'
    else
      return 'captcha'
    end
end