-
Notifications
You must be signed in to change notification settings - Fork 314
Expand file tree
/
Copy pathProgram.cs
More file actions
156 lines (132 loc) · 5.83 KB
/
Program.cs
File metadata and controls
156 lines (132 loc) · 5.83 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.Extensions.FileProviders;
var builder = WebApplication.CreateBuilder(args);
var hostPort = builder.Configuration.GetValue("HostPort", 8080);
var sandboxPort = builder.Configuration.GetValue("SandboxPort", 8081);
// Kestrel: listen on two ports for separate origins (security requirement)
builder.WebHost.ConfigureKestrel(options =>
{
options.ListenLocalhost(hostPort);
options.ListenLocalhost(sandboxPort);
});
builder.Services.AddCors(o =>
o.AddDefaultPolicy(p => p.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader()));
var app = builder.Build();
app.UseCors();
// Resolve Angular dist directory
var distRelPath = builder.Configuration["FrontendDistPath"] ?? "../frontend/dist/browser";
var distPath = Path.GetFullPath(Path.Combine(builder.Environment.ContentRootPath, distRelPath));
// ── Sandbox server (port 8081) ──────────────────────────────────────────────
// Must run before static files middleware so it intercepts all sandbox-port requests.
app.Use(async (context, next) =>
{
if (context.Connection.LocalPort != sandboxPort)
{
await next();
return;
}
var path = context.Request.Path.Value ?? "/";
if (path is "/" or "/sandbox.html")
{
McpUiResourceCsp? csp = null;
if (context.Request.Query.TryGetValue("csp", out var cspJson))
{
try
{
csp = JsonSerializer.Deserialize<McpUiResourceCsp>(
cspJson!,
new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
}
catch (JsonException ex)
{
Console.Error.WriteLine($"[Sandbox] Invalid CSP query param: {ex.Message}");
}
}
context.Response.Headers.ContentSecurityPolicy = BuildCspHeader(csp);
context.Response.Headers.CacheControl = "no-cache, no-store, must-revalidate";
context.Response.Headers.Pragma = "no-cache";
context.Response.Headers.Expires = "0";
context.Response.ContentType = "text/html";
await context.Response.SendFileAsync(Path.Combine(distPath, "sandbox.html"));
}
else if (path == "/sandbox.js")
{
context.Response.ContentType = "application/javascript";
await context.Response.SendFileAsync(Path.Combine(distPath, "sandbox.js"));
}
else
{
context.Response.StatusCode = 404;
await context.Response.WriteAsync("Only sandbox files are served on this port");
}
});
// ── Host server (port 8080) ─────────────────────────────────────────────────
// Block sandbox files on host port — they must come from the sandbox origin.
app.Use(async (context, next) =>
{
if (context.Request.Path.Value is "/sandbox.html" or "/sandbox.js")
{
context.Response.StatusCode = 404;
await context.Response.WriteAsync("Sandbox is served on a different port");
return;
}
await next();
});
// /api/servers — configuration required, no hardcoded fallback
var servers = builder.Configuration.GetSection("Servers").Get<string[]>()
?? throw new InvalidOperationException("'Servers' configuration is required in appsettings.json");
app.MapGet("/api/servers", () => Results.Json(servers));
// Angular SPA static files
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(distPath),
});
// SPA fallback — serve index.html for unknown routes (client-side routing)
app.MapFallback(async context =>
{
context.Response.ContentType = "text/html";
await context.Response.SendFileAsync(Path.Combine(distPath, "index.html"));
});
Console.WriteLine($"Host server: http://localhost:{hostPort}");
Console.WriteLine($"Sandbox server: http://localhost:{sandboxPort}");
Console.WriteLine($"Frontend dist: {distPath}");
Console.WriteLine();
app.Run();
// ── CSP helpers ──────────────────────────────────────────────────────────────
static string BuildCspHeader(McpUiResourceCsp? csp)
{
var rd = string.Join(" ", SanitizeCspDomains(csp?.ResourceDomains));
var cd = string.Join(" ", SanitizeCspDomains(csp?.ConnectDomains));
var frame = csp?.FrameDomains?.Length > 0
? $"frame-src {string.Join(" ", SanitizeCspDomains(csp.FrameDomains))}"
: "frame-src 'none'";
var baseUri = csp?.BaseUriDomains?.Length > 0
? $"base-uri {string.Join(" ", SanitizeCspDomains(csp.BaseUriDomains))}"
: "base-uri 'none'";
return string.Join("; ", new[]
{
"default-src 'self' 'unsafe-inline'",
$"script-src 'self' 'unsafe-inline' 'unsafe-eval' blob: data: {rd}".TrimEnd(),
$"style-src 'self' 'unsafe-inline' blob: data: {rd}".TrimEnd(),
$"img-src 'self' data: blob: {rd}".TrimEnd(),
$"font-src 'self' data: blob: {rd}".TrimEnd(),
$"media-src 'self' data: blob: {rd}".TrimEnd(),
$"connect-src 'self' {cd}".TrimEnd(),
$"worker-src 'self' blob: {rd}".TrimEnd(),
frame,
"object-src 'none'",
baseUri,
});
}
static string[] SanitizeCspDomains(string[]? domains) =>
domains?
.Where(d => !string.IsNullOrEmpty(d)
&& !d.Any(c => c is ';' or '\r' or '\n' or '\'' or '"' or ' '))
.ToArray() ?? [];
record McpUiResourceCsp(
[property: JsonPropertyName("resourceDomains")] string[]? ResourceDomains,
[property: JsonPropertyName("connectDomains")] string[]? ConnectDomains,
[property: JsonPropertyName("frameDomains")] string[]? FrameDomains,
[property: JsonPropertyName("baseUriDomains")] string[]? BaseUriDomains
);