#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 get from 'lodash/get';
import { useFormik } from 'formik';
import { useSelector, useDispatch } from 'react-redux';
import { savePreferences } from 'providers/ReduxStore/slices/app';
@ -12,6 +13,8 @@ const General = ({ close }) => {
const preferencesSchema = Yup.object().shape({
sslVerification: Yup.boolean(),
storeCookies: Yup.boolean(),
sendCookies: Yup.boolean(),
timeout: Yup.mixed()
.transform((value, originalValue) => {
return originalValue === '' ? undefined : value;
@ -28,7 +31,9 @@ const General = ({ close }) => {
const formik = useFormik({
initialValues: {
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,
onSubmit: async (values) => {
@ -47,7 +52,9 @@ const General = ({ close }) => {
...preferences,
request: {
sslVerification: newPreferences.sslVerification,
timeout: newPreferences.timeout
timeout: newPreferences.timeout,
storeCookies: newPreferences.storeCookies,
sendCookies: newPreferences.sendCookies
}
})
)
@ -61,20 +68,46 @@ const General = ({ close }) => {
<StyledWrapper>
<form className="bruno-form" onSubmit={formik.handleSubmit}>
<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
id="ssl-cert-verification"
id="sslVerification"
type="checkbox"
name="sslVerification"
checked={formik.values.sslVerification}
onChange={formik.handleChange}
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 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)
</label>
<input

View File

@ -189,9 +189,11 @@ const configureRequest = async (
request.timeout = preferencesUtil.getRequestTimeout();
// add cookies to request
const cookieString = getCookieStringForUrl(request.url);
if (cookieString && typeof cookieString === 'string' && cookieString.length) {
request.headers['cookie'] = cookieString;
if (preferencesUtil.shouldSendCookies()) {
const cookieString = getCookieStringForUrl(request.url);
if (cookieString && typeof cookieString === 'string' && cookieString.length) {
request.headers['cookie'] = cookieString;
}
}
return axiosInstance;
@ -396,9 +398,6 @@ const registerNetworkIpc = (mainWindow) => {
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', {
type: 'request-sent',
requestSent: {
@ -461,14 +460,16 @@ const registerNetworkIpc = (mainWindow) => {
response.responseTime = responseTime;
// save cookies
let setCookieHeaders = [];
if (response.headers['set-cookie']) {
setCookieHeaders = Array.isArray(response.headers['set-cookie'])
? response.headers['set-cookie']
: [response.headers['set-cookie']];
if (preferencesUtil.shouldStoreCookies()) {
let setCookieHeaders = [];
if (response.headers['set-cookie']) {
setCookieHeaders = Array.isArray(response.headers['set-cookie'])
? response.headers['set-cookie']
: [response.headers['set-cookie']];
for (let setCookieHeader of setCookieHeaders) {
addCookieToJar(setCookieHeader, request.url);
for (let setCookieHeader of setCookieHeaders) {
addCookieToJar(setCookieHeader, request.url);
}
}
}

View File

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