Skip to content

Commit

Permalink
Merge pull request #1172 from adobe/fixLocalhostCookies
Browse files Browse the repository at this point in the history
Fixed an issue where cookies were not included when running on localhost
  • Loading branch information
jonsnyder authored Sep 5, 2024
2 parents 33ddbfd + 87c6e4c commit 7c8f950
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 32 deletions.
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

0 comments on commit 7c8f950

Please sign in to comment.