Skip to content

Security hardening for Agent and MCP subsystems - #997

Merged
threepointone merged 2 commits into
mainfrom
s-fixes
Feb 26, 2026
Merged

Security hardening for Agent and MCP subsystems#997
threepointone merged 2 commits into
mainfrom
s-fixes

Conversation

@threepointone

Copy link
Copy Markdown
Contributor

Summary

Security hardening for the Agent class and MCP subsystem, addressing findings from a security review.

Changes

SSRF protection (src/mcp/client.ts)

  • Added isBlockedUrl() validation to registerServer() and connect()
  • Blocks RFC 1918, loopback, link-local, cloud metadata endpoints, IPv6 unique local (fc00::/7) and link-local (fe80::/10) ranges
  • Malformed URLs are fail-closed (blocked)

OAuth log redaction (src/mcp/do-oauth-client-provider.ts)

  • Removed OAuth state parameter value from consumeState warning log to prevent sensitive data leakage

Error sanitization (src/index.ts)

  • Added sanitizeErrorString() — strips C0 control characters and truncates to 500 chars
  • Applied in getMcpServers() before broadcasting connectionError to clients, mitigating XSS risk

Deprecation warnings (non-breaking, prep for next major)

  • sendIdentityOnConnect: one-time console.warn when default (true) is used without explicit opt-in (src/index.ts)
  • CORS Authorization header: one-time console.warn when Authorization is in allowed headers with wildcard origin (src/mcp/worker-transport.ts, src/mcp/utils.ts)

Tests

  • 11 new SSRF tests covering all blocked IP ranges + positive cases (client-manager.test.ts)
  • 3 new IPv6 private range tests (client-manager.test.ts)
  • 1 new CORS test for explicit non-wildcard origin (worker-transport.test.ts)
  • All 239 existing tests pass across 26 test files

Non-breaking

All changes are backward-compatible. The two items that will become breaking in the next major version (identity default, CORS Authorization default) are behind deprecation warnings only.

Add protections and warnings to MCP/Agent subsystems to reduce SSRF, XSS, and sensitive-data leakage. Key changes:

- Block MCP client connections to private/internal addresses (RFC1918, loopback, link-local, IPv6 unique-local, cloud metadata endpoints) via URL validation in client and registerServer.
- Sanitize MCP error strings before broadcasting (strip control characters and truncate to 500 chars).
- Remove OAuth state value from consumeState warning logs to avoid logging sensitive tokens.
- Add one-time deprecation warnings: sendIdentityOnConnect default will change in next major; warn when CORS allows Authorization with a wildcard origin.
- Adjust CORS helpers/worker transport to respect explicit origin/headers and emit the new warning when applicable.
- Add unit tests covering SSRF URL validation and CORS header behavior.

These changes aim to mitigate SSRF/XSS risks and reduce accidental exposure of sensitive OAuth state in logs.
@changeset-bot

changeset-bot Bot commented Feb 26, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: c5170bf

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
agents Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new

pkg-pr-new Bot commented Feb 26, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/cloudflare/agents@997
npm i https://pkg.pr.new/cloudflare/agents/@cloudflare/ai-chat@997
npm i https://pkg.pr.new/cloudflare/agents/@cloudflare/codemode@997
npm i https://pkg.pr.new/cloudflare/agents/hono-agents@997

commit: c5170bf

Fix isBlockedUrl IPv6 checks to handle hostnames that include brackets (e.g. "[fc00::1]"). The code now detects bracketed IPv6 hostnames, strips the brackets, lowercases the address into `addr`, and correctly checks unique-local (fc00::/7) and link-local (fe80::/10) prefixes. Also updates related comments for clarity.
@threepointone
threepointone merged commit a570ea5 into main Feb 26, 2026
4 checks passed
@threepointone
threepointone deleted the s-fixes branch February 26, 2026 08:06
@github-actions github-actions Bot mentioned this pull request Feb 26, 2026
dmmulroy pushed a commit that referenced this pull request Feb 26, 2026
* Security hardening: SSRF, sanitization, CORS

Add protections and warnings to MCP/Agent subsystems to reduce SSRF, XSS, and sensitive-data leakage. Key changes:

- Block MCP client connections to private/internal addresses (RFC1918, loopback, link-local, IPv6 unique-local, cloud metadata endpoints) via URL validation in client and registerServer.
- Sanitize MCP error strings before broadcasting (strip control characters and truncate to 500 chars).
- Remove OAuth state value from consumeState warning logs to avoid logging sensitive tokens.
- Add one-time deprecation warnings: sendIdentityOnConnect default will change in next major; warn when CORS allows Authorization with a wildcard origin.
- Adjust CORS helpers/worker transport to respect explicit origin/headers and emit the new warning when applicable.
- Add unit tests covering SSRF URL validation and CORS header behavior.

These changes aim to mitigate SSRF/XSS risks and reduce accidental exposure of sensitive OAuth state in logs.

* Handle IPv6 hostnames with brackets

Fix isBlockedUrl IPv6 checks to handle hostnames that include brackets (e.g. "[fc00::1]"). The code now detects bracketed IPv6 hostnames, strips the brackets, lowercases the address into `addr`, and correctly checks unique-local (fc00::/7) and link-local (fe80::/10) prefixes. Also updates related comments for clarity.
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