mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
RN: Allow WS debugger connection to origin from debugger url (#55268)
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/55268 Allow WS debugger connection to origin from debugger url. Also notify the user on Metro if a connection is rejected. Changelog: [General][Fixed] - Reaching Metro from Debugger on urls other than localhost:8081 and 127.0.0.1:8081. Reviewed By: robhogan, yungsters Differential Revision: D91130956 fbshipit-source-id: 107dbc78aa60b888325f5c6dca68cc28f4e6af52
This commit is contained in:
1 parent
5e93036ad5
commit
3c7b4881dd
2 files changed
+113
-37
No files matched your search
@@ -385,40 +385,94 @@ describe.each(['HTTP', 'HTTPS'])(
|
||||
}
|
||||
});
|
||||
|
||||
test('debugger connection with invalid origin is rejected', async () => {
|
||||
const device1 = await createDeviceMock(
|
||||
`${serverRef.serverBaseWsUrl}/inspector/device?device=device1&name=foo&app=bar`,
|
||||
autoCleanup.signal,
|
||||
);
|
||||
try {
|
||||
device1.getPages.mockImplementation(() => [
|
||||
{
|
||||
app: 'bar-app',
|
||||
id: 'page1',
|
||||
title: 'bar-title',
|
||||
vm: 'bar-vm',
|
||||
},
|
||||
]);
|
||||
test.each([
|
||||
'http://[::]:8081',
|
||||
'http://[::]:3213',
|
||||
'http://localhost',
|
||||
'http://0.0.0.0:111',
|
||||
])(
|
||||
'debugger connection with allowed origins: %s can be established',
|
||||
async origin => {
|
||||
const device1 = await createDeviceMock(
|
||||
`${serverRef.serverBaseWsUrl}/inspector/device?device=device1&name=foo&app=bar`,
|
||||
autoCleanup.signal,
|
||||
);
|
||||
try {
|
||||
device1.getPages.mockImplementation(() => [
|
||||
{
|
||||
app: 'bar-app',
|
||||
id: 'page1',
|
||||
title: 'bar-title',
|
||||
vm: 'bar-vm',
|
||||
},
|
||||
]);
|
||||
|
||||
let pageList: JsonPagesListResponse = [];
|
||||
await until(async () => {
|
||||
pageList = (await fetchJson(
|
||||
`${serverRef.serverBaseUrl}/json`,
|
||||
// $FlowFixMe[unclear-type]
|
||||
): any);
|
||||
expect(pageList).toHaveLength(1);
|
||||
});
|
||||
const [{webSocketDebuggerUrl}] = pageList;
|
||||
expect(webSocketDebuggerUrl).toBeDefined();
|
||||
let pageList: JsonPagesListResponse = [];
|
||||
await until(async () => {
|
||||
pageList = (await fetchJson(
|
||||
`${serverRef.serverBaseUrl}/json`,
|
||||
// $FlowFixMe[unclear-type]
|
||||
): any);
|
||||
expect(pageList).toHaveLength(1);
|
||||
});
|
||||
const [{webSocketDebuggerUrl}] = pageList;
|
||||
expect(webSocketDebuggerUrl).toBeDefined();
|
||||
|
||||
await expect(
|
||||
createDebuggerMock(webSocketDebuggerUrl, autoCleanup.signal, {
|
||||
Origin: 'null',
|
||||
}),
|
||||
).rejects.toThrow('Unexpected server response: 401');
|
||||
} finally {
|
||||
device1.close();
|
||||
}
|
||||
});
|
||||
await expect(
|
||||
createDebuggerMock(webSocketDebuggerUrl, autoCleanup.signal, {
|
||||
...(origin ? {Origin: origin} : {}),
|
||||
}),
|
||||
).resolves.toBeDefined();
|
||||
} finally {
|
||||
device1.close();
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
test.each([
|
||||
undefined,
|
||||
'',
|
||||
'invalid',
|
||||
'justhost.com',
|
||||
'http://attacker.com',
|
||||
'http://attacker.com:8083',
|
||||
])(
|
||||
'debugger connection with invalid / different origin: %s is rejected',
|
||||
async origin => {
|
||||
const device1 = await createDeviceMock(
|
||||
`${serverRef.serverBaseWsUrl}/inspector/device?device=device1&name=foo&app=bar`,
|
||||
autoCleanup.signal,
|
||||
);
|
||||
try {
|
||||
device1.getPages.mockImplementation(() => [
|
||||
{
|
||||
app: 'bar-app',
|
||||
id: 'page1',
|
||||
title: 'bar-title',
|
||||
vm: 'bar-vm',
|
||||
},
|
||||
]);
|
||||
|
||||
let pageList: JsonPagesListResponse = [];
|
||||
await until(async () => {
|
||||
pageList = (await fetchJson(
|
||||
`${serverRef.serverBaseUrl}/json`,
|
||||
// $FlowFixMe[unclear-type]
|
||||
): any);
|
||||
expect(pageList).toHaveLength(1);
|
||||
});
|
||||
const [{webSocketDebuggerUrl}] = pageList;
|
||||
expect(webSocketDebuggerUrl).toBeDefined();
|
||||
|
||||
await expect(
|
||||
createDebuggerMock(webSocketDebuggerUrl, autoCleanup.signal, {
|
||||
...(origin ? {Origin: origin} : {}),
|
||||
}),
|
||||
).rejects.toThrow('Unexpected server response: 401');
|
||||
} finally {
|
||||
device1.close();
|
||||
}
|
||||
},
|
||||
);
|
||||
},
|
||||
);
|
||||
@@ -31,9 +31,11 @@ import WS from 'ws';
|
||||
|
||||
const debug = require('debug')('Metro:InspectorProxy');
|
||||
|
||||
const WS_DEBUGGER_ALLOWED_ORIGINS = new Set([
|
||||
'http://localhost:8081',
|
||||
'http://127.0.0.1:8081',
|
||||
const WS_DEBUGGER_ALLOWED_ORIGIN_HOSTNAMES = new Set([
|
||||
'localhost',
|
||||
'127.0.0.1',
|
||||
'0.0.0.0',
|
||||
'[::]',
|
||||
]);
|
||||
|
||||
const WS_DEVICE_URL = '/inspector/device';
|
||||
@@ -495,7 +497,27 @@ export default class InspectorProxy implements InspectorProxyQueries {
|
||||
secure: boolean,
|
||||
req: http$IncomingMessage<>,
|
||||
}>,
|
||||
) => WS_DEBUGGER_ALLOWED_ORIGINS.has(info.origin),
|
||||
) => {
|
||||
if (this.#serverBaseUrl.origin === info.origin) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (URL.canParse(info.origin)) {
|
||||
const {hostname} = new URL(info.origin);
|
||||
if (WS_DEBUGGER_ALLOWED_ORIGIN_HOSTNAMES.has(hostname)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
this.#logger?.error(
|
||||
"Connection from DevTools failed to be established for origin '%s' and path '%s'. Was expecting origin: '%s', or origin hostname to be one of: %s",
|
||||
info.origin,
|
||||
info.req.url,
|
||||
this.#serverBaseUrl.origin,
|
||||
Array.from(WS_DEBUGGER_ALLOWED_ORIGIN_HOSTNAMES).join(', '),
|
||||
);
|
||||
return false;
|
||||
},
|
||||
});
|
||||
|
||||
// $FlowFixMe[value-as-type]
|
||||
|
||||
Reference in new issue
Block a user