Skip to content

Open-source Revit MCP server: let Claude and other AI assistants read and edit BIM models, with human approval on every change.

feat(csharp): default LegacyMode to false, implement central Bearer auth, and add WorkspaceMonitor path validation

A. Sam Mohammad5ad86e75aa79
diff --git a/packages/revit-bridge-addin/src/Bridge/BridgeCommands.cs b/packages/revit-bridge-addin/src/Bridge/BridgeCommands.cs
index 1e17609..1187a10 100644
--- a/packages/revit-bridge-addin/src/Bridge/BridgeCommands.cs
+++ b/packages/revit-bridge-addin/src/Bridge/BridgeCommands.cs
@@ -41,7 +41,7 @@ namespace RevitBridge.Bridge
new MediaBrush(MediaColor.FromRgb(76, 175, 80))
);
- dialog.AddInfoSection("Server Address", "http://127.0.0.1:3000/");
+ dialog.AddInfoSection("Server Address", App.Server != null ? $"http://127.0.0.1:{App.Server.Port}/" : "http://127.0.0.1:3000/");
dialog.SetActionButton("Close");
dialog.ShowDialog();
@@ -163,7 +163,7 @@ namespace RevitBridge.Bridge
dialog.AddSeparator();
// Server Information
- dialog.AddInfoSection("Server Address", "http://127.0.0.1:3000/");
+ dialog.AddInfoSection("Server Address", App.Server != null ? $"http://127.0.0.1:{App.Server.Port}/" : "http://127.0.0.1:3000/");
// Revit Information
string revitInfo = $"Revit {App.RevitVersion ?? "Unknown"}";
diff --git a/packages/revit-bridge-addin/src/Bridge/BridgeServer.cs b/packages/revit-bridge-addin/src/Bridge/BridgeServer.cs
index 8b587c7..88ae9a7 100644
--- a/packages/revit-bridge-addin/src/Bridge/BridgeServer.cs
+++ b/packages/revit-bridge-addin/src/Bridge/BridgeServer.cs
@@ -23,6 +23,7 @@ public class BridgeServer
private int _activeConnections = 0;
private SemaphoreSlim _concurrencySemaphore = new(10, 10);
private const long MaxRequestSizeBytes = 5 * 1024 * 1024; // 5 MB
+ private readonly WorkspaceMonitor _workspaceMonitor = new();
public BridgeServer(CommandQueue queue, ExternalEvent externalEvent)
{
@@ -32,7 +33,7 @@ public class BridgeServer
SessionToken = Guid.NewGuid().ToString("N") + Guid.NewGuid().ToString("N");
var legacyEnv = Environment.GetEnvironmentVariable("MCP_REVIT_LEGACY_PORT");
- LegacyMode = string.IsNullOrEmpty(legacyEnv) || legacyEnv.ToLower() == "true" || legacyEnv == "1";
+ LegacyMode = !string.IsNullOrEmpty(legacyEnv) && (legacyEnv.ToLower() == "true" || legacyEnv == "1");
}
public string SessionToken { get; }
@@ -153,6 +154,16 @@ public class BridgeServer
var path = context.Request.Url?.AbsolutePath ?? "/";
+ if (!LegacyMode && path != "/health")
+ {
+ var authHeader = context.Request.Headers["Authorization"];
+ if (string.IsNullOrEmpty(authHeader) || authHeader != $"Bearer {SessionToken}")
+ {
+ Respond(context, 401, new { error = "Unauthorized" });
+ return;
+ }
+ }
+
if (path == "/health")
{
await HandleHealth(context);
@@ -188,16 +199,6 @@ public class BridgeServer
private async Task HandleExecute(HttpListenerContext context)
{
- if (!LegacyMode)
- {
- var authHeader = context.Request.Headers["Authorization"];
- if (string.IsNullOrEmpty(authHeader) || authHeader != $"Bearer {SessionToken}")
- {
- Respond(context, 401, new { error = "Unauthorized" });
- return;
- }
- }
-
var startTime = DateTime.UtcNow;
using var reader = new StreamReader(context.Request.InputStream);
@@ -209,6 +210,17 @@ public class BridgeServer
var tool = root.GetProperty("tool").GetString() ?? string.Empty;
var payload = root.GetProperty("payload");
+ try
+ {
+ ValidatePayloadPaths(payload);
+ }
+ catch (UnauthorizedAccessException ex)
+ {
+ Log.Warning("Workspace violation in request {RequestId} {Tool}: {Message}", requestId, tool, ex.Message);
+ Respond(context, 403, new { error = "Workspace violation: " + ex.Message });
+ return;
+ }
+
var request = new CommandRequest
{
RequestId = requestId,
@@ -230,6 +242,36 @@ public class BridgeServer
Respond(context, 200, response);
}
+ private void ValidatePayloadPaths(JsonElement element)
+ {
+ if (element.ValueKind == JsonValueKind.Object)
+ {
+ foreach (var property in element.EnumerateObject())
+ {
+ var name = property.Name.ToLower();
+ if (property.Value.ValueKind == JsonValueKind.String)
+ {
+ var val = property.Value.GetString();
+ if (!string.IsNullOrEmpty(val) && (name.Contains("path") || name.Contains("file")))
+ {
+ _workspaceMonitor.AssertInWorkspace(val);
+ }
+ }
+ else
+ {
+ ValidatePayloadPaths(property.Value);
+ }
+ }
+ }
+ else if (element.ValueKind == JsonValueKind.Array)
+ {
+ foreach (var item in element.EnumerateArray())
+ {
+ ValidatePayloadPaths(item);
+ }
+ }
+ }
+
private Task HandleHealth(HttpListenerContext context)
{
var health = new
diff --git a/packages/revit-bridge-addin/src/Bridge/WorkspaceMonitor.cs b/packages/revit-bridge-addin/src/Bridge/WorkspaceMonitor.cs
new file mode 100644
index 0000000..bdbe000
--- /dev/null
+++ b/packages/revit-bridge-addin/src/Bridge/WorkspaceMonitor.cs
@@ -0,0 +1,86 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+
+namespace RevitBridge.Bridge
+{
+ public class WorkspaceMonitor
+ {
+ private readonly List<string> _allowedDirectories = new();
+
+ public WorkspaceMonitor()
+ {
+ var workspaceDir = Environment.GetEnvironmentVariable("MCP_REVIT_WORKSPACE_DIR");
+ var allowedDirsEnv = Environment.GetEnvironmentVariable("MCP_REVIT_ALLOWED_DIRECTORIES");
+
+ if (!string.IsNullOrEmpty(workspaceDir))
+ {
+ try
+ {
+ _allowedDirectories.Add(Path.GetFullPath(workspaceDir));
+ }
+ catch { }
+ }
+
+ if (!string.IsNullOrEmpty(allowedDirsEnv))
+ {
+ foreach (var dir in allowedDirsEnv.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries))
+ {
+ try
+ {
+ var fullPath = Path.GetFullPath(dir.Trim());
+ if (!_allowedDirectories.Contains(fullPath))
+ {
+ _allowedDirectories.Add(fullPath);
+ }
+ }
+ catch { }
+ }
+ }
+ }
+
+ public bool IsInWorkspace(string path)
+ {
+ if (string.IsNullOrEmpty(path)) return false;
+
+ // If it doesn't look like a path (e.g. no directory separators and not rooted), skip validation
+ if (!path.Contains(Path.DirectorySeparatorChar.ToString()) &&
+ !path.Contains(Path.AltDirectorySeparatorChar.ToString()) &&
+ !Path.IsPathRooted(path))
+ {
+ return true;
+ }
+
+ try
+ {
+ var fullPath = Path.GetFullPath(path);
+ foreach (var allowedDir in _allowedDirectories)
+ {
+ if (fullPath.StartsWith(allowedDir, StringComparison.OrdinalIgnoreCase))
+ {
+ if (fullPath.Length == allowedDir.Length ||
+ fullPath[allowedDir.Length] == Path.DirectorySeparatorChar ||
+ fullPath[allowedDir.Length] == Path.AltDirectorySeparatorChar)
+ {
+ return true;
+ }
+ }
+ }
+ }
+ catch
+ {
+ return false;
+ }
+
+ return false;
+ }
+
+ public void AssertInWorkspace(string path)
+ {
+ if (!IsInWorkspace(path))
+ {
+ throw new UnauthorizedAccessException($"Path '{path}' is outside the allowed workspace directories.");
+ }
+ }
+ }
+}