Skip to content

Commit 4a5f53c

Browse files
authored
Merge pull request #297 from seleniumbase/csp-updates
Disable the Content Security Policy of websites by default
2 parents ebbcc79 + 71032cf commit 4a5f53c

File tree

16 files changed

+84
-30
lines changed

16 files changed

+84
-30
lines changed

examples/github_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class GitHubTests(BaseCase):
99
# "Please wait a few minutes before you try again."
1010
# To avoid this, slow down Selenium actions.
1111
def slow_click(self, css_selector):
12-
time.sleep(1)
12+
time.sleep(1.05)
1313
self.click(css_selector)
1414

1515
def test_github(self):

help_docs/ReadMe.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
### ![http://seleniumbase.com](https://cdn2.hubspot.net/hubfs/100006/images/super_logo_tiny.png "SeleniumBase") SeleniumBase Help Documents:
1+
### <img src="https://cdn2.hubspot.net/hubfs/100006/images/super_square_logo_3a.png" title="SeleniumBase" height="32"> SeleniumBase Help Documents
22

33
> **Table of Contents / Navigation:**
44
> - [**SeleniumBase Features List**](https://github.com/seleniumbase/SeleniumBase/blob/master/help_docs/features_list.md)

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ unittest2
88
selenium==3.141.0
99
requests==2.21.0
1010
urllib3==1.24.1
11-
pytest>=4.3.0
11+
pytest>=4.3.1
1212
pytest-cov>=2.6.1
1313
pytest-html>=1.20.0
1414
pytest-rerunfailures>=6.0

seleniumbase/config/settings.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@
1919

2020
# If True, existing logs from past test runs will be saved and take up space.
2121
# If False, only the logs from the most recent test run will be saved locally.
22+
# You can also archive existing logs on the command line with: "--archive_logs"
2223
ARCHIVE_EXISTING_LOGS = False
2324

2425
# If True, existing downloads from past runs will be saved and take up space.
2526
# If False, only the downloads from the most recent run will be saved locally.
2627
ARCHIVE_EXISTING_DOWNLOADS = False
2728

2829
# Default names for files saved during test failures.
29-
# (These files will get saved to the "latest_logs/" folder)
30+
# (These files will get saved to the "latest_logs/" folder.)
3031
SCREENSHOT_NAME = "screenshot.png"
3132
BASIC_INFO_NAME = "basic_test_info.txt"
3233
PAGE_SOURCE_NAME = "page_source.html"
@@ -66,6 +67,11 @@
6667
# Messenger notifications appear when reaching assert statements in Demo Mode.
6768
DEFAULT_MESSAGE_DURATION = 2.55
6869

70+
# If True, the Content Security Policy will be disabled on Chrome and Firefox.
71+
# If False, each website's default Content Security Policy will be used.
72+
# (A website's CSP may prevent SeleniumBase from loading custom JavaScript.)
73+
DISABLE_CONTENT_SECURITY_POLICY = True
74+
6975
# If True, an Exception is raised immediately for invalid proxy string syntax.
7076
# If False, a Warning will appear after the test, with no proxy server used.
7177
# (This applies when using --proxy=[PROXY_STRING] for using a proxy server.)

seleniumbase/console_scripts/ReadMe.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## Console Scripts
1+
## <img src="https://cdn2.hubspot.net/hubfs/100006/images/super_square_logo_3a.png" title="SeleniumBase" height="32"> Console Scripts
22

33
SeleniumBase console scripts help you get things done more easily, such as installing web drivers, creating a test directory with necessary configuration files, converting old Webdriver unittest scripts into SeleniumBase code, and using the Selenium Grid.
44

seleniumbase/core/browser_launcher.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616
from seleniumbase.fixtures import constants
1717
from seleniumbase.fixtures import page_utils
1818
from seleniumbase import drivers # webdriver storage folder for SeleniumBase
19+
from seleniumbase import extensions # browser extensions storage folder
1920
DRIVER_DIR = os.path.dirname(os.path.realpath(drivers.__file__))
21+
EXTENSIONS_DIR = os.path.dirname(os.path.realpath(extensions.__file__))
22+
DISABLE_CSP_ZIP_PATH = "%s/%s" % (EXTENSIONS_DIR, "disable_csp.zip")
2023
PROXY_ZIP_PATH = proxy_helper.PROXY_ZIP_PATH
2124
PROXY_ZIP_PATH_2 = proxy_helper.PROXY_ZIP_PATH_2
2225
PLATFORM = sys.platform
@@ -82,8 +85,16 @@ def _add_chrome_proxy_extension(
8285
return chrome_options
8386

8487

88+
def _add_chrome_disable_csp_extension(chrome_options):
89+
""" Disable Chrome's Content-Security-Policy with a browser extension.
90+
See https://github.com/PhilGrayson/chrome-csp-disable for details. """
91+
disable_csp_zip = DISABLE_CSP_ZIP_PATH
92+
chrome_options.add_extension(disable_csp_zip)
93+
return chrome_options
94+
95+
8596
def _set_chrome_options(
86-
downloads_path, proxy_string, proxy_auth,
97+
downloads_path, headless, proxy_string, proxy_auth,
8798
proxy_user, proxy_pass, user_agent):
8899
chrome_options = webdriver.ChromeOptions()
89100
prefs = {
@@ -108,6 +119,10 @@ def _set_chrome_options(
108119
chrome_options.add_argument("--disable-single-click-autofill")
109120
chrome_options.add_argument("--disable-translate")
110121
chrome_options.add_argument("--disable-web-security")
122+
if settings.DISABLE_CONTENT_SECURITY_POLICY and not headless:
123+
# Headless Chrome doesn't support extensions, which are required
124+
# for disabling the Content Security Policy on Chrome
125+
chrome_options = _add_chrome_disable_csp_extension(chrome_options)
111126
if proxy_string:
112127
if proxy_auth:
113128
chrome_options = _add_chrome_proxy_extension(
@@ -135,7 +150,8 @@ def _create_firefox_profile(downloads_path, proxy_string, user_agent):
135150
profile.set_preference("general.useragent.override", user_agent)
136151
profile.set_preference(
137152
"security.mixed_content.block_active_content", False)
138-
profile.set_preference("security.csp.enable", False)
153+
if settings.DISABLE_CONTENT_SECURITY_POLICY:
154+
profile.set_preference("security.csp.enable", False)
139155
profile.set_preference(
140156
"browser.download.manager.showAlertOnComplete", False)
141157
profile.set_preference("browser.privatebrowsing.autostart", True)
@@ -247,7 +263,7 @@ def get_remote_driver(
247263
desired_caps = capabilities_parser.get_desired_capabilities(cap_file)
248264
if browser_name == constants.Browser.GOOGLE_CHROME:
249265
chrome_options = _set_chrome_options(
250-
downloads_path, proxy_string, proxy_auth,
266+
downloads_path, headless, proxy_string, proxy_auth,
251267
proxy_user, proxy_pass, user_agent)
252268
if headless:
253269
if not proxy_auth:
@@ -458,7 +474,7 @@ def get_local_driver(
458474
elif browser_name == constants.Browser.GOOGLE_CHROME:
459475
try:
460476
chrome_options = _set_chrome_options(
461-
downloads_path, proxy_string, proxy_auth,
477+
downloads_path, headless, proxy_string, proxy_auth,
462478
proxy_user, proxy_pass, user_agent)
463479
if headless:
464480
# Headless Chrome doesn't support extensions, which are

seleniumbase/core/download_helper.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ def reset_downloads_folder():
3535

3636
def reset_downloads_folder_assistant(archived_downloads_folder):
3737
if not os.path.exists(archived_downloads_folder):
38-
os.makedirs(archived_downloads_folder)
38+
try:
39+
os.makedirs(archived_downloads_folder)
40+
except Exception:
41+
pass # Should only be reachable during multi-threaded test runs
3942
new_archived_downloads_sub_folder = "%s/downloads_%s" % (
4043
archived_downloads_folder, int(time.time()))
4144
if os.path.exists(downloads_path):

seleniumbase/core/report_helper.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ def clear_out_old_report_logs(archive_past_runs=True, get_log_folder=False):
6666
abs_path = os.path.abspath('.')
6767
file_path = abs_path + "/%s" % LATEST_REPORT_DIR
6868
if not os.path.exists(file_path):
69-
os.makedirs(file_path)
69+
try:
70+
os.makedirs(file_path)
71+
except Exception:
72+
pass # Should only be reachable during multi-threaded runs
7073

7174
if archive_past_runs:
7275
archive_timestamp = int(time.time())

seleniumbase/drivers/ReadMe.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
### SeleniumBase web driver storage
1+
### <img src="https://cdn2.hubspot.net/hubfs/100006/images/super_square_logo_3a.png" title="SeleniumBase" height="32"> SeleniumBase web driver storage
22

33
#### Usage:
44

seleniumbase/extensions/ReadMe.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
### <img src="https://cdn2.hubspot.net/hubfs/100006/images/super_square_logo_3a.png" title="SeleniumBase" height="32"> SeleniumBase browser extension storage
2+
3+
**The List:**
4+
* disable_csp.zip => Disable Chrome's Content-Security-Policy

0 commit comments

Comments
 (0)