From 650897ac5cf14d1d3ae59dbe8a9bfdd3d2d8d62a Mon Sep 17 00:00:00 2001 From: James Gatz Date: Thu, 13 Apr 2023 11:54:10 +0200 Subject: [PATCH] guard against null requests in the history dropdown (#5890) --- packages/insomnia/src/common/misc.ts | 4 ++-- packages/insomnia/src/models/request-version.ts | 17 ++++++++++++----- .../dropdowns/response-history-dropdown.tsx | 8 +++++--- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/packages/insomnia/src/common/misc.ts b/packages/insomnia/src/common/misc.ts index cdb55ccaf..a1041d7c7 100644 --- a/packages/insomnia/src/common/misc.ts +++ b/packages/insomnia/src/common/misc.ts @@ -216,13 +216,13 @@ export function compressObject(obj: any) { return compressed.toString('base64'); } -export function decompressObject(input: string | null): any { +export function decompressObject(input: string | null): ObjectType | null { if (typeof input !== 'string') { return null; } const jsonBuffer = zlib.gunzipSync(Buffer.from(input, 'base64')); - return JSON.parse(jsonBuffer.toString('utf8')); + return JSON.parse(jsonBuffer.toString('utf8')) as ObjectType; } export function resolveHomePath(p: string) { diff --git a/packages/insomnia/src/models/request-version.ts b/packages/insomnia/src/models/request-version.ts index 6a42c76c9..4e4e960a4 100644 --- a/packages/insomnia/src/models/request-version.ts +++ b/packages/insomnia/src/models/request-version.ts @@ -33,7 +33,7 @@ const FIELDS_TO_IGNORE = [ 'description', 'parentId', 'name', -]; +] as const; export const isRequestVersion = (model: Pick): model is RequestVersion => ( model.type === type @@ -61,7 +61,7 @@ export async function create(request: Request | WebSocketRequest | GrpcRequest) const parentId = request._id; const latestRequestVersion: RequestVersion | null = await getLatestByParentId(parentId); const latestRequest = latestRequestVersion - ? decompressObject(latestRequestVersion.compressedRequest) + ? decompressObject(latestRequestVersion.compressedRequest) : null; const hasChanged = _diffRequests(latestRequest, request); @@ -91,7 +91,12 @@ export async function restore(requestVersionId: string) { return null; } - const requestPatch = decompressObject(requestVersion.compressedRequest); + const requestPatch = decompressObject(requestVersion.compressedRequest); + + if (!requestPatch) { + return null; + } + const originalRequest = await requestOperations.getById(requestPatch._id); if (!originalRequest) { @@ -100,7 +105,9 @@ export async function restore(requestVersionId: string) { // Only restore fields that aren't blacklisted for (const field of FIELDS_TO_IGNORE) { - delete requestPatch[field]; + if (field in requestPatch) { + delete requestPatch[field]; + } } return requestOperations.update(originalRequest, requestPatch); @@ -112,7 +119,7 @@ function _diffRequests(rOld: Request | WebSocketRequest | null, rNew: Request | for (const key of Object.keys(rOld) as (keyof typeof rOld)[]) { // Skip fields that aren't useful - if (FIELDS_TO_IGNORE.includes(key)) { + if (FIELDS_TO_IGNORE.find(field => field === key)) { continue; } if (!deepEqual(rOld[key], rNew[key])) { diff --git a/packages/insomnia/src/ui/components/dropdowns/response-history-dropdown.tsx b/packages/insomnia/src/ui/components/dropdowns/response-history-dropdown.tsx index 50096fa4b..e66897dbc 100644 --- a/packages/insomnia/src/ui/components/dropdowns/response-history-dropdown.tsx +++ b/packages/insomnia/src/ui/components/dropdowns/response-history-dropdown.tsx @@ -4,7 +4,9 @@ import { useSelector } from 'react-redux'; import { decompressObject } from '../../../common/misc'; import * as models from '../../../models/index'; +import { isRequest, Request } from '../../../models/request'; import { Response } from '../../../models/response'; +import { WebSocketRequest } from '../../../models/websocket-request'; import { isWebSocketResponse, WebSocketResponse } from '../../../models/websocket-response'; import { updateRequestMetaByParentId } from '../../hooks/create-request'; import { selectActiveEnvironment, selectActiveRequest, selectActiveRequestResponses, selectRequestVersions } from '../../redux/selectors'; @@ -119,7 +121,7 @@ export const ResponseHistoryDropdown = _id === response.requestVersionId); - const request = requestVersion ? decompressObject(requestVersion.compressedRequest) : null; + const request = requestVersion ? decompressObject(requestVersion.compressedRequest) : null; return (