Skip to content

Commit

Permalink
fix jsdom require in sandbox
Browse files Browse the repository at this point in the history
fix #1128
  • Loading branch information
bjrmatos committed Apr 29, 2024
1 parent d032355 commit ba9399d
Show file tree
Hide file tree
Showing 5 changed files with 214 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ module.exports = function createSandboxRequire (safeExecution, isolateModules, m

if (isolateModules) {
const requireExtensions = Object.create(null)
isolatedRequire.setDefaultRequireExtensions(requireExtensions, modulesCache, compileScript)
isolatedRequire.setDefaultRequireExtensions(requireExtensions, requireFromRootDirectory, compileScript)
modulesMeta.requireExtensions = requireExtensions
}

Expand Down
1 change: 1 addition & 0 deletions packages/jsreport-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
},
"devDependencies": {
"@node-rs/jsonwebtoken": "0.2.0",
"jsdom": "17.0.0",
"mocha": "10.1.0",
"should": "13.2.3",
"standard": "16.0.4",
Expand Down
4 changes: 4 additions & 0 deletions packages/jsreport-core/test/render/moduleWithJSDOM.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = async () => {
const { JSDOM } = require('jsdom')
return typeof JSDOM
}
42 changes: 42 additions & 0 deletions packages/jsreport-core/test/render/runInSandboxTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -1581,6 +1581,47 @@ describe('sandbox', () => {
should(content.require[prop]).be.eql(true)
})
})

it('jsdom module should work', async () => {
reporter.tests.afterRenderEval(async (req, res, { reporter }) => {
const r = await reporter.runInSandbox({
context: {
RESULT: null,
CONTENT: null
},
userCode: `
RESULT = false
try {
const pkg = require('moduleWithJSDOM')
RESULT = true
CONTENT = await pkg()
} catch (err) {}
`,
executionFn: ({ context }) => {
return JSON.stringify({
result: context.RESULT,
content: context.CONTENT
})
}
}, req)

res.content = Buffer.from(r)
})

const res = await reporter.render({
template: {
engine: 'none',
content: ' ',
recipe: 'html'
}
})

const { result, content } = JSON.parse(res.content)

should(result).be.True('expected module "moduleWithJSDOM" to be required normally')
should(typeof content).be.eql('string', 'expected content to be string')
})
}

function createReporterForRequireTests ({ safe, isolate } = {}) {
Expand All @@ -1607,6 +1648,7 @@ describe('sandbox', () => {
'moduleWithBuiltin',
'moduleWithNodeBuiltin',
'moduleWithShebang',
'moduleWithJSDOM',
'moduleWithJSON',
'moduleWithMJS',
'module-with-type-module/index.cjs',
Expand Down

0 comments on commit ba9399d

Please sign in to comment.