From eefa3cb57aaf0f749f82f5178de502079162916d Mon Sep 17 00:00:00 2001 From: augmentedmode Date: Wed, 27 Nov 2024 13:17:05 -0500 Subject: [PATCH 01/16] feat: add websocket support for c2 detection --- app/manifest/v3/_base.json | 4 +++- app/scripts/background.js | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/app/manifest/v3/_base.json b/app/manifest/v3/_base.json index 4d6ee38437d3..89758033f33a 100644 --- a/app/manifest/v3/_base.json +++ b/app/manifest/v3/_base.json @@ -50,7 +50,9 @@ "http://localhost:8545/", "file://*/*", "http://*/*", - "https://*/*" + "https://*/*", + "ws://*/*", + "wss://*/*" ], "icons": { "16": "images/icon-16.png", diff --git a/app/scripts/background.js b/app/scripts/background.js index 90a52b6c0d19..4e09fe5db70b 100644 --- a/app/scripts/background.js +++ b/app/scripts/background.js @@ -329,7 +329,7 @@ function maybeDetectPhishing(theController) { return {}; }, { - urls: ['http://*/*', 'https://*/*'], + urls: ['http://*/*', 'https://*/*', 'ws://*/*', 'wss://*/*'], }, isManifestV2 ? ['blocking'] : [], ); From 6f1da541da566963f582f79830df43048c2bfed6 Mon Sep 17 00:00:00 2001 From: augmentedmode Date: Wed, 27 Nov 2024 14:06:12 -0500 Subject: [PATCH 02/16] fix: add support to v2 manifest --- app/manifest/v2/_base.json | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/manifest/v2/_base.json b/app/manifest/v2/_base.json index f29b7458a9e5..2f41a7e987fa 100644 --- a/app/manifest/v2/_base.json +++ b/app/manifest/v2/_base.json @@ -66,6 +66,8 @@ "clipboardWrite", "http://*/*", "https://*/*", + "ws://*/*", + "wss://*/*", "activeTab", "webRequest", "webRequestBlocking", From 825913b1b0b6b36da9cc3d8db5005dce8b320eb2 Mon Sep 17 00:00:00 2001 From: augmentedmode Date: Mon, 2 Dec 2024 09:53:29 -0500 Subject: [PATCH 03/16] chore: add tests for blocking websocket requests --- .../index.html | 79 ++++++++++++++++++ .../mock-safe-websocket-connection/index.html | 79 ++++++++++++++++++ .../phishing-detection.spec.js | 83 +++++++++++++++++++ 3 files changed, 241 insertions(+) create mode 100644 test/e2e/tests/phishing-controller/mock-malicious-websocket-connection/index.html create mode 100644 test/e2e/tests/phishing-controller/mock-safe-websocket-connection/index.html diff --git a/test/e2e/tests/phishing-controller/mock-malicious-websocket-connection/index.html b/test/e2e/tests/phishing-controller/mock-malicious-websocket-connection/index.html new file mode 100644 index 000000000000..874de7839f90 --- /dev/null +++ b/test/e2e/tests/phishing-controller/mock-malicious-websocket-connection/index.html @@ -0,0 +1,79 @@ + + + + WebSocket Test + + + + +

Mock Malicious WebSocket Test

+ + diff --git a/test/e2e/tests/phishing-controller/mock-safe-websocket-connection/index.html b/test/e2e/tests/phishing-controller/mock-safe-websocket-connection/index.html new file mode 100644 index 000000000000..679c7db76686 --- /dev/null +++ b/test/e2e/tests/phishing-controller/mock-safe-websocket-connection/index.html @@ -0,0 +1,79 @@ + + + + E2E Test Dapp + + + + +

Mock Safe WebSocket Test

