From 449edfbf51f1449850eec4b07ad8b75a1801659a Mon Sep 17 00:00:00 2001 From: Etienne Dusseault Date: Thu, 10 Dec 2020 17:32:29 +0800 Subject: [PATCH 1/6] disable console in contentscript --- app/manifest/_base.json | 1 + app/scripts/disable-console.js | 4 ++++ development/build/static.js | 5 +++++ 3 files changed, 10 insertions(+) create mode 100644 app/scripts/disable-console.js diff --git a/app/manifest/_base.json b/app/manifest/_base.json index 5274261b84d2..416e6918a5dd 100644 --- a/app/manifest/_base.json +++ b/app/manifest/_base.json @@ -38,6 +38,7 @@ { "matches": ["file://*/*", "http://*/*", "https://*/*"], "js": [ + "disable-console.js", "globalthis.js", "lockdown.js", "runLockdown.js", diff --git a/app/scripts/disable-console.js b/app/scripts/disable-console.js new file mode 100644 index 000000000000..d80df0f27eed --- /dev/null +++ b/app/scripts/disable-console.js @@ -0,0 +1,4 @@ +// disable console.log in contentscript to prevent SES/lockdown logging to external page +if (typeof window.console !== undefined) { + console.log = () => {} +} \ No newline at end of file diff --git a/development/build/static.js b/development/build/static.js index ee334164333a..aa4e1f06a1e2 100644 --- a/development/build/static.js +++ b/development/build/static.js @@ -48,6 +48,11 @@ const copyTargets = [ src: `./node_modules/globalthis/dist/browser.js`, dest: `globalthis.js`, }, + { + src: `./app/scripts/`, + pattern: `disable-console.js`, + dest: ``, + }, { src: `./node_modules/ses/dist/lockdown.cjs`, dest: `lockdown.js`, From 47856f8169cf62f0799fa75052d910b4e0b6c906 Mon Sep 17 00:00:00 2001 From: Etienne Dusseault Date: Thu, 10 Dec 2020 18:01:16 +0800 Subject: [PATCH 2/6] lintfix --- app/scripts/disable-console.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/scripts/disable-console.js b/app/scripts/disable-console.js index d80df0f27eed..0b536e37fabf 100644 --- a/app/scripts/disable-console.js +++ b/app/scripts/disable-console.js @@ -1,4 +1,7 @@ // disable console.log in contentscript to prevent SES/lockdown logging to external page +// eslint-disable-next-line import/unambiguous if (typeof window.console !== undefined) { + // eslint-disable-next-line no-empty-function console.log = () => {} -} \ No newline at end of file + +} From c1cb8df6b364a7564775413093ef5d7a27c82c80 Mon Sep 17 00:00:00 2001 From: kumavis Date: Thu, 10 Dec 2020 21:13:18 +0800 Subject: [PATCH 3/6] lint/fix --- app/scripts/disable-console.js | 1 - 1 file changed, 1 deletion(-) diff --git a/app/scripts/disable-console.js b/app/scripts/disable-console.js index 0b536e37fabf..ca21a9294d98 100644 --- a/app/scripts/disable-console.js +++ b/app/scripts/disable-console.js @@ -3,5 +3,4 @@ if (typeof window.console !== undefined) { // eslint-disable-next-line no-empty-function console.log = () => {} - } From 316844eea99240a5ad20da3f6fe5cafceddc1a9c Mon Sep 17 00:00:00 2001 From: Erik Marks Date: Fri, 11 Dec 2020 08:46:08 -0800 Subject: [PATCH 4/6] Maintain console logging in dev mode --- app/scripts/contentscript.js | 1 - app/scripts/disable-console.js | 12 ++++++++---- development/build/scripts.js | 18 ++++++++++++++++++ development/build/static.js | 5 ----- 4 files changed, 26 insertions(+), 10 deletions(-) diff --git a/app/scripts/contentscript.js b/app/scripts/contentscript.js index 5f5f25f9dbe4..619e6aa41417 100644 --- a/app/scripts/contentscript.js +++ b/app/scripts/contentscript.js @@ -223,7 +223,6 @@ function blockedDomainCheck() { * Redirects the current page to a phishing information page */ function redirectToPhishingWarning() { - console.log('MetaMask - routing to Phishing Warning component') const extensionURL = extension.runtime.getURL('phishing.html') window.location.href = `${extensionURL}#${querystring.stringify({ hostname: window.location.hostname, diff --git a/app/scripts/disable-console.js b/app/scripts/disable-console.js index ca21a9294d98..15466f6a7b33 100644 --- a/app/scripts/disable-console.js +++ b/app/scripts/disable-console.js @@ -1,6 +1,10 @@ -// disable console.log in contentscript to prevent SES/lockdown logging to external page +// Disable console.log in contentscript to prevent SES/lockdown logging to external page // eslint-disable-next-line import/unambiguous -if (typeof window.console !== undefined) { - // eslint-disable-next-line no-empty-function - console.log = () => {} +if ( + typeof process !== 'undefined' && + !process.env.METAMASK_DEBUG && + typeof console !== undefined +) { + console.log = () => undefined + console.info = () => undefined } diff --git a/development/build/scripts.js b/development/build/scripts.js index 2f6b577e4358..9236dbb7ea84 100644 --- a/development/build/scripts.js +++ b/development/build/scripts.js @@ -115,6 +115,7 @@ function createScriptTasks({ browserPlatforms, livereload }) { }), ) }) + // inpage must be built before contentscript // because inpage bundle result is included inside contentscript const contentscriptSubtask = createTask( @@ -122,6 +123,12 @@ function createScriptTasks({ browserPlatforms, livereload }) { createTaskForBuildJsExtensionContentscript({ devMode, testing }), ) + // this can run whenever + const disableConsoleSubtask = createTask( + `${taskPrefix}:disable-console`, + createTaskForBuildJsExtensionDisableConsole({ devMode }), + ) + // task for initiating livereload const initiateLiveReload = async () => { if (devMode) { @@ -142,6 +149,7 @@ function createScriptTasks({ browserPlatforms, livereload }) { const allSubtasks = [ ...standardSubtasks, contentscriptSubtask, + disableConsoleSubtask, ].map((subtask) => runInChildProcess(subtask)) // const allSubtasks = [...standardSubtasks, contentscriptSubtask].map(subtask => (subtask)) // make a parent task that runs each task in a child thread @@ -165,6 +173,16 @@ function createScriptTasks({ browserPlatforms, livereload }) { }) } + function createTaskForBuildJsExtensionDisableConsole({ devMode }) { + const filename = 'disable-console' + return bundleTask({ + label: filename, + filename: `${filename}.js`, + filepath: `./app/scripts/${filename}.js`, + devMode, + }) + } + function createTaskForBuildJsExtensionContentscript({ devMode, testing }) { const inpage = 'inpage' const contentscript = 'contentscript' diff --git a/development/build/static.js b/development/build/static.js index aa4e1f06a1e2..ee334164333a 100644 --- a/development/build/static.js +++ b/development/build/static.js @@ -48,11 +48,6 @@ const copyTargets = [ src: `./node_modules/globalthis/dist/browser.js`, dest: `globalthis.js`, }, - { - src: `./app/scripts/`, - pattern: `disable-console.js`, - dest: ``, - }, { src: `./node_modules/ses/dist/lockdown.cjs`, dest: `lockdown.js`, From 31d1dbc2479546207a406c4890e3574a68099e36 Mon Sep 17 00:00:00 2001 From: Erik Marks Date: Mon, 14 Dec 2020 08:17:44 -0800 Subject: [PATCH 5/6] Address review feedback --- app/scripts/contentscript.js | 9 +++++---- app/scripts/disable-console.js | 3 +-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/app/scripts/contentscript.js b/app/scripts/contentscript.js index 619e6aa41417..7e55faec4efd 100644 --- a/app/scripts/contentscript.js +++ b/app/scripts/contentscript.js @@ -38,8 +38,8 @@ function injectScript(content) { scriptTag.textContent = content container.insertBefore(scriptTag, container.children[0]) container.removeChild(scriptTag) - } catch (e) { - console.error('MetaMask provider injection failed.', e) + } catch (error) { + console.error('MetaMask: Provider injection failed.', error) } } @@ -85,7 +85,7 @@ function forwardTrafficBetweenMuxes(channelName, muxA, muxB) { const channelB = muxB.createStream(channelName) pump(channelA, channelB, channelA, (error) => console.debug( - `MetaMask muxed traffic for channel "${channelName}" failed.`, + `MetaMask: Muxed traffic for channel "${channelName}" failed.`, error, ), ) @@ -99,7 +99,7 @@ function forwardTrafficBetweenMuxes(channelName, muxA, muxB) { */ function logStreamDisconnectWarning(remoteLabel, error) { console.debug( - `MetaMask Contentscript: Lost connection to "${remoteLabel}".`, + `MetaMask: Content script lost connection to "${remoteLabel}".`, error, ) } @@ -223,6 +223,7 @@ function blockedDomainCheck() { * Redirects the current page to a phishing information page */ function redirectToPhishingWarning() { + console.debug('MetaMask: Routing to Phishing Warning component.') const extensionURL = extension.runtime.getURL('phishing.html') window.location.href = `${extensionURL}#${querystring.stringify({ hostname: window.location.hostname, diff --git a/app/scripts/disable-console.js b/app/scripts/disable-console.js index 15466f6a7b33..f2666918e364 100644 --- a/app/scripts/disable-console.js +++ b/app/scripts/disable-console.js @@ -1,8 +1,7 @@ // Disable console.log in contentscript to prevent SES/lockdown logging to external page // eslint-disable-next-line import/unambiguous if ( - typeof process !== 'undefined' && - !process.env.METAMASK_DEBUG && + !(typeof process !== 'undefined' && process.env?.METAMASK_DEBUG) && typeof console !== undefined ) { console.log = () => undefined From a73b5b67afa0e648a662e27b03761ed81da88ca2 Mon Sep 17 00:00:00 2001 From: Erik Marks <25517051+rekmarks@users.noreply.github.com> Date: Mon, 14 Dec 2020 10:54:26 -0800 Subject: [PATCH 6/6] Update app/scripts/disable-console.js Co-authored-by: Mark Stacey --- app/scripts/disable-console.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/scripts/disable-console.js b/app/scripts/disable-console.js index f2666918e364..bb7ce8e2460d 100644 --- a/app/scripts/disable-console.js +++ b/app/scripts/disable-console.js @@ -1,7 +1,7 @@ // Disable console.log in contentscript to prevent SES/lockdown logging to external page // eslint-disable-next-line import/unambiguous if ( - !(typeof process !== 'undefined' && process.env?.METAMASK_DEBUG) && + !(typeof process !== 'undefined' && process.env.METAMASK_DEBUG) && typeof console !== undefined ) { console.log = () => undefined