var a = {foo: function () {console.log('Hello');}};
JSON.stringify(a); // yields "{}"var recData = JSON.parse(recDataString, function (key, value) {
if (value && (typeof value === 'string') && value.indexOf("function") === 0) {
var jsFunc = new Function('return ' + value)();
return jsFunc;
}
return value;
});
console.log( "Type: " + typeof recData["runFunction"] )
runFunction in the json is a function which is something like this:
function() {
require("fs");
--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/B5AA737B-3074-4B3C-9551-F1AB89DA6941%40ryandesign.com.
To post to this group, send email to nod...@googlegroups.com.
// code.json
{"myFunction":"function(a) { console.log(a); }"}var code = JSON.parse(fs.readFileSync("./code.json"));
/* magic, insecure function stuff from your post */
code.myFunction("hello");// code.js
exports.myFunction = function(a) { console.log(a); }var code = require("./code.js");
/* look ma, no magic */
code.myFunction("hello");
var recData = JSON.parse(recDataString, function (key, value) {if (value && (typeof value === 'string') && value.indexOf("function") === 0) {
var jsFunc = new Function('return ' + value)();
return jsFunc;
}
return value;
});
console.log( "Type: " + typeof recData["runFunction"] )
runFunction in the json is a function which is something like this:
{"runFunction":"function(){'use strict';\nvar net = require(\"net\");\n\nconsole.log(\"NET\");\n}"}
--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/DD2646F6-CBFF-475B-A82D-23276268AD72%40ryandesign.com.
@Ryan - This is a simpler json string example below. This too does not work for me when i run: recData["runFunction"]();{"runFunction":"function(){'use strict';\nvar net = require(\"net\");\n\nconsole.log(\"NET\");\n}"}
Try it yourself on the console as I do here and see what you get. I say this because I don't see any problems, see below. Or, alternatively, maybe you need to be clearer about what you mean by "does not work".
> var recDataString = JSON.stringify({
... "runFunction": "function(){'use strict';\nvar net = require(\"net\");\n\nconsole.log(\"NET\");\n}"
... });
> var recData = JSON.parse(recDataString, function (key,value) {
... if (value && (typeof value === 'string') && key.indexOf("runFunction") === 0) {
..... return new Function('return ' + value)();
..... }
... return value;
... });
> recData
{ runFunction: [Function] }
> recData["runFunction"]()
NET
>
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/CAMu3Ni1e34zRjv63B%2B49TKqzR783WtjaY99y3V7vUjmNTOTM3w%40mail.gmail.com.