diff --git a/app/extensions.js b/app/extensions.js index 9ba9137db7e..bf97144ac08 100644 --- a/app/extensions.js +++ b/app/extensions.js @@ -75,7 +75,8 @@ let generateBraveManifest = () => { js: [ 'content/scripts/adInsertion.js', 'content/scripts/passwordManager.js', - 'content/scripts/pageInformation.js' + 'content/scripts/pageInformation.js', + 'content/scripts/flashListener.js' ] }, { diff --git a/app/extensions/brave/content/scripts/flashListener.js b/app/extensions/brave/content/scripts/flashListener.js new file mode 100644 index 00000000000..f0a04100851 --- /dev/null +++ b/app/extensions/brave/content/scripts/flashListener.js @@ -0,0 +1,82 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +/** + * Whether a src is a .swf file. + * If so, returns the origin of the file. Otherwise returns false. + * @param {string} src + * @return {boolean|string} + */ +function isSWF (src) { + if (!src) { + return false + } + let a = document.createElement('a') + a.href = src + if (a.pathname && a.pathname.toLowerCase().endsWith('.swf')) { + return a.origin + } else { + return false + } +} + +/** + * Whether an element is invisible or very small. + * @param {Element} elem + * @return {boolean} + */ +function isHiddenElement (el) { + if (!el) { + return false + } + const minSize = 20 + if (el.offsetWidth < minSize || el.offsetHeight < minSize) { + return true + } + if (!el.getClientRects().length) { + return true + } + return false +} + +/** + * Whether there is a small/hidden flash object on the page + * Reference: + * https://helpx.adobe.com/flash/kb/flash-object-embed-tag-attributes.html + * @param {Element} elem - HTML element to search + * @return {Array.} + */ +function hasHiddenFlashElement (elem) { + let foundElement = Array.from(elem.getElementsByTagName('embed')).some((el) => { + return isSWF(el.getAttribute('src')) && isHiddenElement(el) + }) + + if (foundElement) { + return true + } + + return Array.from(elem.getElementsByTagName('object')).some((el) => { + if (!isHiddenElement(el)) { + return false + } + let origin = isSWF(el.getAttribute('data')) + return origin + ? true + : Array.from(el.getElementsByTagName('param')).some((param) => { + let name = param.getAttribute('name') + let origin = isSWF(param.getAttribute('value')) + return name && ['movie', 'src'].includes(name.toLowerCase()) && origin + }) + }) +} + + +// If Flash is enabled but not runnable, show a permission notification for small +// Flash elements +if (chrome.contentSettings.flashEnabled == 'allow' && chrome.contentSettings.plugins != 'allow' && hasHiddenFlashElement(document.documentElement)) { + chrome.ipcRenderer.send('dispatch-action', JSON.stringify([{ + actionType: 'app-flash-permission-requested', + location: window.location.href + }])) +} diff --git a/test/components/flashTest.js b/test/components/flashTest.js index 2630a4a7190..03be763fb50 100644 --- a/test/components/flashTest.js +++ b/test/components/flashTest.js @@ -47,4 +47,26 @@ describe('flash install interception', function () { return this.getText(notificationBar).then((val) => val.includes('run Flash Player')) }) }) + it('shows flash notification bar when small element is loaded', function * () { + const flashUrl = Brave.server.url('flash_small.html') + yield this.app.client + .tabByIndex(0) + .loadUrl(flashUrl) + .windowByUrl(Brave.browserWindowUrl) + .waitForExist(notificationBar) + .waitUntil(function () { + return this.getText(notificationBar).then((val) => val.includes('run Flash Player')) + }) + }) + it('shows flash notification bar when invisible element is loaded', function * () { + const flashUrl = Brave.server.url('flash_invisible.html') + yield this.app.client + .tabByIndex(0) + .loadUrl(flashUrl) + .windowByUrl(Brave.browserWindowUrl) + .waitForExist(notificationBar) + .waitUntil(function () { + return this.getText(notificationBar).then((val) => val.includes('run Flash Player')) + }) + }) }) diff --git a/test/fixtures/flash_invisible.html b/test/fixtures/flash_invisible.html new file mode 100644 index 00000000000..8c247d99dbb --- /dev/null +++ b/test/fixtures/flash_invisible.html @@ -0,0 +1,5 @@ + +
+ +
+ diff --git a/test/fixtures/flash_small.html b/test/fixtures/flash_small.html new file mode 100644 index 00000000000..c90276a0749 --- /dev/null +++ b/test/fixtures/flash_small.html @@ -0,0 +1,3 @@ + + +