Skip to content

Commit

Permalink
fix(Cookie expires): Invalid date on cookie expires (Kong#7614)
Browse files Browse the repository at this point in the history
* Fix always running after-response script

* Fix invalid date in cookies
  • Loading branch information
gatzjames authored and stefancruz committed Jun 30, 2024
1 parent 959f004 commit 2094687
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 5 deletions.
6 changes: 3 additions & 3 deletions packages/insomnia-sdk/src/objects/cookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export interface CookieOptions {
id?: string;
key: string;
value: string;
expires?: Date | string;
expires?: Date | string | null;
maxAge?: string | 'Infinity' | '-Infinity';
domain?: string;
path?: string;
Expand Down Expand Up @@ -180,8 +180,8 @@ export class CookieObject extends CookieList {
constructor(cookieJar: InsomniaCookieJar | null) {
const cookies = cookieJar
? cookieJar.cookies.map((cookie: InsomniaCookie): Cookie => {
let expires: string | Date = '';
if (cookie.expires) {
let expires: string | Date | null = null;
if (cookie.expires || cookie.expires === 0) {
if (typeof cookie.expires === 'number') {
expires = new Date(cookie.expires);
} else {
Expand Down
3 changes: 2 additions & 1 deletion packages/insomnia/src/main/network/libcurl-promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { invariant } from '../../utils/invariant';
invariant(process.type !== 'renderer', 'Native abstractions for Nodejs module unavailable in renderer');

import { Curl, CurlAuth, CurlCode, CurlFeature, CurlHttpVersion, CurlInfoDebug, CurlNetrc, CurlSslOpt } from '@getinsomnia/node-libcurl';
import { isValid } from 'date-fns';
import electron from 'electron';
import fs from 'fs';
import path from 'path';
Expand Down Expand Up @@ -371,7 +372,7 @@ export const createConfiguredCurlInstance = ({
cookie.hostOnly ? 'FALSE' : 'TRUE',
cookie.path,
cookie.secure ? 'TRUE' : 'FALSE',
cookie.expires ? Math.round(new Date(cookie.expires).getTime() / 1000) : 0,
cookie.expires && isValid(new Date(cookie.expires)) ? Math.round(new Date(cookie.expires).getTime() / 1000) : 0,
cookie.key,
cookie.value,
].join('\t');
Expand Down
3 changes: 3 additions & 0 deletions packages/insomnia/src/network/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,9 @@ export const tryToExecuteScript = async (context: RequestAndContextAndOptionalRe
globals: globals?.data || undefined,
},
});
// @TODO This looks overly complicated and could be simplified.
// If the timeout promise finishes first the execution promise is still running while it should be cancelled.
// Since the execution promise is cancellable and uses an abort controller we should add the timeout to the controller instead
const output = await Promise.race([timeoutPromise, executionPromise]) as {
request: Request;
environment: Record<string, any>;
Expand Down
5 changes: 5 additions & 0 deletions packages/insomnia/src/ui/components/cookie-list.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isValid } from 'date-fns';
import React, { FC, useCallback, useState } from 'react';
import { Cookie as ToughCookie } from 'tough-cookie';
import { v4 as uuidv4 } from 'uuid';
Expand Down Expand Up @@ -26,6 +27,10 @@ const CookieRow: FC<{
deleteCookie: (cookie: Cookie) => void;
}> = ({ cookie, deleteCookie }) => {
const [isCookieModalOpen, setIsCookieModalOpen] = useState(false);
if (cookie.expires && !isValid(new Date(cookie.expires))) {
cookie.expires = null;
}

const c = ToughCookie.fromJSON(cookie);
const cookieString = c ? cookieToString(c) : '';
return <tr className="selectable" key={cookie.id}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export const CookieModifyModal = ((props: ModalProps & CookieModifyModalOptions)
if (cookie && cookie.expires && isValid(new Date(cookie.expires))) {
localDateTime = new Date(cookie.expires).toISOString().slice(0, 16);
}

let rawDefaultValue;
if (!cookie) {
rawDefaultValue = '';
Expand Down
3 changes: 2 additions & 1 deletion packages/insomnia/src/ui/routes/request.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,8 @@ export const sendAction: ActionFunction = async ({ request, params }) => {
return null;
}
// disable after-response script here to avoiding rendering it
const afterResponseScript = `${mutatedContext.request.afterResponseScript}`;
// @TODO This should be handled in a better way. Maybe remove the key from the request object we pass in tryToInterpolateRequest
const afterResponseScript = mutatedContext.request.afterResponseScript ? `${mutatedContext.request.afterResponseScript}` : undefined;
mutatedContext.request.afterResponseScript = '';

window.main.addExecutionStep({ requestId, stepName: 'Rendering request' });
Expand Down

0 comments on commit 2094687

Please sign in to comment.