Skip to content

Commit

Permalink
#968, #1023: support for disabling sending and storing of cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
helloanoop committed Dec 1, 2023
1 parent ba994cb commit 32c8bf2
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 21 deletions.
47 changes: 40 additions & 7 deletions packages/bruno-app/src/components/Preferences/General/index.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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;
Expand All @@ -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) => {
Expand All @@ -47,7 +52,9 @@ const General = ({ close }) => {
...preferences,
request: {
sslVerification: newPreferences.sslVerification,
timeout: newPreferences.timeout
timeout: newPreferences.timeout,
storeCookies: newPreferences.storeCookies,
sendCookies: newPreferences.sendCookies
}
})
)
Expand All @@ -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
Expand Down
29 changes: 15 additions & 14 deletions packages/bruno-electron/src/ipc/network/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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']];

for (let setCookieHeader of setCookieHeaders) {
addCookieToJar(setCookieHeader, request.url);
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);
}
}
}

Expand Down
10 changes: 10 additions & 0 deletions packages/bruno-electron/src/store/preferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const { get } = require('lodash');
const defaultPreferences = {
request: {
sslVerification: true,
storeCookies: true,
sendCookies: true,
timeout: 0
},
font: {
Expand All @@ -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({
Expand Down Expand Up @@ -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);
}
};

Expand Down

0 comments on commit 32c8bf2

Please sign in to comment.