diff --git a/app/manifest/_base.json b/app/manifest/_base.json index 5274261b8..416e6918a 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/contentscript.js b/app/scripts/contentscript.js index 05d82c538..a0fbc5500 100644 --- a/app/scripts/contentscript.js +++ b/app/scripts/contentscript.js @@ -41,8 +41,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) } } @@ -95,10 +95,10 @@ async function setupStreams() { function forwardTrafficBetweenMuxers(channelName, muxA, muxB) { const channelA = muxA.createStream(channelName) const channelB = muxB.createStream(channelName) - pump(channelA, channelB, channelA, (err) => - logStreamDisconnectWarning( - `MetaMask muxed traffic for channel "${channelName}" failed.`, - err, + pump(channelA, channelB, channelA, (error) => + console.debug( + `MetaMask: Muxed traffic for channel "${channelName}" failed.`, + error, ), ) } @@ -107,14 +107,13 @@ function forwardTrafficBetweenMuxers(channelName, muxA, muxB) { * Error handler for page to extension stream disconnections * * @param {string} remoteLabel - Remote stream name - * @param {Error} err - Stream connection error + * @param {Error} error - Stream connection error */ -function logStreamDisconnectWarning(remoteLabel, err) { - let warningMsg = `MetamaskContentscript - lost connection to ${remoteLabel}` - if (err) { - warningMsg += `\n${err.stack}` - } - console.warn(warningMsg) +function logStreamDisconnectWarning(remoteLabel, error) { + console.debug( + `MetaMask: Content script lost connection to "${remoteLabel}".`, + error, + ) } /** @@ -214,7 +213,7 @@ function blockedDomainCheck() { * Redirects the current page to a phishing information page */ function redirectToPhishingWarning() { - console.log('MetaMask - routing to Phishing Warning component') + 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 new file mode 100644 index 000000000..bb7ce8e24 --- /dev/null +++ b/app/scripts/disable-console.js @@ -0,0 +1,9 @@ +// 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 console !== undefined +) { + console.log = () => undefined + console.info = () => undefined +} diff --git a/development/build/scripts.js b/development/build/scripts.js index 2f6b577e4..9236dbb7e 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'