-
Notifications
You must be signed in to change notification settings - Fork 27
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
Conversation
Fixes #17 While this gets rid of `eval` in node or in "mixed" environments like node-webkit or Atom Shell apps, it still resolves to eval when using browserify. For a solution that is compatible with CSP policies we'd need actual parsing of the literals (wish coffee-script would do it). See: * https://github.com/substack/vm-browserify/blob/bfd7c5f59edec856dc7efe0b77a4f6b2fa20f226/index.js#L105
|
Use `vm.runInThisContext` instead of eval
v1.0.4 |
💚 |
One way of achieving that without writing a lot of code is to convert the Unless I'm misthinking something. |
@johan I wouldn't want to bet that all escaping rules would be preserved with a solution like that. Falling back to |
I also missed that |
It's a bit too late in the evening for a PR, but I think this sketch might be good, both for quotes = /\\['"\\,]|['"]/g
quoted = {
"'": "'"
'"': '"'
'\\': '\\'
',': '\\,'
}
parseStringLiteral = (literal) ->
json = literal.replace quotes, (ch) ->
'\\' + quoted[ch.charAt ch.length-1]
JSON.parse '"' + json + '"' |
@johan Well, parseStringLiteral("won't") // SyntaxError: Unexpected token '
parseStringLiteral("say \\\"what\\\"") = "say \"what\""
parseStringLiteral("lines end with \\n") = "lines end with \n"
parseStringLiteral("say \\u0022what\\u0022") = "say \"what\""
parseStringLiteral("escape using \\\\") = "escape using \\"
parseStringLiteral("col\\u000Bcol") = "col\u000bcol" Code used: strings = [
"won't"
"say \\\"what\\\""
"lines end with \\n"
"say \\u0022what\\u0022"
"escape using \\\\"
"col\\u000Bcol"
]
strings.forEach (str) ->
try
parsed = parseStringLiteral str
console.log 'parseStringLiteral(%j) = %j', str, parsed
catch err
console.log 'parseStringLiteral(%j) // %s: %s', str, err.name, err.message EDIT: Fixed the tests. I think they are now escaping correctly. |
Your array doesn't actually have legal string tokens in it after compilation; ITYM something more like: strings = [
'"won\'t"'
'"say \\"what\\""'
'"lines end with \\n"'
'"say \\u0022what\\u0022"'
'"escape using \\\\"'
'"col\\u000Bcol"'
] ...but the examples are still good proof my quick hack needs additional work before it can do business. :-) |
Actually, you add the wrapping quotes in your parsing code.* Anyhow, those are the basic "happy path" test cases I could think of. :) [*] Quote: |
Ah, my hack was meant to read |
Fixes #17
While this gets rid of
eval
in node and in "mixed" environments like node-webkit or Atom Shell apps, it still resolves to eval when using browserify.For a solution that is compatible with CSP policies we'd need actual parsing of the literals (wish coffee-script would do it).
See: