-
Notifications
You must be signed in to change notification settings - Fork 315
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(extension): use settings page and local storage to configure…
… extension
- Loading branch information
Showing
5 changed files
with
88 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,12 +7,15 @@ | |
"scripts": ["feature.js"] | ||
}, | ||
"content_scripts": [], | ||
"applications": { | ||
"browser_specific_settings": { | ||
"gecko": { | ||
"id": "[email protected]", | ||
"strict_min_version": "60.0" | ||
"id": "[email protected]" | ||
} | ||
}, | ||
|
||
"options_ui": { | ||
"page": "settings/settings.html" | ||
}, | ||
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self';", | ||
"permissions": [ | ||
"<all_urls>", | ||
|
@@ -25,9 +28,9 @@ | |
"alarms", | ||
"downloads", | ||
"tabs", | ||
"dns", | ||
"mozillaAddons" | ||
"dns" | ||
], | ||
|
||
"experiment_apis": { | ||
"sockets": { | ||
"schema": "./privileged/sockets/schema.json", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
</head> | ||
|
||
<body> | ||
<h1>THIS PAGE SHOULD ONLY BE USED BY SELENIUM TO PROGRAMATICALLY SET THE SETTINGS</h1> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,12 +4,14 @@ | |
import subprocess | ||
import tempfile | ||
from pathlib import Path | ||
from time import sleep | ||
from typing import Any, Dict, Optional, Tuple | ||
|
||
from easyprocess import EasyProcessError | ||
from multiprocess import Queue | ||
from pyvirtualdisplay import Display | ||
from selenium import webdriver | ||
from selenium.webdriver.common.by import By | ||
from selenium.webdriver.firefox.options import Options | ||
from selenium.webdriver.firefox.service import Service | ||
|
||
|
@@ -98,25 +100,6 @@ def deploy_firefox( | |
# because status_queue is read off no matter what. | ||
status_queue.put(("STATUS", "Display", (display_pid, display_port))) | ||
|
||
# Write config file | ||
extension_config: Dict[str, Any] = dict() | ||
extension_config.update(browser_params.to_dict()) | ||
extension_config["logger_address"] = manager_params.logger_address | ||
extension_config["storage_controller_address"] = ( | ||
manager_params.storage_controller_address | ||
) | ||
extension_config["testing"] = manager_params.testing | ||
ext_config_file = browser_profile_path / "browser_params.json" | ||
with open(ext_config_file, "w") as f: | ||
json.dump(extension_config, f, cls=ConfigEncoder) | ||
logger.debug( | ||
"BROWSER %i: Saved extension config file to: %s" | ||
% (browser_params.browser_id, ext_config_file) | ||
) | ||
|
||
# TODO restore detailed logging | ||
# fo.set_preference("[email protected]", "all") | ||
|
||
# Configure privacy settings | ||
configure_firefox.privacy(browser_params, fo) | ||
|
||
|
@@ -159,6 +142,7 @@ def deploy_firefox( | |
logger.debug( | ||
"BROWSER %i: OpenWPM Firefox extension loaded" % browser_params.browser_id | ||
) | ||
apply_extension_configuration(driver, browser_params, manager_params) | ||
|
||
# set window size | ||
driver.set_window_size(*DEFAULT_SCREEN_RES) | ||
|
@@ -172,3 +156,37 @@ def deploy_firefox( | |
status_queue.put(("STATUS", "Browser Launched", int(pid))) | ||
|
||
return driver, browser_profile_path, display | ||
|
||
|
||
def apply_extension_configuration( | ||
driver: webdriver.Firefox, | ||
browser_params: BrowserParamsInternal, | ||
manager_params: ManagerParamsInternal, | ||
) -> None: | ||
# Write config file | ||
extension_config: Dict[str, Any] = dict() | ||
extension_config.update(browser_params.to_dict()) | ||
extension_config["logger_address"] = manager_params.logger_address | ||
extension_config["storage_controller_address"] = ( | ||
manager_params.storage_controller_address | ||
) | ||
extension_config["testing"] = manager_params.testing | ||
config = json.dumps(extension_config, cls=ConfigEncoder) | ||
driver.get("about:debugging#/runtime/this-firefox") | ||
sleep(0.1) | ||
extension_id = driver.find_element( | ||
By.XPATH, "//span[@title='OpenWPM']/../section/dl/div[2]/dd" | ||
).text | ||
driver.get(f"moz-extension://{extension_id}/settings/settings.html") | ||
driver.execute_script( | ||
f""" | ||
browser.storage.local.set({{ | ||
config: {config}, | ||
initialized: true | ||
}}); | ||
""" | ||
) | ||
logger.debug("BROWSER %i: Set extension configuration:", browser_params.browser_id) | ||
|
||
# TODO restore detailed logging | ||
# fo.set_preference("[email protected]", "all") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,12 +8,15 @@ | |
import click | ||
import IPython | ||
from selenium import webdriver | ||
from selenium.webdriver.common.by import By | ||
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary | ||
from selenium.webdriver.firefox.options import Options | ||
from selenium.webdriver.firefox.service import Service | ||
|
||
from openwpm import js_instrumentation as jsi | ||
from openwpm.config import BrowserParams | ||
from openwpm.config import BrowserParams, BrowserParamsInternal, ManagerParamsInternal | ||
from openwpm.deploy_browsers import configure_firefox | ||
from openwpm.deploy_browsers.deploy_firefox import apply_extension_configuration | ||
from openwpm.utilities.platform_utils import get_firefox_binary_path | ||
|
||
from .conftest import xpi | ||
|
@@ -102,11 +105,10 @@ def start_webdriver( | |
""" | ||
firefox_binary_path = get_firefox_binary_path() | ||
|
||
fb = FirefoxBinary(firefox_path=firefox_binary_path) | ||
server, thread = start_server() | ||
|
||
def register_cleanup(driver): | ||
driver.get(BASE_TEST_URL) | ||
# driver.get(BASE_TEST_URL) | ||
|
||
def cleanup_server(): | ||
print("Cleanup before shutdown...") | ||
|
@@ -131,30 +133,42 @@ def cleanup_server(): | |
# fp.set_preference("[email protected]", "all") | ||
configure_firefox.optimize_prefs(fo) | ||
|
||
driver = webdriver.Firefox(firefox_binary=fb, options=fo) | ||
fo.binary_location = firefox_binary_path | ||
geckodriver_path = subprocess.check_output( | ||
"which geckodriver", encoding="utf-8", shell=True | ||
).strip() | ||
driver = webdriver.Firefox( | ||
options=fo, | ||
service=Service( | ||
executable_path=geckodriver_path, | ||
), | ||
) | ||
browser_params = BrowserParamsInternal() | ||
|
||
if load_browser_params is True: | ||
# There's probably more we could do here | ||
# to set more preferences and better emulate | ||
# what happens in TaskManager. But this lets | ||
# us pass some basic things. | ||
|
||
browser_params = BrowserParams() | ||
if browser_params_file is not None: | ||
with open(browser_params_file, "r") as f: | ||
browser_params.from_json(f.read()) | ||
js_request = browser_params.js_instrument_settings | ||
js_request_as_string = jsi.clean_js_instrumentation_settings(js_request) | ||
browser_params.js_instrument_settings = js_request_as_string | ||
|
||
with open(browser_profile_path / "browser_params.json", "w") as f: | ||
f.write(browser_params.to_json()) | ||
|
||
if with_extension: | ||
# add openwpm extension to profile | ||
xpi() | ||
ext_xpi = join(EXT_PATH, "dist", "openwpm-1.0.zip") | ||
driver.install_addon(ext_xpi, temporary=True) | ||
|
||
manager_params = ManagerParamsInternal() | ||
manager_params.logger_address = None | ||
manager_params.storage_controller_address = None | ||
manager_params.testing = True | ||
browser_params.browser_id = 1 | ||
apply_extension_configuration(driver, browser_params, manager_params) | ||
return register_cleanup(driver) | ||
|
||
|
||
|