AI Agents
·IdenticAPI

How to Validate URLs Before an AI Agent Opens Them

Validate URLs before AI agents fetch or open them — protocol restrictions, domain allowlists, redirect handling, and integration with action policies.

AI agents that browse the web, call webhooks, or invoke HTTP tools will eventually propose URLs your runtime must fetch or open. Validating URLs before execution means parsing and normalizing destinations, enforcing scheme and host policy, analyzing redirect chains, and gating the actual fetch behind deterministic rules — not trusting the model to "only visit safe sites."

This guide covers a production validation pipeline for agent architectures, integration with Agent Action Guard, and honest use of URL inspection for destination facts.

Why model-proposed URLs are untrusted

Tool-calling agents output strings like https://docs.vendor.com/guide that may be:

  • Malformed or obfuscated (punycode, userinfo smuggling)
  • Pointing at internal services (SSRF via http://169.254.169.254/)
  • Short links that redirect to disallowed domains
  • javascript: or file: schemes inappropriate for server fetchers

Prompt injection on web pages can steer the model toward hostile destinations in the next tool call. Validation belongs in your orchestrator, synchronously before network I/O — see Validate AI Tool Calls and Secure AI Agent Web Browsing.

Validation pipeline overview

Model proposes URL (browse, http.get, webhook, etc.)
    ↓
1. Parse + normalize
    ↓
2. Static policy (scheme, host, path rules)
    ↓
3. Optional: URL Inspector (status, redirects, final_url)
    ↓
4. Agent Action Guard (tool + action + args + context)
    ↓
5. Sandboxed fetcher (size limits, content isolation)

Stages 1–2 are cheap and local. Stage 3 adds network facts. Stage 4 evaluates semantic policy. Stage 5 executes only after allow.

Stage 1: Parse and normalize

Reject invalid URLs before policy:

function parseAgentUrl(raw: string): URL {
  const trimmed = raw.trim();
  const url = new URL(trimmed); // throws on invalid
  if (url.username || url.password) {
    throw new Error("Credentials in URL forbidden");
  }
  return url;
}

Normalize host to lowercase, decode punycode for display and comparison, reject ambiguous Unicode homographs per tenant policy.

Stage 2: Static policy checks

Apply deterministic rules before any outbound request:

RuleRationale
HTTPS only in productionPrevent cleartext and scheme confusion
Block file://, javascript:, data:Non-HTTP agent fetches
Block RFC1918, link-local, metadata IPSSRF prevention
Block localhost unless dev environmentLocal service access
Path restrictions when using URL allowlistsSee URL vs domain allowlist

DNS rebinding-sensitive deployments resolve hostname and verify IP before connect.

Stage 3: URL inspection (redirect-aware)

Static parsing is insufficient when redirects change destination. Call URL Inspector or equivalent to obtain:

  • HTTP status
  • redirects[] chain
  • final_url after hops
  • Title/metadata for logging (not trust)

Compare final_url host and path against allowlist — not only the model's initial string.

curl -G "https://url-inspector.p.rapidapi.com/inspect" \
  --data-urlencode "url=https://t.co/example" \
  -H "X-RapidAPI-Key: YOUR_RAPIDAPI_KEY" \
  -H "X-RapidAPI-Host: url-inspector.p.rapidapi.com"

Inspection reports HTTP behavior — not malware or phishing verdicts. Use results as policy inputs: block on excessive redirects, unexpected final domain, or non-200 when your tool requires reachable content.

Related: Detect Suspicious URLs Programmatically.

Stage 4: Agent Action Guard integration

Agent Action Guard evaluates proposed tool actions with allow, review, or block decisions. Your runtime must enforce the decision — the API advises; it does not block remotely.

Submit browse and HTTP tool proposals before execution:

curl -X POST https://www.identicapi.com/api/v1/security/agent-action \
  -H "Authorization: Bearer idapi_test_your_key_here" \
  -H "Content-Type: application/json" \
  -d '{
    "tool_name": "browser",
    "action": "navigate",
    "arguments": { "url": "https://docs.example.com/page" },
    "context": "User requested documentation lookup; URL pre-inspected: status 200, final_url matches"
  }'

Combine guard with inspection context:

  • Include final_url and redirect count in context string
  • Escalate to review when final host not in session allowlist
  • Block http.post, webhook.call, and email.send to URLs failing validation

Default policy blocks destructive patterns and secrets in combined context; customize via Agent Action Guard documentation.

Align with agent tool allowlist vs blocklist and least privilege for agents.

Stage 5: Sandboxed fetch execution

After allow, fetch with:

  • Response size caps
  • Content-Type allowlist
  • Timeout bounds
  • No automatic credential forwarding
  • Isolated network egress from agent runtime

Treat fetched content as untrusted data — scan for injection before re-prompting; gate follow-on tools after browse per tool output injection.

Redirect handling policy

Define explicit redirect rules:

  • Maximum hops (e.g., 10)
  • Re-run allowlist on each hop or only on final URL (document choice)
  • Block cross-scheme redirects (https → http)
  • Block redirects to IP literals if hostnames required

Log full chain for security review when final domain differs from proposed domain.

Failure modes and UX

FailureAgent behavior
Parse errorReject; ask model to correct URL
SSRF blockReject; log policy violation
Inspection timeoutFail closed or review per runbook
Guard blockDo not fetch; return policy message
Guard reviewQueue for human or elevated session

Document fail-open vs fail-closed for inspection outages — many security teams prefer review or block over unvalidated fetch.

Testing

CI fixtures should include:

  • Allowlisted https URL with 200
  • Redirect from allowed shortener to allowed final domain
  • Redirect to blocked domain
  • Internal IP literal rejection
  • javascript: scheme rejection
  • Guard block on post-browse exfil attempt

See AI Agent Security Checklist.

Observability

Log structured fields:

  • proposed_url, final_url, redirect_count, http_status
  • guard_decision, matched_rules
  • session_id, tool_name, action

Avoid logging full page bodies in default telemetry.

Validate URLs in deterministic code before agents open them. Combine parsing, redirect-aware inspection, and Agent Action Guard so model intent cannot become unsafe network behavior without passing policy gates.

Frequently asked questions

When should AI agents validate URLs?

Synchronously before any browse, HTTP, or webhook tool executes — after the model proposes a destination but before your runtime performs network I/O. Never rely on the model to self-police destinations.

How does Agent Action Guard fit URL validation?

Submit proposed tool calls to Agent Action Guard with inspection context such as final_url and redirect count. Enforce allow, review, or block in your orchestrator — the API advises but does not block remotely.

Why must allowlists check final_url after redirects?

Short links and open redirects can change the destination host after the initial URL passes a naive check. URL inspection reveals the redirect chain and final resolved URL for policy evaluation.

Does URL validation replace SSRF prevention?

No. Parse and block disallowed schemes, private IP ranges, and metadata endpoints locally before inspection. URL validation complements SSRF controls; it does not replace them.

What should happen when inspection times out?

Define an explicit runbook: fail closed, block fetch, or route to review. Document the choice and test outage behavior in staging.

Related reading