Skip to content

Commit 5fc45ec

Browse files
committed
Sticky pre-population: do not run on chained pages & move cookie parsing into try/catch block
1 parent d3bfd87 commit 5fc45ec

2 files changed

Lines changed: 20 additions & 16 deletions

File tree

packages/scripts/dist/sticky-prepopulation.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,16 @@ export class StickyPrepopulation {
3333
/*
3434
* Determine if we should run the script
3535
* Do not run if RememberMe is active
36+
* Do not run if on a chain link
3637
* Only run if StickyPrepopulation option is set with fields
3738
*/
3839
shouldRun() {
3940
if (ENGrid.getOption("RememberMe")) {
4041
return false;
4142
}
43+
const url = new URL(window.location.href);
4244
const options = ENGrid.getOption("StickyPrepopulation");
43-
if (options && (options === null || options === void 0 ? void 0 : options.fields.length) > 0) {
45+
if (options && (options === null || options === void 0 ? void 0 : options.fields.length) > 0 && !url.searchParams.get("chain")) {
4446
this.options = options;
4547
return true;
4648
}
@@ -97,13 +99,13 @@ export class StickyPrepopulation {
9799
this.logger.log("SupporterId present, not applying sticky prepopulation");
98100
return;
99101
}
100-
const encryptedSupporterDetails = JSON.parse(window.atob(cookieData));
101-
if (!encryptedSupporterDetails || (encryptedSupporterDetails === null || encryptedSupporterDetails === void 0 ? void 0 : encryptedSupporterDetails.pageId) !== ENGrid.getPageID()) {
102-
this.logger.log("No encrypted supporter details found in cookie, or page ID does not match");
103-
return;
104-
}
105102
let supporterDetails = {};
106103
try {
104+
const encryptedSupporterDetails = JSON.parse(window.atob(cookieData));
105+
if (!encryptedSupporterDetails || (encryptedSupporterDetails === null || encryptedSupporterDetails === void 0 ? void 0 : encryptedSupporterDetails.pageId) !== ENGrid.getPageID()) {
106+
this.logger.log("No encrypted supporter details found in cookie, or page ID does not match");
107+
return;
108+
}
107109
supporterDetails = JSON.parse(yield this.decryptSupporterDetails(this.base64ToArrayBuffer(encryptedSupporterDetails.encryptedData), new Uint8Array(this.base64ToArrayBuffer(encryptedSupporterDetails.iv))));
108110
}
109111
catch (e) {

packages/scripts/src/sticky-prepopulation.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,16 @@ export class StickyPrepopulation {
3232
/*
3333
* Determine if we should run the script
3434
* Do not run if RememberMe is active
35+
* Do not run if on a chain link
3536
* Only run if StickyPrepopulation option is set with fields
3637
*/
3738
private shouldRun(): boolean {
3839
if (ENGrid.getOption("RememberMe")) {
3940
return false;
4041
}
42+
const url = new URL(window.location.href);
4143
const options = ENGrid.getOption("StickyPrepopulation");
42-
if (options && options?.fields.length > 0) {
44+
if (options && options?.fields.length > 0 && !url.searchParams.get("chain")) {
4345
this.options = options;
4446
return true;
4547
} else {
@@ -102,18 +104,18 @@ export class StickyPrepopulation {
102104
return;
103105
}
104106

105-
const encryptedSupporterDetails = JSON.parse(
106-
window.atob(cookieData)
107-
);
108-
109-
if (!encryptedSupporterDetails || encryptedSupporterDetails?.pageId !== ENGrid.getPageID()) {
110-
this.logger.log("No encrypted supporter details found in cookie, or page ID does not match");
111-
return;
112-
}
113-
114107
let supporterDetails: { [key: string]: string } = {};
115108

116109
try {
110+
const encryptedSupporterDetails = JSON.parse(
111+
window.atob(cookieData)
112+
);
113+
114+
if (!encryptedSupporterDetails || encryptedSupporterDetails?.pageId !== ENGrid.getPageID()) {
115+
this.logger.log("No encrypted supporter details found in cookie, or page ID does not match");
116+
return;
117+
}
118+
117119
supporterDetails = JSON.parse(
118120
await this.decryptSupporterDetails(
119121
this.base64ToArrayBuffer(encryptedSupporterDetails.encryptedData),

0 commit comments

Comments
 (0)