SignedMessageValidator .java

net.minecraft.network.chat.SignedMessageValidator (interface): members: updateAndValidate, ACCEPT_UNSIGNED, REJECT_ALL, KeyBased, LOGGER. imports: Logger.

net.minecraft.network.chatinterface75 linescommon2.4 KB

API Surface (0)

No public/protected methods detected (fields or constants only).

Fields & Constants (3)

LOGGER
ACCEPT_UNSIGNED
REJECT_ALL

Code Graph: Dependencies (2)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

SignedMessageValidator.java75 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.network.chat;
2
3
import com.mojang.logging.LogUtils;
4
import java.util.function.BooleanSupplier;
5
import net.minecraft.util.SignatureValidator;
6
import org.jspecify.annotations.Nullable;
7
import org.slf4j.Logger;
8
9
@FunctionalInterface
10
public interface SignedMessageValidator {
11
	Logger LOGGER = LogUtils.getLogger();
12
	SignedMessageValidator ACCEPT_UNSIGNED = PlayerChatMessage::removeSignature;
13
	SignedMessageValidator REJECT_ALL = message -> {
14
		LOGGER.error("Received chat message from {}, but they have no chat session initialized and secure chat is enforced", message.sender());
15
		return null;
16
	};
17
18
	@Nullable PlayerChatMessage updateAndValidate(PlayerChatMessage message);
19
20
	class KeyBased implements SignedMessageValidator {
21
		private final SignatureValidator validator;
22
		private final BooleanSupplier expired;
23
		private @Nullable PlayerChatMessage lastMessage;
24
		private boolean isChainValid = true;
25
26
		public KeyBased(final SignatureValidator validator, final BooleanSupplier expired) {
27
			this.validator = validator;
28
			this.expired = expired;
29
		}
30
31
		private boolean validateChain(final PlayerChatMessage message) {
32
			if (message.equals(this.lastMessage)) {
33
				return true;
34
			} else if (this.lastMessage != null && !message.link().isDescendantOf(this.lastMessage.link())) {
35
				LOGGER.error(
36
					"Received out-of-order chat message from {}: expected index > {} for session {}, but was {} for session {}",
37
					message.sender(),
38
					this.lastMessage.link().index(),
39
					this.lastMessage.link().sessionId(),
40
					message.link().index(),
41
					message.link().sessionId()
42
				);
43
				return false;
44
			} else {
45
				return true;
46
			}
47
		}
48
49
		private boolean validate(final PlayerChatMessage message) {
50
			if (this.expired.getAsBoolean()) {
51
				LOGGER.error("Received message with expired profile public key from {} with session {}", message.sender(), message.link().sessionId());
52
				return false;
53
			} else if (!message.verify(this.validator)) {
54
				LOGGER.error(
55
					"Received message with invalid signature (is the session wrong, or signature cache out of sync?): {}", PlayerChatMessage.describeSigned(message)
56
				);
57
				return false;
58
			} else {
59
				return this.validateChain(message);
60
			}
61
		}
62
63
		@Override
64
		public @Nullable PlayerChatMessage updateAndValidate(final PlayerChatMessage message) {
65
			this.isChainValid = this.isChainValid && this.validate(message);
66
			if (!this.isChainValid) {
67
				return null;
68
			}
69
70
			this.lastMessage = message;
71
			return message;
72
		}
73
	}
74
}
75

Decompiled Minecraft 26.3 source of SignedMessageValidator (net/minecraft/network/chat/SignedMessageValidator.java) with Mojang official mappings, stored verbatim on the site.