From 837ea2be7ddc851963269acce2e6bc9080331c18 Mon Sep 17 00:00:00 2001 From: Bingbing Date: Wed, 7 Jan 2026 13:39:01 +0800 Subject: [PATCH] fix: ensure cookie tag respects the URL (#9534) --- .../src/main/templating-worker-database.ts | 7 +++++++ .../src/templating/base-extension-worker.ts | 2 ++ .../insomnia/src/templating/base-extension.ts | 7 +++++++ .../src/templating/local-template-tags.ts | 17 ++++++++++------- packages/insomnia/src/templating/types.ts | 8 +++++++- 5 files changed, 33 insertions(+), 8 deletions(-) diff --git a/packages/insomnia/src/main/templating-worker-database.ts b/packages/insomnia/src/main/templating-worker-database.ts index 8f345bb30b..c585d4be61 100644 --- a/packages/insomnia/src/main/templating-worker-database.ts +++ b/packages/insomnia/src/main/templating-worker-database.ts @@ -6,6 +6,8 @@ import { shell } from 'electron'; import iconv from 'iconv-lite'; import { v4 as uuidv4 } from 'uuid'; +import { jarFromCookies } from '~/common/cookies'; + import { getAppBundlePlugins, RESPONSE_CODE_REASONS } from '../common/constants'; import { isDevelopment } from '../common/constants'; import { database as db } from '../common/database'; @@ -102,6 +104,11 @@ const pluginToMainAPI: Record Promise< 'cookieJar.getOrCreateForParentId': async (body: { parentId: string }) => { return await models.cookieJar.getOrCreateForParentId(body.parentId); }, + 'cookieJar.getCookiesForUrl': async (body: { parentId: string; url: string }) => { + const cookies = await models.cookieJar.getOrCreateForParentId(body.parentId); + const jar = jarFromCookies(cookies.cookies); + return jar.getCookiesSync(body.url); + }, 'response.getLatestForRequestId': async (body: { requestId: string; environmentId: string }) => { return await models.response.getLatestForRequestId(body.requestId, body.environmentId); }, diff --git a/packages/insomnia/src/templating/base-extension-worker.ts b/packages/insomnia/src/templating/base-extension-worker.ts index 2dc9698cef..7631965da0 100644 --- a/packages/insomnia/src/templating/base-extension-worker.ts +++ b/packages/insomnia/src/templating/base-extension-worker.ts @@ -220,6 +220,8 @@ export default class BaseExtension { cookieJar: { getOrCreateForParentId: async (parentId: string) => fetchFromTemplateWorkerDatabase('cookieJar.getOrCreateForParentId', { parentId }), + getCookiesForUrl: async (parentId: string, url: string) => + fetchFromTemplateWorkerDatabase('cookieJar.getCookiesForUrl', { parentId, url }), }, response: { getLatestForRequestId: async (requestId: string, environmentId: string | null) => diff --git a/packages/insomnia/src/templating/base-extension.ts b/packages/insomnia/src/templating/base-extension.ts index c041ddb49e..a53fc82de0 100644 --- a/packages/insomnia/src/templating/base-extension.ts +++ b/packages/insomnia/src/templating/base-extension.ts @@ -4,6 +4,8 @@ import os from 'node:os'; import iconv from 'iconv-lite'; +import { jarFromCookies } from '~/common/cookies'; + import { database as db } from '../common/database'; import { secureReadFile } from '../main/secure-read-file'; import * as models from '../models/index'; @@ -155,6 +157,11 @@ export default class BaseExtension { getOrCreateForParentId: (parentId: string) => { return models.cookieJar.getOrCreateForParentId(parentId); }, + getCookiesForUrl: async (parentId: string, url: string) => { + const cookies = await models.cookieJar.getOrCreateForParentId(parentId); + const jar = jarFromCookies(cookies.cookies); + return jar.getCookiesSync(url); + }, }, response: { getLatestForRequestId: models.response.getLatestForRequestId, diff --git a/packages/insomnia/src/templating/local-template-tags.ts b/packages/insomnia/src/templating/local-template-tags.ts index 0bb226563d..275aa0ce8b 100644 --- a/packages/insomnia/src/templating/local-template-tags.ts +++ b/packages/insomnia/src/templating/local-template-tags.ts @@ -348,11 +348,13 @@ const localTemplatePlugins: { templateTag: PluginTemplateTag }[] = [ throw new Error(`Workspace not found for ${meta.workspaceId}`); } - const cookieJar = await context.util.models.cookieJar.getOrCreateForParentId(workspace._id); - const found = cookieJar.cookies.find(cookie => cookie.key === name); + const cookies = url + ? await context.util.models.cookieJar.getCookiesForUrl(workspace._id, url) + : (await context.util.models.cookieJar.getOrCreateForParentId(workspace._id)).cookies; + const found = cookies.find(cookie => cookie.key === name); invariant( found, - `No cookie with name "${name}" found in cookie jar for url "${url}"\nChoices are [\n\t${cookieJar.cookies.map(c => c.key)}\n] for`, + `No cookie with name "${name}" found in cookie jar for url "${url}"\nChoices are [\n\t${cookies.map(c => c.key)}\n] for`, ); return found.value; }, @@ -900,12 +902,13 @@ const localTemplatePlugins: { templateTag: PluginTemplateTag }[] = [ throw new Error('No cookie specified'); } - const cookieJar = await context.util.models.cookieJar.getOrCreateForParentId(workspace._id); - - const found = cookieJar.cookies.find(cookie => cookie.key === name); + const cookies = request.url + ? await context.util.models.cookieJar.getCookiesForUrl(workspace._id, request.url) + : (await context.util.models.cookieJar.getOrCreateForParentId(workspace._id)).cookies; + const found = cookies.find(cookie => cookie.key === name); invariant( found, - `No cookie with name "${name}" found in cookie jar for url "${request.url}"\nChoices are [\n\t${cookieJar.cookies.map(c => c.key)}\n] for`, + `No cookie with name "${name}" found in cookie jar for url "${request.url}"\nChoices are [\n\t${cookies.map(c => c.key)}\n] for`, ); return found.value; } diff --git a/packages/insomnia/src/templating/types.ts b/packages/insomnia/src/templating/types.ts index 156c85ee5a..71967ffa29 100644 --- a/packages/insomnia/src/templating/types.ts +++ b/packages/insomnia/src/templating/types.ts @@ -1,5 +1,7 @@ import type { BinaryToTextEncoding } from 'node:crypto'; +import type { Cookie } from 'tough-cookie'; + import type { CloudProviderCredential } from '../models/cloud-credential'; import type { CookieJar } from '../models/cookie-jar'; import type { Environment, UserUploadEnvironment } from '../models/environment'; @@ -29,6 +31,7 @@ export type PluginToMainAPIPaths = | 'workspace.getById' | 'oAuth2Token.getByRequestId' | 'cookieJar.getOrCreateForParentId' + | 'cookieJar.getCookiesForUrl' | 'response.getLatestForRequestId' | 'response.getBodyBuffer' | 'pluginData.hasItem' @@ -281,7 +284,10 @@ export interface PluginTemplateTagContext { }; workspace: { getById: (id: string) => Promise }; oAuth2Token: { getByRequestId: (id: string) => Promise }; - cookieJar: { getOrCreateForParentId: (parentId: string) => Promise }; + cookieJar: { + getOrCreateForParentId: (parentId: string) => Promise; + getCookiesForUrl: (parentId: string, url: string) => Promise; + }; response: { getLatestForRequestId: typeof getLatestForRequestId; getBodyBuffer: typeof getBodyBuffer;