Skip to content

Commit

Permalink
fix(lavamoat): allow prefixed builtins
Browse files Browse the repository at this point in the history
Closes #652
  • Loading branch information
boneskull committed Aug 16, 2023
1 parent f69b08b commit 55613cd
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 1 deletion.
7 changes: 6 additions & 1 deletion packages/node/src/parseForPolicy.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const path = require('path')
const { promises: fs } = require('fs')
const { builtinModules: builtinPackages } = require('module')
const { builtinModules } = require('module')
const resolve = require('resolve')
const bindings = require('bindings')
const gypBuild = require('node-gyp-build')
Expand All @@ -19,6 +19,11 @@ const { checkForResolutionOverride } = require('./resolutions')
const commonjsExtensions = ['', '.js', '.cjs']
const resolutionOmittedExtensions = ['.js', '.json']

/**
* Allow use of `node:` prefixed builtins.
*/
const builtinPackages = [...builtinModules].concat(builtinModules.map(id => `node:${id}`))

// approximate polyfill for node builtin
const createRequire = (url) => {
return {
Expand Down
15 changes: 15 additions & 0 deletions packages/node/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,21 @@ test('parseForPolicy - indirectly used packages are included in parent\'s allowl
})
})

test('parseForPolicy - find node:-prefixed builtins', async (t) => {
const projectRoot = `${__dirname}/projects/6`;
const entryId = `${projectRoot}/index.js`;
const policy = await parseForPolicy({ entryId, projectRoot });
t.deepEqual(policy, {
resources: {
a: {
builtin: {
'node:events.EventEmitter': true
}
}
}
});
})

// run lavamoat-node
test('execute - resolutions', async (t) => {
const projectRoot = `${__dirname}/projects/1`
Expand Down
1 change: 1 addition & 0 deletions packages/node/test/projects/6/fake_node_modules/a/index.js

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.

1 change: 1 addition & 0 deletions packages/node/test/projects/6/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require('a');
9 changes: 9 additions & 0 deletions packages/node/test/projects/6/lavamoat/node/policy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"resources": {
"a": {
"builtin": {
"node:events.EventEmitter": true
}
}
}
}
20 changes: 20 additions & 0 deletions packages/node/test/projects/6/package-lock.json

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

9 changes: 9 additions & 0 deletions packages/node/test/projects/6/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "test-6",
"scripts": {
"setup": "npm install"
},
"dependencies": {
"a": "file:./fake_node_modules/a"
}
}

0 comments on commit 55613cd

Please sign in to comment.