Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changelog/8152-update-cookie-wildcard.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
type: Fixed
description: Update the cookie wildcard placeholder
pr: 8152
labels: []
18 changes: 9 additions & 9 deletions clients/fides-js/__tests__/lib/cookie.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -495,10 +495,10 @@ describe("cookies", () => {
});

it("returns true for wildcard cookies", () => {
expect(isWildcardCookie({ name: "_ga[id]" })).toBeTruthy();
expect(isWildcardCookie({ name: "_ga-id-" })).toBeTruthy();
expect(
isWildcardCookie({
name: "_ga_[id]_[id]",
name: "_ga_-id-_-id-",
}),
).toBeTruthy();
});
Expand Down Expand Up @@ -615,7 +615,7 @@ describe("cookies", () => {
other_cookie: "other_value",
} as any);
removeCookiesFromBrowser(
[{ name: "_ga[id]", domain: "foo.com", path: "/bar" }],
[{ name: "_ga-id-", domain: "foo.com", path: "/bar" }],
false,
false,
);
Expand All @@ -629,7 +629,7 @@ describe("cookies", () => {
other_cookie: "other_value",
} as any);
removeCookiesFromBrowser(
[{ name: "_ga[id]", domain: "foo.com", path: "/bar" }],
[{ name: "_ga-id-", domain: "foo.com", path: "/bar" }],
true,
true,
);
Expand All @@ -644,7 +644,7 @@ describe("cookies", () => {
foo_abc: "test_value_2",
} as any);
removeCookiesFromBrowser(
[{ name: "_ga[id]" }, { name: "foo_[id]" }],
[{ name: "_ga-id-" }, { name: "foo_-id-" }],
false,
false,
);
Expand All @@ -659,7 +659,7 @@ describe("cookies", () => {
cab123: "",
} as any);
removeCookiesFromBrowser(
[{ name: "x[id]" }, { name: "ab[id]" }, { name: "y[id]" }],
[{ name: "x-id-" }, { name: "ab-id-" }, { name: "y-id-" }],
false,
false,
);
Expand All @@ -670,7 +670,7 @@ describe("cookies", () => {
ab: "",
ab123: "",
} as any);
removeCookiesFromBrowser([{ name: "ab[id]" }], false, false);
removeCookiesFromBrowser([{ name: "ab-id-" }], false, false);
expect(mockRemoveCookie.mock.calls).toEqual([["ab123", { path: "/" }]]);
});
it("should handle wildcard cookies with special characters", () => {
Expand All @@ -679,7 +679,7 @@ describe("cookies", () => {
[`${prefix}123`]: "test_value",
other_cookie: "other_value",
} as any);
removeCookiesFromBrowser([{ name: `${prefix}[id]` }], false, false);
removeCookiesFromBrowser([{ name: `${prefix}-id-` }], false, false);
expect(mockRemoveCookie.mock.calls).toEqual([
[`${prefix}123`, { path: "/" }],
]);
Expand All @@ -690,7 +690,7 @@ describe("cookies", () => {
"_ga_789.101": "test_value_2",
other_cookie: "other_value",
} as any);
removeCookiesFromBrowser([{ name: "_ga_[id]_[id]" }], false, false);
removeCookiesFromBrowser([{ name: "_ga_-id-_-id-" }], false, false);
expect(mockRemoveCookie.mock.calls).toEqual([
["_ga_123_456", { path: "/" }],
]);
Expand Down
8 changes: 4 additions & 4 deletions clients/fides-js/src/lib/cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -619,13 +619,13 @@ export const makeConsentDefaultsLegacy = (
};

/**
* Determine if the provided cookie is a wildcard cookie, i.e., the name contains `[id]`.
* Determine if the provided cookie is a wildcard cookie, i.e., the name contains `-id-`.
* These are used to represent sets of cookies that have some sort of a unique identifier
* in their name. For example, a site might set multiple cookies like `_ga_12345` and
* `_ga_67890` and both of these will match a wildcard cookie with the name `_ga_[id]`.
* `_ga_67890` and both of these will match a wildcard cookie with the name `_ga_-id-`.
*/
export const isWildcardCookie = (cookie: CookiesType): boolean => {
return cookie.name.includes("[id]");
return cookie.name.includes("-id-");
};

/**
Expand Down Expand Up @@ -666,7 +666,7 @@ export const removeCookiesFromBrowser = (
wildcardCookies.forEach((wCookie) => {
const namePattern = wCookie.name
.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") // Escape special regex chars
.replace(/\\\[id\\\]/g, ".+?"); // Replace \[id\] with non-greedy wildcard
.replace(/-id-/g, ".+?"); // Replace -id- with non-greedy wildcard
const pattern = new RegExp(`^(${namePattern})$`);
Object.keys(allCookies).forEach((name) => {
if (pattern.test(name)) {
Expand Down
Loading