返回提示词目录第 1314 页

修复服务端请求伪造漏洞并测试

审查用户可控地址的服务端请求,实施地址校验与重定向防护,并补充安全回归测试。

完整提示词

<context>
Any server feature that fetches a URL a user chose can be turned against the network behind it: cloud metadata endpoints (`169.254.169.254`, `fd00:ec2::254`, `metadata.google.internal`) hand out credentials, and internal admin services trust the server's address. Checking the hostname string does not work. Attackers use redirects to internal addresses, DNS names that resolve to private IPs, DNS rebinding (a name that resolves to a public IP at check time and a private one at fetch time), and alternate IP spellings (decimal `2130706433`, octal, IPv6-mapped `::ffff:127.0.0.1`, `0.0.0.0`). The reliable fix resolves the name once, checks every resolved address against blocked ranges, connects to that exact address, and repeats the check on every redirect hop. The strongest layer is network-level: run fetches from an egress proxy or isolated worker that cannot reach internal ranges, and require IMDSv2 or the provider's equivalent so metadata needs a header the attacker cannot set.
</context>

<task>
Fix SSRF in {{feature}}.
{{#stack}}Stack: {{stack}}{{/stack}}

1. Find every place the server fetches a URL or connects to a host that came from users, tenants or stored data: webhooks, link previews, image or file import by URL, PDF or screenshot renderers, OAuth or OpenID discovery URLs, XML or SVG processors that load external resources, and integrations with a configurable base URL.
2. Decide per fetch point whether an allowlist of hosts is possible (integrations with known providers) or the feature must reach the open internet (webhooks, previews).
3. Implement one shared safe-fetch helper used by every fetch point:
   - allow only `http` and `https`, and only standard ports unless the feature needs others;
   - resolve the hostname, reject if any address is loopback, private, link-local, carrier-grade NAT, multicast, unspecified, IPv6 unique-local or mapped forms of these, or a metadata address;
   - connect to the checked IP (pin it in the HTTP client) so a second DNS lookup cannot change it;
   - follow redirects manually, re-running the checks on each hop, with a small hop limit;
   - set connect and total timeouts and a response size cap, and do not return raw error bodies or internal timing to the user.
4. Recommend the network layer for {{feature}}: an egress proxy or isolated worker, and metadata protection (IMDSv2 with hop limit 1, or the provider's equivalent).
5. Write tests for the helper: private and metadata IPs, alternate IP encodings, a public name that resolves to a private IP, a redirect to an internal address, a non-HTTP scheme, and an oversized response. Use a stub resolver and a local server; no real external calls.
6. Run the tests and the linter.
</task>

<constraints>
- Never rely on string matching of the hostname or a blocklist of hostnames.
- Do not test against real cloud metadata endpoints or other people's systems.
- Keep legitimate destinations working; list any customer-visible change, such as webhooks to private IPs no longer being allowed.
- Do not change cloud or network configuration yourself; describe it.
{{> guardrails/investigate-before-answering}}
{{> guardrails/verify-before-done}}
</constraints>

<output_format>
## Fetch points
Table: file:line, feature, who controls the URL, allowlist possible (yes or no), status.
## Design
The safe-fetch rules chosen and why, in a short list.
## Changes
One line per file: `path`, what changed.
## Tests
One line per test.
## Verification
Commands run and their actual results.
## Residual risk
What code alone cannot stop, and the network or cloud controls that would.
</output_format>

可替换内容

  • {{feature}}
  • {{#stack}}
  • {{stack}}
  • {{/stack}}
  • {{> guardrails/investigate-before-answering}}
  • {{> guardrails/verify-before-done}}

分类与标签

编程开发、服务端请求伪造、安全代码修复、地址校验、重定向防护、安全回归测试

前往 AI 图片创作