-
Notifications
You must be signed in to change notification settings - Fork 756
/
browser.rb
300 lines (241 loc) · 8.08 KB
/
browser.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
=begin
Copyright 2010-2013 Tasos Laskos <tasos.laskos@gmail.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=end
require 'watir-webdriver'
module Arachni
# Real browser driver providing DOM/JS/AJAX support.
#
# @author Tasos "Zapotek" Laskos <tasos.laskos@gmail.com>
class Browser
# {Browser} error namespace.
#
# All {Browser} errors inherit from and live under it.
#
# @author Tasos "Zapotek" Laskos <tasos.laskos@gmail.com>
class Error < Arachni::Error
# Raised when a given resource can't be loaded.
#
# @author Tasos "Zapotek" Laskos <tasos.laskos@gmail.com>
class Load < Error
end
end
# @return [Hash] Preloaded resources, by URL.
attr_reader :preloads
# @return [Watir::Browser] Watir driver interface.
attr_reader :watir
def initialize
proxy.start_async
@watir = ::Watir::Browser.new(
Selenium::WebDriver.for( :phantomjs,
desired_capabilities: Selenium::WebDriver::Remote::Capabilities.
phantomjs( phantomjs_options ),
args: "--proxy=http://#{proxy.address}/ --ignore-ssl-errors=true"
)
)
@pages = {}
@cache = {}
@preloads = {}
@current_response = nil
end
def close
watir.cookies.clear
watir.close
proxy.shutdown
end
# @return [String] Current URL.
def url
@url || @current_response.url
end
# @return [Page] Converts the current browser window to a {Page page}.
def to_page
return if !@current_response
@current_response.body = source
page = @current_response.to_page
page.cookies |= cookies
page
end
# @param [String, HTTP::Response, Page] resource
# Loads the given resource in the browser. If it is a string it will be
# treated like a URL.
def load( resource )
case resource
when String
goto resource
when HTTP::Response, Page
goto preload( resource )
else
fail Error::Load,
"Can't load resource of type #{resource.class}."
end
nil
end
# @param [String] url Loads the given URL in the browser.
def goto( url )
load_cookies url
watir.goto @url = url
HTTP::Client.update_cookies cookies
nil
end
# @note The preloaded resource will be removed once used, for a persistent
# cache use {#cache}.
#
# @param [HTTP::Response, Page] resource
# Preloads a resource to be instantly available by URL via {#load}.
def preload( resource )
response = case resource
when HTTP::Response
resource
when Page
resource.response
else
fail Error::Load,
"Can't load resource of type #{resource.class}."
end
@preloads[response.url] = response
response.url
end
# @param [HTTP::Response, Page] resource
# Cache a resource in order to be instantly available by URL via {#load}.
def cache( resource = nil )
return @cache if !resource
response = case resource
when HTTP::Response
resource
when Page
resource.response
else
fail Error::Load,
"Can't load resource of type #{resource.class}."
end
@cache[response.url] = response
response.url
end
# Starts capturing requests and parsing them into elements of pages,
# accessible via {#flush_pages}.
#
# @see #stop_capture
# @see #capture?
# @see #flush_pages
def start_capture
@capture = true
end
# Stops the page capture.
#
# @see #start_capture
# @see #capture?
# @see #flush_pages
def stop_capture
@capture = false
end
# @return [Bool]
# `true` if the page capture is enabled, `false` otherwise.
#
# @see #start_capture
# @see #stop_capture
def capture?
!!@capture
end
# @return [Array<Page>] Flushes the buffer of recorded pages.
#
# @see #start_capture
# @see #stop_capture
# @see #capture?
def flush_pages
@pages.values
ensure
@pages.clear
end
# @return [Array<Cookie>] Browser cookies.
def cookies
watir.cookies.to_a.map { |c| Cookie.new c.merge( url: url ) }
end
# @return [String] HTML code of the evaluated (DOM/JS/AJAX) page.
def source
watir.html
end
# @return [Selenium::WebDriver::Driver] Selenium driver interface.
def selenium
watir.driver
end
private
def load_cookies( url )
# First clears the browser's cookies and then tricks it into accepting
# the system cookies for its cookie-jar.
watir.cookies.clear
url = "#{url}/set-cookies-#{Utilities.generate_token}"
watir.goto preload( HTTP::Response.new(
url: url,
headers: {
'Set-Cookie' => HTTP::Client.cookie_jar.for_url( url ).
map( &:to_set_cookie )
}
))
end
def phantomjs_options
{
'phantomjs.page.settings.userAgent' => Options.user_agent
}
end
def proxy
@proxy ||= HTTP::ProxyServer.new( request_handler: method( :request_handler ))
end
def request_handler( request, response )
# Signal the proxy to not actually perform the request if we have a
# preloaded or cached response for it.
return if from_preloads( request, response ) || from_cache( request, response )
# Capture the request as elements of pages -- let's us grab AJAX and
# other browser requests and convert them into system elements we can
# analyze and audit.
capture( request )
# Signal the proxy to continue with its request to the origin server.
true
end
def capture( request )
return if !capture?
if !@pages.include? url
page = Page.from_data( url: url )
page.response.request = request
@pages[url] = page
end
page = @pages[url]
case request.method
when :get
page.links << Link.new( url: url, action: request.url )
when :post
page.forms << Form.new(
url: url,
action: request.url,
method: request.method,
inputs: Utilities.form_parse_request_body( request.body )
)
end
end
def from_preloads( request, response )
return if !(preloaded = preloads.delete( request.url ))
copy_response_data( preloaded, response )
@current_response = preloaded
end
def from_cache( request, response )
return if !(cached = @cache[request.url])
copy_response_data( cached, response )
@current_response = cached
end
def copy_response_data( source, destination )
[:code, :url, :body, :headers, :ip_address, :return_code,
:return_message, :headers_string, :total_time, :time,
:version].each do |m|
destination.send "#{m}=", source.send( m )
end
nil
end
end
end