webpack-util.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. const fs = require('fs');
  2. const babelCore = require('@babel/core');
  3. const WrapperWebpackPlugin = require('wrapper-webpack-plugin');
  4. const entryGlobals = {
  5. 'common': [],
  6. 'injected/content': [],
  7. 'injected/web': [],
  8. };
  9. const entryPathToFilename = path => path === '*'
  10. ? `./src/common/safe-globals-shared.js`
  11. : `./src/${path}/safe-globals.js`;
  12. Object.entries(entryGlobals).forEach(([name, val]) => {
  13. const parts = name.split('/');
  14. if (parts[1]) parts[1] = name;
  15. val.push('*', ...parts);
  16. });
  17. exports.restrictedSyntax = (
  18. // Hiding `code` so eslint doesn't complain about invalid schema
  19. rules => rules.map(r => (
  20. Object.defineProperty(r, 'code', { enumerable: false, value: r.code })
  21. ))
  22. )([{
  23. selector: 'ArrayPattern',
  24. message: 'Destructuring via Symbol.iterator may be spoofed/broken in an unsafe environment',
  25. code: '[window.foo]=[]',
  26. }, {
  27. selector: ':matches(ArrayExpression, CallExpression) > SpreadElement',
  28. message: 'Spreading via Symbol.iterator may be spoofed/broken in an unsafe environment',
  29. code: 'open([...[]])',
  30. }, {
  31. selector: '[callee.object.name="Object"], MemberExpression[object.name="Object"]',
  32. message: 'Using potentially spoofed methods in an unsafe environment',
  33. code: 'Object.assign()',
  34. // TODO: auto-generate the rule using GLOBALS
  35. }, {
  36. selector: `CallExpression[callee.name="defineProperty"]:not(${[
  37. '[arguments.2.properties.0.key.name="__proto__"]',
  38. ':has(CallExpression[callee.name="nullObjFrom"])'
  39. ].join(',')})`,
  40. message: 'Prototype of descriptor may be spoofed/broken in an unsafe environment',
  41. code: 'defineProperty(open, "foo", {foo:1})',
  42. }]);
  43. /**
  44. * Adds a watcher for files in entryGlobals to properly recompile the project on changes.
  45. */
  46. function addWrapperWithGlobals(name, config, defsObj, callback) {
  47. config.module.rules.push({
  48. test: new RegExp(`/${name}/.*?\\.js$`.replace(/\//g, /[/\\]/.source)),
  49. use: [{
  50. loader: './scripts/fake-dep-loader.js',
  51. options: { files: entryGlobals[name].map(entryPathToFilename) },
  52. }],
  53. });
  54. const defsRe = new RegExp(`\\b(${
  55. Object.keys(defsObj)
  56. .join('|')
  57. .replace(/\./g, '\\.')
  58. })\\b`, 'g');
  59. const reader = () => (
  60. entryGlobals[name]
  61. .map(path => readGlobalsFile(path))
  62. .join('\n')
  63. .replace(defsRe, s => defsObj[s])
  64. );
  65. config.plugins.push(new WrapperWebpackPlugin(callback(reader)));
  66. }
  67. function getCodeMirrorThemes() {
  68. const name = 'neo.css';
  69. return fs.readdirSync(
  70. require.resolve(`codemirror/theme/${name}`).slice(0, -name.length),
  71. { withFileTypes: true },
  72. ).map(e => e.isFile() && e.name.endsWith('.css') && e.name.slice(0, -4))
  73. .filter(Boolean);
  74. }
  75. function readGlobalsFile(path, babelOpts = {}) {
  76. const { ast, code = !ast } = babelOpts;
  77. const filename = entryPathToFilename(path);
  78. const src = fs.readFileSync(filename, { encoding: 'utf8' })
  79. .replace(/\bexport\s+(function\s+(\w+))/g, 'const $2 = $1')
  80. .replace(/\bexport\s+(?=(const|let)\s)/g, '');
  81. const res = babelCore.transformSync(src, {
  82. ...babelOpts,
  83. ast,
  84. code,
  85. filename,
  86. });
  87. return ast ? res : res.code;
  88. }
  89. exports.addWrapperWithGlobals = addWrapperWithGlobals;
  90. exports.getCodeMirrorThemes = getCodeMirrorThemes;
  91. exports.readGlobalsFile = readGlobalsFile;