From 70b3d21da2585c5a214a7b1292f5a0f5edd92f5e Mon Sep 17 00:00:00 2001 From: Samuel Reed Date: Thu, 30 Jul 2020 19:20:16 -0400 Subject: [PATCH] Fix clobbering of overridden canvas methods The method may be overridden again after we override, as is done in the `hidpi-canvas-polyfill`. So if we restore the method to the original as we see it, we'll wipe out the polyfill. Fixes #2657, #1226 --- src/js/contentscripts/fingerprinting.js | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/js/contentscripts/fingerprinting.js b/src/js/contentscripts/fingerprinting.js index 28cdb4641b..4af10bb9dc 100644 --- a/src/js/contentscripts/fingerprinting.js +++ b/src/js/contentscripts/fingerprinting.js @@ -204,14 +204,19 @@ function getFpPageScript() { ); item.obj[item.propName] = (function (orig) { + // set to true after the first write, if the method is not + // restorable. Happens if another library also overwrites + // this method. + var skipMonitoring = false; function wrapped() { var args = arguments; if (is_canvas_write) { // to avoid false positives, - // bail if the text being written is too short - if (!args[0] || args[0].length < 5) { + // bail if the text being written is too short, + // of if we've already sent a monitoring payload + if (skipMonitoring || !args[0] || args[0].length < 5) { return orig.apply(this, args); } } @@ -237,7 +242,13 @@ function getFpPageScript() { // optimization: one canvas write is enough, // restore original write method // to this CanvasRenderingContext2D object instance - this[item.propName] = orig; + // Careful! Only restorable if we haven't already been replaced + // by another lib, such as the hidpi polyfill + if (this[item.propName] === wrapped) { + this[item.propName] = orig; + } else { + skipMonitoring = true; + } } return orig.apply(this, args);