@@ -129,14 +129,28 @@ class Database {
129129 }
130130
131131 /**
132- * Gets the persisted payload of a given metric in a given ping.
132+ * Gets and validates the persisted payload of a given metric in a given ping.
133+ *
134+ * If the persisted value is invalid for the metric we are attempting to retrieve,
135+ * the persisted value is deleted and `undefined is returned.
136+ *
137+ * This behaviour is not consistent with what the Glean SDK does, but this is on purpose.
138+ * On the Glean SDK we panic when we can't serialize the persisted value,
139+ * that is because this is an extremely unlikely situation for that environment.
140+ *
141+ * Since Glean.js will run on the browser, it is easy for a consumers / developers
142+ * to mess with the storage which makes this sort of errors plausible.
143+ * That is why we choose to not panic and simply delete the corrupted data here.
144+ *
145+ * Note: This is not a strong guard against consumers / developers messing with the storage on their own.
146+ * Currently Glean.js does not include mechanisms to reliably prevent that.
133147 *
134148 * @param ping The ping from which we want to retrieve the given metric.
135- * @param validateFn A validation function to verify if persisted payload is of the correct type .
149+ * @param validateFn A validation function to verify if persisted payload is in the correct format .
136150 * @param metric An object containing the information about the metric to retrieve.
137151 *
138152 * @returns The payload persisted for the given metric,
139- * `undefined` in case the metric has not been recorded yet.
153+ * `undefined` in case the metric has not been recorded yet or the found valus in invalid .
140154 */
141155 async getMetric < T > (
142156 ping : string ,
@@ -147,13 +161,6 @@ class Database {
147161 const storageKey = metric . identifier ;
148162 const value = await store . get ( [ ping , metric . type , storageKey ] ) ;
149163 if ( ! isUndefined ( value ) && ! validateFn ( value ) ) {
150- // The following behaviour is not consistent with what the Glean SDK does, but this is on purpose.
151- // On the Glean SDK we panic when we can't serialize the given,
152- // that is because this is a extremely unlikely situation for that environment.
153- //
154- // Since Glean.js will run on the browser, it is easy for a user to mess with the persisted data
155- // which makes this sort of errors plausible. That is why we choose to not panic and
156- // simply delete the corrupted data here.
157164 console . error ( `Unexpected value found for metric ${ metric . identifier } : ${ JSON . stringify ( value ) } . Clearing.` ) ;
158165 await store . delete ( [ ping , metric . type , storageKey ] ) ;
159166 return ;
0 commit comments