From e99a74201a1d7ae40b699ec372bdfed58e07142e Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Fri, 3 Nov 2017 19:32:22 +0100 Subject: [PATCH] Fixed raw cookie editor deleting cookies --- app/common/cookies.js | 1 + .../components/modals/cookie-modify-modal.js | 36 ++++++++++++++----- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/app/common/cookies.js b/app/common/cookies.js index c6cbe9c75..4ae18af4a 100644 --- a/app/common/cookies.js +++ b/app/common/cookies.js @@ -12,6 +12,7 @@ export function cookiesFromJar (jar) { console.warn('Failed to get cookies form jar', err); resolve([]); } else { + // NOTE: Perform toJSON so we have a plain JS object instead of Cookie instance resolve(cookies.map(c => c.toJSON())); } }); diff --git a/app/ui/components/modals/cookie-modify-modal.js b/app/ui/components/modals/cookie-modify-modal.js index f44ebac6d..e98ca8357 100644 --- a/app/ui/components/modals/cookie-modify-modal.js +++ b/app/ui/components/modals/cookie-modify-modal.js @@ -78,25 +78,39 @@ class CookieModifyModal extends React.PureComponent { await models.cookieJar.update(cookieJar); } - _handleChangeRawValue (e: Event) { - if (!(e.target instanceof HTMLInputElement)) { - return; - } - - const value = e.target.value; + _handleChangeRawValue (e: SyntheticEvent) { + const value = e.currentTarget.value; clearTimeout(this._rawTimeout); this._rawTimeout = setTimeout(async () => { - const cookie = toughCookie.Cookie.parse(value); - if (!this.state.cookie) { + const oldCookie = this.state.cookie; + let cookie; + try { + // NOTE: Perform toJSON so we have a plain JS object instead of Cookie instance + cookie = toughCookie.Cookie.parse(value).toJSON(); + } catch (err) { + console.warn(`Failed to parse cookie string "${value}"`, err); return; } + if (!this.state.cookie || !oldCookie) { + return; + } + + // Make sure cookie has an id + cookie.id = oldCookie.id; + await this._handleCookieUpdate(cookie); }, DEBOUNCE_MILLIS * 2); } async _handleCookieUpdate (newCookie: Cookie) { + const oldCookie = this.state.cookie; + if (!oldCookie) { + // We don't have a cookie to edit + return; + } + const cookie = clone(newCookie); // Sanitize expires field @@ -111,9 +125,13 @@ class CookieModifyModal extends React.PureComponent { const cookieJar = clone(this.props.cookieJar); const {cookies} = cookieJar; - const index = cookies.findIndex(c => c.id === cookie.id); + if (index < 0) { + console.warn(`Could not find cookie with id=${cookie.id} to edit`); + return; + } + cookieJar.cookies = [ ...cookies.slice(0, index), cookie,