mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-15 04:10:40 -07:00
docs(zh-cn): add security model docs for DoS limits
Add a Chinese security-model tutorial and link it from the Chinese DoS guide to clarify that memoryLimit is cooperative accounting, list uncounted custom conversion cases, and recommend avoiding fully user-defined templates in online services. Co-authored-by: Cursor <[email protected]>
This commit is contained in:
@@ -8,6 +8,8 @@ title: 防止 DoS 攻击
|
||||
|
||||
设置这些选项可以在很大程度上确保你的 LiquidJS 实例不会长时间挂起或消耗过多内存。这些限制基于可用的 JavaScript API,因此它们不是精确的硬性限制,而是确保你的进程不会失败或挂起的阈值。
|
||||
|
||||
关于安全边界和生产环境加固建议,请参见 [安全模型][security-model]。
|
||||
|
||||
```typescript
|
||||
const liquid = new Liquid({
|
||||
parseLimit: 1e8, // 每次渲染的模板的典型大小
|
||||
@@ -52,4 +54,5 @@ const liquid = new Liquid({
|
||||
[paralleljs]: https://www.npmjs.com/package/paralleljs
|
||||
[parseLimit]: /api/interfaces/LiquidOptions.html#parseLimit
|
||||
[renderLimit]: /api/interfaces/LiquidOptions.html#renderLimit
|
||||
[memoryLimit]: /api/interfaces/LiquidOptions.html#memoryLimit
|
||||
[memoryLimit]: /api/interfaces/LiquidOptions.html#memoryLimit
|
||||
[security-model]: /zh-cn/tutorials/security-model.html
|
||||
@@ -0,0 +1,30 @@
|
||||
---
|
||||
title: 安全模型
|
||||
---
|
||||
|
||||
LiquidJS 提供了面向 DoS 的限制选项(`parseLimit`、`renderLimit`、`memoryLimit`)来降低风险,但这些限制是协作式防护,不是严格的运行时隔离。
|
||||
|
||||
## `memoryLimit` 是协作式限制
|
||||
|
||||
`memoryLimit` 只会统计 LiquidJS 内部显式记账的内存敏感分配。它是针对模板滥用的“尽力而为”缓解机制,不是严格的堆内存上限。
|
||||
|
||||
- 它**不等于**进程的 RSS/heap 实际占用。
|
||||
- 它**不是** JavaScript 沙箱。
|
||||
- 在生产环境中应结合进程/容器资源限制和请求超时做分层防护。
|
||||
|
||||
## 它限制什么(以及不限制什么)
|
||||
|
||||
`memoryLimit` 只限制 LiquidJS 自己统计到的操作。
|
||||
|
||||
- 会被统计:LiquidJS 内部调用了内存记账逻辑的内存敏感操作。
|
||||
- 不保证被统计:任意用户对象行为(例如自定义 `toValue()` / `toString()` 链)以及其他发生在 LiquidJS 记账点之外的宿主侧分配。
|
||||
|
||||
换句话说,`memoryLimit` 限制的是 LiquidJS 的“已记账分配”,而不是进程里每一个字节的分配。
|
||||
|
||||
## 在线服务建议
|
||||
|
||||
如果你运行在线服务,建议尽量避免渲染完全由用户定义的模板。
|
||||
|
||||
- 优先使用受控模板或受限模板子集。
|
||||
- 如果必须支持用户自定义模板,请隔离渲染(worker/进程/容器),并同时配置操作系统或容器级的内存/CPU 限额与请求限流。
|
||||
- 将 `parseLimit` / `renderLimit` / `memoryLimit` 视为 DoS 防护体系中的一层,而不是唯一防线。
|
||||
Reference in New Issue
Block a user