Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed an issue where cookies were not included when running on localhost #1172

Merged
merged 1 commit into from
Sep 5, 2024
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
6 changes: 4 additions & 2 deletions src/core/createCookieTransfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ export default ({
* the request body so they can be read by the server.
*/
cookiesToPayload(payload, endpointDomain) {
const isEndpointFirstParty = endpointDomain.endsWith(apexDomain);

// localhost is a special case where the apexDomain is ""
// We want to treat localhost as a third-party domain.
const isEndpointFirstParty =
apexDomain !== "" && endpointDomain.endsWith(apexDomain);
const state = {
domain: apexDomain,
cookiesEnabled: true,
Expand Down
75 changes: 45 additions & 30 deletions test/unit/specs/core/createCookieTransfer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ governing permissions and limitations under the License.
import createCookieTransfer from "../../../../src/core/createCookieTransfer.js";

describe("createCookieTransfer", () => {
const apexDomain = "example.com";
let apexDomain;
const endpointDomain = "thirdparty.com";
let shouldTransferCookie;
let payload;
Expand All @@ -23,20 +23,25 @@ describe("createCookieTransfer", () => {
const dateProvider = () => date;

beforeEach(() => {
apexDomain = "example.com";
shouldTransferCookie = jasmine.createSpy("shouldTransferCookie");
shouldTransferCookie.and.returnValue(false);
payload = jasmine.createSpyObj("payload", ["mergeState"]);
cookieJar = jasmine.createSpyObj("cookieJar", ["get", "set"]);
});

const build = () => {
cookieTransfer = createCookieTransfer({
cookieJar,
shouldTransferCookie,
apexDomain,
dateProvider,
});
});
};

describe("cookiesToPayload", () => {
it("does not transfer cookies to payload if endpoint is first-party", () => {
build();
cookieTransfer.cookiesToPayload(payload, "edge.example.com");
expect(payload.mergeState).toHaveBeenCalledWith({
domain: apexDomain,
Expand All @@ -46,41 +51,46 @@ describe("createCookieTransfer", () => {

it("does not set state.entries if there are no qualifying cookies", () => {
cookieJar.get.and.returnValue({});
build();
cookieTransfer.cookiesToPayload(payload, endpointDomain);
expect(payload.mergeState).toHaveBeenCalledWith({
domain: apexDomain,
cookiesEnabled: true,
});
});

it("transfers eligible cookies to payload", () => {
cookieJar.get.and.returnValue({
kndctr_ABC_CustomOrg_identity: "XYZ@CustomOrg",
ineligible_cookie: "foo",
kndctr_ABC_CustomOrg_optIn: "all",
at_qa_mode:
'{"token":"QATokenString","listedActivitiesOnly":true,"evaluateAsTrueAudienceIds":["2480042"],"previewIndexes":[{"activityIndex":1,"experienceIndex":1}]}',
});
shouldTransferCookie.and.returnValues(true, false, true, true);
cookieTransfer.cookiesToPayload(payload, endpointDomain);
expect(payload.mergeState).toHaveBeenCalledWith({
domain: apexDomain,
cookiesEnabled: true,
entries: [
{
key: "kndctr_ABC_CustomOrg_identity",
value: "XYZ@CustomOrg",
},
{
key: "kndctr_ABC_CustomOrg_optIn",
value: "all",
},
{
key: "at_qa_mode",
value:
'{"token":"QATokenString","listedActivitiesOnly":true,"evaluateAsTrueAudienceIds":["2480042"],"previewIndexes":[{"activityIndex":1,"experienceIndex":1}]}',
},
],
["example.com", ""].forEach((domain) => {
it(`transfers eligible cookies to payload with domain ${domain}`, () => {
apexDomain = domain;
build();
cookieJar.get.and.returnValue({
kndctr_ABC_CustomOrg_identity: "XYZ@CustomOrg",
ineligible_cookie: "foo",
kndctr_ABC_CustomOrg_optIn: "all",
at_qa_mode:
'{"token":"QATokenString","listedActivitiesOnly":true,"evaluateAsTrueAudienceIds":["2480042"],"previewIndexes":[{"activityIndex":1,"experienceIndex":1}]}',
});
shouldTransferCookie.and.returnValues(true, false, true, true);
cookieTransfer.cookiesToPayload(payload, endpointDomain);
expect(payload.mergeState).toHaveBeenCalledWith({
domain: apexDomain,
cookiesEnabled: true,
entries: [
{
key: "kndctr_ABC_CustomOrg_identity",
value: "XYZ@CustomOrg",
},
{
key: "kndctr_ABC_CustomOrg_optIn",
value: "all",
},
{
key: "at_qa_mode",
value:
'{"token":"QATokenString","listedActivitiesOnly":true,"evaluateAsTrueAudienceIds":["2480042"],"previewIndexes":[{"activityIndex":1,"experienceIndex":1}]}',
},
],
});
});
});
});
Expand All @@ -92,6 +102,7 @@ describe("createCookieTransfer", () => {
});

it("adds a cookie with the correct domain", () => {
build();
response.getPayloadsByType.and.returnValue([
{
key: "mykey",
Expand All @@ -105,6 +116,7 @@ describe("createCookieTransfer", () => {
});

it("adds multiple cookies", () => {
build();
response.getPayloadsByType.and.returnValue([
{
key: "mykey1",
Expand All @@ -129,6 +141,7 @@ describe("createCookieTransfer", () => {
});

it("sets the expires attribute", () => {
build();
response.getPayloadsByType.and.returnValue([
{
key: "mykey",
Expand All @@ -143,6 +156,7 @@ describe("createCookieTransfer", () => {
});

it("adds a sameSite=none cookie with secure attribute", () => {
build();
response.getPayloadsByType.and.returnValue([
{
key: "mykey",
Expand All @@ -156,6 +170,7 @@ describe("createCookieTransfer", () => {
});

it("adds a sameSite=strict cookie", () => {
build();
response.getPayloadsByType.and.returnValue([
{
key: "mykey",
Expand Down
Loading