# @handlebars/allow-prototype-access 中文文档

规范地址：https://npmcn.com/package/%40handlebars/allow-prototype-access/

版本：1\.0\.5
npm 元数据更新时间：2025-02-25T22:44:48.832Z
中文译文更新时间：2026-11-17T07:28:37.712Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40handlebars/allow-prototype-access
原文对照：https://npmcn.com/package/%40handlebars/allow-prototype-access/?lang=original
项目主页：https://github.com/handlebars-lang/allow-prototype-access#readme
源码仓库：https://github.com/handlebars-lang/allow-prototype-access

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：11.2 kB，6 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- 运行依赖：0 个
- peer 依赖：handlebars ^4\.7\.2
- 首次发布：2020-01-25T21:59:23.546Z
- 最新版本发布：2021-04-18T12:30:08.137Z
- 近 30 天下载量：32207（2026-10-17 至 2026-11-15，npm 官方下载统计）
- 已发布版本数：6（近 12 个月 0 个）

## 中文简介

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

## 安装

```sh
npm install @handlebars/allow-prototype-access
```

也可以使用：`pnpm add @handlebars/allow-prototype-access`、`yarn add @handlebars/allow-prototype-access`、`bun add @handlebars/allow-prototype-access`

## README

# @ handlebars/allow-prototype-访问

[![NPM version](https://img.shields.io/npm/v/@handlebars/allow-prototype-access.svg)](https://npmjs.com/package/@handlebars/allow-prototype-access)

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

此软件包允许您创建新的 Handlebars 实例, 其行为类似4.5.3 版本并允许访问 原型

## 为什么?

过去 Handlebars 会允许您访问输入对象的原型方法和属性 模板.

```hbs
start{{aString.trim}}end
```

输入

```
{
    aString: '   abc    '
}
```

将会导致产出 `startabcend`。 。 。 .

这种行为产生了多起安全问题。 详情见npm-security咨询 [755 (英语)](https://www.npmjs.com/advisories/755), (中文(简体) ), [1164号](https://www.npmjs.com/advisories/1164), (中文(简体) ), [1316号](https://www.npmjs.com/advisories/1316), (中文(简体) ), [第1324号](https://www.npmjs.com/advisories/1324) 财务报告和审定财务报表 [第1325号](https://www.npmjs.com/advisories/1325) 在博客文章中 联 合 国 [马哈茂德·加马尔](http://mahmoudsec.blogspot.com/2019/04/handlebars-template-injection-and-rce.html)。 。 。 .

这些问题已经解决,但我们不能确定有办法解决。 这就是为什么,在 `handlebars@^4.6.0`。 。 。 . 对对象原型的访问已被完全禁用.

现在,如果你使用自定义课作为 Handlebars 的输入, 你的代码将不再工作.

```js
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](https://npmjs.com/package/typedoc)因为他们在使用这种课程 作为模板的输入.

此软件包会自动为每个模板调用添加运行时间选项,使安全限制失效.

## 什么时候不用?

问题是:谁在写Handlebars执行的模板? 只有你的开发者吗? 是用户吗?

漏报发表在 [npm 咨询 755](https://www.npmjs.com/advisories/755) 意味着有人写一个模板 可以在服务器内有效执行代码 。 虽然披露的开发活动已经固定,但新的开发活动已经 现在这一原则已广为人知,因此:

**如果您的用户正在写入模板并在服务器上执行, 您不应该使用此软件包, 而是 寻找其他方法解决问题.** 我建议你先把课堂转换成普通的JavaScript对象 把它们转到模板函数. 您访问的每一项财产或功能,都必须是其父母的“所有财产”.

```js
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
```

# 用法( 平面管栏)

以下例子说明如何使用该模块:

```js
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
```

# 使用量([快手栏](https://npmjs.com/package/express-handlebars) 财务报告和审定财务报表 [鸽子](https://npmjs.com/package/mongoose)(中文(简体) )

## 建议的解决办法

为这个星座建议的解决办法是: **没有** 软件包,但使用 **倾斜( ) -- 方法** {\\fn黑体\\fs22\\bord1\\shad0\\3aHBE\\4aH00\\fscx67\\fscy66\\2cHFFFFFF\\3cH808080}在莫克湖边. 见 [https://mongoosejs.com/docs/tutorials/lean.html (中文(简体) )](https://mongoosejs.com/docs/tutorials/lean.html) 详细情况。 这将确保,所有传递到模板的属性都是“自有”属性

## 备选案文

`express-handlebars` 不允许您指定要通过的运行时间选项 到模板函数。 此软件包可以帮助您禁用原型检查 给你们的模特.

**只有完全控制服务器中执行的模板时,才会这样做.**

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

见 [利比里亚](https://npmcn.com/package/%40handlebars/allow-prototype-access/LICENSE.md) 详细情况.

# 贡献准则

见 [捐款额](https://npmcn.com/package/%40handlebars/allow-prototype-access/CONTRIBUTING.md)。 。 。 .

## 常见问题

### @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 和提交记录判断。

