Bug 1813809 - Implement safeguard for Feature Callout pref getter function r=omc-reviewers,aminomancer

Differential Revision: https://phabricator.services.mozilla.com/D171925
This commit is contained in:
hanna alemu 2023-03-22 16:17:13 +00:00
parent b737a6612d
commit 36ee12359c
2 changed files with 20 additions and 3 deletions

View File

@ -17,7 +17,18 @@ const ONE_DAY_IN_MS = 24 * 60 * 60 * 1000;
// id found in a given Feature Callout tour progress preference
// and the `complete` property being true
const matchCurrentScreenTargeting = (prefName, screenId) => {
return `'${prefName}' | preferenceValue | regExpMatch('(?<=screen\"\:)"(.*)(?=",)')[1] == '${screenId}' && '${prefName}' | preferenceValue | regExpMatch('(?<=complete\"\:)(.*)(?=})')[1] != "true"`;
const prefVal = `'${prefName}' | preferenceValue`;
//regExpMatch() is a JEXL filter expression. Here we check if 'screen' and 'complete' exist in the pref's value (which is stringified JSON), and return their values. Returns null otherwise
const screenRegEx = '(?<=screen":)"(.*)(?=",)';
const completeRegEx = '(?<=complete":)(.*)(?=})';
const screenMatch = `${prefVal} | regExpMatch('${screenRegEx}')`;
const completeMatch = `${prefVal} | regExpMatch('${completeRegEx}')`;
//We are checking the return of regExpMatch() here. If it's truthy, we grab the matched string and compare it to the desired value
const screenVal = `(${screenMatch}) ? (${screenMatch}[1] == '${screenId}') : false`;
const completeVal = `(${completeMatch}) ? (${completeMatch}[1] != "true") : false`;
return `(${screenVal}) && (${completeVal})`;
};
/**

View File

@ -58,7 +58,13 @@ export class FeatureCallout {
prefName,
'{"screen":"","complete":true}',
this._handlePrefChange.bind(this),
val => JSON.parse(val)
val => {
try {
return JSON.parse(val);
} catch (error) {
return null;
}
}
);
XPCOMUtils.defineLazyPreferenceGetter(
@ -268,7 +274,7 @@ export class FeatureCallout {
// Don't render the callout if the parent element is not present.
// This means the message was misconfigured, mistargeted, or the
// content of the parent page is not as expected.
if (!parent && !this.currentScreen?.content.callout_position_override) {
if (!parent && !this.currentScreen?.content?.callout_position_override) {
if (this.message?.template === "feature_callout") {
Services.telemetry.recordEvent(
"messaging_experiments",