@ handlebars/allow-prototype-访问
将 Hanlebars- instance ^ 4. 6.0 恢复到 4.5. 3 的原始访问行为
此软件包允许您创建新的 Handlebars 实例, 其行为类似4.5.3 版本并允许访问 原型
为什么?
过去 Handlebars 会允许您访问输入对象的原型方法和属性 模板.
start{{aString.trim}}end
输入
{
aString: ' abc '
}
将会导致产出 startabcend。 。 。 .
这种行为产生了多起安全问题。 详情见npm-security咨询 755 (英语), (中文(简体) ), 1164号, (中文(简体) ), 1316号, (中文(简体) ), 第1324号 财务报告和审定财务报表 第1325号 在博客文章中 联 合 国 马哈茂德·加马尔。 。 。 .
这些问题已经解决,但我们不能确定有办法解决。 这就是为什么,在 handlebars@^4.6.0。 。 。 .
对对象原型的访问已被完全禁用.
现在,如果你使用自定义课作为 Handlebars 的输入, 你的代码将不再工作.
class TestClass {
aMethod() {
return "returnValue";
}
}
const Handlebars = require("handlebars");
const template = Handlebars.compile("start {{aMethod}} end");
const output = template(new TestClass());
console.log(output);
Handlebars: Access has been denied to resolve the property "aMethod" because it is not an "own property" of its parent.
You can add a runtime option to disable the check or this warning:
See https://handlebarsjs.com/api-reference/runtime-options.html#options-to-control-prototype-access for details
start end
这已经给项目带来了一些问题,例如: 类型doc因为他们在使用这种课程 作为模板的输入.
此软件包会自动为每个模板调用添加运行时间选项,使安全限制失效.
什么时候不用?
问题是:谁在写Handlebars执行的模板? 只有你的开发者吗? 是用户吗?
漏报发表在 npm 咨询 755 意味着有人写一个模板 可以在服务器内有效执行代码 。 虽然披露的开发活动已经固定,但新的开发活动已经 现在这一原则已广为人知,因此:
如果您的用户正在写入模板并在服务器上执行, 您不应该使用此软件包, 而是 寻找其他方法解决问题. 我建议你先把课堂转换成普通的JavaScript对象 把它们转到模板函数. 您访问的每一项财产或功能,都必须是其父母的“所有财产”.
class TestClass {
aMethod() {
return "returnValue";
}
asTemplateInput() {
return {
aMethod: this.aMethod.bind(this)
};
}
}
const Handlebars = require("handlebars");
const template = Handlebars.compile("{{aMethod}}");
const output = template(new TestClass().asTemplateInput());
console.log(output);
安装
npm install @handlebars/allow-prototype-access
用法( 平面管栏)
以下例子说明如何使用该模块:
class TestClass {
aMethod() {
return "returnValue";
}
}
const Handlebars = require('handlebars')
const {allowInsecurePrototypeAccess} = require('@handlebars/allow-prototype-access')
const insecureHandlebars = allowInsecurePrototypeAccess(Handlebars)
const template = insecureHandlebars.compile('{{aMethod}}')
const output = template(new TestClass);
console.log(output)
这将生成以下输出
returnValue
使用量(快手栏 财务报告和审定财务报表 鸽子(中文(简体) )
建议的解决办法
为这个星座建议的解决办法是: 没有 软件包,但使用 倾斜( ) -- 方法 {\fn黑体\fs22\bord1\shad0\3aHBE\4aH00\fscx67\fscy66\2cHFFFFFF\3cH808080}在莫克湖边. 见 https://mongoosejs.com/docs/tutorials/lean.html (中文(简体) ) 详细情况。 这将确保,所有传递到模板的属性都是“自有”属性
备选案文
express-handlebars 不允许您指定要通过的运行时间选项
到模板函数。 此软件包可以帮助您禁用原型检查
给你们的模特.
只有完全控制服务器中执行的模板时,才会这样做.
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');
...
许可证
@handlebars/allow-prototype-access 在麻省理工学院许可下出版.
见 利比里亚 详细情况.
贡献准则
见 捐款额。 。 。 .