Skip to content

Commit

Permalink
Fixed usage of empty database in HTTP layer
Browse files Browse the repository at this point in the history
Fixed issue #581
  • Loading branch information
lvca committed Oct 27, 2022
1 parent b5b06b9 commit 1574f38
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
3 changes: 3 additions & 0 deletions server/src/main/java/com/arcadedb/server/ArcadeDBServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,9 @@ public String toString() {
}

public synchronized Database getDatabase(final String databaseName, final boolean createIfNotExists, final boolean allowLoad) {
if (databaseName == null || databaseName.trim().isEmpty())
throw new IllegalArgumentException("Invalid database name " + databaseName);

DatabaseInternal db = databases.get(databaseName);

if (db == null || !db.isOpen()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,20 @@ protected boolean requiresDatabase() {

@Override
public void execute(final HttpServerExchange exchange, ServerSecurityUser user, final Database database) {
final Deque<String> databaseName = exchange.getQueryParameters().get("database");
if (databaseName.isEmpty()) {
final Deque<String> databaseNamePar = exchange.getQueryParameters().get("database");
String databaseName = databaseNamePar.isEmpty() ? null : databaseNamePar.getFirst().trim();
if (databaseName.isEmpty())
databaseName = null;

if (databaseName == null) {
exchange.setStatusCode(400);
exchange.getResponseSender().send("{ \"error\" : \"Database parameter is null\"}");
return;
}

httpServer.getServer().getServerMetrics().meter("http.create-database").mark();

httpServer.getServer().createDatabase(databaseName.getFirst(), PaginatedFile.MODE.READ_WRITE);
httpServer.getServer().createDatabase(databaseName, PaginatedFile.MODE.READ_WRITE);

exchange.setStatusCode(200);
exchange.getResponseSender().send("{ \"result\" : \"ok\"}");
Expand Down
22 changes: 22 additions & 0 deletions server/src/test/java/com/arcadedb/server/HTTPGraphIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -582,4 +582,26 @@ public void closeAndReopenDatabase() throws Exception {
}
});
}

@Test
public void testEmptyDatabaseName() throws Exception {
testEachServer((serverIndex) -> {
// CREATE THE DATABASE ''
HttpURLConnection connection = (HttpURLConnection) new URL("http://127.0.0.1:248" + serverIndex + "/api/v1/create/").openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Authorization",
"Basic " + Base64.getEncoder().encodeToString(("root:" + BaseGraphServerTest.DEFAULT_PASSWORD_FOR_TESTS).getBytes()));
connection.connect();

try {
readResponse(connection);
Assertions.fail("Empty database should be an error");
} catch (Exception e) {
Assertions.assertEquals(400, connection.getResponseCode());

} finally {
connection.disconnect();
}
});
}
}

0 comments on commit 1574f38

Please sign in to comment.