Skip to content

Commit 68c1d23

Browse files
committed
Merge remote-tracking branch 'origin/develop' into weizman/lavadome-fix-0.0.11
2 parents 0c4ecb0 + ba3a20f commit 68c1d23

File tree

13 files changed

+480
-617
lines changed

13 files changed

+480
-617
lines changed

app/manifest/v2/_base.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
"lockdown-install.js",
3636
"lockdown-run.js",
3737
"lockdown-more.js",
38-
"contentscript.js"
38+
"contentscript.js",
39+
"inpage.js"
3940
],
4041
"run_at": "document_start",
4142
"all_frames": true

app/scripts/contentscript.js

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,6 @@ import { checkForLastError } from '../../shared/modules/browser-runtime.utils';
99
import { isManifestV3 } from '../../shared/modules/mv3.utils';
1010
import shouldInjectProvider from '../../shared/modules/provider-injection';
1111

12-
// These require calls need to use require to be statically recognized by browserify
13-
const fs = require('fs');
14-
const path = require('path');
15-
16-
const inpageContent = fs.readFileSync(
17-
path.join(__dirname, '..', '..', 'dist', 'chrome', 'inpage.js'),
18-
'utf8',
19-
);
20-
const inpageBundle = inpageContent;
21-
2212
// contexts
2313
const CONTENT_SCRIPT = 'metamask-contentscript';
2414
const INPAGE = 'metamask-inpage';
@@ -61,24 +51,6 @@ let extensionMux,
6151
pageMux,
6252
pageChannel;
6353

64-
/**
65-
* Injects a script tag into the current document
66-
*
67-
* @param {string} content - Code to be executed in the current document
68-
*/
69-
function injectScript(content) {
70-
try {
71-
const container = document.head || document.documentElement;
72-
const scriptTag = document.createElement('script');
73-
scriptTag.setAttribute('async', 'false');
74-
scriptTag.textContent = content;
75-
container.insertBefore(scriptTag, container.children[0]);
76-
container.removeChild(scriptTag);
77-
} catch (error) {
78-
console.error('MetaMask: Provider injection failed.', error);
79-
}
80-
}
81-
8254
/**
8355
* PHISHING STREAM LOGIC
8456
*/
@@ -545,9 +517,6 @@ const start = () => {
545517
}
546518

