#968, #1023: support for disabling sending and storing of cookies

This commit is contained in:
Anoop M D 2023-12-01 23:56:19 +05:30
parent ba994cb5a0
commit 32c8bf296a
3 changed files with 64 additions and 20 deletions

View File

@ -1,4 +1,5 @@
import React from 'react'; import React from 'react';
import get from 'lodash/get';
import { useFormik } from 'formik'; import { useFormik } from 'formik';
import { useSelector, useDispatch } from 'react-redux'; import { useSelector, useDispatch } from 'react-redux';
import { savePreferences } from 'providers/ReduxStore/slices/app'; import { savePreferences } from 'providers/ReduxStore/slices/app';
@ -12,6 +13,8 @@ const General = ({ close }) => {
const preferencesSchema = Yup.object().shape({ const preferencesSchema = Yup.object().shape({
sslVerification: Yup.boolean(), sslVerification: Yup.boolean(),
storeCookies: Yup.boolean(),
sendCookies: Yup.boolean(),
timeout: Yup.mixed() timeout: Yup.mixed()
.transform((value, originalValue) => { .transform((value, originalValue) => {
return originalValue === '' ? undefined : value; return originalValue === '' ? undefined : value;
@ -28,7 +31,9 @@ const General = ({ close }) => {
const formik = useFormik({ const formik = useFormik({
initialValues: { initialValues: {
sslVerification: preferences.request.sslVerification, sslVerification: preferences.request.sslVerification,
timeout: preferences.request.timeout timeout: preferences.request.timeout,
storeCookies: get(preferences, 'request.storeCookies', true),
sendCookies: get(preferences, 'request.sendCookies', true)
}, },
validationSchema: preferencesSchema, validationSchema: preferencesSchema,
onSubmit: async (values) => { onSubmit: async (values) => {
@ -47,7 +52,9 @@ const General = ({ close }) => {
...preferences, ...preferences,
request: { request: {
sslVerification: newPreferences.sslVerification, sslVerification: newPreferences.sslVerification,
timeout: newPreferences.timeout timeout: newPreferences.timeout,
storeCookies: newPreferences.storeCookies,
sendCookies: newPreferences.sendCookies
} }
}) })
) )
@ -61,20 +68,46 @@ const General = ({ close }) => {
<StyledWrapper> <StyledWrapper>
<form className="bruno-form" onSubmit={formik.handleSubmit}> <form className="bruno-form" onSubmit={formik.handleSubmit}>
<div className="flex items-center mt-2"> <div className="flex items-center mt-2">
<label className="block font-medium mr-2 select-none" style={{ minWidth: 200 }} htmlFor="sslVerification">
SSL/TLS Certificate Verification
</label>
<input <input
id="ssl-cert-verification" id="sslVerification"
type="checkbox" type="checkbox"
name="sslVerification" name="sslVerification"
checked={formik.values.sslVerification} checked={formik.values.sslVerification}
onChange={formik.handleChange} onChange={formik.handleChange}
className="mousetrap mr-0" className="mousetrap mr-0"
/> />
<label className="block ml-2 select-none" htmlFor="sslVerification">
SSL/TLS Certificate Verification
</label>
</div>
<div className="flex items-center mt-2">
<input
id="storeCookies"
type="checkbox"
name="storeCookies"
checked={formik.values.storeCookies}
onChange={formik.handleChange}
className="mousetrap mr-0"
/>
<label className="block ml-2 select-none" htmlFor="storeCookies">
Store Cookies automatically
</label>
</div>
<div className="flex items-center mt-2">
<input
id="sendCookies"
type="checkbox"
name="sendCookies"
checked={formik.values.sendCookies}
onChange={formik.handleChange}
className="mousetrap mr-0"
/>
<label className="block ml-2 select-none" htmlFor="sendCookies">
Send Cookies automatically
</label>
</div> </div>
<div className="flex flex-col mt-6"> <div className="flex flex-col mt-6">
<label className="block font-medium select-none" htmlFor="timeout"> <label className="block select-none" htmlFor="timeout">
Request Timeout (in ms) Request Timeout (in ms)
</label> </label>
<input <input

View File

@ -189,9 +189,11 @@ const configureRequest = async (
request.timeout = preferencesUtil.getRequestTimeout(); request.timeout = preferencesUtil.getRequestTimeout();
// add cookies to request // add cookies to request
const cookieString = getCookieStringForUrl(request.url); if (preferencesUtil.shouldSendCookies()) {
if (cookieString && typeof cookieString === 'string' && cookieString.length) { const cookieString = getCookieStringForUrl(request.url);
request.headers['cookie'] = cookieString; if (cookieString && typeof cookieString === 'string' && cookieString.length) {
request.headers['cookie'] = cookieString;
}
} }
return axiosInstance; return axiosInstance;
@ -396,9 +398,6 @@ const registerNetworkIpc = (mainWindow) => {
scriptingConfig scriptingConfig
); );
// todo:
// i have no clue why electron can't send the request object
// without safeParseJSON(safeStringifyJSON(request.data))
mainWindow.webContents.send('main:run-request-event', { mainWindow.webContents.send('main:run-request-event', {
type: 'request-sent', type: 'request-sent',
requestSent: { requestSent: {
@ -461,14 +460,16 @@ const registerNetworkIpc = (mainWindow) => {
response.responseTime = responseTime; response.responseTime = responseTime;
// save cookies // save cookies
let setCookieHeaders = []; if (preferencesUtil.shouldStoreCookies()) {
if (response.headers['set-cookie']) { let setCookieHeaders = [];
setCookieHeaders = Array.isArray(response.headers['set-cookie']) if (response.headers['set-cookie']) {
? response.headers['set-cookie'] setCookieHeaders = Array.isArray(response.headers['set-cookie'])
: [response.headers['set-cookie']]; ? response.headers['set-cookie']
: [response.headers['set-cookie']];
for (let setCookieHeader of setCookieHeaders) { for (let setCookieHeader of setCookieHeaders) {
addCookieToJar(setCookieHeader, request.url); addCookieToJar(setCookieHeader, request.url);
}
} }
} }

View File

@ -11,6 +11,8 @@ const { get } = require('lodash');
const defaultPreferences = { const defaultPreferences = {
request: { request: {
sslVerification: true, sslVerification: true,
storeCookies: true,
sendCookies: true,
timeout: 0 timeout: 0
}, },
font: { font: {
@ -33,6 +35,8 @@ const defaultPreferences = {
const preferencesSchema = Yup.object().shape({ const preferencesSchema = Yup.object().shape({
request: Yup.object().shape({ request: Yup.object().shape({
sslVerification: Yup.boolean(), sslVerification: Yup.boolean(),
storeCookies: Yup.boolean(),
sendCookies: Yup.boolean(),
timeout: Yup.number() timeout: Yup.number()
}), }),
font: Yup.object().shape({ font: Yup.object().shape({
@ -101,6 +105,12 @@ const preferencesUtil = {
}, },
getGlobalProxyConfig: () => { getGlobalProxyConfig: () => {
return get(getPreferences(), 'proxy', {}); return get(getPreferences(), 'proxy', {});
},
shouldStoreCookies: () => {
return get(getPreferences(), 'request.storeCookies', true);
},
shouldSendCookies: () => {
return get(getPreferences(), 'request.sendCookies', true);
} }
}; };