fix: ensure cookie tag respects the URL (#9534)

This commit is contained in:
Bingbing
2026-01-07 13:39:01 +08:00
committed by GitHub
parent 763b67da80
commit 837ea2be7d
5 changed files with 33 additions and 8 deletions

View File

@@ -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<PluginToMainAPIPaths, (...args: any[]) => 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);
},

View File

@@ -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) =>

View File

@@ -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,

View File

@@ -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;
}

View File

@@ -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<Workspace | undefined> };
oAuth2Token: { getByRequestId: (id: string) => Promise<OAuth2Token | undefined> };
cookieJar: { getOrCreateForParentId: (parentId: string) => Promise<CookieJar> };
cookieJar: {
getOrCreateForParentId: (parentId: string) => Promise<CookieJar>;
getCookiesForUrl: (parentId: string, url: string) => Promise<Cookie[]>;
};
response: {
getLatestForRequestId: typeof getLatestForRequestId;
getBodyBuffer: typeof getBodyBuffer;