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

Use vm.runInThisContext instead of eval #18

Merged
merged 1 commit into from
Jan 12, 2015
Merged
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
12 changes: 9 additions & 3 deletions lib/parse.js

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

13 changes: 11 additions & 2 deletions src/parse.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
###

{runInThisContext} = require 'vm'

{nodes} = require 'coffee-script'

defaultReviver = (key, value) -> value
Expand All @@ -46,6 +48,13 @@ syntaxErrorMessage = (csNode, msg) ->
column = columnIdx + 1 if columnIdx?
"Syntax error on line #{line}, column #{column}: #{msg}"

parseStringLiteral = (literal) ->
# In theory this could be replaced by properly resolving
# escape sequences etc.
# We trust the coffee-script lexer to make sure it's just
# a strings and running it has no side effects.
runInThisContext literal

# See:
# http://www.ecma-international.org/ecma-262/5.1/#sec-15.12.2
parse = (source, reviver = defaultReviver) ->
Expand All @@ -69,7 +78,7 @@ parse = (source, reviver = defaultReviver) ->
{value} = node
try
if value[0] == "'"
eval value # we trust the lexer here
parseStringLiteral value
else
JSON.parse value
catch err
Expand Down Expand Up @@ -138,7 +147,7 @@ parse = (source, reviver = defaultReviver) ->
when 'Value'
{value} = csNode.base
switch value[0]
when '\'' then eval value # we trust the lexer here
when '\'' then parseStringLiteral value
when '"' then JSON.parse value
else value

Expand Down