Kong / insomnia

The open-source, cross-platform API client for GraphQL, REST, WebSockets, SSE and gRPC. With Cloud, Local and Git storage.
https://insomnia.rest
Apache License 2.0
33.71k stars 1.88k forks source link

Unable to connect secure endpoint using self signed certificate using custom Insomnia Plugin (net::ERR_CERT_AUTHORITY_INVALID) #3916

Open sailfishdev opened 2 years ago

sailfishdev commented 2 years ago

I am trying to develop a plugin which will connect a website using client-certificate, fetch a token and add to the request context. The node js that I have written for this works fine when executed standalone. However, from Insomnia the request is failing with an error saying net::ERR_CERT_AUTHORITY_INVALID

To Reproduce Import the plugin folder and reload to activate. Trigger an API request and monitor the devtools console.

Example Code main.js:

const https = require('https');
const fs = require('fs');
const axios = require('axios').default;

module.exports.requestHooks = [
    async function (context) {
        try {
            const agent = new https.Agent({
                pfx: fs.readFileSync(keyStorePath),
                passphrase: keyStorePassword,
                ca: fs.readFileSync(keyStorePem)
            });
            const config = {
                url: tokenUrl,
                method: 'POST',
                headers: {
                    'Accept': 'application/json',
                    'Content-Type': 'application/json'
                },
                httpsAgent: agent,
                data: {},
                timeout: 5000
            };
            const response = await axios(config);
            console.log("Response data:", response.data);
        } catch (err) {
            console.error("Request failed", err);
        }
    }
];

package.json:

{
  "name": "insomnia-plugin-ma-secure-request",
  "version": "0.0.1",
  "description": "",
  "private": true,
  "author": "me",
  "license": "ISC",
  "dependencies": {
    "axios": "^0.21.1",
    "uuid": "^8.3.2"
  },
  "insomnia": {
    "name": "ma-secure-request",
    "description": ""
  },
  "main": "main.js"
}

Expected behavior The token should be successfully fetched without throwing any error.

Error logs

/Users/myself/Library/Application Support/Insomnia/plugins/insomnia-plugin-ma-secure-request/node_modules/axios/lib/adapters/xhr.js:177 POST <URL> net::ERR_CERT_AUTHORITY_INVALID
Request failed Error: Network Error
    at createError (VM1709 createError.js:16)
    at XMLHttpRequest.handleError (VM1707 xhr.js:84)

Desktop (please complete the following information):

I have executed the code separately outside Insomnia to ensure it works.

bmarwell commented 2 years ago

They moved a similar issue to discussion, kindly vote: https://github.com/Kong/insomnia/discussions/3617