-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(webpack): ability to inline lockdown
- Loading branch information
Showing
4 changed files
with
133 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,79 @@ | ||
const { readFileSync } = require('node:fs') | ||
const path = require('node:path') | ||
const { | ||
sources: { RawSource }, | ||
sources: { RawSource, ConcatSource }, | ||
} = require('webpack') | ||
|
||
const lockdownSource = readFileSync( | ||
path.join(require.resolve('ses'), '../lockdown.umd.min.js'), | ||
'utf-8' | ||
) | ||
const lockdownSourcePrefix = `;(function(){\n${lockdownSource}\n})();\n` | ||
|
||
module.exports = { | ||
/** | ||
* @param {object} options | ||
* @param {import('webpack').Compilation} options.compilation | ||
* @param {string[]} options.inlineLockdown | ||
* @returns {() => void} | ||
*/ | ||
sesPrefixFiles: | ||
({ compilation, inlineLockdown: files }) => | ||
() => { | ||
files.forEach((file) => { | ||
const asset = compilation.assets[file] | ||
if (!asset) { | ||
throw new Error( | ||
`LavaMoatPlugin: file specified in inlineLockdown not found in compilation: ${file}` | ||
) | ||
} | ||
compilation.assets[file] = new ConcatSource(lockdownSourcePrefix, asset) | ||
}) | ||
}, | ||
/** | ||
* @param {object} options | ||
* @param {import('webpack').Compilation} options.compilation | ||
* @param {import('webpack').WebpackPluginInstance} [options.HtmlWebpackPluginInUse] | ||
* @param {boolean} [options.HtmlWebpackPluginInterop] | ||
* @returns {() => void} | ||
*/ | ||
sesEmitHook: | ||
({ compilation, HtmlWebpackPluginInUse, HtmlWebpackPluginInterop }) => | ||
() => { | ||
const sesFile = readFileSync(require.resolve('ses'), 'utf-8') | ||
// TODO: to consider: instead manually copy to compiler.options.output.path | ||
const asset = new RawSource(sesFile) | ||
const asset = new RawSource(lockdownSource) | ||
|
||
compilation.emitAsset('lockdown', asset) | ||
|
||
if (HtmlWebpackPluginInUse && HtmlWebpackPluginInterop) { | ||
HtmlWebpackPluginInUse.constructor | ||
// @ts-expect-error - incomplete types | ||
.getHooks(compilation) | ||
.beforeEmit.tapAsync('LavaMoatWebpackPlugin-lockdown', (data, cb) => { | ||
const scriptTag = '<script src="./lockdown"></script>' | ||
const headTagRegex = /<head[^>]*>/iu | ||
const scriptTagRegex = /<script/iu | ||
.beforeEmit.tapAsync( | ||
'LavaMoatWebpackPlugin-lockdown', | ||
( | ||
/** @type {{ html: string }} */ data, | ||
/** @type {(arg0: null, arg1: any) => void} */ cb | ||
) => { | ||
const scriptTag = '<script src="./lockdown"></script>' | ||
const headTagRegex = /<head[^>]*>/iu | ||
const scriptTagRegex = /<script/iu | ||
|
||
if (headTagRegex.test(data.html)) { | ||
data.html = data.html.replace(headTagRegex, `$&${scriptTag}`) | ||
} else if (scriptTagRegex.test(data.html)) { | ||
data.html = data.html.replace( | ||
scriptTagRegex, | ||
`${scriptTag}<script` | ||
) | ||
} else { | ||
throw Error( | ||
'LavaMoat: Could not insert lockdown script tag, no suitable location found in the html template' | ||
) | ||
if (headTagRegex.test(data.html)) { | ||
data.html = data.html.replace(headTagRegex, `$&${scriptTag}`) | ||
} else if (scriptTagRegex.test(data.html)) { | ||
data.html = data.html.replace( | ||
scriptTagRegex, | ||
`${scriptTag}<script` | ||
) | ||
} else { | ||
throw Error( | ||
'LavaMoat: Could not insert lockdown script tag, no suitable location found in the html template' | ||
) | ||
} | ||
cb(null, data) | ||
} | ||
cb(null, data) | ||
}) | ||
) | ||
} | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
const test = require('ava') | ||
const { scaffold, runScript } = require('./scaffold.js') | ||
const { makeConfig } = require('./fixtures/main/webpack.config.js') | ||
|
||
test.before(async (t) => { | ||
const webpackConfig = makeConfig({ | ||
inlineLockdown: ['app.js'], | ||
}) | ||
await t.notThrowsAsync(async () => { | ||
t.context.build = await scaffold(webpackConfig) | ||
}, 'Expected the build to succeed') | ||
t.context.bundle = t.context.build.snapshot['/dist/app.js'] | ||
}) | ||
|
||
test('webpack/lockdown-inline - bundle runs under lockdown', (t) => { | ||
t.notThrows(() => { | ||
runScript( | ||
t.context.bundle + | ||
` | ||
;; | ||
if (!Object.isFrozen(Object.prototype)) { | ||
throw Error('expected Object.prototype to be frozen'); | ||
} | ||
if (!Compartment || !harden) { | ||
throw Error('expected SES globals to be available'); | ||
}` | ||
) | ||
}) | ||
}) |