Skip to content

Commit

Permalink
fix(webpack): fix global aliases in non-root compartments
Browse files Browse the repository at this point in the history
  • Loading branch information
naugtur committed Mar 13, 2024
1 parent f20a28e commit 18840ea
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 18 deletions.
21 changes: 16 additions & 5 deletions packages/webpack/src/runtime/runtime.js
Expand Up @@ -9,6 +9,7 @@ const {
defineProperty,
defineProperties,
getOwnPropertyDescriptors,
fromEntries,
} = Object
const warn = typeof console === 'object' ? console.warn : () => {}
// Avoid running any wrapped code or using compartment if lockdown was not called.
Expand Down Expand Up @@ -79,6 +80,7 @@ const enforcePolicy = (requestedResourceId, referrerResourceId) => {
const theRealGlobalThis = globalThis
/** @type {any} */
let rootCompartmentGlobalThis
const globalAliases = ['globalThis', 'window', 'self']
/**
* Installs globals for a specific policy resource.
*
Expand All @@ -89,11 +91,11 @@ let rootCompartmentGlobalThis
const installGlobalsForPolicy = (resourceId, packageCompartmentGlobal) => {
if (resourceId === LAVAMOAT.root) {
rootCompartmentGlobalThis = packageCompartmentGlobal
copyWrappedGlobals(theRealGlobalThis, rootCompartmentGlobalThis, [
'globalThis',
'window',
'self',
])
copyWrappedGlobals(
theRealGlobalThis,
rootCompartmentGlobalThis,
globalAliases
)
} else {
// TODO: getEndowmentsForConfig doesn't implement support for "write"
const endowments = getEndowmentsForConfig(
Expand All @@ -103,6 +105,15 @@ const installGlobalsForPolicy = (resourceId, packageCompartmentGlobal) => {
packageCompartmentGlobal
)

defineProperties(
packageCompartmentGlobal,
fromEntries(
globalAliases.map((alias) => [
alias,
{ value: packageCompartmentGlobal },
])
)
)
defineProperties(
packageCompartmentGlobal,
getOwnPropertyDescriptors(endowments)
Expand Down
35 changes: 35 additions & 0 deletions packages/webpack/test/e2e-manual.js
@@ -0,0 +1,35 @@
// run this with node to produce the test build to disk
const fs = require('fs')
const path = require('path')
const { scaffold, runScriptWithSES } = require('./scaffold.js')
const webpackConfigDefault = require('./fixtures/main/webpack.config.js')
const webpackConfig = {
...webpackConfigDefault,
// mode: 'development',
// devtool: false,
}

const now = new Date().toISOString()

const cache = new Set()
function assertFolderFor(file) {
const folder = path.dirname(file)
if (cache.has(folder)) return
const target = folder.replace(/^\//, '')
fs.mkdirSync(path.join('.', 'tmp', now, target), { recursive: true })
cache.add(folder)
}

function writeFromSnapshot(snapshot) {
const files = Object.keys(snapshot)
for (const file of files) {
assertFolderFor(file)
fs.writeFileSync(path.join('.', 'tmp', now, file), snapshot[file])
}
}

scaffold(webpackConfig).then(({ stdout, snapshot }) => {
console.log(stdout)
writeFromSnapshot(snapshot)
runScriptWithSES(snapshot['/dist/app.js'])
})
8 changes: 4 additions & 4 deletions packages/webpack/test/fixtures/main/main.js
@@ -1,15 +1,14 @@
import { hello as helloCommonJS } from 'commonjs-package'
import { re } from 'commonjs-quirks'
import { hello as helloES6 } from 'es6-module-package'
import globals from 'globals-package'
import 'side-effects-package/styles.css'
import { hello as helloTypeScript } from 'typescript-package'
import { hello as helloUMD } from 'umd-package'
import { thing } from './src/other.mjs'
import { what } from './src/hello.ts'
import 'node-global-package'
import { thing } from './src/other.mjs'

// deliberately unused
import { treeshakeable } from './src/treeshakeable.mjs'
import './src/style.css'

function run() {
Expand All @@ -20,7 +19,8 @@ function run() {
helloTypeScript(),
helloUMD(),
thing(),
what
what,
globals
)
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

2 changes: 1 addition & 1 deletion packages/webpack/test/fixtures/main/package.json
Expand Up @@ -7,8 +7,8 @@
"commonjs-quirks": "1.0.0",
"es6-module-package": "1.0.0",
"fetch-package": "1.0.0",
"globals-package": "1.0.0",
"loader-hack": "1.0.0",
"node-global-package": "1.0.0",
"prototype-poisoning-package": "1.0.0",
"side-effects-package": "1.0.0",
"typescript-package": "1.0.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/webpack/test/fixtures/main/policy-broken/policy.json
Expand Up @@ -10,9 +10,9 @@
"umd-package": true
}
},
"node-global-package": {
"globals-package": {
"globals": {
"console.log": true
"console": true
}
},
"umd-package": {
Expand Down
4 changes: 2 additions & 2 deletions packages/webpack/test/fixtures/main/policy/policy.json
Expand Up @@ -10,9 +10,9 @@
"umd-package": true
}
},
"node-global-package": {
"globals-package": {
"globals": {
"console.log": true
"console": true
}
},
"umd-package": {
Expand Down
4 changes: 2 additions & 2 deletions packages/webpack/test/snapshots/e2e-policy-gen.spec.js.md
Expand Up @@ -20,9 +20,9 @@ Generated by [AVA](https://avajs.dev).
"umd-package": true␊
}␊
},␊
"node-global-package": {␊
"globals-package": {␊
"globals": {␊
"console.log": true␊
"console": true␊
}␊
},␊
"umd-package": {␊
Expand Down
Binary file modified packages/webpack/test/snapshots/e2e-policy-gen.spec.js.snap
Binary file not shown.

0 comments on commit 18840ea

Please sign in to comment.