Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement sandboxing for js tasks #42

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const path = require('path')
const chalk = require('chalk')
const mm = require('micromatch')
const requireFromString = require('require-from-string')
const runJavaScript = require('./runJavaScript')
const logger = require('./logger')
const readMaidFile = require('./readMaidFile')
const MaidError = require('./MaidError')
Expand Down Expand Up @@ -63,24 +63,26 @@ class Maid {
throw new MaidError(`Task '${task.name}' failed.\n${err.stack}`)
}
if (checkTypes(task, ['sh', 'bash'])) {
return runCLICommand({ task, resolve, reject })
runCLICommand({ task, resolve, reject })
return
Copy link
Owner

@egoist egoist Jun 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why split the return statement?

Copy link
Contributor Author

@tunnckoCore tunnckoCore Jun 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because it's more meaningful and it may be a bit confusing. we dont return promise from there anyway. and we dont need to return from there either because we are passing resolve and reject to the runCLICommand.

Copy link
Contributor Author

@tunnckoCore tunnckoCore Jun 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, return frome promise constructor make no sense, you are forced to use resolve and reject anyway. only if something fail in that constructor only then it goes to the catch handler, otherwise you are forced to use resolve and reject

in this case it will just throw , because we directly await the promise

}
if (checkTypes(task, ['py', 'python'])) {
return runCLICommand({ type: 'python', task, resolve, reject })
runCLICommand({ type: 'python', task, resolve, reject })
return
}
if (checkTypes(task, ['js', 'javascript'])) {
let res
try {
res = requireFromString(task.script, this.maidfile.filepath)
} catch (err) {
return handleError(err)
}
res = res.default || res
return resolve(
typeof res === 'function'
? Promise.resolve(res()).catch(handleError)
: res
)
runJavaScript(task.script, this.maidfile.filepath)
.then(result => {
result = (result && result.default) || result

// if code fence module.exports a function (even async one)
// we call it and passing it with `task` object - it may be useful
return typeof result === 'function' ? result(task) : result
})
.then(resolve)
.catch(handleError)

return
}

return resolve()
Expand Down
45 changes: 45 additions & 0 deletions lib/runJavaScript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const vm = require('vm')

/**
* Example
*
* ```js
* runJavaScript(`var bar = 123; if (bar) console.log(bar + 5)`)
* .then(() => {
* console.log('done')
* })
* .catch(console.error)
* ```
*/

const runJavaScript = (content, filepath) => {
const run = vm.runInNewContext(
`(() => new Promise((____resolve) => {
____resolve(${content});
}))`,
{
global: global,
process: process,
require: require,
console: console,
exports: exports,
module: module
},
filepath
)

return run()
}

// Example
//
// runJavaScript(
// `module.exports = async () => process.env.NODE_ENV`,
// 'lib/loadFile.js'
// )
// .then(fn => {
// console.log('done', fn())
// })
// .catch(console.error)

module.exports = runJavaScript
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
"joycon": "^1.0.4",
"markdown-it": "^8.4.1",
"micromatch": "^3.1.10",
"require-from-string": "^2.0.2",
"rexrex": "^1.2.0"
},
"devDependencies": {
Expand Down
4 changes: 0 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3496,10 +3496,6 @@ [email protected]:
version "1.0.0"
resolved "https://registry.yarnpkg.com/replace-ext/-/replace-ext-1.0.0.tgz#de63128373fcbf7c3ccfa4de5a480c45a67958eb"

require-from-string@^2.0.2:
version "2.0.2"
resolved "https://registry.yarnpkg.com/require-from-string/-/require-from-string-2.0.2.tgz#89a7fdd938261267318eafe14f9c32e598c36909"

require-precompiled@^0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/require-precompiled/-/require-precompiled-0.1.0.tgz#5a1b52eb70ebed43eb982e974c85ab59571e56fa"
Expand Down