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 { 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,15 +17,24 @@ 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));
if (hostname && port) {
finalConfig.proxy = {
host: hostname,
port,
port: parseInt(port, 10),
};
}
}
const response = await axios(finalConfig);

View File

@ -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<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): {
app: Record<string, any>;
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<string | null> {
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
if (options.html) {
body.innerHTML = options.html;
}
return this.dialog(title, body);
},
},
// @ts-expect-error -- TSCONVERSION
__private: {
axios,
analytics,