返回提示词目录第 1306 页

排查修复服务端模板注入

追踪模板编译中的用户输入,修改危险渲染方式,补充回归测试并说明沙箱残余风险。

完整提示词

<context>
Server-side template injection happens when user input becomes part of the template source rather than data passed into it. The difference is one line: `render_template_string("Hello " + name)` compiles the name as Jinja, while a fixed template string that references a `name` variable, rendered with `name=name`, does not. Because most template engines can reach the host language's objects, SSTI usually means remote code execution, not just XSS: Jinja via object attributes, Freemarker via `?new()` and `freemarker.template.utility.Execute`, Velocity and Thymeleaf expressions via reflection, Twig via filters and callbacks, ERB directly. The risky features are the ones products ask for: user-editable email or invoice templates, "custom message" fields, white-label pages, and report builders. Escaping or blocklisting characters does not fix it; the engines have too many syntax paths. Real fixes, in order: never compile user input (pass it as a variable); if users truly must write templates, use a logic-less engine (Mustache or Liquid with strict settings) or the engine's sandbox (`jinja2.sandbox.SandboxedEnvironment`, Twig's sandbox extension with an allowlist policy, Freemarker's `TemplateClassResolver.ALLOWS_NOTHING_RESOLVER` and restricted object wrapper) plus limits on render time and output size, accepting that sandboxes have had escapes. Client-side template injection (Angular expressions in server-rendered HTML) is a related XSS issue.
</context>

<task>
Find and fix server-side template injection in {{scope}}.

1. Identify the template engines in use from dependencies and imports.
2. Search for template compilation from strings: `render_template_string`, `Template(`, `Environment().from_string`, `createTemplate`, `new Template(` with a string reader, `Velocity.evaluate`, `Handlebars.compile`, `ERB.new`, `twig->createTemplate`, and template names built from request input (which can load arbitrary templates).
3. Trace each call site back to its inputs and mark which ones carry user-controlled text, including stored values like a saved email template or a profile field.
4. Fix each real finding with the strongest option that keeps the feature: pass input as variables to a fixed template; otherwise move user-authored templates to a logic-less engine or a configured sandbox with an allowlist, render time and size limits, and no access to application objects beyond a small data dict.
5. Write a regression test per fixed site that sends a harmless engine-specific arithmetic probe (`7*7` wrapped in that engine's expression delimiters) and asserts it is rendered literally rather than as 49, plus a test that legitimate templates still render.
6. Run the tests and report results.
</task>

<constraints>
- Do not rely on escaping or blocklisting template syntax as the fix.
- Do not write or run payloads that execute commands, read files or reach the network; use arithmetic probes only.
- Treat stored values (saved templates, profile fields) as user input.
- Mark sandbox-based fixes as reduced risk, not eliminated, and say why.
{{> guardrails/investigate-before-answering}}
{{> guardrails/scope-discipline}}
{{> guardrails/verify-before-done}}
</constraints>

<output_format>
## Findings
Table: location, engine, input source, exploitable (yes, no, unclear), fix.
## Changes
One line per file: `path`, what changed.
## Tests
The regression tests and their real results.
## Residual risk
What remains, such as sandboxed user templates, and the follow-up to remove it.
</output_format>

可替换内容

  • {{scope}}
  • {{> guardrails/investigate-before-answering}}
  • {{> guardrails/scope-discipline}}
  • {{> guardrails/verify-before-done}}

分类与标签

编程开发、模板注入、安全代码审查、漏洞修复、回归测试

前往 AI 图片创作