puppeteer: set Host header using `setExtraHTTPHeaders` `net::ERR_INVALID_ARGUMENT`
Steps to reproduce
- Puppeteer version: 5.0.0
What steps will reproduce the problem?
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({
args: [
'--no-sandbox',
'--headless',
'--disable-gpu',
'--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Firefox/78.0'
]
});
await new Promise(resolve => setTimeout(resolve, 5000));
const page = await browser.newPage();
await page.setExtraHTTPHeaders({'Host': 'bukdjango_captcha.com'})
await page.goto('http://bukdjango_captcha.com:8000/recaptcha_v3');
await page.screenshot({path: 'example.png'});
console.log(await page.content())
await browser.close();
})();
What is the expected result? I can override host header.
What happens instead?
tests_browser_1 | (node:1) UnhandledPromiseRejectionWarning: Error: net::ERR_INVALID_ARGUMENT at http://bukdjango_captcha.com:8000/recaptcha_v3
tests_browser_1 | at navigate (/dockertestapp/node_modules/puppeteer/lib/cjs/common/FrameManager.js:96:23)
tests_browser_1 | at processTicksAndRejections (internal/process/task_queues.js:93:5)
tests_browser_1 | at async FrameManager.navigateFrame (/dockertestapp/node_modules/puppeteer/lib/cjs/common/FrameManager.js:71:21)
tests_browser_1 | at async Frame.goto (/dockertestapp/node_modules/puppeteer/lib/cjs/common/FrameManager.js:296:16)
tests_browser_1 | at async Page.goto (/dockertestapp/node_modules/puppeteer/lib/cjs/common/Page.js:749:16)
tests_browser_1 | at async /dockertestapp/test.js:16:3
tests_browser_1 | -- ASYNC --
tests_browser_1 | at Frame.<anonymous> (/dockertestapp/node_modules/puppeteer/lib/cjs/common/helper.js:109:19)
tests_browser_1 | at Page.goto (/dockertestapp/node_modules/puppeteer/lib/cjs/common/Page.js:749:53)
tests_browser_1 | at Page.<anonymous> (/dockertestapp/node_modules/puppeteer/lib/cjs/common/helper.js:110:27)
tests_browser_1 | at /dockertestapp/test.js:16:14
tests_browser_1 | at processTicksAndRejections (internal/process/task_queues.js:93:5)
tests_browser_1 | (Use `node --trace-warnings ...` to show where the warning was created)
tests_browser_1 | (node:1) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise whi
ch was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (r
ejection id: 1)
tests_browser_1 | (node:1) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exi
t code.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 11
- Comments: 17
I see setting Host header is actually disabled by default in chromium source code: see here: https://github.com/chromium/chromium/blob/8abfec7efff916f7056a14632cc66c42ff7c24cd/services/network/public/cpp/header_util.cc#L39 comment says: “Disallow setting the Host header because it can conflict with specified URL and logic related to isolating sites.”
Got same problem with chromedp on Golang.Did you find any sollution yet?
already tried… didnt work
@pxCaptcha Thanks man, works! For everyone working with DDEV: you have to set the hostname to your project.tld and then take the internal ddev web-container name (usually ddev-[yourprojectname]-web) as goto url: ` const browser = await puppeteer.launch({ headless: true, ignoreHTTPSErrors: true, args: [ “–host-resolver-rules=MAP yourprojectname.localhost ddev-yourprojectname-web”, “–no-sandbox”,
“–disable-dev-shm-usage”, “–disable-setuid-sandbox”,
“–ignore-certificate-errors”, “–allow-insecure-localhost”, “–disable-gpu”, ‘–disable-web-security’, ] });
const page = await browser.newPage(); await page.setViewport({width: 1920, height: 1024});
await page.setUserAgent(‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36’);
await page.evaluateHandle(‘document.fonts.ready’); await page.goto(“https://yourprojectname.localhost/”, { waitUntil: ‘networkidle0’ });
await page.screenshot({path: “test.png”, fullPage: true}); await browser.close(); ` EDIT: Instead using the Host-Header (which works but faking the Header leads to other problems) I would recommend to use the --host-resolver-rules mapping like above 😃
const options = { args, headless: false, ignoreHTTPSErrors: true, Headers:{ "host":"www.host.com", } };
Hey guys! Its me again, this fixed it for me back then. Hope this works for you guys as well:)