@handlebars/allow-prototype-access

中文文档
1.0.5发布于 2021/04/18MITTypeScriptESM + CJS零依赖近 30 天下载 3.2万

将 Hanlebars- instance ^ 4. 6.0 恢复到 4.5. 3 的原始访问行为

中文使用文档
文档目录4 节
  1. 为什么?
  2. 什么时候不用?
  3. 建议的解决办法
  4. 备选案文

@ handlebars/allow-prototype-访问

NPM version

将 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 在麻省理工学院许可下出版.

见 利比里亚 详细情况.

贡献准则

见 捐款额。 。 。 .

版本与发布记录

@handlebars/allow-prototype-access 共发布 6 个版本,首次发布于 2020/01/26;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.5latest2021/04/185 年前
1.0.42021/04/185 年前
1.0.32020/02/056 年前
1.0.22020/02/056 年前
1.0.12020/01/266 年前
1.0.02020/01/266 年前

关于 @handlebars/allow-prototype-access 的常见问题

@handlebars/allow-prototype-access 是什么?

将 Hanlebars- instance ^ 4. 6.0 恢复到 4.5. 3 的原始访问行为

如何安装 @handlebars/allow-prototype-access?

在项目目录运行 npm install @handlebars/allow-prototype-access,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:handlebars(^4.7.2)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@handlebars/allow-prototype-access 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@handlebars/allow-prototype-access 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@handlebars/allow-prototype-access 可以商用吗?

@handlebars/allow-prototype-access 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@handlebars/allow-prototype-access 的下载量有多少?

根据 npm 官方下载统计,@handlebars/allow-prototype-access 在 2026/10/17 – 2026/11/15 这 30 天内被下载 32,207 次,日均约 1,074 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@handlebars/allow-prototype-access 还在维护吗?

最新版本 1.0.5 发布于 2021/04/18(5 年前);至今共发布 6 个版本,其中过去 12 个月发布了 0 个;首次发布于 2020/01/26。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。