Repository navigation
feat: add DNS rebinding protection conformance tests #115
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 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1a3d913
feat: add DNS rebinding protection conformance tests
pcarleton 30cda52
feat: add negative test case for DNS rebinding protection
pcarleton b1e3a47
fix: accept any 4xx status code for DNS rebinding rejection
pcarleton b1cf903
refactor: address PR review comments
pcarleton f3210a1
fix: include error details in catch block
pcarleton 2420796
refactor: simplify negative test server to stateless
pcarleton 7c1b3db
refactor: check both Host and Origin headers for DNS rebinding protec…
pcarleton 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
157 changes: 157 additions & 0 deletions
157
examples/servers/typescript/no-dns-rebinding-protection.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,157 @@ | ||
| #!/usr/bin/env node | ||
|
|
||
| /** | ||
| * MCP Server WITHOUT DNS Rebinding Protection - Negative Test Case | ||
| * | ||
| * This server intentionally omits DNS rebinding protection to demonstrate | ||
| * what a vulnerable server looks like. DO NOT use this pattern in production. | ||
| * | ||
| * This is a negative test case for the conformance suite - it should FAIL | ||
| * the dns-rebinding-protection scenario. | ||
| */ | ||
|
|
||
| import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'; | ||
| import { | ||
| StreamableHTTPServerTransport, | ||
| EventStore, | ||
| EventId, | ||
| StreamId | ||
| } from '@modelcontextprotocol/sdk/server/streamableHttp.js'; | ||
| import express from 'express'; | ||
| import { randomUUID } from 'crypto'; | ||
|
|
||
| // In-memory event store for SSE | ||
|
pcarleton marked this conversation as resolved.
Outdated
|
||
| function createEventStore(): EventStore { | ||
| const events = new Map<StreamId, Map<EventId, string>>(); | ||
|
|
||
| return { | ||
| async storeEvent( | ||
| streamId: StreamId, | ||
| eventId: EventId, | ||
| message: string | ||
| ): Promise<void> { | ||
| if (!events.has(streamId)) { | ||
| events.set(streamId, new Map()); | ||
| } | ||
| events.get(streamId)!.set(eventId, message); | ||
| }, | ||
| async replayEventsAfter( | ||
| streamId: StreamId, | ||
| lastEventId: EventId | ||
| ): Promise<string[]> { | ||
| const streamEvents = events.get(streamId); | ||
| if (!streamEvents) { | ||
| return []; | ||
| } | ||
| const entries = Array.from(streamEvents.entries()).sort( | ||
| ([a], [b]) => Number(a) - Number(b) | ||
| ); | ||
| const startIdx = entries.findIndex(([id]) => id === lastEventId); | ||
| if (startIdx === -1) { | ||
| return entries.map(([, msg]) => msg); | ||
| } | ||
| return entries.slice(startIdx + 1).map(([, msg]) => msg); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| // Create MCP server with minimal functionality | ||
| function createMcpServer(): McpServer { | ||
| const server = new McpServer({ | ||
| name: 'no-dns-rebinding-protection-server', | ||
| version: '1.0.0' | ||
| }); | ||
|
|
||
| // Add a simple tool | ||
| server.tool( | ||
| 'echo', | ||
| 'Echo the input back', | ||
| { message: { type: 'string', description: 'Message to echo' } }, | ||
| async ({ message }) => { | ||
| return { | ||
| content: [{ type: 'text', text: `Echo: ${message}` }] | ||
| }; | ||
| } | ||
| ); | ||
|
|
||
| return server; | ||
| } | ||
|
|
||
| // Track active transports and servers | ||
| const transports: Record<string, StreamableHTTPServerTransport> = {}; | ||
| const servers: Record<string, McpServer> = {}; | ||
|
|
||
| function isInitializeRequest(body: any): boolean { | ||
| return body?.method === 'initialize'; | ||
| } | ||
|
|
||
| // === VULNERABLE EXPRESS APP === | ||
| // This intentionally does NOT use createMcpExpressApp() or localhostHostValidation() | ||
| // to demonstrate a server without DNS rebinding protection. | ||
|
|
||
| const app = express(); | ||
| app.use(express.json()); | ||
|
|
||
| // NO DNS rebinding protection middleware here! | ||
| // This is intentionally vulnerable for testing purposes. | ||
|
|
||
| app.post('/mcp', async (req, res) => { | ||
| const sessionId = req.headers['mcp-session-id'] as string | undefined; | ||
|
|
||
| try { | ||
| let transport: StreamableHTTPServerTransport; | ||
|
|
||
| if (sessionId && transports[sessionId]) { | ||
| transport = transports[sessionId]; | ||
| } else if (!sessionId && isInitializeRequest(req.body)) { | ||
| const mcpServer = createMcpServer(); | ||
|
|
||
| transport = new StreamableHTTPServerTransport({ | ||
| sessionIdGenerator: () => randomUUID(), | ||
| eventStore: createEventStore(), | ||
| onsessioninitialized: (newSessionId) => { | ||
| transports[newSessionId] = transport; | ||
| servers[newSessionId] = mcpServer; | ||
| } | ||
| }); | ||
|
|
||
| transport.onclose = () => { | ||
| const sid = transport.sessionId; | ||
| if (sid && transports[sid]) { | ||
| delete transports[sid]; | ||
| if (servers[sid]) { | ||
| delete servers[sid]; | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| await mcpServer.connect(transport); | ||
| } else { | ||
| res.status(400).json({ | ||
| jsonrpc: '2.0', | ||
| error: { code: -32000, message: 'Bad Request: No valid session' }, | ||
| id: null | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| await transport.handleRequest(req, res, req.body); | ||
| } catch (error) { | ||
| console.error('Error handling request:', error); | ||
| if (!res.headersSent) { | ||
| res.status(500).json({ | ||
| jsonrpc: '2.0', | ||
| error: { code: -32603, message: 'Internal error' }, | ||
| id: null | ||
| }); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| // Start server | ||
| const PORT = parseInt(process.env.PORT || '3003', 10); | ||
| app.listen(PORT, '127.0.0.1', () => { | ||
| console.log(`Vulnerable server running on http://localhost:${PORT}`); | ||
| console.log(` - MCP endpoint: http://localhost:${PORT}/mcp`); | ||
| console.log(` - WARNING: No DNS rebinding protection enabled!`); | ||
| }); | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Oops, something went wrong.
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.
note: this example is going to get deleted in a follow-up PR to make the typescript-sdk example the source of truth. updating it in place for now.