Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Add workaround for CDP host security validation #968

Merged
merged 1 commit into from
Nov 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion lib/commands/context/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import path from 'node:path';
import http from 'node:http';
import Chromedriver from 'appium-chromedriver';
import {toDetailsCacheKey, getWebviewDetails, WEBVIEWS_DETAILS_CACHE} from './cache';
import dns from 'node:dns/promises';

// https://cs.chromium.org/chromium/src/chrome/browser/devtools/device/android_device_info_query.cc
export const CHROME_BROWSER_PACKAGE_ACTIVITY = /** @type {const} */ ({
Expand Down Expand Up @@ -96,9 +97,11 @@ async function getFreePort() {
* @returns {Promise<object[]>}
*/
async function cdpGetRequest(host, port, endpoint) {
// Workaround for https://github.com/puppeteer/puppeteer/issues/2242, https://github.com/appium/appium/issues/20782
const compatibleHost = isCompatibleCdpHost(host) ? host : (await dns.lookup(host)).address;
return (
await axios({
url: `http://${host}:${port}${endpoint}`,
url: `http://${compatibleHost}:${port}${endpoint}`,
timeout: CDP_REQ_TIMEOUT,
// We need to set this from Node.js v19 onwards.
// Otherwise, in situation with multiple webviews,
Expand Down Expand Up @@ -810,6 +813,18 @@ export async function dismissChromeWelcome() {
}
}

/**
* https://github.com/puppeteer/puppeteer/issues/2242#issuecomment-544219536
*
* @param {string} host
* @returns {boolean}
*/
function isCompatibleCdpHost (host) {
return ['localhost', 'localhost.localdomain'].includes(host)
|| host.endsWith('.localhost')
|| Boolean(net.isIP(host));
}

/**
* @typedef {import('appium-adb').ADB} ADB
*/