-
Notifications
You must be signed in to change notification settings - Fork 76
feat(cloudflare): add IP-based rate limiting for MCP and OAuth routes #619
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
packages/mcp-cloudflare/src/server/utils/client-ip.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import { describe, it, expect } from "vitest"; | ||
| import { getClientIp } from "./client-ip"; | ||
|
|
||
| describe("getClientIp", () => { | ||
| it("returns CF-Connecting-IP when available", () => { | ||
| const request = new Request("https://example.com", { | ||
| headers: { | ||
| "CF-Connecting-IP": "192.0.2.1", | ||
| "X-Real-IP": "192.0.2.2", | ||
| "X-Forwarded-For": "192.0.2.3, 192.0.2.4", | ||
| }, | ||
| }); | ||
|
|
||
| expect(getClientIp(request)).toBe("192.0.2.1"); | ||
| }); | ||
|
|
||
| it("returns X-Real-IP when CF-Connecting-IP not available", () => { | ||
| const request = new Request("https://example.com", { | ||
| headers: { | ||
| "X-Real-IP": "192.0.2.2", | ||
| "X-Forwarded-For": "192.0.2.3, 192.0.2.4", | ||
| }, | ||
| }); | ||
|
|
||
| expect(getClientIp(request)).toBe("192.0.2.2"); | ||
| }); | ||
|
|
||
| it("returns first IP from X-Forwarded-For when others not available", () => { | ||
| const request = new Request("https://example.com", { | ||
| headers: { | ||
| "X-Forwarded-For": "192.0.2.3, 192.0.2.4, 192.0.2.5", | ||
| }, | ||
| }); | ||
|
|
||
| expect(getClientIp(request)).toBe("192.0.2.3"); | ||
| }); | ||
|
|
||
| it("trims whitespace from X-Forwarded-For IP", () => { | ||
| const request = new Request("https://example.com", { | ||
| headers: { | ||
| "X-Forwarded-For": " 192.0.2.3 , 192.0.2.4", | ||
| }, | ||
| }); | ||
|
|
||
| expect(getClientIp(request)).toBe("192.0.2.3"); | ||
| }); | ||
|
|
||
| it("returns null when no IP headers present", () => { | ||
| const request = new Request("https://example.com"); | ||
|
|
||
| expect(getClientIp(request)).toBe(null); | ||
| }); | ||
|
|
||
| it("returns null when X-Forwarded-For is empty", () => { | ||
| const request = new Request("https://example.com", { | ||
| headers: { | ||
| "X-Forwarded-For": "", | ||
| }, | ||
| }); | ||
|
|
||
| expect(getClientIp(request)).toBe(null); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| /** | ||
| * Extract client IP address from request headers. | ||
| * | ||
| * Checks headers in priority order: | ||
| * 1. CF-Connecting-IP (Cloudflare's most reliable header) | ||
| * 2. X-Real-IP (common reverse proxy header) | ||
| * 3. X-Forwarded-For (fallback, uses first IP in list) | ||
| * | ||
| * @param request - Native Request object | ||
| * @returns Client IP address, or null if not found | ||
| * | ||
| * @example | ||
| * ```typescript | ||
| * // With native Request | ||
| * const ip = getClientIp(request); | ||
| * if (!ip) { | ||
| * throw new Error("Failed to extract client IP"); | ||
| * } | ||
| * | ||
| * // With Hono Context - use c.req.raw | ||
| * const ip = getClientIp(c.req.raw); | ||
| * ``` | ||
| */ | ||
| export function getClientIp(request: Request): string | null { | ||
| const cfConnectingIp = request.headers.get("CF-Connecting-IP"); | ||
| if (cfConnectingIp) return cfConnectingIp; | ||
|
|
||
| const xRealIp = request.headers.get("X-Real-IP"); | ||
| if (xRealIp) return xRealIp; | ||
|
|
||
| const xForwardedFor = request.headers.get("X-Forwarded-For"); | ||
| if (xForwardedFor) { | ||
| const firstIp = xForwardedFor.split(",")[0]?.trim(); | ||
| if (firstIp) return firstIp; | ||
| } | ||
|
|
||
| return null; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Rate Limiting Gap for MCP Manifested Routes
The rate limiting condition for MCP routes only checks for paths starting with
/mcp. However, the application also defines MCP endpoints under/.mcp. This leaves/.mcproutes unprotected by rate limiting, creating a potential vulnerability for abuse.