返回提示词目录第 1432 页

排查并修复 JavaScript 原型污染

检查对象合并、路径设置和依赖中的污染入口,修复危险键处理并补充针对请求数据的安全测试。

完整提示词

<context>
Prototype pollution happens when code copies attacker-controlled keys into objects and a key is `__proto__`, `constructor` or `prototype`, so the write lands on `Object.prototype` and every object in the process inherits it. Typical sources are recursive merge and clone helpers, `set(obj, path, value)` style helpers with dotted or bracketed paths, query string parsers that build nested objects (`a[__proto__][isAdmin]=1`), and config loaders that merge user JSON. `JSON.parse` creates an own property named `__proto__`, which becomes dangerous only when a merge copies it later. Impact depends on gadgets: an `isAdmin` check that reads a missing property, options objects in template engines or `child_process.spawn` (which has led to remote code execution), and denial of service by overriding `toString`. Fixes: skip the three dangerous keys in every recursive copy; build lookup tables with `Object.create(null)` or `Map`; check `Object.hasOwn` instead of truthiness for security flags; validate input with a strict schema that forbids extra keys; update vulnerable dependencies (older lodash, minimist, qs, set-value and similar). `Object.freeze(Object.prototype)` or the Node `--disable-proto` flag can harden the process but may break dependencies, so they need testing.
</context>

<task>
Fix prototype pollution in {{target}}.

1. Find the sources: custom deep merge, clone, extend and path-set helpers, calls to dependency helpers that merge user data, nested query parsing settings, and anywhere request JSON is merged into defaults or config.
2. Check the dependency tree for packages with known prototype pollution advisories using the package manager's audit command, and note which are reachable from user input.
3. Fix each source: skip `__proto__`, `constructor` and `prototype` keys in every recursive copy, or replace the helper with a maintained safe one; build dictionaries from user keys with `Map` or `Object.create(null)`; validate request bodies with a schema that rejects unknown keys.
4. Fix the likely gadgets near the sources: security checks that read properties without `Object.hasOwn`, and options objects passed to spawn or template rendering built from merged data.
5. Upgrade vulnerable dependencies where a fixed version exists, within the project's version range policy; list any that need a major upgrade.
6. Add tests that send `{"__proto__": {"polluted": true}}`, `{"constructor": {"prototype": {"polluted": true}}}` and the query string form, then assert `({}).polluted` is undefined and clean up `Object.prototype` after each test.
7. Run the tests, the type check and the audit command again.
</task>

<constraints>
- Always restore `Object.prototype` in test teardown so a failure cannot leak into other tests.
- Do not freeze `Object.prototype` or add `--disable-proto` without running the full test suite and reporting the result.
- Do not upgrade dependencies across major versions without asking.
{{> guardrails/investigate-before-answering}}
{{> guardrails/scope-discipline}}
{{> guardrails/verify-before-done}}
</constraints>

<output_format>
## Pollution points
Table: file:line or package, kind (merge, path set, parser, config), input source, gadget nearby, fix chosen.
## Changes
One line per file or package: what changed.
## Tests
One line per test with its payload.
## Verification
Commands run and their actual results, including the audit output summary.
## Not fixed
Anything left, why, and the proposed follow-up.
</output_format>

可替换内容

  • {{target}}
  • {{> guardrails/investigate-before-answering}}
  • {{> guardrails/scope-discipline}}
  • {{> guardrails/verify-before-done}}
  • [__proto__]
  • [isAdmin]

分类与标签

编程开发、JavaScript 安全、漏洞排查、依赖审计

前往 AI 图片创作