From e9b19f1cb34322be170d0e3420d75b965534f191 Mon Sep 17 00:00:00 2001 From: Jan-Felix Date: Wed, 4 Jan 2023 15:45:58 +0100 Subject: [PATCH 1/4] fix an issue with the request overrides this makes etherscan work again --- extension/public/manifest.json | 9 ---- extension/public/requestRules.json | 17 ------- extension/src/background.ts | 56 ++++++++++++++++++++-- extension/src/settings/connectionHooks.tsx | 4 +- 4 files changed, 55 insertions(+), 31 deletions(-) delete mode 100644 extension/public/requestRules.json diff --git a/extension/public/manifest.json b/extension/public/manifest.json index 25634442..1f911b33 100644 --- a/extension/public/manifest.json +++ b/extension/public/manifest.json @@ -16,15 +16,6 @@ "background": { "service_worker": "build/background.js" }, - "declarative_net_request": { - "rule_resources": [ - { - "id": "rules1", - "enabled": true, - "path": "requestRules.json" - } - ] - }, "host_permissions": [""], "content_scripts": [ { diff --git a/extension/public/requestRules.json b/extension/public/requestRules.json deleted file mode 100644 index bd7eb226..00000000 --- a/extension/public/requestRules.json +++ /dev/null @@ -1,17 +0,0 @@ -[ - { - "id": 1, - "priority": 1, - "action": { - "type": "modifyHeaders", - "responseHeaders": [ - { "header": "x-frame-options", "operation": "remove" }, - { "header": "content-security-policy", "operation": "remove" } - ] - }, - "condition": { - "resourceTypes": ["sub_frame"], - "initiatorDomains": ["pilot.gnosisguild.org"] - } - } -] diff --git a/extension/src/background.ts b/extension/src/background.ts index b3edcbef..8ea581da 100644 --- a/extension/src/background.ts +++ b/extension/src/background.ts @@ -1,6 +1,39 @@ // Attention: The URL must also be updated in manifest.json const PILOT_URL = 'https://pilot.gnosisguild.org/' +// Track tabs showing our extension, so we can dynamically adjust the declarativeNetRequest rule. +// This rule removes some headers so foreign pages can be loaded in iframes. We don't want to +// generally circumvent this security mechanism, so we only apply it to extension tabs. +const activeExtensionTabs = new Set() + +const updateRule = () => { + const RULE_ID = 1 + chrome.declarativeNetRequest.updateSessionRules({ + addRules: [ + { + id: RULE_ID, + priority: 1, + action: { + // @ts-expect-error @types/chrome has not been updated for Chrome Extensions Manifest V3 + type: 'modifyHeaders', + responseHeaders: [ + // @ts-expect-error @types/chrome has not been updated for Chrome Extensions Manifest V3 + { header: 'x-frame-options', operation: 'remove' }, + // @ts-expect-error @types/chrome has not been updated for Chrome Extensions Manifest V3 + { header: 'content-security-policy', operation: 'remove' }, + ], + }, + condition: { + // @ts-expect-error @types/chrome has not been updated for Chrome Extensions Manifest V3 + resourceTypes: ['sub_frame'], + tabIds: Array.from(activeExtensionTabs), + }, + }, + ], + removeRuleIds: [RULE_ID], + }) +} + // When clicking the extension button, load the current tab's page in the simulation browser const toggle = async (tab: chrome.tabs.Tab) => { if (!tab.id || !tab.url) return @@ -8,6 +41,9 @@ const toggle = async (tab: chrome.tabs.Tab) => { if (!tab.url.startsWith(PILOT_URL)) { console.log('activate Zodiac Pilot') + // add to tracked list + activeExtensionTabs.add(tab.id) + const url = tab.url.startsWith('chrome://') || tab.url.startsWith('about:') ? '' @@ -18,6 +54,9 @@ const toggle = async (tab: chrome.tabs.Tab) => { } else { console.log('deactivate Zodiac Pilot') + // remove from tracked list + activeExtensionTabs.delete(tab.id) + const url = new URL(tab.url) const appUrl = decodeURIComponent(url.hash.slice(1)) @@ -28,9 +67,20 @@ const toggle = async (tab: chrome.tabs.Tab) => { } chrome.action.onClicked.addListener(toggle) -// launch extension script on matching URLs -chrome.tabs.onUpdated.addListener(function (tabId, changeInfo) { - if (changeInfo.status === 'complete') { +chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) { + const isExtensionTab = !!tab.url?.startsWith(PILOT_URL) + const wasExtensionTab = activeExtensionTabs.has(tabId) + + if (isExtensionTab && !wasExtensionTab) { + activeExtensionTabs.add(tabId) + updateRule() + } + if (!isExtensionTab && wasExtensionTab) { + activeExtensionTabs.delete(tabId) + updateRule() + } + + if (changeInfo.status === 'complete' && isExtensionTab) { chrome.tabs.sendMessage(tabId, { type: 'navigationDetected' }) } }) diff --git a/extension/src/settings/connectionHooks.tsx b/extension/src/settings/connectionHooks.tsx index 104e0a7a..07fc606d 100644 --- a/extension/src/settings/connectionHooks.tsx +++ b/extension/src/settings/connectionHooks.tsx @@ -36,9 +36,9 @@ export const ProvideConnections: React.FC<{ children: ReactNode }> = ({ DEFAULT_VALUE, 'connections' ) - console.log({ storedConnections }) + const connections = migrateConnections(storedConnections) - console.log({ connections }) + const [selectedConnectionId, setSelectedConnectionId] = useStickyState(connections[0].id, 'selectedConnection') From 577b1135104b01774aad802f5f4e2fd7daa7935b Mon Sep 17 00:00:00 2001 From: Jan-Felix Date: Thu, 5 Jan 2023 10:34:19 +0100 Subject: [PATCH 2/4] make connecting to etherscan work --- extension/src/bridge/iframe.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/extension/src/bridge/iframe.ts b/extension/src/bridge/iframe.ts index 901104d1..2462c4af 100644 --- a/extension/src/bridge/iframe.ts +++ b/extension/src/bridge/iframe.ts @@ -94,4 +94,9 @@ export default class BridgeIframe extends EventEmitter { } isZodiacPilot = true + + // This is required for connecting to Etherscan + enable() { + return Promise.resolve() + } } From 420b4a45d08bd7ce683c400c95b7e8a56ac9d50f Mon Sep 17 00:00:00 2001 From: Jan-Felix Date: Thu, 5 Jan 2023 11:39:57 +0100 Subject: [PATCH 3/4] fix connection to Stakewise --- extension/src/bridge/iframe.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/extension/src/bridge/iframe.ts b/extension/src/bridge/iframe.ts index 2462c4af..b897ea5f 100644 --- a/extension/src/bridge/iframe.ts +++ b/extension/src/bridge/iframe.ts @@ -99,4 +99,7 @@ export default class BridgeIframe extends EventEmitter { enable() { return Promise.resolve() } + + // Stakewise only supports MetaMask and Tally as injected providers, so we pretend to be MetaMask + isMetaMask = window.location.hostname === 'app.stakewise.io' } From 8fc657139a6868e07b02be70be30389659073b03 Mon Sep 17 00:00:00 2001 From: Jan-Felix Date: Thu, 5 Jan 2023 11:54:33 +0100 Subject: [PATCH 4/4] spell check fix --- extension/.cspell.json | 1 + 1 file changed, 1 insertion(+) diff --git a/extension/.cspell.json b/extension/.cspell.json index 982ecaae..5abc2f0a 100644 --- a/extension/.cspell.json +++ b/extension/.cspell.json @@ -34,6 +34,7 @@ "Sindre", "Sorhus", "Sourcify", + "Stakewise", "Sushiswap", "toastify", "typechain",