Skip to content

Rename URL black/white list to block/allow list #188

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ end
`Cuprite`-specific options are:

* options `Hash`
* `:url_blacklist` (Array) - array of strings to match against requested URLs
* `:url_whitelist` (Array) - array of strings to match against requested URLs
* `:url_blocklist` (Array) - array of strings to match against requested URLs
* `:url_allowlist` (Array) - array of strings to match against requested URLs


## Debugging
Expand Down Expand Up @@ -188,24 +188,24 @@ Besides capybara screenshot method you can get image as Base64:
* `page.driver.set_proxy(ip, port, type, user, password)`


## URL Blacklisting & Whitelisting
## URL Allowlist & Blocklist

Cuprite supports URL blacklisting, which allows you to prevent scripts from
Cuprite supports a blocklist of URLs, which allows you to prevent scripts from
running on designated domains:

```ruby
page.driver.browser.url_blacklist = ["http://www.example.com"]
page.driver.browser.url_blocklist = ["http://www.example.com"]
```

and also URL whitelisting, which allows scripts to only run on designated
and also an allowlist of URLs, which allows scripts to only run on designated
domains:

```ruby
page.driver.browser.url_whitelist = ["http://www.example.com"]
page.driver.browser.url_allowlist = ["http://www.example.com"]
```

If you are experiencing slower run times, consider creating a URL whitelist of
domains that are essential or a blacklist of domains that are not essential,
If you are experiencing slower run times, consider creating a URL allowlist of
domains that are essential or a blocklist of domains that are not essential,
such as ad networks or analytics, to your testing environment.

## License
Expand Down
22 changes: 13 additions & 9 deletions lib/capybara/cuprite/browser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ class Browser < Ferrum::Browser
find_modal accept_confirm dismiss_confirm accept_prompt
dismiss_prompt reset_modals] => :page

attr_reader :url_blacklist, :url_whitelist
attr_reader :url_blocklist, :url_allowlist
alias url_blacklist url_blocklist
alias url_whitelist url_allowlist

def initialize(options = nil)
options ||= {}
@client = nil
self.url_blacklist = options[:url_blacklist]
self.url_whitelist = options[:url_whitelist]
self.url_blocklist = options[:url_blocklist] || options[:url_blacklist]
self.url_allowlist = options[:url_allowlist] || options[:url_whitelist]

super
@page = false
Expand All @@ -39,15 +41,17 @@ def quit
@page = false
end

def url_whitelist=(patterns)
@url_whitelist = prepare_wildcards(patterns)
page.network.intercept if @client && !@url_whitelist.empty?
def url_allowlist=(patterns)
@url_allowlist = prepare_wildcards(patterns)
page.network.intercept if @client && !@url_allowlist.empty?
end
alias url_whitelist= url_allowlist=

def url_blacklist=(patterns)
@url_blacklist = prepare_wildcards(patterns)
page.network.intercept if @client && !@url_blacklist.empty?
def url_blocklist=(patterns)
@url_blocklist = prepare_wildcards(patterns)
page.network.intercept if @client && !@url_blocklist.empty?
end
alias url_blacklist= url_blocklist=

def visit(*args)
goto(*args)
Expand Down
4 changes: 2 additions & 2 deletions lib/capybara/cuprite/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ def no_such_window_error
def reset!
@zoom_factor = nil
@paper_size = nil
browser.url_blacklist = @options[:url_blacklist]
browser.url_whitelist = @options[:url_whitelist]
browser.url_blocklist = @options[:url_blocklist] || @options[:url_blacklist]
browser.url_allowlist = @options[:url_allowlist] || @options[:url_whitelist]
browser.reset
@started = false
end
Expand Down
12 changes: 6 additions & 6 deletions lib/capybara/cuprite/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,18 +136,18 @@ def title
def prepare_page
super

network.intercept if !Array(@browser.url_whitelist).empty? ||
!Array(@browser.url_blacklist).empty?
network.intercept if !Array(@browser.url_allowlist).empty? ||
!Array(@browser.url_blocklist).empty?

on(:request) do |request, index, total|
if @browser.url_blacklist && !@browser.url_blacklist.empty?
if @browser.url_blacklist.any? { |r| request.match?(r) }
if @browser.url_blocklist && !@browser.url_blocklist.empty?
if @browser.url_blocklist.any? { |r| request.match?(r) }
request.abort and next
else
request.continue and next
end
elsif @browser.url_whitelist && !@browser.url_whitelist.empty?
if @browser.url_whitelist.any? { |r| request.match?(r) }
elsif @browser.url_allowlist && !@browser.url_allowlist.empty?
if @browser.url_allowlist.any? { |r| request.match?(r) }
request.continue and next
else
request.abort and next
Expand Down
80 changes: 40 additions & 40 deletions spec/features/driver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -694,9 +694,9 @@ def create_screenshot(file, *args)
end

