Skip to content

Commit

Permalink
fix: prevent unparseable response cookie from causing request to fail
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan-willis committed Jan 10, 2025
1 parent 3ff0d34 commit 052b551
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 3 deletions.
5 changes: 5 additions & 0 deletions packages/insomnia-sdk/src/objects/__tests__/cookies.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ describe('test Cookie object', () => {
expect(
Cookie.unparseSingle(cookie1Opt)
).toEqual(expectedCookieString);

const malformedCookieString = '=gingerale';
expect(
Cookie.parse(malformedCookieString)
).toBeFalsy();
});

it('test cookie transforming', () => {
Expand Down
5 changes: 4 additions & 1 deletion packages/insomnia-sdk/src/objects/cookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface CookieOptions extends InsomniaCookieExtensions {
export class Cookie extends Property {
override readonly _kind: string = 'Cookie';
protected cookie: ToughCookie;
public isValid: boolean = true;
private extensions?: { key: string; value: string }[];
private insoExtensions: InsomniaCookieExtensions = {};

Expand All @@ -38,6 +39,7 @@ export class Cookie extends Property {
if (typeof cookieDef === 'string') {
const cookieDefParsed = Cookie.parse(cookieDef);
if (!cookieDefParsed) {
this.isValid = false;
throw Error('failed to parse cookie, the cookie string seems invalid');
}
cookieDef = cookieDefParsed;
Expand Down Expand Up @@ -71,7 +73,8 @@ export class Cookie extends Property {
static parse(cookieStr: string) {
const cookieObj = ToughCookie.parse(cookieStr);
if (!cookieObj) {
throw Error('failed to parse cookie, the cookie string seems invalid');
// throw Error('failed to parse cookie, the cookie string seems invalid');
return false;
}

const hostOnly = cookieObj.extensions?.includes('HostOnly') || false;
Expand Down
2 changes: 1 addition & 1 deletion packages/insomnia-sdk/src/objects/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class Response extends Property {
this.body = options.body || '';
this.code = options.code;
this.cookies = new CookieList(
options.cookie?.map(cookie => new Cookie(cookie)) || [],
(options.cookie?.map(cookie => new Cookie(cookie)) || []).filter(cookie => cookie.isValid),
);
this.headers = new HeaderList(
undefined,
Expand Down
2 changes: 1 addition & 1 deletion packages/insomnia-sdk/src/objects/send-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ async function curlOutputToResponse(
};
}

return cookieObj;
return undefined;
})
.filter(cookieOpt => cookieOpt !== undefined);

Expand Down

0 comments on commit 052b551

Please sign in to comment.