'How to download a resource using https behind a proxy without using "rejectUnauthorized: false"?

I'm trying to download https://www.stackoverflow.com or https://www.google.com using got while I'm behind a proxy

I keep running into this error RequestError: unable to get local issue if rejectUnauthorized: false is not used. I know that this rejectUnauthorized: false workaround is a security issue.

stackoverflow.com and google.com must have trusted well-known CAs, so why am I getting this error?

import got from "got";
import { HttpsProxyAgent } from "hpagent";

const result = await got("https://www.google.com", {
  agent: {
    https: new HttpsProxyAgent({
      proxy: process.env.https_proxy,
      rejectUnauthorized: false, // If true => RequestError: unable to get local issuer certificate
    }),
  },
}).text();

console.log("result:", result);

On the other hand, this request to https://jsonplaceholder.typicode.com works without setting rejectUnauthorized: false

const result = await got("https://jsonplaceholder.typicode.com", {
  agent: {
    https: new HttpsProxyAgent({
      proxy: process.env.https_proxy,
    }),
  },
}).text();

Can you please explain this inconsistency and how to resolve it?

Note: I'm using Node.js 14.17.6



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source