'Ignoring axios error for invalid certificates when creating a vscode extension

I am currently working on a Visual Studio Code extension that makes HTTPS GET requests and I am trying to ignore invalid certificates e.g. expired certificates.

Here is a very simple script in typescript that works:

import * as https from "https";
import axios from "axios";

try {
    const test = async () => {
        await axios.get( 
            'https://expired.badssl.com/',
            
            {
                httpsAgent: new https.Agent({
                    rejectUnauthorized: false
                })
            }
        );
    };
    test();
} catch (e) {
    console.log(e);
}

As expected, when I compile the file and run it, it returns nothing and if I change rejectUnauthorized to true, it logs an expired certificate error.

However, when I create a simple typescript extension as described in https://code.visualstudio.com/api/get-started/your-first-extension with yo code and make a similar axios request, it gives a certificate has expired error regardless of the parameter given torejectUnauthorized

Similar typescript code as above made with yo code template:

import * as vscode from 'vscode';
import * as https from "https";
import axios from "axios";

export async function activate(context: vscode.ExtensionContext) {
    let disposable = vscode.commands.registerCommand('nameOfExtension.helloWorld', async () => {
        try {
            await axios.get(
                'https://expired.badssl.com/', 
                {
                    httpsAgent: new https.Agent({
                        rejectUnauthorized: false
                    })
                }
            );
        } catch (e) {
            console.log(e);
        }
        
    });

    context.subscriptions.push(disposable);
}

export function deactivate() {}

(where nameOfExtension is the name of the extension in package.json)

How the extension works: User opens up the command palette (cmd+P on mac) and runs the 'hello world' command which runs the axios request. If it works, nothing happens, otherwise the error message is printed to the console.

One way I found in which I could make the extension ignore the certificate is by adding in the line of code https.globalAgent.options.rejectUnauthorized = false which sets the globalAgent to always be false.

But setting this globally is not something I wish to do and I would like the rejectUnauthorized to work for a single instance.

I was wondering if anyone knew why the rejectUnauthorized method shown in the example would not work on vscode?

Here are my thoughts:

  • Could it be something to do with the environment?
  • Is the global setting applied automatically when the extension is run and is it overriding the the local setting that I apply?

Additional information:

"devDependencies": {
        "@types/vscode": "^1.61.0",
        "@types/glob": "^7.1.4",
        "@types/mocha": "^9.0.0",
        "@types/node": "14.x",
        "@typescript-eslint/eslint-plugin": "^4.31.1",
        "@typescript-eslint/parser": "^4.31.1",
        "eslint": "^7.32.0",
        "glob": "^7.1.7",
        "mocha": "^9.1.1",
        "typescript": "^4.4.3",
        "@vscode/test-electron": "^1.6.2"
},
"dependencies": {
        "axios": "^0.21.4",
        "https": "^1.0.0"
}


Sources

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

Source: Stack Overflow

Solution Source