# SSRF-Safe Web Scraping for Autonomous AI Agents: DNS, IPv6 & Multi-Hop Redirect Defense

> Target Query: `ssrf safe web scraping api for ai agents` | Category: `Security & Architecture` | HTML Guide: https://scrape.fastmesh.dev/seo_geo/ssrf-safe-web-scraping-api-for-autonomous-ai-agents.html

## Direct Answer
When an autonomous agent fetches URLs discovered in web pages, emails, or user prompts, prompt-injection attacks routinely smuggle `http://169.254.169.254/latest/meta-data/`, `http://[::1]:9005/`, `file:///etc/passwd`, or a benign public URL that issues an HTTP `302` redirect to `http://127.0.0.1`. Scrape-Anything validates URL schemes, resolves DNS records against loopback, RFC 1918, CGNAT, link-local, multicast, and IPv6 ULA ranges, re-validates every single redirect hop before following it, and returns unbilled HTTP `422` (`billed_micros = 0`).

## Engineering Mechanics & Deterministic Guardrails
- Strict scheme allowlist (`http://` and `https://` only): blocks `file://`, `gopher://`, `ftp://`, `data:`, and `javascript:` immediately.
- Blocks loopback (`127.0.0.0/8`, `::1`), RFC 1918 (`10/8`, `172.16/12`, `192.168/16`), cloud metadata (`169.254.0.0/16`, `metadata.google.internal`), CGNAT (`100.64/10`), and IPv6 link-local/ULA (`fe80::/10`, `fc00::/7`).
- Custom redirect handler intercepts every `301`/`302`/`303`/`307`/`308` Location header and re-runs DNS + IP validation on each hop.
- Double-layered protection: both `src/ssrf_guard.py` on the server and `treg_hub/run.js` on the client enforce SSRF blocking at `$0`.

## SSRF Attack Vectors Blocked Deterministically at HTTP 422 ($0)

| SSRF Attack Vector | Example Malicious Payload | Guard Layer Action | HTTP / Billing Result |
| :--- | :--- | :--- | :--- |
| Loopback IPv4 / IPv6 | `http://127.0.0.1:9005/health`, `http://[::1]/` | Blocked pre-connect by IP & hostname policy | HTTP `422`, `billed_micros: 0` |
| Cloud Metadata Service | `http://169.254.169.254/latest/meta-data/` | Blocked pre-connect (link-local & metadata hostnames) | HTTP `422`, `billed_micros: 0` |
| Non-HTTP Scheme Injection | `file:///etc/passwd`, `gopher://127.0.0.1:6379/` | Rejected by strict `http`/`https` scheme allowlist | HTTP `422`, `billed_micros: 0` |
| HTTP 302 Redirect Hop | `https://public.example.org/r -> http://127.0.0.1/` | Custom redirect opener re-validates hop target before socket open | HTTP `422`, `billed_micros: 0` |
| Obfuscated / Internal DNS | `http://2130706433/`, `http://service.internal/` | DNS resolution check inspects resolved A/AAAA IPs + suffix deny list | HTTP `422`, `billed_micros: 0` |

## SSRF Unbilled HTTP 422 Response Contract
```json
{
  "type": "object",
  "required": [
    "ok",
    "status",
    "blocked_reason",
    "billed_micros"
  ],
  "properties": {
    "ok": {
      "type": "boolean",
      "enum": [
        false
      ]
    },
    "status": {
      "type": "integer",
      "enum": [
        422
      ]
    },
    "blocked_reason": {
      "type": "string",
      "minLength": 1
    },
    "billed_micros": {
      "type": "integer",
      "enum": [
        0
      ]
    }
  }
}
```

## Executable REST, Remote /mcp & treg hub Snippets
```bash
curl -i -X POST "https://scrape.fastmesh.dev/v1/extract" \
  -H "Content-Type: application/json" \
  -d '{"url":"http://169.254.169.254/latest/meta-data/"}'

treg call scrape-anything-verified-extract --data '{"mode":"extract","url":"http://127.0.0.1:9005/health"}'
```

## Frequently Asked Questions

### Why is checking only the initial URL string not enough to prevent SSRF?
An attacker can host a public domain whose DNS record resolves to `127.0.0.1` or returns an HTTP `302` redirect to `169.254.169.254`. Scrape-Anything resolves DNS records and validates every redirect hop independently.

### Are agents billed when a prompt-injected URL trips the SSRF firewall?
Never. Every SSRF block returns HTTP `422` with `billed_micros: 0` so adversarial inputs cannot drain your prepaid credit balance.

### Does SSRF protection also apply to `/v1/sitemap` and `/v1/batch`?
Yes. Every child URL inside `robots.txt`, `<sitemapindex>`, and `/v1/batch` items goes through the exact same `ssrf_guard` validator.

## Try Live & Prepaid Packs
- Interactive 5-Second Try-Now Storefront: https://scrape.fastmesh.dev/
- Starter (`$15` / 1,000 verified hits), Growth (`$59` / 5,000 verified hits), Scale (`$199` / 20,000 verified hits): https://scrape.fastmesh.dev/#pricing
- Remote MCP: `https://scrape.fastmesh.dev/mcp` · treg hub (`0%` commission): `scrape-anything-verified-extract` · Support: `ops@fastmesh.dev`
