Skip to content

Commit

Permalink
fix(webpack): compatibility fix for overwriting exports reference in cjs
Browse files Browse the repository at this point in the history
  • Loading branch information
naugtur committed Dec 12, 2023
1 parent 97ba036 commit 2259d74
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions packages/webpack/src/runtime/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ if (LOCKDOWN_ON) {
lockdown(LAVAMOAT.options.lockdown)
} else {
warn(
'LavaMoat: runtime execution started without SES present, switching to no-op.',
'LavaMoat: runtime execution started without SES present, switching to no-op.'
)
}

Expand All @@ -41,8 +41,8 @@ const stricterScopeTerminator = freeze(
freeze({
// TODO: emulate a reference error in a getter.
has: freeze(() => true),
}),
),
})
)
)

// Policy implementation
Expand All @@ -62,7 +62,7 @@ const enforcePolicy = (requestedResourceId, referrerResourceId) => {
return
}
throw Error(
`Policy does not allow importing ${requestedResourceId} from ${referrerResourceId}`,
`Policy does not allow importing ${requestedResourceId} from ${referrerResourceId}`
)
}

Expand All @@ -82,20 +82,20 @@ const installGlobalsForPolicy = (resourceId, packageCompartmentGlobal) => {
rootCompartmentGlobalThis,
LAVAMOAT.policy.resources[resourceId] || {},
globalThis,
packageCompartmentGlobal,
packageCompartmentGlobal
)

defineProperties(
packageCompartmentGlobal,
getOwnPropertyDescriptors(endowments),
getOwnPropertyDescriptors(endowments)
)
}
}

const compartmentMap = new Map()
const findResourceId = (moduleId) => {
const found = LAVAMOAT.idmap.find(([, moduleIds]) =>
moduleIds.includes(moduleId),
moduleIds.includes(moduleId)
)
if (found) {
return found[0]
Expand Down Expand Up @@ -147,11 +147,13 @@ const lavamoatRuntimeWrapper = (resourceId, runtimeKit) => {
{
has: (target, prop) => {
warn(
`A module attempted to read ${String(prop)} directly from webpack's module cache`,
`A module attempted to read ${String(
prop
)} directly from webpack's module cache`
)
return false
},
},
}
)

// override nmd to limit what it can mutate
Expand All @@ -171,6 +173,14 @@ const lavamoatRuntimeWrapper = (resourceId, runtimeKit) => {
get: () => module,
set: () => {},
})
// Make it possible to overwrite `exports` locally despite runtimeHandler being frozen
let exportsReference = runtimeHandler.exports
defineProperty(runtimeHandler, 'exports', {
get: () => exportsReference,
set: (value) => {
exportsReference = value
},
})
freeze(runtimeHandler)

if (!compartmentMap.has(resourceId)) {
Expand Down

0 comments on commit 2259d74

Please sign in to comment.