Update axios-request to respect the validateSSL app setting (#4042)

* Update axios-request to respect the validateSSL app setting

* Update types and formatting
This commit is contained in:
James Gatz 2021-09-22 14:57:51 +02:00 committed by GitHub
parent 0333bf62f2
commit e7d44bf206
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 99 additions and 44 deletions

View File

@ -1,13 +1,14 @@
import axios from 'axios'; import axios, { AxiosRequestConfig } from 'axios';
import * as https from 'https';
import { setDefaultProtocol } from 'insomnia-url'; import { setDefaultProtocol } from 'insomnia-url';
import { parse as urlParse } from 'url'; import { parse as urlParse } from 'url';
import { isDevelopment } from '../common/constants'; import { isDevelopment } from '../common/constants';
import * as models from '../models'; import * as models from '../models';
export async function axiosRequest(config) { export async function axiosRequest(config: AxiosRequestConfig) {
const settings = await models.settings.getOrCreate(); 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; let proxyUrl: string | null = null;
if (isHttps && settings.httpsProxy) { if (isHttps && settings.httpsProxy) {
@ -16,14 +17,23 @@ export async function axiosRequest(config) {
proxyUrl = settings.httpProxy; 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) { if (proxyUrl) {
const { hostname, port } = urlParse(setDefaultProtocol(proxyUrl)); const { hostname, port } = urlParse(setDefaultProtocol(proxyUrl));
finalConfig.proxy = {
host: hostname, if (hostname && port) {
port, finalConfig.proxy = {
}; host: hostname,
port: parseInt(port, 10),
};
}
} }
const response = await axios(finalConfig); const response = await axios(finalConfig);

View File

@ -12,13 +12,67 @@ import {
} from '../../common/render'; } from '../../common/render';
import HtmlElementWrapper from '../../ui/components/html-element-wrapper'; import HtmlElementWrapper from '../../ui/components/html-element-wrapper';
import { showAlert, showModal, showPrompt } from '../../ui/components/modals'; import { showAlert, showModal, showPrompt } from '../../ui/components/modals';
import { PromptModalOptions } from '../../ui/components/modals/prompt-modal';
import WrapperModal from '../../ui/components/modals/wrapper-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<undefined> | ReturnType<typeof showAlert>;
dialog: (title: string, body: HTMLElement, options?: DialogOptions) => void;
prompt: (title: string, options?: Pick<PromptModalOptions, 'label' | 'defaultValue' | 'submitName' | 'cancelable'>) => Promise<string>;
getPath: (name: string) => string;
getInfo: () => AppInfo;
showSaveDialog: (options?: ShowDialogOptions) => Promise<string | null>;
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): { export function init(renderPurpose: RenderPurpose = RENDER_PURPOSE_GENERAL): {
app: Record<string, any>; app: AppContext;
__private: PrivateProperties;
} { } {
const canShowDialogs = const canShowDialogs =
renderPurpose === RENDER_PURPOSE_SEND || renderPurpose === RENDER_PURPOSE_NO_RENDER; renderPurpose === RENDER_PURPOSE_SEND ||
renderPurpose === RENDER_PURPOSE_NO_RENDER;
return { return {
app: { app: {
alert(title: string, message?: string) { alert(title: string, message?: string) {
@ -34,15 +88,13 @@ export function init(renderPurpose: RenderPurpose = RENDER_PURPOSE_GENERAL): {
dialog( dialog(
title, title,
body: HTMLElement, body,
options: { options = {},
onHide?: () => void;
tall?: boolean;
skinny?: boolean;
wide?: boolean;
} = {},
) { ) {
if (renderPurpose !== RENDER_PURPOSE_SEND && renderPurpose !== RENDER_PURPOSE_NO_RENDER) { if (
renderPurpose !== RENDER_PURPOSE_SEND &&
renderPurpose !== RENDER_PURPOSE_NO_RENDER
) {
return; return;
} }
@ -56,16 +108,9 @@ export function init(renderPurpose: RenderPurpose = RENDER_PURPOSE_GENERAL): {
}, },
prompt( prompt(
title: string, title,
options?: { options = {},
label?: string;
defaultValue?: string;
submitName?: string;
cancelable?: boolean;
},
) { ) {
options = options || {};
if (!canShowDialogs) { if (!canShowDialogs) {
return Promise.resolve(options.defaultValue || ''); return Promise.resolve(options.defaultValue || '');
} }
@ -116,9 +161,7 @@ export function init(renderPurpose: RenderPurpose = RENDER_PURPOSE_GENERAL): {
}, },
async showSaveDialog( async showSaveDialog(
options: { options = {},
defaultPath?: string;
} = {},
): Promise<string | null> { ): Promise<string | null> {
if (!canShowDialogs) { if (!canShowDialogs) {
return Promise.resolve(null); return Promise.resolve(null);
@ -129,20 +172,22 @@ export function init(renderPurpose: RenderPurpose = RENDER_PURPOSE_GENERAL): {
buttonLabel: 'Save', buttonLabel: 'Save',
defaultPath: options.defaultPath, defaultPath: options.defaultPath,
}; };
const { filePath } = await electron.remote.dialog.showSaveDialog(saveOptions); const { filePath } = await electron.remote.dialog.showSaveDialog(
saveOptions
);
return filePath || null; return filePath || null;
}, },
clipboard: { clipboard: {
readText(): string { readText() {
return electron.clipboard.readText(); return electron.clipboard.readText();
}, },
writeText(text: string): void { writeText(text) {
electron.clipboard.writeText(text); electron.clipboard.writeText(text);
}, },
clear(): void { clear() {
electron.clipboard.clear(); electron.clipboard.clear();
}, },
}, },
@ -150,23 +195,23 @@ export function init(renderPurpose: RenderPurpose = RENDER_PURPOSE_GENERAL): {
// ~~~~~~~~~~~~~~~~~~ // // ~~~~~~~~~~~~~~~~~~ //
// Deprecated Methods // // Deprecated Methods //
// ~~~~~~~~~~~~~~~~~~ // // ~~~~~~~~~~~~~~~~~~ //
/** @deprecated as it was never officially supported */
showGenericModalDialog( showGenericModalDialog(
title: string, title,
options: { options = {},
html?: string;
} = {},
) { ) {
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 // Create DOM node so we can adapt to the new dialog() method
const body = document.createElement('div'); 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); return this.dialog(title, body);
}, },
}, },
// @ts-expect-error -- TSCONVERSION
__private: { __private: {
axios, axios,
analytics, analytics,