it "keeps track of blocked network traffic" do
@driver.browser.url_blacklist = ["unwanted"]
@driver.browser.url_blocklist = ["unwanted"]

@session.visit "/cuprite/url_blacklist"
@session.visit "/cuprite/url_blocklist"

blocked_urls = @driver.network_traffic(:blocked).map { |e| e.request.url }

Expand Down Expand Up @@ -746,9 +746,9 @@ def create_screenshot(file, *args)
end

it "blocked requests get cleared along with network traffic" do
@driver.browser.url_blacklist = ["unwanted"]
@driver.browser.url_blocklist = ["unwanted"]

@session.visit "/cuprite/url_blacklist"
@session.visit "/cuprite/url_blocklist"

expect(@driver.network_traffic(:blocked).length).to eq(3)

Expand Down Expand Up @@ -985,32 +985,32 @@ def create_screenshot(file, *args)
expect(new_tab.size).to eq [1200, 800]
end

it "inherits url_blacklist" do
@driver.browser.url_blacklist = ["unwanted"]
it "inherits url_blocklist" do
@driver.browser.url_blocklist = ["unwanted"]
@session.visit "/"
new_tab = @session.open_new_window
@session.within_window(new_tab) do
@session.visit "/cuprite/url_blacklist"
@session.visit "/cuprite/url_blocklist"
expect(@session).to have_content("We are loading some unwanted action here")
@session.within_frame "framename" do
expect(@session.html).not_to include("We shouldn't see this.")
end
end
end

it "inherits url_whitelist" do
it "inherits url_allowlist" do
@session.visit "/"
@driver.browser.url_whitelist = ["url_whitelist", "/cuprite/wanted"]
@driver.browser.url_allowlist = ["url_allowlist", "/cuprite/wanted"]
new_tab = @session.open_new_window
@session.within_window(new_tab) do
@session.visit "/cuprite/url_whitelist"
@session.visit "/cuprite/url_allowlist"

expect(@session).to have_content("We are loading some wanted action here")
@session.within_frame "framename" do
expect(@session).to have_content("We should see this.")
end
@session.within_frame "unwantedframe" do
# make sure non whitelisted urls are blocked
# make sure non allowlisted urls are blocked
expect(@session).not_to have_content("We shouldn't see this.")
end
end
Expand Down Expand Up @@ -1106,11 +1106,11 @@ def create_screenshot(file, *args)
end
end

context "blacklisting urls for resource requests" do
context "blocklisting urls for resource requests" do
it "blocks unwanted urls" do
@driver.browser.url_blacklist = ["unwanted"]
@driver.browser.url_blocklist = ["unwanted"]

@session.visit "/cuprite/url_blacklist"
@session.visit "/cuprite/url_blocklist"

expect(@session.status_code).to eq(200)
expect(@session).to have_content("We are loading some unwanted action here")
Expand All @@ -1120,9 +1120,9 @@ def create_screenshot(file, *args)
end

it "supports wildcards" do
@driver.browser.url_blacklist = ["*wanted"]
@driver.browser.url_blocklist = ["*wanted"]

@session.visit "/cuprite/url_whitelist"
@session.visit "/cuprite/url_allowlist"

expect(@session.status_code).to eq(200)
expect(@session).to have_content("We are loading some wanted action here")
Expand All @@ -1135,33 +1135,33 @@ def create_screenshot(file, *args)
end

it "can be configured in the driver and survive reset" do
Capybara.register_driver :cuprite_blacklist do |app|
Capybara::Cuprite::Driver.new(app, @driver.options.merge(url_blacklist: ["unwanted"]))
Capybara.register_driver :cuprite_blocklist do |app|
Capybara::Cuprite::Driver.new(app, @driver.options.merge(url_blocklist: ["unwanted"]))
end

session = Capybara::Session.new(:cuprite_blacklist, @session.app)
session = Capybara::Session.new(:cuprite_blocklist, @session.app)

session.visit "/cuprite/url_blacklist"
session.visit "/cuprite/url_blocklist"
expect(session).to have_content("We are loading some unwanted action here")
session.within_frame "framename" do
expect(session.html).not_to include("We shouldn't see this.")
end

session.reset!

session.visit "/cuprite/url_blacklist"
session.visit "/cuprite/url_blocklist"
expect(session).to have_content("We are loading some unwanted action here")
session.within_frame "framename" do
expect(session.html).not_to include("We shouldn't see this.")
end
end
end

context "whitelisting urls for resource requests" do
it "allows whitelisted urls" do
@driver.browser.url_whitelist = ["url_whitelist", "/wanted"]
context "allowlisting urls for resource requests" do
it "permits allowlisted urls" do
@driver.browser.url_allowlist = ["url_allowlist", "/wanted"]

@session.visit "/cuprite/url_whitelist"
@session.visit "/cuprite/url_allowlist"

