Skip to content

Commit 621321c

Browse files
committed
chore: lavamoat policy overrides working
1 parent 12c29b0 commit 621321c

File tree

3 files changed

+2298
-21
lines changed

3 files changed

+2298
-21
lines changed

config-overrides.js

+91-21
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
const webpack = require('webpack');
22
const LavaMoatPlugin = require('@lavamoat/webpack')
3+
const fs = require('fs');
4+
const path = require('path');
35

46
module.exports = function override(config, env) {
57
config.optimization = {
68
...config.optimization,
7-
minimize: false
9+
minimize: false,
10+
concatenateModules: false
811
};
912

1013
config.resolve.fallback = {
@@ -21,26 +24,93 @@ module.exports = function override(config, env) {
2124
vm: require.resolve('vm-browserify'),
2225
};
2326

24-
config.plugins = [
25-
new LavaMoatPlugin({
26-
writeAutoConfig: true,
27-
inlineLockdown: /index\.js/,
28-
lockdown: {
29-
consoleTaming: 'unsafe',
30-
errorTrapping: 'none',
31-
unhandledRejectionTrapping: 'none',
32-
overrideTaming: 'moderate',
33-
}
34-
}),
35-
...config.plugins,
36-
new webpack.ProvidePlugin({
37-
process: 'process/browser',
38-
Buffer: ['buffer', 'Buffer'],
39-
}),
40-
new webpack.DefinePlugin({
41-
'process.env': JSON.stringify(process.env),
42-
}),
43-
];
27+
// Read the generated policy file
28+
let policy = {};
29+
const policyPath = path.join(__dirname, 'lavamoat', 'webpack');
30+
const policyFile = path.join(policyPath, 'policy.json');
31+
32+
try {
33+
policy = JSON.parse(fs.readFileSync(policyFile, 'utf8'));
34+
} catch (err) {
35+
console.warn('No existing LavaMoat policy found, will generate one');
36+
}
37+
38+
// Add custom policies
39+
/* if (policy.resources) {
40+
// WalletConnect policy
41+
if (!policy.resources['@walletconnect/core']) {
42+
policy.resources['@walletconnect/core'] = {
43+
globals: {
44+
ArrayBuffer: true,
45+
Uint8Array: true,
46+
Object: true,
47+
'Object.prototype': true,
48+
'Object.getPrototypeOf': true,
49+
Symbol: true,
50+
'Function.prototype': true
51+
}
52+
};
53+
}
54+
55+
// React policy
56+
if (!policy.resources['react']) {
57+
policy.resources['react'] = {
58+
globals: {
59+
Object: true,
60+
'Object.prototype': true,
61+
'Function.prototype': true
62+
}
63+
};
64+
}
65+
66+
// Lodash policy
67+
if (!policy.resources['lodash']) {
68+
policy.resources['lodash'] = {
69+
globals: {
70+
Object: true,
71+
'Object.prototype': true,
72+
'Function.prototype': true
73+
}
74+
};
75+
}
76+
}*/
77+
78+
// Don't use LavaMoat when running in electron
79+
if (process.env.ELECTRON_RUN === 'true') {
80+
config.plugins = [
81+
...config.plugins,
82+
new webpack.ProvidePlugin({
83+
process: 'process/browser',
84+
Buffer: ['buffer', 'Buffer'],
85+
}),
86+
new webpack.DefinePlugin({
87+
'process.env': JSON.stringify(process.env),
88+
}),
89+
];
90+
} else {
91+
config.plugins = [
92+
new LavaMoatPlugin({
93+
generatePolicy: true,
94+
HtmlWebpackPluginInterop: true,
95+
readableResourceIds: true,
96+
diagnosticsVerbosity: 1,
97+
lockdown: {
98+
consoleTaming: 'unsafe',
99+
errorTrapping: 'none',
100+
unhandledRejectionTrapping: 'none',
101+
overrideTaming: 'severe',
102+
}
103+
}),
104+
...config.plugins,
105+
new webpack.ProvidePlugin({
106+
process: 'process/browser',
107+
Buffer: ['buffer', 'Buffer'],
108+
}),
109+
new webpack.DefinePlugin({
110+
'process.env': JSON.stringify(process.env),
111+
}),
112+
];
113+
}
44114

45115
config.module.rules.push({
46116
test: /\.m?js/,

lavamoat/webpack/policy-override.json

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
{
2+
"resources": {
3+
"viz.js": {
4+
"globals": {
5+
"document.title": true
6+
}
7+
},
8+
"jquery": {
9+
"globals": {
10+
"document": true,
11+
"window": true,
12+
"navigator": true,
13+
"location": true,
14+
"setTimeout": true,
15+
"clearTimeout": true,
16+
"setInterval": true,
17+
"clearInterval": true
18+
}
19+
},
20+
"unleash-proxy-client": {
21+
"globals": {
22+
"fetch": true,
23+
"window": true,
24+
"window.fetch": true
25+
}
26+
},
27+
"react": {
28+
"globals": {
29+
"Error": true,
30+
"Object": true,
31+
"Symbol": true,
32+
"Map": true,
33+
"Set": true,
34+
"WeakMap": true,
35+
"WeakSet": true,
36+
"Proxy": true,
37+
"Reflect": true,
38+
"process.env.NODE_ENV": true,
39+
"Function.prototype": true,
40+
"Object.prototype": true
41+
}
42+
},
43+
"react-dom": {
44+
"globals": {
45+
"Error": true,
46+
"Object": true,
47+
"Symbol": true,
48+
"Map": true,
49+
"Set": true,
50+
"WeakMap": true,
51+
"WeakSet": true,
52+
"window": true,
53+
"document": true,
54+
"MessageChannel": true,
55+
"MutationObserver": true,
56+
"requestAnimationFrame": true,
57+
"cancelAnimationFrame": true,
58+
"performance": true,
59+
"Event": true,
60+
"Node": true,
61+
"Element": true,
62+
"HTMLElement": true,
63+
"HTMLIFrameElement": true,
64+
"DocumentFragment": true,
65+
"Function.prototype": true,
66+
"Object.prototype": true,
67+
"Error.prototype": true
68+
}
69+
},
70+
"scheduler": {
71+
"globals": {
72+
"MessageChannel": true,
73+
"clearTimeout": true,
74+
"setTimeout": true,
75+
"window": true,
76+
"Error": true,
77+
"Object": true,
78+
"Function.prototype": true,
79+
"Object.prototype": true
80+
}
81+
}
82+
}
83+
}

0 commit comments

Comments
 (0)