Skip to content

Commit

Permalink
refactor(extension): use settings page and local storage to configure…
Browse files Browse the repository at this point in the history
… extension
  • Loading branch information
vringar committed Aug 6, 2024
1 parent b50226e commit 45adb0d
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 45 deletions.
13 changes: 8 additions & 5 deletions Extension/bundled/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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>",
Expand All @@ -25,9 +28,9 @@
"alarms",
"downloads",
"tabs",
"dns",
"mozillaAddons"
"dns"
],

"experiment_apis": {
"sockets": {
"schema": "./privileged/sockets/schema.json",
Expand Down
10 changes: 10 additions & 0 deletions Extension/bundled/settings/settings.html
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>
20 changes: 10 additions & 10 deletions Extension/src/feature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,9 @@ import { NavigationInstrument } from "./background/navigation-instrument";
import * as loggingDB from "./loggingdb";
import { CallstackInstrument } from "./callstack-instrument";

async function main() {
// Read the browser configuration from file
const filename = "browser_params.json";
const raw_config = await browser.profileDirIO.readFile(filename);
let config: any;
if (raw_config) {
config = JSON.parse(raw_config);
console.log("Browser Config:", config);
} else {
async function setup(config: any) {
console.log("Browser Config:", config);
if(!config){
config = {
navigation_instrument: true,
cookie_instrument: true,
Expand Down Expand Up @@ -104,4 +98,10 @@ async function main() {
await browser.profileDirIO.writeFile("OPENWPM_STARTUP_SUCCESS.txt", "");
}

main();
browser.storage.local.onChanged.addListener((changes) => {
const changedItems = Object.keys(changes);
if (!changedItems.includes("initialized")) {
return;
}
setup(changes.config?.newValue)
});
58 changes: 38 additions & 20 deletions openwpm/deploy_browsers/deploy_firefox.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -155,10 +138,11 @@ def deploy_firefox(
# Install extension
ext_loc = os.path.join(root_dir, "../../Extension/openwpm.xpi")
ext_loc = os.path.normpath(ext_loc)
driver.install_addon(ext_loc, temporary=True)
driver.install_addon(ext_loc)
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)
Expand All @@ -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")
34 changes: 24 additions & 10 deletions test/manual_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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...")
Expand All @@ -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)

driver.install_addon(ext_xpi)
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)


Expand Down

0 comments on commit 45adb0d

Please sign in to comment.