+ + diff --git a/test/e2e/tests/phishing-controller/phishing-detection.spec.js b/test/e2e/tests/phishing-controller/phishing-detection.spec.js index ad199cea1e70..732b8b8b9f22 100644 --- a/test/e2e/tests/phishing-controller/phishing-detection.spec.js +++ b/test/e2e/tests/phishing-controller/phishing-detection.spec.js @@ -315,6 +315,89 @@ describe('Phishing Detection', function () { ); }); + it('should block a website that makes a WebSocket connection to a malicious c2 site', async function () { + const blockedDomain = 'blocked.example.com'; + const testPageURL = 'http://localhost:8080'; + + await withFixtures( + { + fixtures: new FixtureBuilder().build(), + ganacheOptions: defaultGanacheOptions, + title: this.test.fullTitle(), + testSpecificMock: async (mockServer) => { + await setupPhishingDetectionMocks(mockServer, { + blockProvider: BlockProvider.MetaMask, + c2DomainBlocklist: [ + 'ffd6df34371d7cfc68aef89e124bc84ea874d573d5979290fc22d59a73ae8539', + ], + }); + }, + dapp: true, + dappPaths: [ + './tests/phishing-controller/mock-malicious-websocket-connection', + ], + }, + async ({ driver }) => { + await unlockWallet(driver); + + await driver.openNewPage(testPageURL); + + await driver.switchToWindowWithTitle( + 'MetaMask Phishing Detection', + 10000, + ); + + await driver.waitForSelector({ + testId: 'unsafe-continue-loaded', + }); + + await driver.clickElement({ + text: 'Back to safety', + }); + + const currentUrl = await driver.getCurrentUrl(); + const expectedPortfolioUrl = `https://portfolio.metamask.io/?metamaskEntry=phishing_page_portfolio_button`; + + assert.equal(currentUrl, expectedPortfolioUrl); + }, + ); + }); + + it('should not block a website that makes a safe WebSocket connection', async function () { + const testPageURL = 'http://localhost:8080/'; + + await withFixtures( + { + fixtures: new FixtureBuilder().build(), + ganacheOptions: defaultGanacheOptions, + title: this.test.fullTitle(), + testSpecificMock: async (mockServer) => { + await setupPhishingDetectionMocks(mockServer, { + blockProvider: BlockProvider.MetaMask, + c2DomainBlocklist: [ + 'ffd6df34371d7cfc68aef89e124bc84ea874d573d5979290fc22d59a73ae8539', // malicious c2 domain + ], + }); + }, + dapp: true, + dappPaths: [ + './tests/phishing-controller/mock-safe-websocket-connection', + ], + }, + async ({ driver }) => { + await unlockWallet(driver); + + await driver.openNewPage(testPageURL); + + await driver.wait(until.titleIs(WINDOW_TITLES.TestDApp), 10000); + + const currentUrl = await driver.getCurrentUrl(); + + assert.equal(currentUrl, testPageURL); + }, + ); + }); + describe('Phishing redirect protections', function () { /** * Status codes 305 (via Location header) and 306 (Set-Proxy) header do not From b7d45d199a7af256b360224df1cc90f792933f46 Mon Sep 17 00:00:00 2001 From: augmentedmode Date: Mon, 2 Dec 2024 10:03:07 -0500 Subject: [PATCH 04/16] fix: tests --- .../index.html | 21 ++++++++++------ .../mock-safe-websocket-connection/index.html | 25 ++++++++++--------- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/test/e2e/tests/phishing-controller/mock-malicious-websocket-connection/index.html b/test/e2e/tests/phishing-controller/mock-malicious-websocket-connection/index.html index 874de7839f90..305900979896 100644 --- a/test/e2e/tests/phishing-controller/mock-malicious-websocket-connection/index.html +++ b/test/e2e/tests/phishing-controller/mock-malicious-websocket-connection/index.html @@ -13,15 +13,20 @@ // Simulate connection behavior setTimeout(() => { - if (url.includes('blocked.example.com')) { - window.location.href = `http://localhost:9999/#hostname=${url}&href=${encodeURIComponent( - url, - )}`; - } else { - this.readyState = 1; - if (this.onopen) { - this.onopen(); + try { + const { hostname } = new URL(url); + if (hostname === 'blocked.example.com') { + window.location.href = `http://localhost:9999/#hostname=${url}&href=${encodeURIComponent( + url, + )}`; + } else { + this.readyState = 1; // OPEN + if (this.onopen) { + this.onopen(); + } } + } catch (error) { + console.error('Invalid URL:', url); } }, 100); } diff --git a/test/e2e/tests/phishing-controller/mock-safe-websocket-connection/index.html b/test/e2e/tests/phishing-controller/mock-safe-websocket-connection/index.html index 679c7db76686..59340aa29680 100644 --- a/test/e2e/tests/phishing-controller/mock-safe-websocket-connection/index.html +++ b/test/e2e/tests/phishing-controller/mock-safe-websocket-connection/index.html @@ -8,20 +8,22 @@ constructor(url) { this.url = url; this.readyState = 0; - - console.log(`MockWebSocket: Attempting to connect to ${url}`); - // Simulate connection behavior setTimeout(() => { - if (url.includes('blocked.example.com')) { - window.location.href = `http://localhost:9999/#hostname=${url}&href=${encodeURIComponent( - url, - )}`; - } else { - this.readyState = 1; - if (this.onopen) { - this.onopen(); + try { + const { hostname } = new URL(url); + if (hostname === 'safe.example.com') { + window.location.href = `http://localhost:9999/#hostname=${url}&href=${encodeURIComponent( + url, + )}`; + } else { + this.readyState = 1; // OPEN + if (this.onopen) { + this.onopen(); + } } + } catch (error) { + console.error('Invalid URL:', url); } }, 100); } @@ -30,7 +32,6 @@ if (this.readyState !== 1) { throw new Error('WebSocket is not open'); } - console.log(`MockWebSocket: Sent message: ${message}`); setTimeout(() => { if (this.onmessage) { this.onmessage({ data: `Echo: ${message}` }); From 34459627e3d2730dfe55459fbe40b00e4b91fd2a Mon Sep 17 00:00:00 2001 From: augmentedmode Date: Mon, 2 Dec 2024 10:34:53 -0500 Subject: [PATCH 05/16] chore: remove unused var --- test/e2e/tests/phishing-controller/phishing-detection.spec.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/e2e/tests/phishing-controller/phishing-detection.spec.js b/test/e2e/tests/phishing-controller/phishing-detection.spec.js index 732b8b8b9f22..24b11d691441 100644 --- a/test/e2e/tests/phishing-controller/phishing-detection.spec.js +++ b/test/e2e/tests/phishing-controller/phishing-detection.spec.js @@ -316,7 +316,6 @@ describe('Phishing Detection', function () { }); it('should block a website that makes a WebSocket connection to a malicious c2 site', async function () { - const blockedDomain = 'blocked.example.com'; const testPageURL = 'http://localhost:8080'; await withFixtures( @@ -328,7 +327,7 @@ describe('Phishing Detection', function () { await setupPhishingDetectionMocks(mockServer, { blockProvider: BlockProvider.MetaMask, c2DomainBlocklist: [ - 'ffd6df34371d7cfc68aef89e124bc84ea874d573d5979290fc22d59a73ae8539', + 'ffd6df34371d7cfc68aef89e124bc84ea874d573d5979290fc22d59a73ae8539', // malicious c2 domain ], }); }, From f350970087bb079183ce0c695cbaa995738c6439 Mon Sep 17 00:00:00 2001 From: augmentedmode Date: Tue, 3 Dec 2024 08:57:09 -0500 Subject: [PATCH 06/16] chore: fix test name for malicious c2 --- test/e2e/tests/phishing-controller/phishing-detection.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/e2e/tests/phishing-controller/phishing-detection.spec.js b/test/e2e/tests/phishing-controller/phishing-detection.spec.js index 24b11d691441..eaacf0d14626 100644 --- a/test/e2e/tests/phishing-controller/phishing-detection.spec.js +++ b/test/e2e/tests/phishing-controller/phishing-detection.spec.js @@ -315,7 +315,7 @@ describe('Phishing Detection', function () { ); }); - it('should block a website that makes a WebSocket connection to a malicious c2 site', async function () { + it('should block a website that makes a WebSocket connection to a malicious command and control server', async function () { const testPageURL = 'http://localhost:8080'; await withFixtures( From d02b738316301573a5e771d0f73a945f35013050 Mon Sep 17 00:00:00 2001 From: augmentedmode Date: Tue, 3 Dec 2024 11:53:51 -0500 Subject: [PATCH 07/16] chore: cleanup --- test/e2e/tests/phishing-controller/phishing-detection.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/e2e/tests/phishing-controller/phishing-detection.spec.js b/test/e2e/tests/phishing-controller/phishing-detection.spec.js index eaacf0d14626..8320c2ec0fb4 100644 --- a/test/e2e/tests/phishing-controller/phishing-detection.spec.js +++ b/test/e2e/tests/phishing-controller/phishing-detection.spec.js @@ -315,7 +315,7 @@ describe('Phishing Detection', function () { ); }); - it('should block a website that makes a WebSocket connection to a malicious command and control server', async function () { + it('should block a website that makes a websocket connection to a malicious command and control server', async function () { const testPageURL = 'http://localhost:8080'; await withFixtures( From 47e8249e088ec4354eab9ec39d9bf744d6c424a0 Mon Sep 17 00:00:00 2001 From: augmentedmode Date: Mon, 9 Dec 2024 12:15:04 -0500 Subject: [PATCH 08/16] feat: add webocket support to fixtures --- test/e2e/helpers.js | 63 ++++++++++++++ .../index.html | 84 ------------------- .../mock-safe-websocket-connection/index.html | 80 ------------------ test/e2e/tests/phishing-controller/mocks.js | 19 ++--- .../phishing-detection.spec.js | 17 ++-- 5 files changed, 79 insertions(+), 184 deletions(-) delete mode 100644 test/e2e/tests/phishing-controller/mock-malicious-websocket-connection/index.html delete mode 100644 test/e2e/tests/phishing-controller/mock-safe-websocket-connection/index.html diff --git a/test/e2e/helpers.js b/test/e2e/helpers.js index b06c29b17acf..68c553769d1e 100644 --- a/test/e2e/helpers.js +++ b/test/e2e/helpers.js @@ -4,6 +4,7 @@ const BigNumber = require('bignumber.js'); const mockttp = require('mockttp'); const detectPort = require('detect-port'); const { difference } = require('lodash'); +const WebSocket = require('ws'); const createStaticServer = require('../../development/create-static-server'); const { setupMocking } = require('./mock-e2e'); const { Ganache } = require('./seeder/ganache'); @@ -77,6 +78,7 @@ async function withFixtures(options, testSuite) { usePaymaster, ethConversionInUsd, manifestFlags, + enableWebSocketServer = false, } = options; const fixtureServer = new FixtureServer(); @@ -92,6 +94,8 @@ async function withFixtures(options, testSuite) { const dappServer = []; const phishingPageServer = new PhishingWarningPageServer(); + let webSocketServer; + if (!disableServerMochaToBackground) { getServerMochaToBackground(); } @@ -117,6 +121,16 @@ async function withFixtures(options, testSuite) { contractRegistry = ganacheSeeder.getContractRegistry(); } + if (enableWebSocketServer) { + webSocketServer = new WebSocket.Server({ port: 8081, host: '0.0.0.0' }); + + webSocketServer.on('connection', function connection(ws) { + ws.on('message', function incoming(message) { + ws.send('Hello, client! I am the server.', message); + }); + }); + } + await fixtureServer.start(); fixtureServer.loadJsonState(fixtures, contractRegistry); @@ -307,6 +321,12 @@ async function withFixtures(options, testSuite) { await ganacheServer.quit(); } + if (webSocketServer) { + webSocketServer.close(() => { + console.log('WebSocket server closed'); + }); + } + if (ganacheOptions?.concurrent) { secondaryGanacheServer.forEach(async (server) => { await server.quit(); @@ -640,6 +660,48 @@ async function unlockWallet( } } +/** + * Simulates a WebSocket connection by executing a script in the browser context. + * + * @param {WebDriver} driver - The WebDriver instance. + * @param {string} hostname - The hostname to connect to. + */ +async function createWebSocketConnection(driver, hostname) { + try { + await driver.executeScript(async (wsHostname) => { + const url = `ws://${wsHostname}:8081`; + + const socket = new WebSocket(url); + + socket.onopen = () => { + console.log('WebSocket connection opened'); + socket.send('Hello, server!'); + }; + + socket.onerror = (error) => { + console.error( + 'WebSocket error:', + error.message || 'Connection blocked', + ); + }; + + socket.onmessage = (event) => { + console.log('Message received from server:', event.data); + }; + + socket.onclose = () => { + console.log('WebSocket connection closed'); + }; + }, hostname); + } catch (error) { + console.error( + `Failed to execute WebSocket connection script for ws://${hostname}:8081`, + error, + ); + throw error; + } +} + const logInWithBalanceValidation = async (driver, ganacheServer) => { await unlockWallet(driver); // Wait for balance to load @@ -975,4 +1037,5 @@ module.exports = { tempToggleSettingRedesignedTransactionConfirmations, openMenuSafe, sentryRegEx, + createWebSocketConnection, }; diff --git a/test/e2e/tests/phishing-controller/mock-malicious-websocket-connection/index.html b/test/e2e/tests/phishing-controller/mock-malicious-websocket-connection/index.html deleted file mode 100644 index 305900979896..000000000000 --- a/test/e2e/tests/phishing-controller/mock-malicious-websocket-connection/index.html +++ /dev/null @@ -1,84 +0,0 @@ - - - - WebSocket Test - - - - -

Mock Malicious WebSocket Test

- - diff --git a/test/e2e/tests/phishing-controller/mock-safe-websocket-connection/index.html b/test/e2e/tests/phishing-controller/mock-safe-websocket-connection/index.html deleted file mode 100644 index 59340aa29680..000000000000 --- a/test/e2e/tests/phishing-controller/mock-safe-websocket-connection/index.html +++ /dev/null @@ -1,80 +0,0 @@ - - - - E2E Test Dapp - - - - -

Mock Safe WebSocket Test

- - diff --git a/test/e2e/tests/phishing-controller/mocks.js b/test/e2e/tests/phishing-controller/mocks.js index fe11118c6fd2..3f7ff23f453a 100644 --- a/test/e2e/tests/phishing-controller/mocks.js +++ b/test/e2e/tests/phishing-controller/mocks.js @@ -10,7 +10,9 @@ const { const lastUpdated = 1; const defaultHotlist = { data: [] }; const defaultC2DomainBlocklist = { - recentlyAdded: [], + recentlyAdded: [ + '33c8e026e76cea2df82322428554c932961cd80080fa379454350d7f13371f36', + ], recentlyRemoved: [], lastFetchedAt: '2024-08-27T15:30:45Z', }; @@ -95,15 +97,12 @@ async function setupPhishingDetectionMocks( }; }); - await mockServer - .forGet(C2_DOMAIN_BLOCKLIST_URL) - .withQuery({ timestamp: '2024-08-27T15:30:45Z' }) - .thenCallback(() => { - return { - statusCode: 200, - json: defaultC2DomainBlocklist, - }; - }); + await mockServer.forGet(C2_DOMAIN_BLOCKLIST_URL).thenCallback(() => { + return { + statusCode: 200, + json: defaultC2DomainBlocklist, + }; + }); await mockServer .forGet('https://github.com/MetaMask/eth-phishing-detect/issues/new') diff --git a/test/e2e/tests/phishing-controller/phishing-detection.spec.js b/test/e2e/tests/phishing-controller/phishing-detection.spec.js index 8320c2ec0fb4..e1837688518f 100644 --- a/test/e2e/tests/phishing-controller/phishing-detection.spec.js +++ b/test/e2e/tests/phishing-controller/phishing-detection.spec.js @@ -2,13 +2,13 @@ const { strict: assert } = require('assert'); const { createServer } = require('node:http'); const { createDeferredPromise } = require('@metamask/utils'); const { until } = require('selenium-webdriver'); - const { defaultGanacheOptions, withFixtures, openDapp, unlockWallet, WINDOW_TITLES, + createWebSocketConnection, } = require('../../helpers'); const FixtureBuilder = require('../../fixture-builder'); const { @@ -326,21 +326,18 @@ describe('Phishing Detection', function () { testSpecificMock: async (mockServer) => { await setupPhishingDetectionMocks(mockServer, { blockProvider: BlockProvider.MetaMask, - c2DomainBlocklist: [ - 'ffd6df34371d7cfc68aef89e124bc84ea874d573d5979290fc22d59a73ae8539', // malicious c2 domain - ], }); }, dapp: true, - dappPaths: [ - './tests/phishing-controller/mock-malicious-websocket-connection', - ], + enableWebSocketServer: true, }, async ({ driver }) => { await unlockWallet(driver); await driver.openNewPage(testPageURL); + await createWebSocketConnection(driver, 'malicious.localhost'); + await driver.switchToWindowWithTitle( 'MetaMask Phishing Detection', 10000, @@ -379,15 +376,15 @@ describe('Phishing Detection', function () { }); }, dapp: true, - dappPaths: [ - './tests/phishing-controller/mock-safe-websocket-connection', - ], + enableWebSocketServer: true, }, async ({ driver }) => { await unlockWallet(driver); await driver.openNewPage(testPageURL); + await createWebSocketConnection(driver, 'safe.localhost'); + await driver.wait(until.titleIs(WINDOW_TITLES.TestDApp), 10000); const currentUrl = await driver.getCurrentUrl(); From bb5505d7390d10eca1c886428d8377b46817f4ec Mon Sep 17 00:00:00 2001 From: augmentedmode Date: Mon, 9 Dec 2024 12:20:55 -0500 Subject: [PATCH 09/16] chore: cleanup --- test/e2e/tests/phishing-controller/phishing-detection.spec.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/test/e2e/tests/phishing-controller/phishing-detection.spec.js b/test/e2e/tests/phishing-controller/phishing-detection.spec.js index e1837688518f..703ebe91f4c6 100644 --- a/test/e2e/tests/phishing-controller/phishing-detection.spec.js +++ b/test/e2e/tests/phishing-controller/phishing-detection.spec.js @@ -370,9 +370,6 @@ describe('Phishing Detection', function () { testSpecificMock: async (mockServer) => { await setupPhishingDetectionMocks(mockServer, { blockProvider: BlockProvider.MetaMask, - c2DomainBlocklist: [ - 'ffd6df34371d7cfc68aef89e124bc84ea874d573d5979290fc22d59a73ae8539', // malicious c2 domain - ], }); }, dapp: true, From e52d45c03c966beeb6bee49895f6acb80de3fc28 Mon Sep 17 00:00:00 2001 From: augmentedmode Date: Mon, 9 Dec 2024 13:49:10 -0500 Subject: [PATCH 10/16] chore: add context to c2 hash in mocks --- test/e2e/tests/phishing-controller/mocks.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/e2e/tests/phishing-controller/mocks.js b/test/e2e/tests/phishing-controller/mocks.js index 3f7ff23f453a..3165847740bf 100644 --- a/test/e2e/tests/phishing-controller/mocks.js +++ b/test/e2e/tests/phishing-controller/mocks.js @@ -11,7 +11,7 @@ const lastUpdated = 1; const defaultHotlist = { data: [] }; const defaultC2DomainBlocklist = { recentlyAdded: [ - '33c8e026e76cea2df82322428554c932961cd80080fa379454350d7f13371f36', + '33c8e026e76cea2df82322428554c932961cd80080fa379454350d7f13371f36', // hash for malicious.localhost ], recentlyRemoved: [], lastFetchedAt: '2024-08-27T15:30:45Z', From a3dced824f08dfbd4a21e117a8554c3bce25b5d5 Mon Sep 17 00:00:00 2001 From: augmentedmode Date: Mon, 9 Dec 2024 14:20:04 -0500 Subject: [PATCH 11/16] fix: add promise to websocket close --- test/e2e/helpers.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/test/e2e/helpers.js b/test/e2e/helpers.js index 68c553769d1e..29cb5dd3d7cc 100644 --- a/test/e2e/helpers.js +++ b/test/e2e/helpers.js @@ -322,8 +322,15 @@ async function withFixtures(options, testSuite) { } if (webSocketServer) { - webSocketServer.close(() => { - console.log('WebSocket server closed'); + await new Promise((resolve, reject) => { + webSocketServer.close((err) => { + if (err) { + console.error('Error closing WebSocket server:', err); + return reject(err); + } + console.log('WebSocket server closed'); + return resolve(); + }); }); } From dabc7a880cf5331dd90f486a3305a78562c41860 Mon Sep 17 00:00:00 2001 From: augmentedmode Date: Mon, 9 Dec 2024 14:43:40 -0500 Subject: [PATCH 12/16] fix: await for websocket close hangs --- test/e2e/helpers.js | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/test/e2e/helpers.js b/test/e2e/helpers.js index 29cb5dd3d7cc..68c553769d1e 100644 --- a/test/e2e/helpers.js +++ b/test/e2e/helpers.js @@ -322,15 +322,8 @@ async function withFixtures(options, testSuite) { } if (webSocketServer) { - await new Promise((resolve, reject) => { - webSocketServer.close((err) => { - if (err) { - console.error('Error closing WebSocket server:', err); - return reject(err); - } - console.log('WebSocket server closed'); - return resolve(); - }); + webSocketServer.close(() => { + console.log('WebSocket server closed'); }); } From 627619c9e9790f347f0cae82f4d75ac63130d6e8 Mon Sep 17 00:00:00 2001 From: augmentedmode Date: Tue, 10 Dec 2024 15:13:57 -0500 Subject: [PATCH 13/16] fix: now mocks the websocket server --- privacy-snapshot.json | 3 ++- test/e2e/helpers.js | 21 +-------------------- test/e2e/mock-e2e.js | 2 ++ 3 files changed, 5 insertions(+), 21 deletions(-) diff --git a/privacy-snapshot.json b/privacy-snapshot.json index 49eedf275364..230634421d52 100644 --- a/privacy-snapshot.json +++ b/privacy-snapshot.json @@ -72,5 +72,6 @@ "unresponsive-rpc.test", "unresponsive-rpc.url", "user-storage.api.cx.metamask.io", - "www.4byte.directory" + "www.4byte.directory", + "verify.walletconnect.com" ] diff --git a/test/e2e/helpers.js b/test/e2e/helpers.js index 68c553769d1e..4ade3f2e48ba 100644 --- a/test/e2e/helpers.js +++ b/test/e2e/helpers.js @@ -78,7 +78,6 @@ async function withFixtures(options, testSuite) { usePaymaster, ethConversionInUsd, manifestFlags, - enableWebSocketServer = false, } = options; const fixtureServer = new FixtureServer(); @@ -94,8 +93,6 @@ async function withFixtures(options, testSuite) { const dappServer = []; const phishingPageServer = new PhishingWarningPageServer(); - let webSocketServer; - if (!disableServerMochaToBackground) { getServerMochaToBackground(); } @@ -121,16 +118,6 @@ async function withFixtures(options, testSuite) { contractRegistry = ganacheSeeder.getContractRegistry(); } - if (enableWebSocketServer) { - webSocketServer = new WebSocket.Server({ port: 8081, host: '0.0.0.0' }); - - webSocketServer.on('connection', function connection(ws) { - ws.on('message', function incoming(message) { - ws.send('Hello, client! I am the server.', message); - }); - }); - } - await fixtureServer.start(); fixtureServer.loadJsonState(fixtures, contractRegistry); @@ -321,12 +308,6 @@ async function withFixtures(options, testSuite) { await ganacheServer.quit(); } - if (webSocketServer) { - webSocketServer.close(() => { - console.log('WebSocket server closed'); - }); - } - if (ganacheOptions?.concurrent) { secondaryGanacheServer.forEach(async (server) => { await server.quit(); @@ -669,7 +650,7 @@ async function unlockWallet( async function createWebSocketConnection(driver, hostname) { try { await driver.executeScript(async (wsHostname) => { - const url = `ws://${wsHostname}:8081`; + const url = `ws://${wsHostname}:8000`; const socket = new WebSocket(url); diff --git a/test/e2e/mock-e2e.js b/test/e2e/mock-e2e.js index c7d8b02d4c7d..7644a2d479a0 100644 --- a/test/e2e/mock-e2e.js +++ b/test/e2e/mock-e2e.js @@ -125,6 +125,8 @@ async function setupMocking( }, }); + await server.forAnyWebSocket().thenEcho(); + const mockedEndpoint = await testSpecificMock(server); // Mocks below this line can be overridden by test-specific mocks From d64c13c62ec630a906d24a1c1b1959daf58f66f3 Mon Sep 17 00:00:00 2001 From: augmentedmode Date: Tue, 10 Dec 2024 15:42:15 -0500 Subject: [PATCH 14/16] chore: cleanup --- test/e2e/tests/phishing-controller/phishing-detection.spec.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/e2e/tests/phishing-controller/phishing-detection.spec.js b/test/e2e/tests/phishing-controller/phishing-detection.spec.js index 703ebe91f4c6..63ebb9ede2d6 100644 --- a/test/e2e/tests/phishing-controller/phishing-detection.spec.js +++ b/test/e2e/tests/phishing-controller/phishing-detection.spec.js @@ -317,7 +317,6 @@ describe('Phishing Detection', function () { it('should block a website that makes a websocket connection to a malicious command and control server', async function () { const testPageURL = 'http://localhost:8080'; - await withFixtures( { fixtures: new FixtureBuilder().build(), @@ -361,7 +360,6 @@ describe('Phishing Detection', function () { it('should not block a website that makes a safe WebSocket connection', async function () { const testPageURL = 'http://localhost:8080/'; - await withFixtures( { fixtures: new FixtureBuilder().build(), From fe74a38b058b5c167d75f17a1cdf2db18f2cd54e Mon Sep 17 00:00:00 2001 From: augmentedmode Date: Tue, 10 Dec 2024 23:16:49 -0500 Subject: [PATCH 15/16] chore: remove unused var --- test/e2e/tests/phishing-controller/phishing-detection.spec.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/e2e/tests/phishing-controller/phishing-detection.spec.js b/test/e2e/tests/phishing-controller/phishing-detection.spec.js index 63ebb9ede2d6..fd0f590cb1e8 100644 --- a/test/e2e/tests/phishing-controller/phishing-detection.spec.js +++ b/test/e2e/tests/phishing-controller/phishing-detection.spec.js @@ -328,7 +328,6 @@ describe('Phishing Detection', function () { }); }, dapp: true, - enableWebSocketServer: true, }, async ({ driver }) => { await unlockWallet(driver); @@ -371,7 +370,6 @@ describe('Phishing Detection', function () { }); }, dapp: true, - enableWebSocketServer: true, }, async ({ driver }) => { await unlockWallet(driver); From d11f81a0bf342e57b8d25c6481bccbf5bc40a1f1 Mon Sep 17 00:00:00 2001 From: augmentedmode Date: Wed, 11 Dec 2024 12:57:21 -0500 Subject: [PATCH 16/16] fix: move websocket mock to specific tests for flexibility --- test/e2e/mock-e2e.js | 2 -- test/e2e/tests/phishing-controller/phishing-detection.spec.js | 2 ++ 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/e2e/mock-e2e.js b/test/e2e/mock-e2e.js index 7644a2d479a0..c7d8b02d4c7d 100644 --- a/test/e2e/mock-e2e.js +++ b/test/e2e/mock-e2e.js @@ -125,8 +125,6 @@ async function setupMocking( }, }); - await server.forAnyWebSocket().thenEcho(); - const mockedEndpoint = await testSpecificMock(server); // Mocks below this line can be overridden by test-specific mocks diff --git a/test/e2e/tests/phishing-controller/phishing-detection.spec.js b/test/e2e/tests/phishing-controller/phishing-detection.spec.js index fd0f590cb1e8..98184b85224e 100644 --- a/test/e2e/tests/phishing-controller/phishing-detection.spec.js +++ b/test/e2e/tests/phishing-controller/phishing-detection.spec.js @@ -323,6 +323,7 @@ describe('Phishing Detection', function () { ganacheOptions: defaultGanacheOptions, title: this.test.fullTitle(), testSpecificMock: async (mockServer) => { + await mockServer.forAnyWebSocket().thenEcho(); await setupPhishingDetectionMocks(mockServer, { blockProvider: BlockProvider.MetaMask, }); @@ -365,6 +366,7 @@ describe('Phishing Detection', function () { ganacheOptions: defaultGanacheOptions, title: this.test.fullTitle(), testSpecificMock: async (mockServer) => { + await mockServer.forAnyWebSocket().thenEcho(); await setupPhishingDetectionMocks(mockServer, { blockProvider: BlockProvider.MetaMask, });