Skip to content

Commit c143f16

Browse files
committed
improve disallowed character check to also check compatibility with the
db collation
1 parent 0b0a12d commit c143f16

File tree

5 files changed

+34
-8
lines changed

5 files changed

+34
-8
lines changed

src/main/java/pro/cloudnode/smp/bankaccounts/BankConfig.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.List;
2525
import java.util.Objects;
2626
import java.util.Optional;
27+
import java.util.Set;
2728
import java.util.TimeZone;
2829

2930
public final class BankConfig {
@@ -624,10 +625,10 @@ public int invoicePerPage() {
624625
}
625626

626627
// messages.errors.disallowed-characters
627-
public @NotNull Component messagesErrorsDisallowedCharacters(final @NotNull String characters) {
628+
public @NotNull Component messagesErrorsDisallowedCharacters(final @NotNull Set<@NotNull String> characters) {
628629
return MiniMessage.miniMessage().deserialize(
629630
Objects.requireNonNull(config.getString("messages.errors.disallowed-characters")),
630-
Placeholder.unparsed("characters", characters)
631+
Placeholder.unparsed("characters", String.join("", characters))
631632
);
632633
}
633634

src/main/java/pro/cloudnode/smp/bankaccounts/Command.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
import java.util.List;
1414
import java.util.Optional;
15+
import java.util.Set;
16+
import java.util.stream.Collectors;
1517

1618
public abstract class Command implements CommandExecutor, TabCompleter {
1719
/**
@@ -81,4 +83,18 @@ public final boolean onCommand(final @NotNull CommandSender sender, final @NotNu
8183
final @Nullable List<@NotNull String> suggestions = tab(sender, args);
8284
return Optional.ofNullable(suggestions).map(s -> s.stream().filter(suggestion -> suggestion.toLowerCase().startsWith(args[args.length - 1].toLowerCase())).toList()).orElse(null);
8385
}
86+
87+
protected static @NotNull Set<@NotNull String> getDisallowedCharacters(final @Nullable String input) {
88+
if (input == null) return Set.of();
89+
final @NotNull Set<@NotNull String> chars = input
90+
.codePoints()
91+
.filter(codePoint -> codePoint > 0xFFFF)
92+
.mapToObj(codePoint -> new String(Character.toChars(codePoint)))
93+
.collect(Collectors.toSet());
94+
if (input.contains("<"))
95+
chars.add("<");
96+
if (input.contains(">"))
97+
chars.add(">");
98+
return chars;
99+
}
84100
}

src/main/java/pro/cloudnode/smp/bankaccounts/commands/BankCommand.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.Arrays;
2121
import java.util.Objects;
2222
import java.util.Optional;
23+
import java.util.Set;
2324
import java.util.stream.Collectors;
2425
import java.util.stream.Stream;
2526

@@ -382,8 +383,9 @@ public static boolean setName(final @NotNull CommandSender sender, final @NotNul
382383
name = name.length() > 32 ? name.substring(0, 32) : name;
383384
name = name.isEmpty() ? null : name;
384385

385-
if (name != null && (name.contains("<") || name.contains(">")))
386-
return sendMessage(sender, BankAccounts.getInstance().config().messagesErrorsDisallowedCharacters("<>"));
386+
final @NotNull Set<@NotNull String> disallowedChars = getDisallowedCharacters(name);
387+
if (!disallowedChars.isEmpty())
388+
return sendMessage(sender, BankAccounts.getInstance().config().messagesErrorsDisallowedCharacters(disallowedChars));
387389

388390
account.get().name = name;
389391
account.get().update();
@@ -501,8 +503,9 @@ public static boolean transfer(final @NotNull CommandSender sender, final @NotNu
501503
.join(" ", Arrays.copyOfRange(argsCopy, 3, argsCopy.length)).trim() : null;
502504
if (description != null && description.length() > 64) description = description.substring(0, 64);
503505

504-
if (description != null && (description.contains("<") || description.contains(">")))
505-
return sendMessage(sender, BankAccounts.getInstance().config().messagesErrorsDisallowedCharacters("<>"));
506+
final @NotNull Set<@NotNull String> disallowedChars = getDisallowedCharacters(description);
507+
if (!disallowedChars.isEmpty())
508+
return sendMessage(sender, BankAccounts.getInstance().config().messagesErrorsDisallowedCharacters(disallowedChars));
506509

507510
if (!confirm && BankAccounts.getInstance().config().transferConfirmationEnabled()) {
508511
final @NotNull BigDecimal minAmount = BankAccounts.getInstance().config().transferConfirmationMinAmount();

src/main/java/pro/cloudnode/smp/bankaccounts/commands/InvoiceCommand.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.util.Arrays;
1818
import java.util.List;
1919
import java.util.Optional;
20+
import java.util.Set;
2021
import java.util.stream.Stream;
2122

2223
public final class InvoiceCommand extends Command {
@@ -177,6 +178,9 @@ public static boolean create(final @NotNull CommandSender sender, @NotNull Strin
177178
return sendMessage(sender, BankAccounts.getInstance().config().messagesErrorsFrozen(account.get()));
178179

179180
final @Nullable String description = argsCopy.length < 3 ? null : String.join(" ", Arrays.copyOfRange(argsCopy, 2, argsCopy.length));
181+
final @NotNull Set<@NotNull String> disallowedChars = getDisallowedCharacters(description);
182+
if (!disallowedChars.isEmpty())
183+
return sendMessage(sender, BankAccounts.getInstance().config().messagesErrorsDisallowedCharacters(disallowedChars));
180184

181185
final @NotNull Invoice invoice = new Invoice(account.get(), amount, description, target);
182186
invoice.insert();

src/main/java/pro/cloudnode/smp/bankaccounts/commands/POSCommand.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.Arrays;
2121
import java.util.Date;
2222
import java.util.Optional;
23+
import java.util.Set;
2324

2425
/**
2526
* Create a POS at the location the player is looking at.
@@ -78,8 +79,9 @@ public boolean execute(final @NotNull CommandSender sender, final @NotNull Strin
7879
@Nullable String description = args.length > 2 ? String.join(" ", Arrays.copyOfRange(args, 2, args.length)) : null;
7980
if (description != null && description.length() > 64) description = description.substring(0, 64);
8081

81-
if (description != null && (description.contains("<") || description.contains(">")))
82-
return sendMessage(sender, BankAccounts.getInstance().config().messagesErrorsDisallowedCharacters("<>"));
82+
final @NotNull Set<@NotNull String> disallowedChars = getDisallowedCharacters(description);
83+
if (!disallowedChars.isEmpty())
84+
return sendMessage(sender, BankAccounts.getInstance().config().messagesErrorsDisallowedCharacters(disallowedChars));
8385

8486
final @NotNull POS pos = new POS(target.getLocation(), price, description, account.get(), new Date());
8587
pos.save();

0 commit comments

Comments
 (0)