Skip to content

Commit efc1437

Browse files
robfrankclaude
andcommitted
refactor: centralize MCP database auth check in resolveDatabase
Move canAccessToDatabase guard into MCPToolUtils.resolveDatabase so all tools benefit consistently and restricted users who mistype a database name get the helpful "available databases" list rather than a bare SecurityException. Also switch to removeIf + canAccessToDatabase for the available-databases filter, dropping the manual wildcard check. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 85e00f8 commit efc1437

4 files changed

Lines changed: 4 additions & 13 deletions

File tree

server/src/main/java/com/arcadedb/server/mcp/tools/ExecuteCommandTool.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,6 @@ public static JSONObject execute(final ArcadeDBServer server, final ServerSecuri
7272
final String command = args.getString("command");
7373
final int limit = args.getInt("limit", DEFAULT_LIMIT);
7474

75-
if (!user.canAccessToDatabase(databaseName))
76-
throw new SecurityException("User '" + user.getName() + "' is not authorized to access database '" + databaseName + "'");
77-
7875
final Database database = MCPToolUtils.resolveDatabase(server, user, databaseName);
7976

8077
// Analyze once for both permission checking and execution (avoids double parsing)

server/src/main/java/com/arcadedb/server/mcp/tools/GetSchemaTool.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,6 @@ public static JSONObject execute(final ArcadeDBServer server, final ServerSecuri
5757

5858
final String databaseName = args.getString("database");
5959

60-
if (!user.canAccessToDatabase(databaseName))
61-
throw new SecurityException("User '" + user.getName() + "' is not authorized to access database '" + databaseName + "'");
62-
6360
final Database database = MCPToolUtils.resolveDatabase(server, user, databaseName);
6461

6562
final Schema schema = database.getSchema();

server/src/main/java/com/arcadedb/server/mcp/tools/MCPToolUtils.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ public static ServerDatabase resolveDatabase(final ArcadeDBServer server, final
3939
final String databaseName) {
4040
if (!server.existsDatabase(databaseName)) {
4141
final Set<String> installed = new TreeSet<>(server.getDatabaseNames());
42-
final Set<String> allowed = user.getAuthorizedDatabases();
43-
if (!allowed.contains("*"))
44-
installed.retainAll(allowed);
42+
installed.removeIf(db -> !user.canAccessToDatabase(db));
4543
throw new IllegalArgumentException(
4644
"Database '" + databaseName + "' does not exist. Available databases: " + installed
4745
+ ". Use one of these names or call list_databases to refresh the list.");
4846
}
47+
if (!user.canAccessToDatabase(databaseName))
48+
throw new SecurityException("User '" + user.getName() + "' is not authorized to access database '" + databaseName + "'");
4949
return server.getDatabase(databaseName);
5050
}
5151
}

server/src/main/java/com/arcadedb/server/mcp/tools/QueryTool.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020

2121
import com.arcadedb.database.Database;
2222
import com.arcadedb.query.QueryEngine;
23-
import com.arcadedb.server.mcp.MCPConfiguration;
2423
import com.arcadedb.query.sql.executor.Result;
2524
import com.arcadedb.query.sql.executor.ResultSet;
2625
import com.arcadedb.serializer.JsonSerializer;
2726
import com.arcadedb.serializer.json.JSONArray;
2827
import com.arcadedb.serializer.json.JSONObject;
2928
import com.arcadedb.server.ArcadeDBServer;
29+
import com.arcadedb.server.mcp.MCPConfiguration;
3030
import com.arcadedb.server.security.ServerSecurityUser;
3131

3232
import java.util.Collections;
@@ -73,9 +73,6 @@ public static JSONObject execute(final ArcadeDBServer server, final ServerSecuri
7373
final String query = args.getString("query");
7474
final int limit = args.getInt("limit", DEFAULT_LIMIT);
7575

76-
if (!user.canAccessToDatabase(databaseName))
77-
throw new SecurityException("User '" + user.getName() + "' is not authorized to access database '" + databaseName + "'");
78-
7976
final Database database = MCPToolUtils.resolveDatabase(server, user, databaseName);
8077

8178
// Verify the query is actually read-only using semantic analysis

0 commit comments

Comments
 (0)