-
Notifications
You must be signed in to change notification settings - Fork 728
Expand file tree
/
Copy pathMcpApplicationBuilderExtensions.cs
More file actions
47 lines (43 loc) · 1.77 KB
/
Copy pathMcpApplicationBuilderExtensions.cs
File metadata and controls
47 lines (43 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using ModelContextProtocol.AspNetCore;
namespace Microsoft.AspNetCore.Builder;
/// <summary>
/// Extension methods for adding MCP middleware to an <see cref="IApplicationBuilder"/>.
/// </summary>
public static class McpApplicationBuilderExtensions
{
/// <summary>
/// Adds DNS rebinding protection middleware for MCP servers running on localhost.
/// </summary>
/// <param name="app">The <see cref="IApplicationBuilder"/>.</param>
/// <returns>The <see cref="IApplicationBuilder"/> for chaining.</returns>
/// <remarks>
/// <para>
/// This method provides protection against DNS rebinding attacks by validating that both
/// Host and Origin headers (when present) resolve to localhost addresses.
/// </para>
/// <para>
/// DNS rebinding attacks can allow malicious websites to bypass browser same-origin policy and make requests
/// to localhost services. This protection is recommended for any MCP server that binds to localhost.
/// </para>
/// <para>
/// For more information, see the <see href="https://github.com/modelcontextprotocol/typescript-sdk/security/advisories/GHSA-w48q-cv73-mx4w">MCP SDK security advisory</see>.
/// </para>
/// </remarks>
/// <example>
/// <code>
/// var builder = WebApplication.CreateBuilder(args);
/// builder.Services.AddMcpServer().WithHttpTransport();
///
/// var app = builder.Build();
/// app.UseMcpDnsRebindingProtection(); // Add before MapMcp()
/// app.MapMcp();
/// app.Run();
/// </code>
/// </example>
public static IApplicationBuilder UseMcpDnsRebindingProtection(this IApplicationBuilder app)
{
ArgumentNullException.ThrowIfNull(app);
app.UseMiddleware<DnsRebindingProtectionMiddleware>();
return app;
}
}