首页 > 解决方案 > Puppeteer:“您要提交的信息不安全”

问题描述

使用 Puppeteer 提交表单后,我收到以下消息:

您要提交的信息不安全。 由于该站点使用的连接并不完全安全,因此您的信息将对其他人可见。

Location该消息是由不安全的响应标头(HTTP 资源而不是 HTTPS)触发的。

为了解决这个问题,我一直在考虑两种选择:

  1. 避免此消息。如何避免?
  2. 拦截 HTTP 请求并将其修改为 HTTPS 请求。怎么做?

关于选项 2,我尝试了以下方法:

const browser = await puppeteer.launch();
const page = await browser.newPage();

await page.setRequestInterception(true);

page.on('request', request => {
    console.log('Request', request.url());

    if (request.url().startsWith('http://')) {
        console.log('HTTP Request')
        
        let secureUrl = request.url().replace('http://', 'https://');

        // TO-DO: set here the 'secureUrl' to the request.
    }
    
    request.continue();
});

标签: javascriptnode.jshttp-headerspuppeteerchromium

解决方案


此页面似乎在 puppeteer 请求事件之前触发,因此无法修改,解决方法可能只是:

if (await page.$('.insecure-form') !== null) {
    await page.click('#proceed-button')
    await page.waitForNavigation({ waitUntil: 'networkidle0' })
}

推荐阅读