当我升级车把套件时,我遇到了同样的问题。为了尽快让您的系统恢复在线状态,请删除 package.json 中的 handbars 条目,然后在其位置插入此行。
"handlebars": "4.5.3",
从版本 4.6.0 开始,Handlebars 默认禁止访问上下文对象的原型属性和方法。这与此处描述的安全问题有关:https://mahmoudsec.blogspot.com/2019/04/handlebars-template-injection-and-rce.html https://mahmoudsec.blogspot.com/2019/04/handlebars-template-injection-and-rce.html
参考https://github.com/wycats/handlebars.js/issues/1642 https://github.com/wycats/handlebars.js/issues/1642
上面 URL 中的示例显示了 Mongoose 响应转换为 JSON 的情况:
app.get('/test', function (_req, res) {
Kitten.find({}).then(kittens => {
res.render('test.hbs', {
kittens: kittens.map(kitten => kitten.toJSON())
})
})
});
如果您确定只有受信任的开发人员并且没有最终用户有权访问车把模板,则可以通过安装以下软件包来允许原型访问:
npm install @handlebars/allow-prototype-access
这是它的使用示例:
const express = require('express');
const Handlebars = require('handlebars')
const expressHandlebars = require('express-handlebars');
const {allowInsecurePrototypeAccess} = require('@handlebars/allow-prototype-access')
const app = express();
app.engine('handlebars', expressHandlebars({
handlebars: allowInsecurePrototypeAccess(Handlebars)
}));
app.set('view engine', 'handlebars');
...
另一种选择是使用 mongoose .lean() 函数。这样做的好处是比传统的猫鼬查询快得多。但它也有一些缺点。默认情况下,Mongoose 查询返回 Mongoose Document 类的实例。这些对象包含大量用于更改跟踪的内部状态,并具有其他方法,例如 .save()。启用精简选项告诉 Mongoose 跳过实例化完整的 Mongoose 文档,只返回纯 JavaScript 对象。