diff --git a/app/common/lib/suggestion.js b/app/common/lib/suggestion.js index 093273750a..518b6b7348 100644 --- a/app/common/lib/suggestion.js +++ b/app/common/lib/suggestion.js @@ -234,8 +234,8 @@ const getSortByDomain = (userInputLower, userInputHost) => { // any count or frequency calculation. // Note that for parsed URLs that are not complete, the pathname contains // what the user is entering as the host and the host is null. - const host1 = s1.parsedUrl.host || s1.parsedUrl.pathname - const host2 = s2.parsedUrl.host || s2.parsedUrl.pathname + const host1 = s1.parsedUrl.host || s1.parsedUrl.pathname || s1.location || '' + const host2 = s2.parsedUrl.host || s2.parsedUrl.pathname || s2.location || '' let pos1 = host1.indexOf(userInputHost) let pos2 = host2.indexOf(userInputHost) diff --git a/js/state/contentSettings.js b/js/state/contentSettings.js index e89f44fbbf..21c54f3602 100644 --- a/js/state/contentSettings.js +++ b/js/state/contentSettings.js @@ -22,12 +22,18 @@ const net = require('net') // backward compatibility with appState siteSettings const parseSiteSettingsPattern = (pattern) => { + if (pattern === 'file:///') { + return 'file:///*' + } let normalizedPattern = pattern.replace('https?', 'https') let parsed = urlParse(normalizedPattern) if (net.isIP(parsed.hostname)) { return parsed.host - } else { + } else if (parsed.host) { return '[*.]' + parsed.host + } else { + // Probably won't match anything. Fail closed. + return pattern } } @@ -237,7 +243,7 @@ const siteSettingsToContentSettings = (currentSiteSettings, defaultContentSettin noScriptExceptions.forEach((value, origin) => { if (value === false) { contentSettings = addContentSettings(contentSettings, 'javascript', - primaryPattern, origin, 'block') + primaryPattern, origin === 'file:///' ? 'file:///*' : origin, 'block') } }) } else if (noScriptExceptions && noScriptExceptions.size) { diff --git a/test/bravery-components/noScriptTest.js b/test/bravery-components/noScriptTest.js index 01b3aa4cf9..f1aa342ebc 100644 --- a/test/bravery-components/noScriptTest.js +++ b/test/bravery-components/noScriptTest.js @@ -78,6 +78,26 @@ describe('noscript info', function () { .loadUrl(this.url) .waitForTextValue(result, '2') }) + + it('can allow scripts on a file:// URL without allowing all scripts', function * () { + const fileUrl = Brave.fixtureUrl('noscript2.html') + yield this.app.client + .tabByIndex(0) + .loadUrl(fileUrl) + .windowByUrl(Brave.browserWindowUrl) + .waitForVisible(noScriptNavButton) + .click(noScriptNavButton) + .waitForVisible(noScriptAllowTempButton) + .click(noScriptAllowTempButton) + .tabByIndex(0) + .loadUrl(fileUrl) + .waitForTextValue(result, 'scripts running') + .windowByUrl(Brave.browserWindowUrl) + .tabByIndex(0) + .loadUrl(this.url) + .windowByUrl(Brave.browserWindowUrl) + .waitForVisible(noScriptNavButton) + }) }) describe('noscript', function () { diff --git a/test/fixtures/noscript2.html b/test/fixtures/noscript2.html new file mode 100644 index 0000000000..3f53f11b37 --- /dev/null +++ b/test/fixtures/noscript2.html @@ -0,0 +1,8 @@ +
+ + + + diff --git a/test/fixtures/noscript2.js b/test/fixtures/noscript2.js new file mode 100644 index 0000000000..e14c4f2a95 --- /dev/null +++ b/test/fixtures/noscript2.js @@ -0,0 +1 @@ +console.log(1) diff --git a/test/unit/app/common/lib/suggestionTest.js b/test/unit/app/common/lib/suggestionTest.js index 1faf67ad6c..a773cae2a0 100644 --- a/test/unit/app/common/lib/suggestionTest.js +++ b/test/unit/app/common/lib/suggestionTest.js @@ -250,6 +250,9 @@ describe('suggestion unit tests', function () { it('simple domain gets matched higher', function () { assert(this.sort('https://www.google.com', 'https://www.google.com/extra') < 0) }) + it('does not throw error for file:// URL', function () { + assert(this.sort('https://google.com', 'file://') < 0) + }) }) describe('getSortForSuggestions', function () { describe('with url entered as path', function () {