Skip to content

Commit 3ee8ae4

Browse files
committed
Prevent unauthorized access to categories
1 parent a7f4e8a commit 3ee8ae4

File tree

6 files changed

+37
-4
lines changed

6 files changed

+37
-4
lines changed

common/src/main/java/revxrsal/commands/command/CommandCategory.java

+9
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,15 @@ public interface CommandCategory {
7070
*/
7171
@NotNull CommandPermission getPermission();
7272

73+
/**
74+
* Returns whether is this category secret or not. This
75+
* will only return true if all the children categories and commands
76+
* of this category are secret.
77+
*
78+
* @return Is this category secret or not.
79+
*/
80+
boolean isSecret();
81+
7382
/**
7483
* Returns an unmodifiable view of all the sub-categories in this
7584
* category.

common/src/main/java/revxrsal/commands/core/BaseAutoCompleter.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ private List<String> getCompletions(CommandActor actor, @Unmodifiable ArgumentSt
199199
if (!c.isSecret() && c.getPermission().canExecute(actor)) suggestions.add(c.getName());
200200
});
201201
category.getCategories().values().forEach(c -> {
202-
if (c.getPermission().canExecute(actor)) suggestions.add(c.getName());
202+
if (!c.isSecret() && c.getPermission().canExecute(actor)) suggestions.add(c.getName());
203203
});
204204
}
205205
return suggestions

common/src/main/java/revxrsal/commands/core/BaseCommandCategory.java

+12
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,18 @@ final class BaseCommandCategory implements CommandCategory {
5050
return permission;
5151
}
5252

53+
@Override public boolean isSecret() {
54+
for (ExecutableCommand command : commands.values()) {
55+
if (command.isSecret()) continue;
56+
return false;
57+
}
58+
for (CommandCategory category : categories.values()) {
59+
if (category.isSecret()) continue;
60+
return false;
61+
}
62+
return true;
63+
}
64+
5365
private final Map<CommandPath, CommandCategory> unmodifiableCategories = Collections.unmodifiableMap(categories);
5466

5567
@Override public @NotNull @UnmodifiableView Map<CommandPath, CommandCategory> getCategories() {

common/src/main/java/revxrsal/commands/core/BaseCommandDispatcher.java

+2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ private Object searchCategory(CommandActor actor, BaseCommandCategory category,
6060
arguments.removeFirst();
6161
return execute(executable, actor, arguments);
6262
}
63+
if (!category.getPermission().canExecute(actor))
64+
throw new NoPermissionException(null, category, category.getPermission());
6365
BaseCommandCategory found = (BaseCommandCategory) category.getCategories().get(path);
6466
if (found == null) {
6567
if (category.defaultAction == null)

common/src/main/java/revxrsal/commands/core/BaseCommandHandler.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public BaseCommandHandler() {
107107
});
108108
registerCondition((actor, command, arguments) -> {
109109
if (!command.getPermission().canExecute(actor))
110-
throw new NoPermissionException(command, command.getPermission());
110+
throw new NoPermissionException(command, command.getParent(), command.getPermission());
111111
});
112112
}
113113

common/src/main/java/revxrsal/commands/exception/NoPermissionException.java

+12-2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@
33
import lombok.AllArgsConstructor;
44
import lombok.Getter;
55
import org.jetbrains.annotations.NotNull;
6+
import org.jetbrains.annotations.Nullable;
67
import revxrsal.commands.command.CommandActor;
8+
import revxrsal.commands.command.CommandCategory;
79
import revxrsal.commands.command.CommandPermission;
810
import revxrsal.commands.command.ExecutableCommand;
911

1012
/**
1113
* Thrown when a {@link CommandActor} lacks the required permission to
12-
* execute the given command
14+
* execute the given command or category.
15+
* <p>
16+
* Note that {@link #getCommand()} may be null when the user attempts
17+
* to access a category in which they do not have permission.
1318
*/
1419
@Getter
1520
@AllArgsConstructor
@@ -19,7 +24,12 @@ public class NoPermissionException extends RuntimeException {
1924
/**
2025
* The command being executed
2126
*/
22-
private final @NotNull ExecutableCommand command;
27+
private final @Nullable ExecutableCommand command;
28+
29+
/**
30+
* The category being accessed.
31+
*/
32+
private final CommandCategory category;
2333

2434
/**
2535
* The permission the actor lacks

0 commit comments

Comments
 (0)