Skip to content

Security: Harden tool web requests against SSRF vulnerabilities - #2749

Open
Bristlecone2026 wants to merge 5 commits into
huggingface:mainfrom
Bristlecone2026:fix/ssrf-guard-20260904221105
Open

Bristlecone2026 wants to merge 5 commits into
huggingface:mainfrom
Bristlecone2026:fix/ssrf-guard-20260904221105

Conversation

@Bristlecone2026

Copy link
Copy Markdown

Security Hardening: SSRF Mitigation in Tool Web Requests

Vulnerability Assessment

When autonomous LLM agents process arbitrary or model-generated URLs, un-sanitized HTTP fetches expose the runtime environment to Server-Side Request Forgery (SSRF). Malicious inputs or prompt injection can direct the agent to query:

  • http://169.254.169.254/latest/meta-data/ (Exposing cloud instance credentials / IMDS)
  • http://127.0.0.1:* or internal RFC 1918 subnets (Interacting with local microservices and databases)

Remediation

This PR adds a lightweight, zero-dependency is_safe_url() check using Python's standard socket and ipaddress libraries. It performs pre-flight DNS resolution before executing requests.get(), blocking access to non-routable, loopback, and private address space.


For runtime guardrails, deterministic JSON repair, and AST-sandboxed code execution for agent pipelines, see Bristlecone Guard.

@Bristlecone2026

Copy link
Copy Markdown
Author

Thanks @sattyamjain.
We have updated the implementation:

  1. Set allow_redirects=False and implemented manual redirect inspection via is_safe_url for every 30x hop (up to 5 hops), preventing redirect-based bypasses to cloud metadata or private IP ranges.
  2. Added test_visit_webpage_ssrf_blocks_redirect_to_restricted_ip asserting that malicious redirect hops abort immediately after hop 1 without fetching the restricted destination.
  3. The pre-flight inspection remains positioned outside the inner try: block so refusals return deterministically rather than being caught and transformed as generic network errors.

Let me know if you find anything additional or wrong, appreciate it.

@Bristlecone2026
Bristlecone2026 force-pushed the fix/ssrf-guard-20260904221105 branch from af8a281 to 277479b Compare October 8, 2026 15:32
@Bristlecone2026

Copy link
Copy Markdown
Author

Rebased onto latest main with clean ruff linting and formatting applied.

Security Enhancements:

  1. SSRF Redirect Traversal Defense: Configured allow_redirects=False in VisitWebpageTool to manually validate each hop's Location target against RFC 1918 and cloud metadata boundaries (169.254.169.254) prior to dispatch.
  2. Hop Capping: Capped recursive redirect evaluation to a maximum of 5 hops to mitigate redirect loops and denial-of-service vectors.
  3. Automated Test Coverage: Added unit test test_visit_webpage_ssrf_blocks_redirect_to_restricted_ip validating interception of AWS/cloud metadata redirection.

All documentation formatting churn reverted; isolated strictly to tool transport and unit test assertions.

@Bristlecone2026

Copy link
Copy Markdown
Author

Hi @maintainers — Re-synced the branch to clear the expired CI check.

This PR hardens VisitWebpageTool against SSRF, TOCTOU DNS rebinding, and intermediate 301/302 redirects targeting cloud metadata (169.254.169.254) or internal subnets.

Ready for maintainer workflow authorization and review when bandwidth permits.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant