Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bungeeguard-backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>me.lucko</groupId>
<artifactId>bungeeguard</artifactId>
<version>1.4.9</version>
<version>1.4.10</version>
</parent>

<artifactId>bungeeguard-backend</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion bungeeguard-bungee/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>me.lucko</groupId>
<artifactId>bungeeguard</artifactId>
<version>1.4.9</version>
<version>1.4.10</version>
</parent>

<artifactId>bungeeguard-bungee</artifactId>
Expand Down
4 changes: 2 additions & 2 deletions bungeeguard-spigot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
<parent>
<groupId>me.lucko</groupId>
<artifactId>bungeeguard</artifactId>
<version>1.4.9</version>
<version>1.4.10</version>
</parent>

<artifactId>bungeeguard-spigot</artifactId>
<version>1.4.9</version>
<version>1.4.10</version>

<build>
<resources>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,28 @@ private static BungeeCordHandshake decodeAndVerify0(String handshake, TokenStore
boolean hasProperties = split.length >= 4 && isPropertiesArray(split[split.length - 1]);
int socketAddressIndex = split.length - (hasProperties ? 3 : 2);
int floodgateDataIndex = findFloodgateDataIndex(split, socketAddressIndex);
boolean isFloodgate = floodgateDataIndex >= 1;
int validatedFloodgateMarkerIndex = findValidatedFloodgateMarkerIndex(split, socketAddressIndex);
boolean hasValidatedFloodgateMarker = validatedFloodgateMarkerIndex >= 1
&& FloodgateHandshakeIntegration.consumeValidatedMarker(split[validatedFloodgateMarkerIndex]);
boolean isFloodgate = floodgateDataIndex >= 1 || hasValidatedFloodgateMarker;

if (isFloodgate) {
int uuidIndex = hasProperties ? split.length - 2 : split.length - 1;
if (socketAddressIndex < 1 || uuidIndex <= socketAddressIndex) {
return new Fail(Fail.Reason.INVALID_HANDSHAKE, encodeBase64(handshake));
}

String serverHostname = String.join("\00", Arrays.copyOfRange(split, 0, socketAddressIndex));
List<String> hostnameComponents = new ArrayList<>();
for (int i = 0; i < socketAddressIndex; i++) {
// Floodgate's backend handler consumes the encrypted payload before Paper's
// PlayerHandshakeEvent fires. Our one-use marker bridges that lifecycle gap;
// strip it so it never reaches the actual Minecraft server hostname.
if (hasValidatedFloodgateMarker && i == validatedFloodgateMarkerIndex) {
continue;
}
hostnameComponents.add(split[i]);
}
String serverHostname = String.join("\00", hostnameComponents);
String socketAddressHostname = split[socketAddressIndex];
UUID uniqueId;
try {
Expand Down Expand Up @@ -180,6 +193,18 @@ private static BungeeCordHandshake decodeAndVerify0(String handshake, TokenStore
return new Success(serverHostname, socketAddressHostname, uniqueId, newPropertiesString);
}

private static int findValidatedFloodgateMarkerIndex(String[] split, int socketAddressIndex) {
if (socketAddressIndex <= 1) {
return -1;
}
for (int i = 1; i < socketAddressIndex; i++) {
if (FloodgateHandshakeIntegration.isValidatedMarkerCandidate(split[i])) {
return i;
}
}
return -1;
}

private static boolean isPropertiesArray(String value) {
String trimmed = value.trim();
return trimmed.startsWith("[") && trimmed.endsWith("]");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public void onEnable() {
saveDefaultConfig();
this.tokenStore = new TokenStore(this);
this.tokenStore.load();
FloodgateHandshakeIntegration.register(getLogger());

if (hasSpigotConfigMethod() && !isBungeeEnabled()) {
getLogger().severe("*-----------------------------------------------------------*");
Expand Down Expand Up @@ -106,6 +107,11 @@ public void onEnable() {
getServer().getPluginManager().registerEvents(new ExtraProtectionListener(logger), this);
}

@Override
public void onDisable() {
FloodgateHandshakeIntegration.unregister();
}

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof ConsoleCommandSender)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
/*
* This file is part of BungeeGuard+, licensed under the MIT License.
*/
package me.lucko.bungeeguard.spigot;

import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.security.SecureRandom;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* Bridges Floodgate's successful, per-connection encrypted-handshake validation to Paper's
* PlayerHandshakeEvent. Floodgate removes its encrypted payload before Paper emits that event,
* so BungeeGuard cannot safely infer validation merely from the now-missing hostname marker.
*
* A one-use, unpredictable marker is appended only from Floodgate's own HandshakeHandler callback
* after Floodgate has decrypted and validated the payload. BungeeGuard consumes the marker once
* and removes it from the forwarded hostname. Without the Floodgate callback, a spoofed marker
* cannot pass the token gate.
*/
public final class FloodgateHandshakeIntegration {
private static final String MARKER_PREFIX = "^RealmsNetworkBungeeGuard^";
private static final long MARKER_TTL_MILLIS = 10_000L;
private static final SecureRandom RANDOM = new SecureRandom();
private static final Map<String, Long> PENDING_MARKERS = new ConcurrentHashMap<>();

private static Object floodgateHandlers;
private static int handlerId = -1;

private FloodgateHandshakeIntegration() {
}

/** Register a Floodgate handshake callback if Floodgate is installed and exposes its 2.x API. */
public static synchronized boolean register(Logger logger) {
Plugin floodgate = Bukkit.getPluginManager().getPlugin("floodgate");
if (floodgate == null || !floodgate.isEnabled()) {
return false;
}
if (handlerId >= 0) {
return true;
}

try {
ClassLoader loader = floodgate.getClass().getClassLoader();
Class<?> handlerType = loader.loadClass("org.geysermc.floodgate.api.handshake.HandshakeHandler");
Class<?> instanceHolderType = loader.loadClass("org.geysermc.floodgate.api.InstanceHolder");
Object handlers = instanceHolderType.getMethod("getHandshakeHandlers").invoke(null);
if (handlers == null) {
logger.warning("Floodgate is present, but its handshake API is not initialized; Bedrock/Education bypass is unavailable.");
return false;
}

InvocationHandler callback = (proxy, method, args) -> {
if ("handle".equals(method.getName()) && args != null && args.length == 1 && args[0] != null) {
handleValidatedFloodgateHandshake(args[0], logger);
} else if (method.getDeclaringClass() == Object.class) {
if ("toString".equals(method.getName())) return "BungeeGuardFloodgateHandshakeHandler";
if ("hashCode".equals(method.getName())) return System.identityHashCode(proxy);
if ("equals".equals(method.getName())) return proxy == args[0];
}
return null;
};

Object adapter = Proxy.newProxyInstance(loader, new Class<?>[]{handlerType}, callback);
Method addHandler = handlers.getClass().getMethod("addHandshakeHandler", handlerType);
Object id = addHandler.invoke(handlers, adapter);
if (!(id instanceof Number) || ((Number) id).intValue() < 0) {
logger.warning("Could not register with Floodgate's handshake API; Bedrock/Education bypass is unavailable.");
return false;
}
floodgateHandlers = handlers;
handlerId = ((Number) id).intValue();
logger.info("Floodgate handshake integration enabled; its encrypted validation remains authoritative for Bedrock/Education connections.");
return true;
} catch (ReflectiveOperationException | LinkageError ex) {
logger.log(Level.WARNING, "Floodgate is present, but its handshake API could not be connected; Bedrock/Education bypass is unavailable.", ex);
return false;
}
}

private static void handleValidatedFloodgateHandshake(Object data, Logger logger) {
try {
boolean isFloodgatePlayer = Boolean.TRUE.equals(data.getClass().getMethod("isFloodgatePlayer").invoke(data));
if (!isFloodgatePlayer) {
return;
}

Method getHostname = data.getClass().getMethod("getHostname");
Method setHostname = data.getClass().getMethod("setHostname", String.class);
String hostname = (String) getHostname.invoke(data);
if (hostname == null || hostname.contains(MARKER_PREFIX)) {
return;
}

String marker = createValidatedMarker();
String markedHostname = insertMarkerAfterHostname(hostname, marker);
if (markedHostname == null) {
PENDING_MARKERS.remove(marker);
logger.warning("Floodgate validated a player, but its sanitized handshake had no forwarding separator; unable to add the BungeeGuard marker.");
return;
}
try {
// Insert after the first (server hostname) component. Floodgate's callback
// receives the whole sanitized handshake host, including the proxy-forwarded
// address, UUID, and properties. Appending would put the marker after the
// properties array, where BungeeGuard's handshake parser cannot find it.
setHostname.invoke(data, markedHostname);
} catch (ReflectiveOperationException | RuntimeException ex) {
PENDING_MARKERS.remove(marker);
throw ex;
}
} catch (ReflectiveOperationException | RuntimeException ex) {
logger.log(Level.WARNING, "Could not mark a Floodgate-validated handshake for BungeeGuard; the connection may still require a BungeeGuard token.", ex);
}
}

static String insertMarkerAfterHostname(String hostname, String marker) {
if (hostname == null || marker == null) {
return null;
}
int separator = hostname.indexOf('\0');
if (separator < 0) {
return null;
}
return hostname.substring(0, separator) + '\0' + marker + hostname.substring(separator);
}

static String createValidatedMarker() {
long now = System.currentTimeMillis();
PENDING_MARKERS.entrySet().removeIf(entry -> entry.getValue() < now);

byte[] nonce = new byte[24];
RANDOM.nextBytes(nonce);
StringBuilder hex = new StringBuilder(nonce.length * 2);
for (byte b : nonce) {
hex.append(Character.forDigit((b >>> 4) & 0xF, 16));
hex.append(Character.forDigit(b & 0xF, 16));
}
String marker = MARKER_PREFIX + hex;
PENDING_MARKERS.put(marker, now + MARKER_TTL_MILLIS);
return marker;
}

static boolean isValidatedMarkerCandidate(String value) {
return value != null && value.startsWith(MARKER_PREFIX) && value.length() > MARKER_PREFIX.length();
}

static boolean consumeValidatedMarker(String marker) {
if (!isValidatedMarkerCandidate(marker)) {
return false;
}
Long expiresAt = PENDING_MARKERS.remove(marker);
return expiresAt != null && expiresAt >= System.currentTimeMillis();
}

public static synchronized void unregister() {
if (floodgateHandlers != null && handlerId >= 0) {
try {
floodgateHandlers.getClass().getMethod("removeHandshakeHandler", int.class).invoke(floodgateHandlers, handlerId);
} catch (ReflectiveOperationException ignored) {
// Floodgate may already be shutting down. This integration is optional.
}
}
floodgateHandlers = null;
handlerId = -1;
PENDING_MARKERS.clear();
}
}
2 changes: 1 addition & 1 deletion bungeeguard-spigot/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ authors: [Luck, NickUC]
load: STARTUP

main: me.lucko.bungeeguard.spigot.BungeeGuardBackendPlugin
softdepend: [ProtocolLib]
softdepend: [ProtocolLib, floodgate]
api-version: 1.13
folia-supported: true

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,46 @@ public void acceptsCurrentFloodgateMarkerAndDoesNotRequireBungeeGuardToken() {
assertEquals("[]", success.propertiesJson());
}

@Test
public void acceptsOneUseMarkerMintedOnlyAfterFloodgateValidation() {
String marker = FloodgateHandshakeIntegration.createValidatedMarker();
String sanitizedHostname = String.join("\00", HOST, IP, UUID_VALUE.toString(), "[]");
String handshake = FloodgateHandshakeIntegration.insertMarkerAfterHostname(sanitizedHostname, marker);
assertEquals(String.join("\00", HOST, marker, IP, UUID_VALUE.toString(), "[]"), handshake);

BungeeCordHandshake result = BungeeCordHandshake.decodeAndVerify(handshake, defaultTokenStore());

assertTrue("A marker from Floodgate's successful decrypt callback should bypass the Java token gate", result instanceof BungeeCordHandshake.Success);
BungeeCordHandshake.Success success = (BungeeCordHandshake.Success) result;
assertEquals("Floodgate's private marker must not reach the server hostname", HOST, success.serverHostname());
assertEquals(IP, success.socketAddressHostname());
assertEquals(UUID_VALUE, success.uniqueId());

BungeeCordHandshake secondAttempt = BungeeCordHandshake.decodeAndVerify(handshake, configuredTokenStore());
assertTrue("The marker must be single-use", secondAttempt instanceof BungeeCordHandshake.Fail);
}

@Test
public void doesNotAppendValidatedMarkerAfterProperties() {
String marker = FloodgateHandshakeIntegration.createValidatedMarker();
String handshake = String.join("\00", HOST, IP, UUID_VALUE.toString(), "[]");

String marked = FloodgateHandshakeIntegration.insertMarkerAfterHostname(handshake, marker);

assertEquals(String.join("\00", HOST, marker, IP, UUID_VALUE.toString(), "[]"), marked);
assertFalse(marked.endsWith(marker));
}

@Test
public void forgedFloodgateIntegrationMarkerDoesNotBypassTokenValidation() {
String forged = "^RealmsNetworkBungeeGuard^not-a-real-nonce";
String handshake = String.join("\00", HOST, forged, IP, UUID_VALUE.toString(), "[]");

BungeeCordHandshake result = BungeeCordHandshake.decodeAndVerify(handshake, configuredTokenStore());

assertTrue(result instanceof BungeeCordHandshake.Fail);
}

@Test
public void recognizesFloodgateMarkerAfterAdditionalHostnameComponent() {
String payload = "^Floodgate^>encrypted-bedrock-payload";
Expand Down
2 changes: 1 addition & 1 deletion bungeeguard-sponge/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>me.lucko</groupId>
<artifactId>bungeeguard</artifactId>
<version>1.4.9</version>
<version>1.4.10</version>
</parent>

<artifactId>bungeeguard-sponge</artifactId>
Expand Down
4 changes: 2 additions & 2 deletions bungeeguard-universal/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
<parent>
<groupId>me.lucko</groupId>
<artifactId>bungeeguard</artifactId>
<version>1.4.9</version>
<version>1.4.10</version>
</parent>

<artifactId>bungeeguard-universal</artifactId>
<version>1.4.9</version>
<version>1.4.10</version>

<build>
<defaultGoal>clean package</defaultGoal>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>me.lucko</groupId>
<artifactId>bungeeguard</artifactId>
<version>1.4.9</version>
<version>1.4.10</version>
<packaging>pom</packaging>

<modules>
Expand Down
Loading