expect(@session.status_code).to eq(200)
expect(@session).to have_content("We are loading some wanted action here")
Expand All @@ -1174,9 +1174,9 @@ def create_screenshot(file, *args)
end

it "supports wildcards" do
@driver.browser.url_whitelist = ["url_whitelist", "/*wanted"]
@driver.browser.url_allowlist = ["url_allowlist", "/*wanted"]

@session.visit "/cuprite/url_whitelist"
@session.visit "/cuprite/url_allowlist"

expect(@session.status_code).to eq(200)
expect(@session).to have_content("We are loading some wanted action here")
Expand All @@ -1189,19 +1189,19 @@ def create_screenshot(file, *args)
end

it "blocks overruled urls" do
@driver.browser.url_whitelist = ["url_whitelist"]
@driver.browser.url_blacklist = ["url_whitelist"]
@driver.browser.url_allowlist = ["url_allowlist"]
@driver.browser.url_blocklist = ["url_allowlist"]

@session.visit "/cuprite/url_whitelist"
@session.visit "/cuprite/url_allowlist"

expect(@session.status_code).to eq(nil)
expect(@session).not_to have_content("We are loading some wanted action here")
end

it "allows urls when the whitelist is empty" do
@driver.browser.url_whitelist = []
it "allows urls when the allowlist is empty" do
@driver.browser.url_allowlist = []

@session.visit "/cuprite/url_whitelist"
@session.visit "/cuprite/url_allowlist"

expect(@session.status_code).to eq(200)
expect(@session).to have_content("We are loading some wanted action here")
Expand All @@ -1211,33 +1211,33 @@ def create_screenshot(file, *args)
end

it "can be configured in the driver and survive reset" do
Capybara.register_driver :cuprite_whitelist do |app|
Capybara.register_driver :cuprite_allowlist do |app|
Capybara::Cuprite::Driver.new(app,
@driver.options.merge(url_whitelist: ["url_whitelist", "/cuprite/wanted"]))
@driver.options.merge(url_allowlist: ["url_allowlist", "/cuprite/wanted"]))
end

session = Capybara::Session.new(:cuprite_whitelist, @session.app)
session = Capybara::Session.new(:cuprite_allowlist, @session.app)

session.visit "/cuprite/url_whitelist"
session.visit "/cuprite/url_allowlist"
expect(session).to have_content("We are loading some wanted action here")
session.within_frame "framename" do
expect(session).to have_content("We should see this.")
end

session.within_frame "unwantedframe" do
# make sure non whitelisted urls are blocked
# make sure non allowlisted urls are blocked
expect(session).not_to have_content("We shouldn't see this.")
end

session.reset!

session.visit "/cuprite/url_whitelist"
session.visit "/cuprite/url_allowlist"
expect(session).to have_content("We are loading some wanted action here")
session.within_frame "framename" do
expect(session).to have_content("We should see this.")
end
session.within_frame "unwantedframe" do
# make sure non whitelisted urls are blocked
# make sure non allowlisted urls are blocked
expect(session).not_to have_content("We shouldn't see this.")
end
end
Expand Down
37 changes: 37 additions & 0 deletions spec/lib/cuprite/browser_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

describe Capybara::Cuprite::Browser do
describe "url_blacklist option" do
it "is an alias of url_blocklist, for backwards compatibility" do
browser = described_class.new(url_blacklist: ["example"])

expect([browser.url_blocklist, browser.url_blacklist]).to all contain_exactly(/example/i)
end
end

describe "url_whitelist option" do
it "is an alias of url_allowlist, for backwards compatibility" do
browser = described_class.new(url_whitelist: ["example"])

expect([browser.url_allowlist, browser.url_whitelist]).to all contain_exactly(/example/i)
end
end

describe "#url_blacklist=" do
it "is an alias of #url_blocklist=, for backwards compatibility" do
browser = described_class.new
browser.url_blacklist = ["example"]

expect(browser.url_blocklist).to contain_exactly(/example/i)
end
end

describe "#url_whitelist=" do
it "is an alias of #url_allowlist=, for backwards compatibility" do
browser = described_class.new
browser.url_whitelist = ["example"]

expect(browser.url_allowlist).to contain_exactly(/example/i)
end
end
end
19 changes: 19 additions & 0 deletions spec/lib/cuprite/driver_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

describe Capybara::Cuprite::Driver do
describe "#reset!" do
it "aliases the url_blacklist option, for backwards compatibility" do
driver = described_class.new(nil, { url_blacklist: ["unwanted"] })
driver.reset!

expect(driver.browser.url_blocklist).to contain_exactly(/unwanted/i)
end

it "aliases the url_whitelist option, for backwards compatibility" do
driver = described_class.new(nil, { url_whitelist: ["allowed"] })
driver.reset!

expect(driver.browser.url_allowlist).to contain_exactly(/allowed/i)
end
end
end