Skip to content

Commit

Permalink
Fix up konvajs single pixel getImageData issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanKingston committed Mar 5, 2021
1 parent 46e7ed7 commit 84dee37
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions shared/js/content-scope/fingerprint.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,40 @@ function initCanvasProtection (args) {
initExemptionList(stringExemptionList)
const domainKey = site.domain

const _getImageData = CanvasRenderingContext2D.prototype.getImageData
function computeOffScreenCanvas (canvas) {
const ctx = canvas.getContext('2d')
// We *always* compute the random pixels on the complete pixel set, then pass back the subset later
let imageData = _getImageData.apply(ctx, [0, 0, canvas.width, canvas.height])
imageData = modifyPixelData(imageData, sessionKey, domainKey)

// Make a off-screen canvas and put the data there
const offScreenCanvas = document.createElement('canvas')
offScreenCanvas.width = canvas.width
offScreenCanvas.height = canvas.height
const offScreenCtx = offScreenCanvas.getContext('2d')
offScreenCtx.putImageData(imageData, 0, 0)

return { offScreenCanvas, offScreenCtx }
}

// Using proxies here to swallow calls to toString etc
const getImageDataProxy = new Proxy(CanvasRenderingContext2D.prototype.getImageData, {
const getImageDataProxy = new Proxy(_getImageData, {
apply (target, thisArg, args) {
// The normal return value
const imageData = target.apply(thisArg, args)
if (shouldExemptMethod()) {
const imageData = target.apply(thisArg, args)
return imageData
}
// Anything we do here should be caught and ignored silently
try {
return modifyPixelData(imageData, sessionKey, domainKey)
const { offScreenCtx } = computeOffScreenCanvas(thisArg.canvas)
// Call the original method on the modified off-screen canvas
return target.apply(offScreenCtx, args)
} catch {
}

const imageData = target.apply(thisArg, args)
return imageData
}
})
Expand All @@ -124,16 +145,7 @@ function initCanvasProtection (args) {
return target.apply(thisArg, args)
}
try {
const ctx = thisArg.getContext('2d')
const imageData = ctx.getImageData(0, 0, thisArg.width, thisArg.height)

// Make a off-screen canvas and put the data there
const offScreenCanvas = document.createElement('canvas')
offScreenCanvas.width = thisArg.width
offScreenCanvas.height = thisArg.height
const offScreenCtx = offScreenCanvas.getContext('2d')
offScreenCtx.putImageData(imageData, 0, 0)

const { offScreenCanvas } = computeOffScreenCanvas(thisArg.canvas)
// Call the original method on the modified off-screen canvas
return target.apply(offScreenCanvas, args)
} catch {
Expand Down

0 comments on commit 84dee37

Please sign in to comment.