From e7d44bf2065edaf6d41de04d74edd97de742a9b0 Mon Sep 17 00:00:00 2001 From: James Gatz Date: Wed, 22 Sep 2021 14:57:51 +0200 Subject: [PATCH] Update axios-request to respect the validateSSL app setting (#4042) * Update axios-request to respect the validateSSL app setting * Update types and formatting --- .../insomnia-app/app/network/axios-request.ts | 26 ++-- .../insomnia-app/app/plugins/context/app.tsx | 117 ++++++++++++------ 2 files changed, 99 insertions(+), 44 deletions(-) diff --git a/packages/insomnia-app/app/network/axios-request.ts b/packages/insomnia-app/app/network/axios-request.ts index 995e1f789..44fef6db8 100644 --- a/packages/insomnia-app/app/network/axios-request.ts +++ b/packages/insomnia-app/app/network/axios-request.ts @@ -1,13 +1,14 @@ -import axios from 'axios'; +import axios, { AxiosRequestConfig } from 'axios'; +import * as https from 'https'; import { setDefaultProtocol } from 'insomnia-url'; import { parse as urlParse } from 'url'; import { isDevelopment } from '../common/constants'; import * as models from '../models'; -export async function axiosRequest(config) { +export async function axiosRequest(config: AxiosRequestConfig) { const settings = await models.settings.getOrCreate(); - const isHttps = config.url.indexOf('https:') === 0; + const isHttps = config.url?.indexOf('https:') === 0; let proxyUrl: string | null = null; if (isHttps && settings.httpsProxy) { @@ -16,14 +17,23 @@ export async function axiosRequest(config) { proxyUrl = settings.httpProxy; } - const finalConfig = { ...config, adapter: global.require('axios/lib/adapters/http') }; + const finalConfig: AxiosRequestConfig = { + ...config, + adapter: global.require('axios/lib/adapters/http'), + httpsAgent: new https.Agent({ + rejectUnauthorized: settings.validateSSL, + }), + }; if (proxyUrl) { const { hostname, port } = urlParse(setDefaultProtocol(proxyUrl)); - finalConfig.proxy = { - host: hostname, - port, - }; + + if (hostname && port) { + finalConfig.proxy = { + host: hostname, + port: parseInt(port, 10), + }; + } } const response = await axios(finalConfig); diff --git a/packages/insomnia-app/app/plugins/context/app.tsx b/packages/insomnia-app/app/plugins/context/app.tsx index 330398020..81084b621 100644 --- a/packages/insomnia-app/app/plugins/context/app.tsx +++ b/packages/insomnia-app/app/plugins/context/app.tsx @@ -12,13 +12,67 @@ import { } from '../../common/render'; import HtmlElementWrapper from '../../ui/components/html-element-wrapper'; import { showAlert, showModal, showPrompt } from '../../ui/components/modals'; +import { PromptModalOptions } from '../../ui/components/modals/prompt-modal'; import WrapperModal from '../../ui/components/modals/wrapper-modal'; +interface DialogOptions { + onHide?: () => void; + tall?: boolean; + skinny?: boolean; + wide?: boolean; +} + +interface AppInfo { + version: string; + platform: NodeJS.Platform; +} + +interface ShowDialogOptions { + defaultPath?: string; +} + +interface AppClipboard { + readText(): string; + writeText(text: string): void; + clear(): void; +} + +interface ShowGenericModalDialogOptions { + html?: string; +} + +export interface AppContext { + alert: ( + title: string, + message?: string + ) => Promise | ReturnType; + dialog: (title: string, body: HTMLElement, options?: DialogOptions) => void; + prompt: (title: string, options?: Pick) => Promise; + getPath: (name: string) => string; + getInfo: () => AppInfo; + showSaveDialog: (options?: ShowDialogOptions) => Promise; + clipboard: AppClipboard; + /** + * @deprecated as it was never officially supported + */ + showGenericModalDialog: ( + title: string, + options?: ShowGenericModalDialogOptions + ) => void; +} + +export interface PrivateProperties { + axios: typeof axios; + analytics: typeof analytics; +} + export function init(renderPurpose: RenderPurpose = RENDER_PURPOSE_GENERAL): { - app: Record; + app: AppContext; + __private: PrivateProperties; } { const canShowDialogs = - renderPurpose === RENDER_PURPOSE_SEND || renderPurpose === RENDER_PURPOSE_NO_RENDER; + renderPurpose === RENDER_PURPOSE_SEND || + renderPurpose === RENDER_PURPOSE_NO_RENDER; return { app: { alert(title: string, message?: string) { @@ -34,15 +88,13 @@ export function init(renderPurpose: RenderPurpose = RENDER_PURPOSE_GENERAL): { dialog( title, - body: HTMLElement, - options: { - onHide?: () => void; - tall?: boolean; - skinny?: boolean; - wide?: boolean; - } = {}, + body, + options = {}, ) { - if (renderPurpose !== RENDER_PURPOSE_SEND && renderPurpose !== RENDER_PURPOSE_NO_RENDER) { + if ( + renderPurpose !== RENDER_PURPOSE_SEND && + renderPurpose !== RENDER_PURPOSE_NO_RENDER + ) { return; } @@ -56,16 +108,9 @@ export function init(renderPurpose: RenderPurpose = RENDER_PURPOSE_GENERAL): { }, prompt( - title: string, - options?: { - label?: string; - defaultValue?: string; - submitName?: string; - cancelable?: boolean; - }, + title, + options = {}, ) { - options = options || {}; - if (!canShowDialogs) { return Promise.resolve(options.defaultValue || ''); } @@ -116,9 +161,7 @@ export function init(renderPurpose: RenderPurpose = RENDER_PURPOSE_GENERAL): { }, async showSaveDialog( - options: { - defaultPath?: string; - } = {}, + options = {}, ): Promise { if (!canShowDialogs) { return Promise.resolve(null); @@ -129,20 +172,22 @@ export function init(renderPurpose: RenderPurpose = RENDER_PURPOSE_GENERAL): { buttonLabel: 'Save', defaultPath: options.defaultPath, }; - const { filePath } = await electron.remote.dialog.showSaveDialog(saveOptions); + const { filePath } = await electron.remote.dialog.showSaveDialog( + saveOptions + ); return filePath || null; }, clipboard: { - readText(): string { + readText() { return electron.clipboard.readText(); }, - writeText(text: string): void { + writeText(text) { electron.clipboard.writeText(text); }, - clear(): void { + clear() { electron.clipboard.clear(); }, }, @@ -150,23 +195,23 @@ export function init(renderPurpose: RenderPurpose = RENDER_PURPOSE_GENERAL): { // ~~~~~~~~~~~~~~~~~~ // // Deprecated Methods // // ~~~~~~~~~~~~~~~~~~ // - - /** @deprecated as it was never officially supported */ showGenericModalDialog( - title: string, - options: { - html?: string; - } = {}, + title, + options = {}, ) { - console.warn('app.showGenericModalDialog() is deprecated. Use app.dialog() instead.'); + console.warn( + 'app.showGenericModalDialog() is deprecated. Use app.dialog() instead.' + ); // Create DOM node so we can adapt to the new dialog() method const body = document.createElement('div'); - // @ts-expect-error -- TSCONVERSION - body.innerHTML = options.html; + + if (options.html) { + body.innerHTML = options.html; + } + return this.dialog(title, body); }, }, - // @ts-expect-error -- TSCONVERSION __private: { axios, analytics,