Class: PxModule::FirstPartyManager

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

Instance Method Summary collapse

Constructor Details

#initialize(px_config, px_http_client, logger) ⇒ FirstPartyManager

Returns a new instance of FirstPartyManager.



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/perimeterx/internal/first_party/px_first_party.rb', line 4

def initialize(px_config, px_http_client, logger)
    @px_config = px_config
    @app_id = px_config[:app_id]
    @px_http_client = px_http_client
    @logger = logger
    @from = [
        "/#{@app_id[2..-1]}/init.js",
        "/#{@app_id[2..-1]}/captcha",
        "/#{@app_id[2..-1]}/xhr"
    ]
end

Instance Method Details

#extract_headers(req) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/perimeterx/internal/first_party/px_first_party.rb', line 90

def extract_headers(req)
    headers = Hash.new
    req.headers.each do |k, v|
        if (k.start_with? 'HTTP_') && (!@px_config[:sensitive_headers].include? k)
            header = k.to_s.gsub('HTTP_', '')
            header = header.gsub('_', '-').downcase
            headers[header] = PerimeterXContext.force_utf8(v)
        end
    end
    return headers
end

#get_captcha(req, uri, headers) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/perimeterx/internal/first_party/px_first_party.rb', line 48

def get_captcha(req, uri, headers)
    @logger.debug("FirstPartyManager[get_captcha]")
    
    # define host
    headers["host"] = PxModule::CAPTCHA_HOST

    # define request url
    path_and_query = uri.request_uri
    uri_suffix = path_and_query.sub "/#{@app_id[2..-1]}/captcha", ""
    url = "#{uri.scheme}://#{PxModule::CAPTCHA_HOST}#{uri_suffix}"

    # send request
    return @px_http_client.get(url, headers)
end

#get_client(req, uri, headers) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/perimeterx/internal/first_party/px_first_party.rb', line 35

def get_client(req, uri, headers)
    @logger.debug("FirstPartyManager[get_client]")
    
    # define host
    headers["host"] = PxModule::CLIENT_HOST
    
    # define request url
    url = "#{uri.scheme}://#{PxModule::CLIENT_HOST}/#{@app_id}/main.min.js"
    
    # send request
    return @px_http_client.get(url, headers)
end

#get_first_party_request_type(req) ⇒ Object

-1 - not first party request 0 - /init.js 1 - /captcha 2 - /xhr



106
107
108
109
110
111
112
113
114
# File 'lib/perimeterx/internal/first_party/px_first_party.rb', line 106

def get_first_party_request_type(req)
    url_path = URI.parse(req.original_url).path
    @from.each_with_index do |val,index|
        if url_path.start_with?(val)
            return index
        end
    end
    return -1
end

#get_response_content_type(req) ⇒ Object



120
121
122
# File 'lib/perimeterx/internal/first_party/px_first_party.rb', line 120

def get_response_content_type(req)
    return get_first_party_request_type(req) == 2 ? :json : :js
end

#is_first_party_request(req) ⇒ Object



116
117
118
# File 'lib/perimeterx/internal/first_party/px_first_party.rb', line 116

def is_first_party_request(req)
    return get_first_party_request_type(req) != -1
end

#send_first_party_request(req) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/perimeterx/internal/first_party/px_first_party.rb', line 16

def send_first_party_request(req)
    uri = URI.parse(req.original_url)
    url_path = uri.path
    
    headers = extract_headers(req)
    headers["x-px-first-party"] = "1"
    headers["x-px-enforcer-true-ip"] = PerimeterXContext.extract_ip(req, @px_config)

    if url_path.start_with?(@from[0])
        return get_client(req, uri, headers)
    elsif url_path.start_with?(@from[1]) 
        return get_captcha(req, uri, headers)
    elsif url_path.start_with?(@from[2])
        return send_xhr(req, uri, headers)
    else
        return nil
    end
end

#send_xhr(req, uri, headers) ⇒ Object



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
# File 'lib/perimeterx/internal/first_party/px_first_party.rb', line 63

def send_xhr(req, uri, headers)
    @logger.debug("FirstPartyManager[send_xhr]")

    # handle vid cookies
    if !req.cookies.nil?
        if req.cookies.key?("_pxvid")
            vid = PerimeterXContext.force_utf8(req.cookies["_pxvid"])
            if headers.key?('cookie')
                headers['cookie'] += "; pxvid=#{vid}";
            else
                headers['cookie'] = "pxvid=#{vid}";
            end
        end
    end

    # define host
    headers["host"] = "collector-#{@app_id.downcase}.perimeterx.net"
    
    # define request url
    path_and_query = uri.request_uri
    path_suffix = path_and_query.sub "/#{@app_id[2..-1]}/xhr", ""
    url = "#{uri.scheme}://collector-#{@app_id.downcase}.perimeterx.net#{path_suffix}"

    # send request
    return @px_http_client.post_xhr(url, req.body.string, headers)
end