From 8f64e6f262757ba7378958ce23b64bceb8086bb5 Mon Sep 17 00:00:00 2001 From: Cedric van Putten Date: Mon, 4 Mar 2024 13:37:02 +0100 Subject: [PATCH 1/4] test(dev-middleware): add test for loading sourcemaps with lan addresses --- .../InspectorProxyCdpRewritingHacks-test.js | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/packages/dev-middleware/src/__tests__/InspectorProxyCdpRewritingHacks-test.js b/packages/dev-middleware/src/__tests__/InspectorProxyCdpRewritingHacks-test.js index 51545ecd9be702..0b4c9421a94dda 100644 --- a/packages/dev-middleware/src/__tests__/InspectorProxyCdpRewritingHacks-test.js +++ b/packages/dev-middleware/src/__tests__/InspectorProxyCdpRewritingHacks-test.js @@ -26,6 +26,7 @@ import { } from './ServerUtils'; import {createHash} from 'crypto'; import fs from 'fs'; +import {networkInterfaces} from 'os'; import path from 'path'; const fsPromise = fs.promises; @@ -95,6 +96,78 @@ describe.each(['HTTP', 'HTTPS'])( } }); + test('source map fetching when using lan in Debugger.scriptParsed', async () => { + serverRef.app.use( + '/source-map', + serveStaticJson({ + version: 3, + // Mojibake insurance. + file: '\u2757.js', + }), + ); + + // Find the current LAN IP address of this machine + const lanInterface = Object.values(networkInterfaces()) + .flat() + .find(interface => !interface.internal && interface.family === 'IPv4'); + if (!lanInterface) { + throw new Error('No IPv4 LAN interface found'); + } + + const serverRefWithLanUrls = { + ...serverRef, + serverBaseUrl: serverRef.serverBaseUrl.replace( + 'localhost', + lanInterface.address, + ), + serverBaseWsUrl: serverRef.serverBaseWsUrl.replace( + 'localhost', + lanInterface.address, + ), + }; + if (serverRefWithLanUrls.serverBaseUrl === serverRef.serverBaseUrl) { + throw new Error( + 'Failed to replace localhost with LAN address for "serverBaseUrl"', + ); + } + if (serverRefWithLanUrls.serverBaseWsUrl === serverRef.serverBaseWsUrl) { + throw new Error( + 'Failed to replace localhost with LAN address for "serverBaseWsUrl"', + ); + } + + // Connect to the target using the LAN address instead of localhost + const {device, debugger_} = await createAndConnectTarget( + serverRefWithLanUrls, + autoCleanup.signal, + { + app: 'bar-app', + id: 'page1', + title: 'bar-title', + vm: 'bar-vm', + }, + ); + try { + const scriptParsedMessage = await sendFromTargetToDebugger( + device, + debugger_, + 'page1', + { + method: 'Debugger.scriptParsed', + params: { + sourceMapURL: `${serverRefWithLanUrls.serverBaseUrl}/source-map`, + }, + }, + ); + expect( + parseJsonFromDataUri(scriptParsedMessage.params.sourceMapURL), + ).toEqual({version: 3, file: '\u2757.js'}); + } finally { + device.close(); + debugger_.close(); + } + }); + test('handling of failure to fetch source map', async () => { const {device, debugger_} = await createAndConnectTarget( serverRef, From 7adf6692b36801597ca2601fe0b6f1479d14f8fb Mon Sep 17 00:00:00 2001 From: Cedric van Putten Date: Mon, 4 Mar 2024 13:45:38 +0100 Subject: [PATCH 2/4] refactor(dev-middleware): move server ref with lan URLs to server utilities --- .../InspectorProxyCdpRewritingHacks-test.js | 35 +++---------------- .../src/__tests__/ServerUtils.js | 30 ++++++++++++++++ 2 files changed, 34 insertions(+), 31 deletions(-) diff --git a/packages/dev-middleware/src/__tests__/InspectorProxyCdpRewritingHacks-test.js b/packages/dev-middleware/src/__tests__/InspectorProxyCdpRewritingHacks-test.js index 0b4c9421a94dda..2c04986d36beb3 100644 --- a/packages/dev-middleware/src/__tests__/InspectorProxyCdpRewritingHacks-test.js +++ b/packages/dev-middleware/src/__tests__/InspectorProxyCdpRewritingHacks-test.js @@ -20,13 +20,13 @@ import { } from './InspectorProtocolUtils'; import {withAbortSignalForEachTest} from './ResourceUtils'; import { + getServerRefWithLanUrls, serveStaticJson, serveStaticText, withServerForEachTest, -} from './ServerUtils'; +} from "./ServerUtils"; import {createHash} from 'crypto'; import fs from 'fs'; -import {networkInterfaces} from 'os'; import path from 'path'; const fsPromise = fs.promises; @@ -106,35 +106,8 @@ describe.each(['HTTP', 'HTTPS'])( }), ); - // Find the current LAN IP address of this machine - const lanInterface = Object.values(networkInterfaces()) - .flat() - .find(interface => !interface.internal && interface.family === 'IPv4'); - if (!lanInterface) { - throw new Error('No IPv4 LAN interface found'); - } - - const serverRefWithLanUrls = { - ...serverRef, - serverBaseUrl: serverRef.serverBaseUrl.replace( - 'localhost', - lanInterface.address, - ), - serverBaseWsUrl: serverRef.serverBaseWsUrl.replace( - 'localhost', - lanInterface.address, - ), - }; - if (serverRefWithLanUrls.serverBaseUrl === serverRef.serverBaseUrl) { - throw new Error( - 'Failed to replace localhost with LAN address for "serverBaseUrl"', - ); - } - if (serverRefWithLanUrls.serverBaseWsUrl === serverRef.serverBaseWsUrl) { - throw new Error( - 'Failed to replace localhost with LAN address for "serverBaseWsUrl"', - ); - } + // Get the server ref with LAN addresses instead of localhost + const serverRefWithLanUrls = getServerRefWithLanUrls(serverRef); // Connect to the target using the LAN address instead of localhost const {device, debugger_} = await createAndConnectTarget( diff --git a/packages/dev-middleware/src/__tests__/ServerUtils.js b/packages/dev-middleware/src/__tests__/ServerUtils.js index bcc3db08115cf9..97a7fd391febde 100644 --- a/packages/dev-middleware/src/__tests__/ServerUtils.js +++ b/packages/dev-middleware/src/__tests__/ServerUtils.js @@ -18,6 +18,7 @@ import http from 'http'; import https from 'https'; import * as selfsigned from 'selfsigned'; import url from 'url'; +import {networkInterfaces} from 'os'; type CreateDevMiddlewareOptions = Parameters[0]; type CreateServerOptions = { @@ -160,3 +161,32 @@ export function serveStaticText(text: string): HandleFunction { res.end(text); }; } + +export function getServerRefWithLanUrls( + serverRef: ReturnType, +): ReturnType { + const {serverBaseUrl, serverBaseWsUrl} = serverRef; + const lanInterface = Object.values(networkInterfaces()) + .flat() + .find(({internal, family}) => !internal && family === 'IPv4'); + + if (!lanInterface) { + throw new Error('No IPv4 LAN interface found'); + } + + const lanBaseUrl = serverBaseUrl.replace('localhost', lanInterface.address); + const lanBaseWsUrl = serverBaseWsUrl.replace( + 'localhost', + lanInterface.address, + ); + + if (lanBaseUrl === serverBaseUrl || lanBaseWsUrl === serverBaseWsUrl) { + throw new Error('Could not replace localhost with LAN address'); + } + + return { + ...serverRef, + serverBaseUrl: lanBaseUrl, + serverBaseWsUrl: lanBaseWsUrl, + }; +} From ee560aa9f9d3a2396a8d6e8a6f28881b739d1d00 Mon Sep 17 00:00:00 2001 From: Cedric van Putten Date: Mon, 4 Mar 2024 13:51:13 +0100 Subject: [PATCH 3/4] fix(dev-middleware): allow inspector proxy to fetch sourcemaps on lan connections --- .../src/__tests__/InspectorProxyCdpRewritingHacks-test.js | 2 +- packages/dev-middleware/src/inspector-proxy/Device.js | 4 ---- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/packages/dev-middleware/src/__tests__/InspectorProxyCdpRewritingHacks-test.js b/packages/dev-middleware/src/__tests__/InspectorProxyCdpRewritingHacks-test.js index 2c04986d36beb3..a5edb31a81e572 100644 --- a/packages/dev-middleware/src/__tests__/InspectorProxyCdpRewritingHacks-test.js +++ b/packages/dev-middleware/src/__tests__/InspectorProxyCdpRewritingHacks-test.js @@ -24,7 +24,7 @@ import { serveStaticJson, serveStaticText, withServerForEachTest, -} from "./ServerUtils"; +} from './ServerUtils'; import {createHash} from 'crypto'; import fs from 'fs'; import path from 'path'; diff --git a/packages/dev-middleware/src/inspector-proxy/Device.js b/packages/dev-middleware/src/inspector-proxy/Device.js index 26befc0b1185c4..455a89fd09f051 100644 --- a/packages/dev-middleware/src/inspector-proxy/Device.js +++ b/packages/dev-middleware/src/inspector-proxy/Device.js @@ -758,10 +758,6 @@ export default class Device { // Fetch text, raising an exception if the text could not be fetched, // or is too large. async #fetchText(url: URL): Promise { - if (!['localhost', '127.0.0.1'].includes(url.hostname)) { - throw new Error('remote fetches not permitted'); - } - // $FlowFixMe[incompatible-call] Suppress arvr node-fetch flow error const response = await fetch(url); if (!response.ok) { From 35f9ab0360574b0f7565fa8e28ffa3005e896128 Mon Sep 17 00:00:00 2001 From: Rob Hogan <2590098+robhogan@users.noreply.github.com> Date: Mon, 11 Mar 2024 09:27:49 +0000 Subject: [PATCH 4/4] [robhogan] Remove new test --- .../InspectorProxyCdpRewritingHacks-test.js | 46 ------------------- .../src/__tests__/ServerUtils.js | 30 ------------ 2 files changed, 76 deletions(-) diff --git a/packages/dev-middleware/src/__tests__/InspectorProxyCdpRewritingHacks-test.js b/packages/dev-middleware/src/__tests__/InspectorProxyCdpRewritingHacks-test.js index a5edb31a81e572..51545ecd9be702 100644 --- a/packages/dev-middleware/src/__tests__/InspectorProxyCdpRewritingHacks-test.js +++ b/packages/dev-middleware/src/__tests__/InspectorProxyCdpRewritingHacks-test.js @@ -20,7 +20,6 @@ import { } from './InspectorProtocolUtils'; import {withAbortSignalForEachTest} from './ResourceUtils'; import { - getServerRefWithLanUrls, serveStaticJson, serveStaticText, withServerForEachTest, @@ -96,51 +95,6 @@ describe.each(['HTTP', 'HTTPS'])( } }); - test('source map fetching when using lan in Debugger.scriptParsed', async () => { - serverRef.app.use( - '/source-map', - serveStaticJson({ - version: 3, - // Mojibake insurance. - file: '\u2757.js', - }), - ); - - // Get the server ref with LAN addresses instead of localhost - const serverRefWithLanUrls = getServerRefWithLanUrls(serverRef); - - // Connect to the target using the LAN address instead of localhost - const {device, debugger_} = await createAndConnectTarget( - serverRefWithLanUrls, - autoCleanup.signal, - { - app: 'bar-app', - id: 'page1', - title: 'bar-title', - vm: 'bar-vm', - }, - ); - try { - const scriptParsedMessage = await sendFromTargetToDebugger( - device, - debugger_, - 'page1', - { - method: 'Debugger.scriptParsed', - params: { - sourceMapURL: `${serverRefWithLanUrls.serverBaseUrl}/source-map`, - }, - }, - ); - expect( - parseJsonFromDataUri(scriptParsedMessage.params.sourceMapURL), - ).toEqual({version: 3, file: '\u2757.js'}); - } finally { - device.close(); - debugger_.close(); - } - }); - test('handling of failure to fetch source map', async () => { const {device, debugger_} = await createAndConnectTarget( serverRef, diff --git a/packages/dev-middleware/src/__tests__/ServerUtils.js b/packages/dev-middleware/src/__tests__/ServerUtils.js index 97a7fd391febde..bcc3db08115cf9 100644 --- a/packages/dev-middleware/src/__tests__/ServerUtils.js +++ b/packages/dev-middleware/src/__tests__/ServerUtils.js @@ -18,7 +18,6 @@ import http from 'http'; import https from 'https'; import * as selfsigned from 'selfsigned'; import url from 'url'; -import {networkInterfaces} from 'os'; type CreateDevMiddlewareOptions = Parameters[0]; type CreateServerOptions = { @@ -161,32 +160,3 @@ export function serveStaticText(text: string): HandleFunction { res.end(text); }; } - -export function getServerRefWithLanUrls( - serverRef: ReturnType, -): ReturnType { - const {serverBaseUrl, serverBaseWsUrl} = serverRef; - const lanInterface = Object.values(networkInterfaces()) - .flat() - .find(({internal, family}) => !internal && family === 'IPv4'); - - if (!lanInterface) { - throw new Error('No IPv4 LAN interface found'); - } - - const lanBaseUrl = serverBaseUrl.replace('localhost', lanInterface.address); - const lanBaseWsUrl = serverBaseWsUrl.replace( - 'localhost', - lanInterface.address, - ); - - if (lanBaseUrl === serverBaseUrl || lanBaseWsUrl === serverBaseWsUrl) { - throw new Error('Could not replace localhost with LAN address'); - } - - return { - ...serverRef, - serverBaseUrl: lanBaseUrl, - serverBaseWsUrl: lanBaseWsUrl, - }; -}