|
| 1 | +/* |
| 2 | + * Copyright (c) 2014-2024 Wurst-Imperium and contributors. |
| 3 | + * |
| 4 | + * This source code is subject to the terms of the GNU General Public |
| 5 | + * License, version 3. If a copy of the GPL was not distributed with this |
| 6 | + * file, You can obtain one at: https://www.gnu.org/licenses/gpl-3.0.txt |
| 7 | + */ |
| 8 | +package net.wurstclient.mixin; |
| 9 | + |
| 10 | +import org.spongepowered.asm.mixin.Mixin; |
| 11 | +import org.spongepowered.asm.mixin.injection.At; |
| 12 | + |
| 13 | +import com.llamalad7.mixinextras.injector.wrapoperation.Operation; |
| 14 | +import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; |
| 15 | + |
| 16 | +import net.minecraft.text.TextContent; |
| 17 | +import net.minecraft.text.TranslatableTextContent; |
| 18 | +import net.minecraft.util.Language; |
| 19 | + |
| 20 | +@Mixin(TranslatableTextContent.class) |
| 21 | +public abstract class TranslatableTextContentMixin implements TextContent |
| 22 | +{ |
| 23 | + /** |
| 24 | + * Ensures that any chat messages, written books, signs, etc. cannot resolve |
| 25 | + * Wurst-related translation keys. |
| 26 | + * |
| 27 | + * <p> |
| 28 | + * Fixes at least one security vulnerability affecting Minecraft 1.20 and |
| 29 | + * later versions, where the server can detect the presence of Wurst by |
| 30 | + * abusing Minecraft's sign editing feature. When a player edits a sign, any |
| 31 | + * translated text and keybind text components on that sign are resolved by |
| 32 | + * the client and sent back to the server as plain text. This allows the |
| 33 | + * server to detect the presence of non-vanilla translation keys. |
| 34 | + * |
| 35 | + * <p> |
| 36 | + * It is likely that similar vulnerabilities exist or will exist in other |
| 37 | + * parts of the game, such as chat messages and written books. Mojang has a |
| 38 | + * long history of failing to properly secure their text component system |
| 39 | + * (see BookHack, OP-Sign, BookDupe). Therefore it's best to cut off this |
| 40 | + * entire attack vector at the source. |
| 41 | + */ |
| 42 | + @WrapOperation(at = @At(value = "INVOKE", |
| 43 | + target = "Lnet/minecraft/util/Language;get(Ljava/lang/String;)Ljava/lang/String;", |
| 44 | + ordinal = 0), method = "updateTranslations()V") |
| 45 | + private String translate(Language instance, String key, |
| 46 | + Operation<String> original) |
| 47 | + { |
| 48 | + if(key != null && key.contains("wurst")) |
| 49 | + return key; |
| 50 | + |
| 51 | + return original.call(instance, key); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Same as above, but for translatable text components with a fallback. |
| 56 | + */ |
| 57 | + @WrapOperation(at = @At(value = "INVOKE", |
| 58 | + target = "Lnet/minecraft/util/Language;get(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;", |
| 59 | + ordinal = 0), method = "updateTranslations()V") |
| 60 | + private String translateWithFallback(Language instance, String key, |
| 61 | + String fallback, Operation<String> original) |
| 62 | + { |
| 63 | + if(key != null && key.contains("wurst")) |
| 64 | + return fallback; |
| 65 | + |
| 66 | + return original.call(instance, key, fallback); |
| 67 | + } |
| 68 | +} |
0 commit comments