1
0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2024-12-22 21:00:23 +01:00

Add iFrame test.

This commit is contained in:
kkapsner 2019-05-24 18:31:11 +02:00
parent c8c311a82f
commit fb0ff7ac02
3 changed files with 141 additions and 0 deletions

39
test/iframeTest.html Normal file
View File

@ -0,0 +1,39 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>iFrame Test</title>
<link href="testIcon.svg" type="image/png" rel="icon">
<link href="testIcon.svg" type="image/png" rel="shortcut icon">
</head>
<body>
<script src="iframeTest.js"></script>
<iframe></iframe><script>
const iframe = window[0];
log("iframe in html:", compare(test(iframe), reference));
iframe.addEventListener("load", function(){
log("iframe after loading:", compare(test(iframe), reference));
});
document.write("<iframe></iframe><script>log(\"iframe and script in document.write:\", compare(test(window[1]), reference));<\/script>");
log("iframe in document.write:", compare(test(window[1]), reference));
document.write("<iframe></iframe>");
document.write("<script>log(\"iframe and script in separate document.write:\", compare(test(window[2]), reference));<\/script>");
"<iframe></iframe><script>log(\"iframe and script in fragmented document.write:\", compare(test(window[3]), reference));<\/script>".split(/(?=<)/).forEach(function(part){
document.write(part);
});
document.writeln("<iframe></iframe><script>log(\"iframe and script in document.writeln:\", compare(test(window[4]), reference));<\/script>");
document.write("<script src=\"iframeTest.js\"><\/script><iframe></iframe><script>log(\"script with src, iframe and script in document.write:\", compare(test(window[5]), reference));<\/script>");
window.addEventListener("load", function(){
// document.open();
document.write("<script src=\"iframeTest.js\"><\/script><iframe></iframe><script>log(\"reopened document: script with src, iframe and script in document.write:\", compare(test(window[0]), reference, true));<\/script>");
// document.close();
});
window.setTimeout(function(){
document.body.innerHTML = "<iframe></iframe>";
console.log("innerHTML after 1000ms:", compare(test(window[0]), reference));
document.body.innerHTML = "<h1>Iframe protection</h1>Open console to see results.";
}, 1000);
</script>
</body>
</html>

101
test/iframeTest.js Normal file
View File

@ -0,0 +1,101 @@
var log = function(){
"use strict";
return function log(...str){
// eslint-disable-next-line no-console
console.log(...str);
};
}();
function draw(canvas){
"use strict";
canvas.setAttribute("width", 220);
canvas.setAttribute("height", 30);
var fp_text = "BrowserLeaks,com <canvas> 10";
var ctx = canvas.getContext("2d");
ctx.textBaseline = "top";
ctx.font = "14px 'Arial'";
ctx.textBaseline = "alphabetic";
ctx.fillStyle = "#f60";
ctx.fillRect(125, 1, 62, 20);
ctx.fillStyle = "#069";
ctx.fillText(fp_text, 2, 15);
ctx.fillStyle = "rgba(102, 204, 0, 07)";
ctx.fillText(fp_text, 4, 17);
return ctx;
}
function test(window){
"use strict";
// create window canvas
var canvas = document.createElement("canvas");
// draw image in window canvas
var ctx = draw(canvas);
return window.HTMLCanvasElement.prototype.toDataURL.call(canvas);
}
function hash(string){
"use strict";
var buffer = new TextEncoder("utf-8").encode(string);
return crypto.subtle.digest("SHA-256", buffer).then(function(hash){
var chunks = [];
(new Uint32Array(hash)).forEach(function(num){
chunks.push(num.toString(16));
});
return chunks.map(function(chunk){
return "0".repeat(8 - chunk.length) + chunk;
}).join("");
});
}
function compare(string1, string2, alwaysOutputHashes){
"use strict";
function outputHashes(message){
Promise.all([
hash(string1),
hash(string2)
]).then(function(hashes){
// eslint-disable-next-line no-console
console.log(message, ...hashes);
});
}
if (string1 === string2){
if (alwaysOutputHashes){
outputHashes("Matching hashes:");
}
return "match";
}
else {
outputHashes("Hashes that differ:");
if (string1.length === string2.length){
let i;
for (i = 0; i < string1.length; i += 1){
if (string1.charAt(i) !== string2.charAt(i)){
break;
}
}
return "missmatch (first at " + i + ")";
}
else {
let i;
for (i = 0; i < Math.min(string1.length, string2.length); i += 1){
if (string1.charAt(i) !== string2.charAt(i)){
break;
}
}
return "missmatch (different lengths, first at " + i + ")";
}
}
}
var reference = test(window);
hash(reference).then(function(hash){
"use strict";
log("reference hash:", hash);
});

View File

@ -19,5 +19,6 @@
<li><a href="webGL-Test.html">Support for webGL</a></li>
<li><a href="navigatorTest.php">Navigator test</a></li>
<li><a href="settingsLoading.php">Settings loading</a></li>
<li><a href="iframeTest.html">Iframe protection</a></li>
</ul>
</body></html>