Skip to content

Commit a1e6686

Browse files
committed
Disable the Content Security Policy of websites by default
1 parent 8deb6a1 commit a1e6686

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

seleniumbase/config/settings.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@
6767
# Messenger notifications appear when reaching assert statements in Demo Mode.
6868
DEFAULT_MESSAGE_DURATION = 2.55
6969

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+
7075
# If True, an Exception is raised immediately for invalid proxy string syntax.
7176
# If False, a Warning will appear after the test, with no proxy server used.
7277
# (This applies when using --proxy=[PROXY_STRING] for using a proxy server.)

seleniumbase/core/browser_launcher.py

Lines changed: 15 additions & 1 deletion
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,6 +85,14 @@ 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(
8697
downloads_path, proxy_string, proxy_auth,
8798
proxy_user, proxy_pass, user_agent):
@@ -108,6 +119,8 @@ 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:
123+
chrome_options = _add_chrome_disable_csp_extension(chrome_options)
111124
if proxy_string:
112125
if proxy_auth:
113126
chrome_options = _add_chrome_proxy_extension(
@@ -135,7 +148,8 @@ def _create_firefox_profile(downloads_path, proxy_string, user_agent):
135148
profile.set_preference("general.useragent.override", user_agent)
136149
profile.set_preference(
137150
"security.mixed_content.block_active_content", False)
138-
profile.set_preference("security.csp.enable", False)
151+
if settings.DISABLE_CONTENT_SECURITY_POLICY:
152+
profile.set_preference("security.csp.enable", False)
139153
profile.set_preference(
140154
"browser.download.manager.showAlertOnComplete", False)
141155
profile.set_preference("browser.privatebrowsing.autostart", True)

0 commit comments

Comments
 (0)