From 1e5f3471699840ab11eed101c5ea38cc5f53600c Mon Sep 17 00:00:00 2001 From: Owen1212055 <23108066+Owen1212055@users.noreply.github.com> Date: Wed, 6 Dec 2023 20:26:58 -0500 Subject: [PATCH] Clean up username validation logic --- patches/server/Validate-usernames.patch | 65 +++++++++++++------------ 1 file changed, 33 insertions(+), 32 deletions(-) diff --git a/patches/server/Validate-usernames.patch b/patches/server/Validate-usernames.patch index 26dd83291a..fcc7c1165c 100644 --- a/patches/server/Validate-usernames.patch +++ b/patches/server/Validate-usernames.patch @@ -17,41 +17,11 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 public ServerLoginPacketListenerImpl(MinecraftServer server, Connection connection) { this.state = ServerLoginPacketListenerImpl.State.HELLO; @@ -0,0 +0,0 @@ public class ServerLoginPacketListenerImpl implements ServerLoginPacketListener, - return this.requestedUsername != null ? this.requestedUsername + " (" + s + ")" : s; - } - -+ // Paper start - validate usernames -+ public static boolean validateUsername(String in) { -+ if (in == null || in.isEmpty() || in.length() > 16) { -+ return false; -+ } -+ -+ for (int i = 0, len = in.length(); i < len; ++i) { -+ char c = in.charAt(i); -+ -+ if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || (c == '_' || c == '.')) { -+ continue; -+ } -+ -+ return false; -+ } -+ -+ return true; -+ } -+ // Paper end - validate usernames -+ @Override public void handleHello(ServerboundHelloPacket packet) { Validate.validState(this.state == ServerLoginPacketListenerImpl.State.HELLO, "Unexpected hello packet", new Object[0]); - Validate.validState(Player.isValidUsername(packet.name()), "Invalid characters in username", new Object[0]); -+ // Paper start - validate usernames -- TODO: Do we need this? -+ if (io.papermc.paper.configuration.GlobalConfiguration.get().proxies.isProxyOnlineMode() && io.papermc.paper.configuration.GlobalConfiguration.get().unsupportedSettings.performUsernameValidation) { -+ if (!this.iKnowThisMayNotBeTheBestIdeaButPleaseDisableUsernameValidation && !validateUsername(packet.name())) { -+ ServerLoginPacketListenerImpl.this.disconnect("Failed to verify username!"); -+ return; -+ } -+ } -+ // Paper end - validate usernames +- Validate.validState(Player.isValidUsername(packet.name()), "Invalid characters in username", new Object[0]); ++ if (io.papermc.paper.configuration.GlobalConfiguration.get().proxies.isProxyOnlineMode() && io.papermc.paper.configuration.GlobalConfiguration.get().unsupportedSettings.performUsernameValidation && !this.iKnowThisMayNotBeTheBestIdeaButPleaseDisableUsernameValidation) Validate.validState(Player.isValidUsername(packet.name()), "Invalid characters in username", new Object[0]); // Paper - config username validation this.requestedUsername = packet.name(); GameProfile gameprofile = this.server.getSingleplayerProfile(); @@ -68,3 +38,34 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 list.add(entityplayer); } } +diff --git a/src/main/java/net/minecraft/world/entity/player/Player.java b/src/main/java/net/minecraft/world/entity/player/Player.java +index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 +--- a/src/main/java/net/minecraft/world/entity/player/Player.java ++++ b/src/main/java/net/minecraft/world/entity/player/Player.java +@@ -0,0 +0,0 @@ public abstract class Player extends LivingEntity { + } + + public static boolean isValidUsername(String name) { +- return name.length() > 16 ? false : name.chars().filter((i) -> { +- return i <= 32 || i >= 127; +- }).findAny().isEmpty(); ++ // Paper start ++ if (name == null || name.isEmpty() || name.length() > 16) { ++ return false; ++ } ++ ++ for (int i = 0, len = name.length(); i < len; ++i) { ++ char c = name.charAt(i); ++ ++ if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || (c == '_' || c == '.')) { ++ continue; ++ } ++ ++ return false; ++ } ++ ++ return true; ++ // Paper end + } + + public static float getPickRange(boolean creative) {