From 77484c1e41c7948cd1fe6008426217229ec0dc86 Mon Sep 17 00:00:00 2001 From: Harry Denley Date: Mon, 17 Jul 2017 19:31:34 +0100 Subject: [PATCH] Added logic to perform the blacklisting on document_start - this makes the redirect faster, and doesn't wait for page load, which caused sometimes the phishing alert to not happen. --- js/DomManipulator.js | 94 +------------------------------------------ js/DomainBlacklist.js | 91 +++++++++++++++++++++++++++++++++++++++++ manifest.json | 6 ++- 3 files changed, 98 insertions(+), 93 deletions(-) create mode 100644 js/DomainBlacklist.js diff --git a/js/DomManipulator.js b/js/DomManipulator.js index c528689a..6aa4d1d3 100644 --- a/js/DomManipulator.js +++ b/js/DomManipulator.js @@ -4,57 +4,18 @@ class EtherAddressLookup { constructor() { - console.log("Init"); + console.log("Init EAL"); this.setDefaultExtensionSettings(); this.init(); } - levenshtein(a, b) { - if(a.length == 0) return b.length; - if(b.length == 0) return a.length; - - // swap to save some memory O(min(a,b)) instead of O(a) - if(a.length > b.length) { - var tmp = a; - a = b; - b = tmp; - } - - var row = []; - // init the row - for(var i = 0; i <= a.length; i++){ - row[i] = i; - } - - // fill in the rest - for(var i = 1; i <= b.length; i++){ - var prev = i; - for(var j = 1; j <= a.length; j++){ - var val; - if(b.charAt(i-1) == a.charAt(j-1)){ - val = row[j-1]; // match - } else { - val = Math.min(row[j-1] + 1, // substitution - prev + 1, // insertion - row[j] + 1); // deletion - } - row[j - 1] = prev; - prev = val; - } - row[a.length] = prev; - } - - return row[a.length]; - } - setDefaultExtensionSettings() { this.blHighlight = false; - this.blBlacklistDomains = true; this.strBlockchainExplorer = "https://etherscan.io/address"; this.intSettingsCount = 0; - this.intSettingsTotalCount = 3; + this.intSettingsTotalCount = 2; } //Gets extension settings and then converts addresses to links @@ -75,14 +36,6 @@ class EtherAddressLookup { ++this.intSettingsCount; }.bind(this)); - //Get the blacklist domains option for the user - objBrowser.runtime.sendMessage({func: "blacklist_domains"}, function(objResponse) { - if(objResponse && objResponse.hasOwnProperty("resp")) { - this.blBlacklistDomains = (objResponse.resp == 1 ? true : false); - } - ++this.intSettingsCount; - }.bind(this)); - //Update the DOM once all settings have been received... setTimeout(function() { if(true || this.intSettingsCount === this.intSettingsTotalCount) { @@ -141,49 +94,6 @@ class EtherAddressLookup { } return false; } - - //Detects if the current tab is in the blacklisted domains file - blacklistedDomainCheck() - { - let objBrowser = chrome ? chrome : browser; - var self = this; - var arrBlacklistedDomains = []; - var arrWhitelistedDomains = ["www.myetherwallet.com", "myetherwallet.com"]; - objBrowser.runtime.sendMessage({func: "blacklist_domain_list"}, function(objResponse) { - if(objResponse && objResponse.hasOwnProperty("resp")) { - arrBlacklistedDomains = objResponse.resp; - } - }.bind(arrBlacklistedDomains)); - - objBrowser.runtime.sendMessage({func: "whitelist_domain_list"}, function(objResponse) { - if(objResponse && objResponse.hasOwnProperty("resp")) { - arrWhitelistedDomains = objResponse.resp; - } - }.bind(arrWhitelistedDomains)); - - setTimeout(function() { - if(arrBlacklistedDomains.length > 0) { - var strCurrentTab = window.location.hostname; - - //Domain is whitelisted, don't check the blacklist. - if(arrWhitelistedDomains.includes(strCurrentTab)) { - console.log("Domain "+ strCurrentTab +" is whitelisted on EAL!"); - return; - } - - //Levenshtien - @sogoiii - var isBlacklisted = arrBlacklistedDomains.includes(strCurrentTab); - var source = strCurrentTab.replace(/\./g,''); - var intHolisticMetric = self.levenshtein(source, 'myetherwallet'); - var intHolisticLimit = 7 // How different can the word be? - var blHolisticStatus = (intHolisticMetric > 0 && intHolisticMetric < intHolisticLimit) ? true : false; - - if (isBlacklisted || blHolisticStatus ) { - window.location.href = "https://harrydenley.com/EtherAddressLookup/phishing.html"; - } - } - }.bind(arrBlacklistedDomains), 500) - } } window.addEventListener("load", function() { diff --git a/js/DomainBlacklist.js b/js/DomainBlacklist.js new file mode 100644 index 00000000..d7294226 --- /dev/null +++ b/js/DomainBlacklist.js @@ -0,0 +1,91 @@ +(function() { + let objBrowser = chrome ? chrome : browser; + //Get the blacklist domains option for the user + objBrowser.runtime.sendMessage({func: "blacklist_domains"}, function(objResponse) { + if(objResponse && objResponse.hasOwnProperty("resp")) { + if(objResponse.resp == 1) { + blacklistedDomainCheck(); + } + } + }); + + //Detects if the current tab is in the blacklisted domains file + function blacklistedDomainCheck() + { + let objBrowser = chrome ? chrome : browser; + var arrBlacklistedDomains = []; + var arrWhitelistedDomains = ["www.myetherwallet.com", "myetherwallet.com"]; + objBrowser.runtime.sendMessage({func: "blacklist_domain_list"}, function(objResponse) { + if(objResponse && objResponse.hasOwnProperty("resp")) { + arrBlacklistedDomains = objResponse.resp; + objBrowser.runtime.sendMessage({func: "whitelist_domain_list"}, function(objResponse) { + if(objResponse && objResponse.hasOwnProperty("resp")) { + arrWhitelistedDomains = objResponse.resp; + return doBlacklistCheck(); + } + }.bind(arrWhitelistedDomains)); + } + }.bind(arrBlacklistedDomains)); + + function doBlacklistCheck() { + if(arrBlacklistedDomains.length > 0) { + var strCurrentTab = window.location.hostname; + + //Domain is whitelisted, don't check the blacklist. + if(arrWhitelistedDomains.includes(strCurrentTab)) { + console.log("Domain "+ strCurrentTab +" is whitelisted on EAL!"); + return; + } + + //Levenshtien - @sogoiii + var isBlacklisted = arrBlacklistedDomains.includes(strCurrentTab); + var source = strCurrentTab.replace(/\./g,''); + var intHolisticMetric = levenshtein(source, 'myetherwallet'); + var intHolisticLimit = 7 // How different can the word be? + var blHolisticStatus = (intHolisticMetric > 0 && intHolisticMetric < intHolisticLimit) ? true : false; + + if (isBlacklisted || blHolisticStatus ) { + window.location.href = "https://harrydenley.com/EtherAddressLookup/phishing.html"; + } + } + } + } + + function levenshtein(a, b) { + if(a.length == 0) return b.length; + if(b.length == 0) return a.length; + + // swap to save some memory O(min(a,b)) instead of O(a) + if(a.length > b.length) { + var tmp = a; + a = b; + b = tmp; + } + + var row = []; + // init the row + for(var i = 0; i <= a.length; i++){ + row[i] = i; + } + + // fill in the rest + for(var i = 1; i <= b.length; i++){ + var prev = i; + for(var j = 1; j <= a.length; j++){ + var val; + if(b.charAt(i-1) == a.charAt(j-1)){ + val = row[j-1]; // match + } else { + val = Math.min(row[j-1] + 1, // substitution + prev + 1, // insertion + row[j] + 1); // deletion + } + row[j - 1] = prev; + prev = val; + } + row[a.length] = prev; + } + + return row[a.length]; + } +})(); diff --git a/manifest.json b/manifest.json index 1a25c2ba..26e0bdd3 100644 --- a/manifest.json +++ b/manifest.json @@ -4,7 +4,7 @@ "name": "EtherAddressLookup", "short_name": "EtherAddressLookup", "description": "Adds links to strings that look like Ethereum addresses to your favorite blockchain explorer.", - "version": "1.4.0", + "version": "1.4.1", "browser_action": { "default_icon": "images/icon.png", @@ -17,6 +17,10 @@ ], "content_scripts":[{ + "run_at": "document_start", + "matches": ["http://*/*", "https://*/*"], + "js": ["js/DomainBlacklist.js"] + },{ "run_at": "document_end", "matches": ["http://*/*", "https://*/*"], "js": ["js/DomManipulator.js"],