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

Fix for #1035: Giving the eval sandbox a real Module instance #1487

Closed
wants to merge 2 commits into from
Closed
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
37 changes: 22 additions & 15 deletions lib/coffee-script.js

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

16 changes: 2 additions & 14 deletions lib/repl.js

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

18 changes: 11 additions & 7 deletions src/coffee-script.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
fs = require 'fs'
path = require 'path'
vm = require 'vm'
{Module} = require 'module'
{Lexer,RESERVED} = require './lexer'
{parser} = require './parser'

Expand Down Expand Up @@ -78,16 +79,19 @@ exports.run = (code, options) ->
# The CoffeeScript REPL uses this to run the input.
exports.eval = (code, options = {}) ->
return unless code = code.trim()
sandbox = options.sandbox
unless sandbox
sandbox =
require: require
module : { exports: {} }
sandbox = options.sandbox ? {}
unless sandbox and sandbox.require
sandbox.module = new Module('repl')
sandbox.require = (path) -> Module._load path, sandbox.module
sandbox.require[x] = require[x] for x of require
sandbox.require.resolve = (request) -> Module._resolveFilename request, sandbox.module
sandbox[g] = global[g] for g in Object.getOwnPropertyNames global
sandbox.global = sandbox
sandbox.global.global = sandbox.global.root = sandbox.global.GLOBAL = sandbox
sandbox.__filename = options.filename || 'eval'
sandbox.__dirname = path.dirname sandbox.__filename
sandbox.global = sandbox
sandbox.global.global = sandbox.global.root = sandbox.global.GLOBAL = sandbox
sandbox.__filename = sandbox.module.filename = options.filename || 'eval'
sandbox.__dirname = path.dirname sandbox.__filename
o = {}; o[k] = v for k, v of options
o.bare = on # ensure return value
js = compile "_=(#{code}\n)", o
Expand Down
7 changes: 1 addition & 6 deletions src/repl.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,7 @@ backlog = ''
# Attempt to evaluate the command. If there's an exception, print it out instead
# of exiting.
run = do ->
sandbox =
require: require
module : { exports: {} }
sandbox[g] = global[g] for g in Object.getOwnPropertyNames global
sandbox.global = sandbox
sandbox.global.global = sandbox.global.root = sandbox.global.GLOBAL = sandbox
sandbox = {} # properties are set by `CoffeeScript.eval`
(buffer) ->
code = backlog += '\n' + buffer.toString()
if code[code.length - 1] is '\\'
Expand Down