Skip to content

Commit

Permalink
chore: lavamoat policy overrides working
Browse files Browse the repository at this point in the history
  • Loading branch information
andreabadesso committed Dec 19, 2024
1 parent 12c29b0 commit ed4d231
Show file tree
Hide file tree
Showing 3 changed files with 2,354 additions and 21 deletions.
112 changes: 91 additions & 21 deletions config-overrides.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
const webpack = require('webpack');
const LavaMoatPlugin = require('@lavamoat/webpack')
const fs = require('fs');
const path = require('path');

module.exports = function override(config, env) {
config.optimization = {
...config.optimization,
minimize: false
minimize: false,
concatenateModules: false
};

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

config.plugins = [
new LavaMoatPlugin({
writeAutoConfig: true,
inlineLockdown: /index\.js/,
lockdown: {
consoleTaming: 'unsafe',
errorTrapping: 'none',
unhandledRejectionTrapping: 'none',
overrideTaming: 'moderate',
}
}),
...config.plugins,
new webpack.ProvidePlugin({
process: 'process/browser',
Buffer: ['buffer', 'Buffer'],
}),
new webpack.DefinePlugin({
'process.env': JSON.stringify(process.env),
}),
];
// Read the generated policy file
let policy = {};
const policyPath = path.join(__dirname, 'lavamoat', 'webpack');
const policyFile = path.join(policyPath, 'policy.json');

try {
policy = JSON.parse(fs.readFileSync(policyFile, 'utf8'));
} catch (err) {
console.warn('No existing LavaMoat policy found, will generate one');
}

// Add custom policies
/* if (policy.resources) {
// WalletConnect policy
if (!policy.resources['@walletconnect/core']) {
policy.resources['@walletconnect/core'] = {
globals: {
ArrayBuffer: true,
Uint8Array: true,
Object: true,
'Object.prototype': true,
'Object.getPrototypeOf': true,
Symbol: true,
'Function.prototype': true
}
};
}
// React policy
if (!policy.resources['react']) {
policy.resources['react'] = {
globals: {
Object: true,
'Object.prototype': true,
'Function.prototype': true
}
};
}
// Lodash policy
if (!policy.resources['lodash']) {
policy.resources['lodash'] = {
globals: {
Object: true,
'Object.prototype': true,
'Function.prototype': true
}
};
}
}*/

// Don't use LavaMoat when running in electron
if (process.env.ELECTRON_RUN === 'true') {
config.plugins = [
...config.plugins,
new webpack.ProvidePlugin({
process: 'process/browser',
Buffer: ['buffer', 'Buffer'],
}),
new webpack.DefinePlugin({
'process.env': JSON.stringify(process.env),
}),
];
} else {
config.plugins = [
new LavaMoatPlugin({
generatePolicy: true,
HtmlWebpackPluginInterop: true,
readableResourceIds: true,
diagnosticsVerbosity: 1,
lockdown: {
consoleTaming: 'unsafe',
errorTrapping: 'none',
unhandledRejectionTrapping: 'none',
overrideTaming: 'severe',
}
}),
...config.plugins,
new webpack.ProvidePlugin({
process: 'process/browser',
Buffer: ['buffer', 'Buffer'],
}),
new webpack.DefinePlugin({
'process.env': JSON.stringify(process.env),
}),
];
}

config.module.rules.push({
test: /\.m?js/,
Expand Down
139 changes: 139 additions & 0 deletions lavamoat/webpack/policy-override.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
{
"resources": {
"viz.js": {
"globals": {
"document.title": true
}
},
"jquery": {
"globals": {
"document": true,
"window": true,
"window.getComputedStyle": true,
"getComputedStyle": true,
"navigator": true,
"location": true,
"setTimeout": true,
"clearTimeout": true,
"setInterval": true,
"clearInterval": true
}
},
"unleash-proxy-client": {
"globals": {
"fetch": true,
"window": true,
"window.fetch": true
}
},
"react": {
"globals": {
"Error": true,
"Object": true,
"Symbol": true,
"Map": true,
"Set": true,
"WeakMap": true,
"WeakSet": true,
"Proxy": true,
"Reflect": true,
"process.env.NODE_ENV": true,
"Function.prototype": true,
"Object.prototype": true
}
},
"react-dom": {
"globals": {
"Error": true,
"Object": true,
"Symbol": true,
"Map": true,
"Set": true,
"WeakMap": true,
"WeakSet": true,
"window": true,
"document": true,
"MessageChannel": true,
"MutationObserver": true,
"requestAnimationFrame": true,
"cancelAnimationFrame": true,
"performance": true,
"Event": true,
"Node": true,
"Element": true,
"HTMLElement": true,
"HTMLIFrameElement": true,
"DocumentFragment": true,
"Function.prototype": true,
"Object.prototype": true,
"Error.prototype": true
}
},
"scheduler": {
"globals": {
"MessageChannel": true,
"clearTimeout": true,
"setTimeout": true,
"window": true,
"Error": true,
"Object": true,
"Function.prototype": true,
"Object.prototype": true
}
},
"bitcore-lib": {
"globals": {
"Error": true,
"Object": true,
"Math": true,
"Buffer": true,
"Function.prototype": true,
"Object.prototype": true,
"Error.prototype": true,
"Math.prototype": true,
"console": true,
"Function": true,
"Array": true,
"Number": true,
"BigInt": true
},
"packages": {
"bn.js": true,
"elliptic": true,
"buffer": true
}
},
"bn.js": {
"globals": {
"Error": true,
"Object": true,
"Math": true,
"Buffer": true,
"Number": true,
"Array": true,
"Function": true,
"Function.prototype": true,
"Object.prototype": true,
"Array.prototype": true
}
},
"elliptic": {
"globals": {
"Error": true,
"Object": true,
"Math": true,
"Buffer": true,
"Function": true,
"Function.prototype": true,
"Object.prototype": true,
"Error.prototype": true,
"Math.prototype": true,
"Array": true,
"Number": true
},
"packages": {
"bn.js": true
}
}
}
}
Loading

0 comments on commit ed4d231

Please sign in to comment.