Repository: brave/browser-laptop (JavaScript)

Issue:
'www.' URL normalization code is not correct
there are a bunch of places in the code that call something like `host = host.replace('www.', '')`, presumably for the purpose of considering 'www.example.com' and 'example.com' to be equal. 

this doesn't work on URLs like `wowww.com` (which turns into `wocom`).


Discussion:
this is done in app/common/lib/suggestion.js and app/extensions/brave/content/scripts/adInsertion.js; the latter could be a potential security issue
Can this be a first bug? I can take care of it right now
@catilac definitely!
yesss on it! <ANIMATED_GIF>
@catilac @diracdeltas any manual testing required? It seems everything is covered by automated tests 😄 
@bsclifton :+1: 



Patch:
diff --git a/app/common/lib/suggestion.js b/app/common/lib/suggestion.js
index e89a0e6cc9c..cd4c616e93b 100644
--- a/app/common/lib/suggestion.js
+++ b/app/common/lib/suggestion.js
@@ -124,9 +124,8 @@ const isParsedUrlSimpleDomainNameValue = (parsed) => {
  */
 const normalizeLocation = (location) => {
   if (typeof location === 'string') {
-    location = location.replace(/www\./, '')
-    location = location.replace(/^http:\/\//, '')
-    location = location.replace(/^https:\/\//, '')
+    // remove http://, https:// and www. from beginning of location string
+    location = location.replace(/^(https?:\/\/)?(www\.)?/, '')
   }
   return location
 }
@@ -250,8 +249,8 @@ const getSortByDomainForSites = (userInputLower, userInputHost) => {
     // what the user is entering as the host and the host is null.
     let host1 = s1.get('parsedUrl').host || s1.get('parsedUrl').pathname || s1.get('location') || ''
     let host2 = s2.get('parsedUrl').host || s2.get('parsedUrl').pathname || s2.get('location') || ''
-    host1 = host1.replace('www.', '')
-    host2 = host2.replace('www.', '')
+    host1 = normalizeLocation(host1)
+    host2 = normalizeLocation(host2)
 
     let pos1 = host1.indexOf(userInputHost)
     let pos2 = host2.indexOf(userInputHost)
@@ -287,8 +286,8 @@ const getSortByDomainForSites = (userInputLower, userInputHost) => {
  */
 const getSortByDomainForHosts = (userInputHost) => {
   return (host1, host2) => {
-    host1 = host1.replace('www.', '')
-    host2 = host2.replace('www.', '')
+    host1 = normalizeLocation(host1)
+    host2 = normalizeLocation(host2)
     let pos1 = host1.indexOf(userInputHost)
     let pos2 = host2.indexOf(userInputHost)
     if (pos1 !== -1 && pos2 === -1) {
diff --git a/app/extensions/brave/content/scripts/adInsertion.js b/app/extensions/brave/content/scripts/adInsertion.js
index 5430444eed8..0fc314f2eea 100644
--- a/app/extensions/brave/content/scripts/adInsertion.js
+++ b/app/extensions/brave/content/scripts/adInsertion.js
@@ -157,7 +157,7 @@ if (chrome.contentSettings.adInsertion == 'allow') {
 
   var host = document.location.hostname
   if (host) {
-    host = host.replace('www.', '')
+    host = host.replace(/^www\./, '')
     chrome.ipcRenderer.on('set-ad-div-candidates', (e, divHost, adDivCandidates, placeholderUrl) => {
       // don't accidentally intercept messages not intended for this host
       if (host === divHost) {


Test patch:
diff --git a/test/unit/app/common/lib/suggestionTest.js b/test/unit/app/common/lib/suggestionTest.js
index 5318d5af531..de3f811bbba 100644
--- a/test/unit/app/common/lib/suggestionTest.js
+++ b/test/unit/app/common/lib/suggestionTest.js
@@ -58,6 +58,12 @@ describe('suggestion unit tests', function () {
     it('normalizes location', function () {
       assert.ok(suggestion.normalizeLocation('https://www.site.com') === 'site.com', 'www. prefix removed')
       assert.ok(suggestion.normalizeLocation('http://site.com') === 'site.com', 'location not modified')
+      assert.ok(suggestion.normalizeLocation('http://wowww.com') === 'wowww.com', 'location not modified')
+      assert.ok(suggestion.normalizeLocation('https://wowww.com') === 'wowww.com', 'location not modified')
+      assert.ok(suggestion.normalizeLocation('wowww.com') === 'wowww.com', 'location not modified')
+      assert.ok(suggestion.normalizeLocation('http://www.wowww.com') === 'wowww.com', 'www. prefix removed')
+      assert.ok(suggestion.normalizeLocation('https://www.wowww.com') === 'wowww.com', 'www. prefix removed')
+      assert.ok(suggestion.normalizeLocation('www.wowww.com') === 'wowww.com', 'www. prefix removed')
     })
   })