547519
if (shouldInjectProvider()) {
548-
if (!isManifestV3) {
549-
injectScript(inpageBundle);
550-
}
551520
initStreams();
552521

553522
// https://bugs.chromium.org/p/chromium/issues/detail?id=1457040

development/build/scripts.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const { callbackify } = require('util');
44
const path = require('path');
5-
const { writeFileSync, readFileSync } = require('fs');
5+
const { writeFileSync, readFileSync, unlinkSync } = require('fs');
66
const EventEmitter = require('events');
77
const assert = require('assert');
88
const gulp = require('gulp');
@@ -43,6 +43,7 @@ const {
4343
getBuildName,
4444
getBuildAppId,
4545
getBuildIcon,
46+
makeSelfInjecting,
4647
} = require('./utils');
4748

4849
const {
@@ -471,6 +472,26 @@ function createScriptTasks({
471472
version,
472473
applyLavaMoat,
473474
}),
475+
() => {
476+
// MV3 injects inpage into the tab's main world, but in MV2 we need
477+
// to do it manually:
478+
if (process.env.ENABLE_MV3) {
479+
return;
480+
}
481+
// stringify inpage.js into itself, and then make it inject itself into the page
482+
browserPlatforms.forEach((browser) => {
483+
makeSelfInjecting(
484+
path.join(__dirname, `../../dist/${browser}/${inpage}.js`),
485+
);
486+
});
487+
// delete the inpage.js source map, as it no longer represents inpage.js
488+
// and so `yarn source-map-explorer` can't handle it. It's also not
489+
// useful anyway, as inpage.js is injected as a `script.textContent`,
490+
// and not tracked in Sentry or browsers devtools anyway.
491+
unlinkSync(
492+
path.join(__dirname, `../../dist/sourcemaps/${inpage}.js.map`),
493+
);
494+
},
474495
createNormalBundle({
475496
buildTarget,
476497
buildType,

development/build/utils.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const path = require('path');
2-
const { readFileSync } = require('fs');
2+
const { readFileSync, writeFileSync } = require('fs');
33
const semver = require('semver');
44
const { capitalize } = require('lodash');
55
const { loadBuildTypesConfig } = require('../lib/build-type');
@@ -287,6 +287,21 @@ function getBuildIcon({ buildType }) {
287287
const svg = readFileSync(svgLogoPath, 'utf8');
288288
return `data:image/svg+xml,${encodeURIComponent(svg)}`;
289289
}
290+
/**
291+
* Takes the given JavaScript file at `filePath` and replaces its contents with
292+
* a script that injects the original file contents into the document in which
293+
* the file is loaded. Useful for MV2 extensions to run scripts synchronously in the
294+
* "MAIN" world.
295+
*
296+
* @param {string} filePath - The path to the file to convert to a self-injecting
297+
* script.
298+
*/
299+
function makeSelfInjecting(filePath) {
300+
const fileContents = readFileSync(filePath, 'utf8');
301+
const textContent = JSON.stringify(fileContents);
302+
const js = `{let d=document,s=d.createElement('script');s.textContent=${textContent};d.documentElement.appendChild(s).remove();}`;
303+
writeFileSync(filePath, js, 'utf8');
304+
}
290305

291306
module.exports = {
292307
getBrowserVersionMap,
@@ -299,4 +314,5 @@ module.exports = {
299314
logError,
300315
getPathInsideNodeModules,
301316
wrapAgainstScuttling,
317+
makeSelfInjecting,
302318
};

development/sourcemap-validator.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ async function start() {
2525
`common-0.js`,
2626
`background-0.js`,
2727
`ui-0.js`,
28-
// `contentscript.js`, skipped because the validator is erroneously sampling the inlined `inpage.js` script
29-
`inpage.js`,
28+
`contentscript.js`,
29+
// `inpage.js`, skipped because the validator can't sample the inlined `inpage.js` script
3030
];
3131
let valid = true;
3232

lavamoat/browserify/beta/policy.json

Lines changed: 33 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -736,8 +736,8 @@
736736
},
737737
"packages": {
738738
"@metamask/address-book-controller>@metamask/controller-utils>@metamask/utils": true,
739+
"@metamask/address-book-controller>@metamask/controller-utils>ethjs-unit": true,
739740
"@metamask/controller-utils>@spruceid/siwe-parser": true,
740-
"@metamask/logging-controller>@metamask/controller-utils>ethjs-unit": true,
741741
"browserify>buffer": true,
742742
"eslint>fast-deep-equal": true,
743743
"eth-ens-namehash": true,
@@ -757,6 +757,12 @@
757757
"superstruct": true
758758
}
759759
},
760+
"@metamask/address-book-controller>@metamask/controller-utils>ethjs-unit": {
761+
"packages": {
762+
"@metamask/ethjs>number-to-bn": true,
763+
"bn.js": true
764+
}
765+
},
760766
"@metamask/announcement-controller": {
761767
"packages": {
762768
"@metamask/base-controller": true
@@ -1047,9 +1053,10 @@
10471053
},
10481054
"@metamask/eth-token-tracker>deep-equal": {
10491055
"packages": {
1056+
"@lavamoat/lavapack>json-stable-stringify>isarray": true,
1057+
"@lavamoat/lavapack>json-stable-stringify>object-keys": true,
10501058
"@metamask/eth-token-tracker>deep-equal>es-get-iterator": true,
10511059
"@metamask/eth-token-tracker>deep-equal>is-date-object": true,
1052-
"@metamask/eth-token-tracker>deep-equal>isarray": true,
10531060
"@metamask/eth-token-tracker>deep-equal>which-boxed-primitive": true,
10541061
"@metamask/eth-token-tracker>deep-equal>which-collection": true,
10551062
"@ngraveio/bc-ur>assert>object-is": true,
@@ -1061,17 +1068,16 @@
10611068
"string.prototype.matchall>es-abstract>is-array-buffer": true,
10621069
"string.prototype.matchall>es-abstract>is-regex": true,
10631070
"string.prototype.matchall>es-abstract>is-shared-array-buffer": true,
1064-
"string.prototype.matchall>es-abstract>object-keys": true,
10651071
"string.prototype.matchall>get-intrinsic": true,
10661072
"string.prototype.matchall>regexp.prototype.flags": true,
10671073
"string.prototype.matchall>side-channel": true
10681074
}
10691075
},
10701076
"@metamask/eth-token-tracker>deep-equal>es-get-iterator": {
10711077
"packages": {
1078+
"@lavamoat/lavapack>json-stable-stringify>isarray": true,
10721079
"@metamask/eth-token-tracker>deep-equal>es-get-iterator>is-map": true,
10731080
"@metamask/eth-token-tracker>deep-equal>es-get-iterator>is-set": true,
1074-
"@metamask/eth-token-tracker>deep-equal>es-get-iterator>isarray": true,
10751081
"@metamask/eth-token-tracker>deep-equal>es-get-iterator>stop-iteration-iterator": true,
10761082
"browserify>process": true,
10771083
"browserify>util>is-arguments": true,
@@ -1465,24 +1471,10 @@
14651471
},
14661472
"@metamask/logging-controller": {
14671473
"packages": {
1468-
"@metamask/logging-controller>@metamask/base-controller": true,
1474+
"@metamask/base-controller": true,
14691475
"uuid": true
14701476
}
14711477
},
1472-
"@metamask/logging-controller>@metamask/base-controller": {
1473-
"globals": {
1474-
"setTimeout": true
1475-
},
1476-
"packages": {
1477-
"immer": true
1478-
}
1479-
},
1480-
"@metamask/logging-controller>@metamask/controller-utils>ethjs-unit": {
1481-
"packages": {
1482-
"@metamask/ethjs>number-to-bn": true,
1483-
"bn.js": true
1484-
}
1485-
},
14861478
"@metamask/logo": {
14871479
"globals": {
14881480
"addEventListener": true,
@@ -1792,42 +1784,17 @@
17921784
"console.info": true
17931785
},
17941786
"packages": {
1787+
"@metamask/base-controller": true,
1788+
"@metamask/controller-utils": true,
17951789
"@metamask/logging-controller": true,
17961790
"@metamask/message-manager": true,
1797-
"@metamask/signature-controller>@metamask/base-controller": true,
1798-
"@metamask/signature-controller>@metamask/controller-utils": true,
1791+
"@metamask/providers>@metamask/rpc-errors": true,
17991792
"browserify>buffer": true,
1800-
"eth-rpc-errors": true,
18011793
"ethereumjs-util": true,
18021794
"lodash": true,
18031795
"webpack>events": true
18041796
}
18051797
},
1806-
"@metamask/signature-controller>@metamask/base-controller": {
1807-
"globals": {
1808-
"setTimeout": true
1809-
},
1810-
"packages": {
1811-
"immer": true
1812-
}
1813-
},
1814-
"@metamask/signature-controller>@metamask/controller-utils": {
1815-
"globals": {
1816-
"URL": true,
1817-
"console.error": true,
1818-
"fetch": true,
1819-
"setTimeout": true
1820-
},
1821-
"packages": {
1822-
"@metamask/controller-utils>@spruceid/siwe-parser": true,
1823-
"@metamask/logging-controller>@metamask/controller-utils>ethjs-unit": true,
1824-
"@metamask/utils": true,
1825-
"browserify>buffer": true,
1826-
"eslint>fast-deep-equal": true,
1827-
"eth-ens-namehash": true,
1828-
"ethereumjs-util": true
1829-
}
1830-
},
18311798
"@metamask/smart-transactions-controller": {
18321799
"globals": {
18331800
"URLSearchParams": true,
@@ -1864,8 +1831,8 @@
18641831
"setTimeout": true
18651832
},
18661833
"packages": {
1834+
"@metamask/address-book-controller>@metamask/controller-utils>ethjs-unit": true,
18671835
"@metamask/controller-utils>@spruceid/siwe-parser": true,
1868-
"@metamask/logging-controller>@metamask/controller-utils>ethjs-unit": true,
18691836
"@metamask/utils": true,
18701837
"browserify>buffer": true,
18711838
"eslint>fast-deep-equal": true,
@@ -2312,9 +2279,7 @@
23122279
"clearTimeout": true,
23132280
"console.error": true,
23142281
"document": true,
2315-
"new": true,
2316-
"setTimeout": true,
2317-
"target": true
2282+
"setTimeout": true
23182283
},
23192284
"packages": {
23202285
"browserify>process": true
@@ -3419,9 +3384,7 @@
34193384
"globals": {
34203385
"AbortController": true,
34213386
"AggregateError": true,
3422-
"Blob": true,
3423-
"new": true,
3424-
"target": true
3387+
"Blob": true
34253388
},
34263389
"packages": {
34273390
"browserify>buffer": true,
@@ -3474,9 +3437,9 @@
34743437
},
34753438
"gulp>vinyl-fs>object.assign": {
34763439
"packages": {
3440+
"@lavamoat/lavapack>json-stable-stringify>object-keys": true,
34773441
"string.prototype.matchall>call-bind": true,
34783442
"string.prototype.matchall>define-properties": true,
3479-
"string.prototype.matchall>es-abstract>object-keys": true,
34803443
"string.prototype.matchall>has-symbols": true
34813444
}
34823445
},
@@ -4188,18 +4151,30 @@
41884151
"string.prototype.matchall>call-bind": {
41894152
"packages": {
41904153
"browserify>has>function-bind": true,
4154+
"string.prototype.matchall>call-bind>es-errors": true,
4155+
"string.prototype.matchall>call-bind>set-function-length": true,
41914156
"string.prototype.matchall>get-intrinsic": true
41924157
}
41934158
},
4194-
"string.prototype.matchall>define-properties": {
4159+
"string.prototype.matchall>call-bind>set-function-length": {
41954160
"packages": {
4161+
"string.prototype.matchall>call-bind>es-errors": true,
41964162
"string.prototype.matchall>define-properties>define-data-property": true,
4163+
"string.prototype.matchall>es-abstract>gopd": true,
41974164
"string.prototype.matchall>es-abstract>has-property-descriptors": true,
4198-
"string.prototype.matchall>es-abstract>object-keys": true
4165+
"string.prototype.matchall>get-intrinsic": true
4166+
}
4167+
},
4168+
"string.prototype.matchall>define-properties": {
4169+
"packages": {
4170+
"@lavamoat/lavapack>json-stable-stringify>object-keys": true,
4171+
"string.prototype.matchall>define-properties>define-data-property": true,
4172+
"string.prototype.matchall>es-abstract>has-property-descriptors": true
41994173
}
42004174
},
42014175
"string.prototype.matchall>define-properties>define-data-property": {
42024176
"packages": {
4177+
"string.prototype.matchall>call-bind>es-errors": true,
42034178
"string.prototype.matchall>es-abstract>gopd": true,
42044179
"string.prototype.matchall>es-abstract>has-property-descriptors": true,
42054180
"string.prototype.matchall>get-intrinsic": true
@@ -4258,6 +4233,7 @@
42584233
"packages": {
42594234
"browserify>has>function-bind": true,
42604235
"depcheck>is-core-module>hasown": true,
4236+
"string.prototype.matchall>call-bind>es-errors": true,
42614237
"string.prototype.matchall>es-abstract>has-proto": true,
42624238
"string.prototype.matchall>has-symbols": true
42634239
}

0 commit comments

Comments
 (0)