我应该如何使用Node.js解析JSON ?是否有一些模块可以安全地验证和解析JSON ?


当前回答

如果你需要用Node.js以一种安全的方式解析JSON(也就是说:用户可以输入数据,或者一个公共API),我建议使用secure- JSON -parse。

其用法类似于默认的JSON。解析,但它会保护你的代码:

原型中毒 和构造函数滥用:

const badJson = '{ "a": 5, "b": 6, "__proto__": { "x": 7 }, "constructor": {"prototype": {"bar": "baz"} } }'

const infected = JSON.parse(badJson)
console.log(infected.x) // print undefined

const x = Object.assign({}, infected)
console.log(x.x) // print 7

const sjson = require('secure-json-parse')
console.log(sjson.parse(badJson)) // it will throw by default, you can ignore malicious data also

其他回答

这里的每个人都讲过JSON。所以我想说点别的。有一个伟大的模块连接许多中间件,使应用程序的开发更容易和更好。中间件之一是bodyParser。它可以解析JSON, html表单等。还有一个仅用于JSON解析的特定中间件noop。

看看上面的链接,它可能对你很有帮助。

如果你想在你的JSON中添加一些注释,并允许尾随逗号,你可能想使用下面的实现:

var fs = require('fs');

var data = parseJsData('./message.json');

console.log('[INFO] data:', data);

function parseJsData(filename) {
    var json = fs.readFileSync(filename, 'utf8')
        .replace(/\s*\/\/.+/g, '')
        .replace(/,(\s*\})/g, '}')
    ;
    return JSON.parse(json);
}

注意,如果JSON中有"abc": "foo // bar"这样的东西,它可能无法正常工作。所以YMMV。

正如这里提到的其他答案,你可能想要一个本地json文件,你知道是安全的,就像一个配置文件:

var objectFromRequire = require('path/to/my/config.json'); 

或者使用全局JSON对象将字符串值解析为对象:

var stringContainingJson = '\"json that is obtained from somewhere\"';
var objectFromParse = JSON.parse(stringContainingJson);

请注意,当您需要一个文件时,该文件的内容会被评估,如果它不是json文件而是js文件,则会引入安全风险。

在这里,我发布了一个演示,你可以看到这两种方法,并在线使用它们(解析示例在app.js文件中-然后点击运行按钮,在终端中看到结果): http://staging1.codefresh.io/labs/api/env/json-parse-example

您可以修改代码并查看影响…

使用JSON配置Node.js?阅读这篇文章,让你的配置技能超过9000…

Note: People claiming that data = require('./data.json'); is a security risk and downvoting people's answers with zealous zeal: You're exactly and completely wrong. Try placing non-JSON in that file... Node will give you an error, exactly like it would if you did the same thing with the much slower and harder to code manual file read and then subsequent JSON.parse(). Please stop spreading misinformation; you're hurting the world, not helping. Node was designed to allow this; it is not a security risk!

正确的应用程序有3层以上的配置:

服务器/容器配置 应用程序配置 (可选)租户/社区/组织配置 用户配置

大多数开发者都认为他们的服务器和应用配置是可以改变的。它不能。您可以在更高的层上逐层进行更改,但是您正在修改基本需求。有些东西需要存在!让你的配置像不可变一样,因为它的一些基本是不可变的,就像你的源代码一样。

如果没有看到你的很多东西在启动后不会改变,就会导致反模式,比如在配置加载中到处都是try/catch块,并且假装你可以在没有正确设置应用程序的情况下继续运行。你不能。如果可以,那就属于社区/用户配置层,而不是服务器/应用配置层。你只是做错了。当应用程序完成引导时,可选的东西应该分层在顶部。

不要用头撞墙:你的配置应该非常简单。

看看用一个简单的json配置文件和一个简单的app.js文件来设置一个像协议无关和数据源无关的服务框架这样复杂的东西是多么容易……

container-config.js……

{
    "service": {
        "type"  : "http",
        "name"  : "login",
        "port"  : 8085
    },
    "data": {
        "type"  : "mysql",
        "host"  : "localhost",
        "user"  : "notRoot",
        "pass"  : "oober1337",
        "name"  : "connect"
    }
}

index.js……(驱动一切的引擎)

var config      = require('./container-config.json');       // Get our service configuration.
var data        = require(config.data.type);            // Load our data source plugin ('npm install mysql' for mysql).
var service     = require(config.service.type);         // Load our service plugin ('http' is built-in to node).
var processor   = require('./app.js');                  // Load our processor (the code you write).

var connection  = data.createConnection({ host: config.data.host, user: config.data.user, password: config.data.pass, database: config.data.name });
var server      = service.createServer(processor);
connection.connect();
server.listen(config.service.port, function() { console.log("%s service listening on port %s", config.service.type, config.service.port); });

app.js……(支持协议不可知和数据源不可知服务的代码)

module.exports = function(request, response){
    response.end('Responding to: ' + request.url);
}

使用这种模式,你现在可以在你启动的应用程序上加载社区和用户配置的东西,开发运维已经准备好将你的工作推到一个容器中并扩展它。你是为多租户的。用户域是隔离的。现在,您可以分离使用的服务协议和数据库类型,只关注编写好的代码。

因为你在使用层,你可以在任何时候依赖于一个单一的真实来源(分层配置对象),并避免在每一步进行错误检查,担心“哦,糟糕,没有正确的配置,我怎么能让这个工作?!?”

只是想完成答案(因为我挣扎了一段时间),想展示如何访问json信息,这个例子显示了访问json数组:

Var request = require('request'); 请求(“https://server/run?oper_get_groups_joined_by_user_id &user_id=5111298845048832',函数(错误,响应,body) { 如果(!错误&&响应。statusCode == 200) { var jsonArr = JSON.parse(body); console.log (jsonArr); console.log("group id:" + jsonArr[0].id); } })