Skip to content

Commit

Permalink
Fix clobbering of overridden canvas methods
Browse files Browse the repository at this point in the history
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
  • Loading branch information
STRML committed Jul 30, 2020
1 parent 0a8a9a7 commit fa9b3ea
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/js/contentscripts/fingerprinting.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand All @@ -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);
Expand Down

0 comments on commit fa9b3ea

Please sign in to comment.