Prevent SSRF in AI Agent Web Tools: Build URL Allowlists, DNS Checks, and Safe Fetch Proxies
Quick Answer: To prevent SSRF in an AI agent web tool, do not let the agent hand your server an arbitrary URL. Put a policy boundary in front of every fetch, allow only exact hosts and schemes, resolve DNS once and reject nonpublic addresses, then open the connection from an isolated proxy using the pinned IP. Deny redirects, log every decision, and fail closed when any check is uncertain.
AI agents that can browse the web turn a text prompt into a real network request, which is why ai agent security threats such as server-side request forgery (SSRF) deserve a dedicated design review. The model did not break your network; your fetch tool did exactly what it was told. If the agent asks for http://169.254.169.254/latest/meta-data/, a naive tool will happily read cloud credentials and feed them back into the conversation. This guide answers a practical search intent: how do I build URL allowlists, DNS checks, and a safe fetch proxy that hold up under real agent behavior?
What You Will Learn
- Where SSRF sits in the wider agent attack surface, beyond the model itself
- How to design an authorization boundary before any provider or internal service is reached
- How to implement an exact URL allowlist, a rebinding-resistant DNS check, and a pinned fetch proxy
- How to test, monitor, govern, and document the controls without promising compliance outcomes
Contents
- Why ai agent security threats start with a single URL
- Map the attack surface behind ai agent security threats
- Reference ai agent security architecture attack surface and defense
- Step 1: Build a strict URL allowlist
- Step 2: Add DNS checks that survive rebinding
- Step 3: Route fetches through a safe proxy
- Step 4: Apply ai agent security best practices at the boundary
- Test and monitor the controls
- Governance, evidence, and regulated environments
- Where ai agent security tools and governed gateways fit
- Conclusion
- FAQ
- Further Reading
Why ai agent security threats start with a single URL
An SSRF flaw in an agent tool is simpler than most people expect. The agent produces a URL, the application fetches it server-side, and the server carries privileges the agent never should have had. That gap is the whole bug. A reasoning model can be perfectly aligned and still produce an attacker-supplied link, because the link arrived through retrieved content, a polluted memory entry, or a tool result from a compromised connector.
The classic targets are worth naming. Loopback addresses expose local admin panels. Link-local 169.254.169.254 exposes cloud instance metadata. Private ranges reach internal APIs that assume they are unreachable from the internet. IPv6 equivalents and decimal IP encodings reach the same places with different syntax. A redirect makes it worse: a public URL can answer with 302 and point the fetcher at an internal host after your first check already passed.
Treat this as an input-validation and authorization problem, not a prompt problem. Better prompting reduces bad suggestions; it does not stop a tool from opening a socket. The selected search intent behind this guide is hands-on prevention: concrete checks you can place in code and configuration today.
The next step is to see where those trust decisions actually live in an agent system.
Map the attack surface behind ai agent security threats
An agent is not one process with one trust level. It is a chain of components, each of which can influence the next. Mapping that chain tells you where a fetch decision can be corrupted.
| Surface | What can go wrong | Relevant control |
|---|---|---|
| Identity | Agent acts with a shared service credential | Distinct agent identity tied to the requesting human |
| Prompts | Indirect prompt injection in fetched page asks for a new URL | Treat retrieved text as data, not instructions |
| Memory | Poisoned long-term memory reuses an old malicious link | Provenance tags and expiry on stored URLs |
| Tools | Web tool accepts arbitrary scheme, host, port, or redirect | Exact allowlist and per-tool capability scope |
| Connectors | Third-party connector forwards a crafted target | Connector-level authorization before invocation |
| Credentials | API keys live inside the agent runtime | Delegated, just-in-time grants held outside runtime |
| Data | Tool output writes secret text back into context | Output scanning and result labels |
| Execution | Multi-step plan chains a safe step into an unsafe one | Approval checkpoint between planning and side effects |
Multi-step execution deserves special attention. An agent may fetch a public page, read an instruction embedded in that page, then call a second tool with a new URL. Each individual call might look reasonable in isolation. The policy engine needs request context, including who asked and which plan step produced the call, to judge the whole chain.
A useful mental model is that every tool call crosses a trust boundary. Boundaries need a gate. The following architecture puts that gate in one place instead of scattering checks across every tool wrapper.
Reference ai agent security architecture attack surface and defense
The pattern below is a governed gateway: a single enforcement point between the agent and everything it wants to reach. The important property is ordering. Identity and policy are resolved before credentials are issued and before any provider, connector, or internal service sees the request.
Human user
|
| request + consent
v
Agent runtime ---- tool call (URL, intent, trace) ----> Policy gateway
| |
| receives protected result | allow / deny / approve
| v
| Credential broker (JIT)
| |
| v
| Fetch proxy -> approved target
| (DNS pin, egress isolation)
| |
+------------------- labeled result + evidence <--------+
Three rules make this work. First, the agent never holds long-lived secrets. Second, the policy gateway sees the full call context, not just the URL string. Third, the fetch proxy is the only component with network egress, and it reuses the decision made upstream.
gateway:
default_decision: deny
require_authenticated_caller: true
agent_identity: bound_to_requesting_human
decision_order: [identity, policy, approval, credential, fetch]
fetch_proxy:
egress_network: isolated-subnet
revalidate_redirects: true
evidence: [request_hash, decision_id, policy_version, trace_id]
This is a reference model, not a product requirement. You can build it with a reverse proxy, a small service, or an existing API gateway. What matters is that the boundary exists and that nothing bypasses it.
That boundary only works if the first check, the URL itself, is strict.
Step 1: Build a strict URL allowlist
An allowlist is a list of exact values, not a pattern-matching exercise. Prefer full host equality over wildcards, allow one scheme, and pin the port. Reject userinfo, because https://[email protected] parses with api.example.com as userinfo and evil.example as the real host.
from urllib.parse import urlsplit
ALLOWED_HOSTS = {"api.example.com", "docs.example.com"}
ALLOWED_SCHEMES = {"https"}
ALLOWED_PORTS = {443}
def check_url_allowlist(url: str) -> tuple[str, int, str]:
parts = urlsplit(url)
if parts.scheme not in ALLOWED_SCHEMES:
raise ValueError("scheme_not_allowed")
if parts.username is not None or parts.password is not None:
raise ValueError("userinfo_not_allowed")
host = (parts.hostname or "").lower().rstrip(".")
if host not in ALLOWED_HOSTS:
raise ValueError("host_not_allowlisted")
port = parts.port or 443
if port not in ALLOWED_PORTS:
raise ValueError("port_not_allowed")
return host, port, parts.path or "/"
Three details matter in production. Normalize the host with a trailing-dot strip and lowercase comparison. Use the parser’s hostname and port properties, since they handle bracketed IPv6 and invalid ports more safely than string splitting. Return the parsed path so downstream code cannot reinterpret a raw URL differently.
A real-world pattern is to make the allowlist per tool, not global. A docs lookup tool gets docs.example.com, while a billing tool gets api.example.com. That limits the damage when one connector is compromised.
A perfect allowlist still trusts DNS, which an attacker can manipulate after approval.
Step 2: Add DNS checks that survive rebinding
DNS rebinding beats a one-time validation. The attacker sets a short TTL, returns a public address when your validator asks, then returns 127.0.0.1 when the fetcher asks. The fix is to resolve once, check every answer, and connect to the address you checked.
import ipaddress
import socket
BLOCKED_NETS = [
ipaddress.ip_network(n) for n in (
"0.0.0.0/8", "10.0.0.0/8", "100.64.0.0/10", "127.0.0.0/8",
"169.254.0.0/16", "172.16.0.0/12", "192.0.0.0/24",
"192.168.0.0/16", "198.18.0.0/15", "224.0.0.0/4",
"240.0.0.0/4", "::1/128", "fc00::/7", "fe80::/10",
)
]
def is_public_address(raw_ip: str) -> bool:
addr = ipaddress.ip_address(raw_ip)
if addr.is_multicast or addr.is_reserved or addr.is_unspecified:
return False
return not any(addr in net for net in BLOCKED_NETS)
def resolve_and_pin(host: str, port: int) -> str:
answers = socket.getaddrinfo(host, port, type=socket.SOCK_STREAM)
ips = {info[4][0] for info in answers}
if not ips or not all(is_public_address(ip) for ip in ips):
raise ValueError("dns_check_failed")
return sorted(ips)[0]
Requiring every answer to be public is stricter than most implementations, and that is deliberate. One private answer among ten is enough to create a path an attacker can steer.
Two more rules complete this step. Disable automatic redirect following and send every redirect target back through the allowlist and DNS check. Keep the resolution result for the lifetime of the connection; a second lookup at connect time reopens the rebinding window.
Even validated DNS does not help if the agent runtime can open its own sockets.
Step 3: Route fetches through a safe proxy
The proxy is the only component allowed to make outbound connections. The agent submits a decision token, not a raw URL. The proxy verifies the token, connects to the pinned address, and sets TLS SNI and the Host header to the original hostname so certificate validation still works.
def fetch_with_pinned_ip(host: str, port: int, path: str, pinned_ip: str):
if not is_public_address(pinned_ip):
raise ValueError("pin_failed")
# Simplified: a real implementation pins the TCP destination to pinned_ip
# while validating the TLS certificate against host, then sets Host: host.
return open_https_connection(
destination_ip=pinned_ip,
tls_server_name=host,
host_header=host,
request_path=path,
max_redirects=0,
)
The proxy runs in its own network segment with no route to internal services apart from explicitly approved targets. Egress firewall rules should deny cloud metadata ranges by default, so a bug in application code does not become a credential leak.
fetch_proxy:
mode: decision-token-only
allow_self_service_urls: false
dns:
resolve_once: true
require_all_answers_public: true
redirects:
follow: false
revalidate_each_hop: true
network:
egress: isolated-subnet
deny_ranges: [loopback, link_local, rfc1918, ipv6_ula]
failure_mode: closed
Test the failure mode explicitly. When the DNS resolver times out, the policy service is unreachable, or the decision token is malformed, the proxy must return a denial. A fetch tool that falls back to direct access when the gateway is down defeats the entire design.
The proxy is the enforcement point, but the rules around it are what make it safe.
Step 4: Apply ai agent security best practices at the boundary
The SSRF controls above are necessary and insufficient on their own. Ai agent security best practices treat every tool call as an authorization request with a human owner, not as a function call with a string argument.
| Control | Practical implementation |
|---|---|
| Least privilege | Each tool exposes one capability, with its own host list and method list |
| Delegated identity | Agent acts under a subject bound to the requesting human, with scoped grants |
| Just-in-time access | Credentials are issued for one decision and expire quickly |
| Approval checkpoints | Risky targets require scoped human approval before fetch |
| Input protection | Treat retrieved pages as data; strip instructions from tool output where possible |
| Output protection | Label untrusted content and scan responses for secrets before adding them to context |
| Fail closed | Missing policy, resolver, or context means deny, never retry directly |
Approval deserves a concrete example. A request to fetch a new, previously unseen domain should pause the plan and ask the human for a one-time grant scoped to that exact host and action. A request to fetch 169.254.169.254 should never reach an approval prompt; it is denied by policy. Approval is for ambiguity, not for bypassing hard rules.
Credential placement is equally important. A broker issues a short-lived token after the policy decision, and the agent receives only the protected result. Keeping credentials outside the agent runtime means a prompt injection cannot ask the model to print them.
Rules need proof they ran, which is where testing and monitoring come in.
Test and monitor the controls
Build a small negative-test suite and run it on every change to the policy code. Use a local target that stands in for internal services, then confirm each case returns a denial and an evidence record.
for target in \
"http://169.254.169.254/latest/meta-data/" \
"http://127.0.0.1:8080/admin" \
"https://[email protected]/x" \
"https://docs.example.com:8443/x" \
"http://[::1]/admin"; do
code=$(curl -sS -o /dev/null -w '%{http_code}' \
-H 'Content-Type: application/json' \
-d "{\"url\":\"$target\"}" \
http://localhost:8080/fetch)
printf '%s -> %s\n' "$target" "$code"
done
Expected result: every line returns 403, and every 403 produces a log entry. Also test the positive path, a redirect to a private address, and a hostname whose DNS answer flips between validation and connect.
{"event":"fetch_denied","reason":"host_not_allowlisted","requested_host":"169.254.169.254","agent_id":"agent-7","policy_id":"web-fetch-v3","policy_version":4,"trace_id":"9c1f"}
Monitor for repeated denials from one agent identity, spikes in new-host requests, and any approval that is granted faster than a human could plausibly read it. Those signals often show up before a successful exploit does.
Logs are useful during an incident only if governance defines what to keep and who can use it.
Governance, evidence, and regulated environments
An evidence trail should connect the requesting human, the agent identity, the policy decision, the credentials issued, and the outcome. Store decision IDs and policy versions with each tool call so you can reconstruct what the system believed at the time. Make revocation a normal operation: withdrawn delegated authority should take effect on the next request without redeploying the agent.
Incident response for an agent SSRF attempt follows a familiar shape with one addition. Preserve the full prompt and tool trace, including retrieved content, because the instruction that triggered the call may live in a page the agent read earlier. Then check whether any protected result entered model context, and whether downstream actions used it.
In regulated environments, these controls can support your own control mapping, for example around access control, data minimization, and audit logging. They do not satisfy a regulation or grant a certification by themselves. Requirements differ by jurisdiction and sector, so check current primary sources and involve your compliance team before making any claim externally.
Governance defines the authority model; the tooling market offers different ways to implement it.
Where ai agent security tools and governed gateways fit
Most ai agent security tools fall into three groups: policy engines that decide whether a call proceeds, gateways or proxies that enforce the decision at runtime, and observability layers that record what happened. You can assemble these from open components or buy them as a platform. The pattern matters more than the packaging.
Axec.dev is one example described in its product brief as a governed gateway for AI access. The brief lists a distinct agent identity bound to the requesting human, delegated OAuth with exact resource-bound grants, policy decisions that allow, deny, or require scoped human approval, just-in-time credentials kept outside the agent runtime, MCP/API/connector boundaries that expose only approved capabilities, protected results with a linked evidence trail, and revocation of delegated authority without redeploying the agent. Those are vendor-stated capabilities; verify them against current primary sources before relying on them. The transferable idea is the ordering: identity, policy, approval, credential, then fetch. Any implementation that follows that order is heading in the right direction.
Putting these pieces together brings the discussion back to the original question.
Conclusion
SSRF is one of the most concrete ai agent security threats because it converts a harmless-looking URL into a privileged server-side request. The defense is layered and boring by design: exact URL allowlists, single-pass DNS validation with public-address checks, a pinned isolated fetch proxy, scoped human approval, short-lived delegated credentials, and fail-closed behavior when any check is missing. None of these steps requires a smarter model. They require a boundary that the agent cannot step around.
If you are designing or reviewing this boundary in your own stack, I am happy to compare notes. You can book a 30-minute architecture discussion here: https://cal.id/axec/demo?duration=30
FAQ
What is SSRF in an AI agent web tool?
It happens when an agent-supplied URL makes your server request an internal or cloud metadata address. The server has privileges the agent should not have.
How does a URL allowlist reduce ai agent security threats?
It replaces arbitrary URLs with an exact list of approved hosts, schemes, and ports. Anything else is denied before a connection is attempted.
Why is DNS validation not enough on its own?
A hostname can resolve to a public IP during validation and a private IP during the request. Resolve once, reject nonpublic answers, and connect to the pinned address.
Which ai agent security best practices matter most here?
Use a distinct agent identity, least-privilege delegated grants, just-in-time credentials, approval checkpoints, output checks, and fail-closed defaults.
Where do ai agent security tools fit?
Put enforcement in a gateway or proxy between the agent and the target, so policy, credentials, and evidence stay outside the agent runtime.