remove username character restriction introduced in 22w06a, close #150

This commit is contained in:
Haowei Wen 2022-03-16 01:06:54 +08:00
parent a55936fbf4
commit 8fe84c802e
2 changed files with 100 additions and 0 deletions

View file

@ -62,6 +62,7 @@ import moe.yushi.authlibinjector.transform.support.MC52974_1710Workaround;
import moe.yushi.authlibinjector.transform.support.MainArgumentsTransformer;
import moe.yushi.authlibinjector.transform.support.ProxyParameterWorkaround;
import moe.yushi.authlibinjector.transform.support.SkinWhitelistTransformUnit;
import moe.yushi.authlibinjector.transform.support.UsernameCharacterCheckTransformer;
import moe.yushi.authlibinjector.transform.support.YggdrasilKeyTransformUnit;
import moe.yushi.authlibinjector.yggdrasil.CustomYggdrasilAPIProvider;
import moe.yushi.authlibinjector.yggdrasil.MojangYggdrasilAPIProvider;
@ -266,6 +267,7 @@ public final class AuthlibInjector {
transformer.units.add(new CitizensTransformer());
transformer.units.add(new ConcatenateURLTransformUnit());
transformer.units.add(new BungeeCordAllowedCharactersTransformer());
transformer.units.add(new UsernameCharacterCheckTransformer());
transformer.units.add(new SkinWhitelistTransformUnit());
SkinWhitelistTransformUnit.getWhitelistedDomains().addAll(config.getSkinDomains());

View file

@ -0,0 +1,98 @@
/*
* Copyright (C) 2022 Haowei Wen <yushijinhun@gmail.com> and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package moe.yushi.authlibinjector.transform.support;
import static org.objectweb.asm.Opcodes.*;
import java.util.Optional;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.MethodVisitor;
import moe.yushi.authlibinjector.transform.TransformContext;
import moe.yushi.authlibinjector.transform.TransformUnit;
/**
* Starting from 22w06a, Minecraft allows only certain ASCII characters (33 ~ 126)
* in the username. This transformer removes the restriction.
*/
public class UsernameCharacterCheckTransformer implements TransformUnit {
@Override
public Optional<ClassVisitor> transform(ClassLoader classLoader, String className, ClassVisitor writer, TransformContext context) {
return Optional.of(new ClassVisitor(ASM9, writer) {
@Override
public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) {
return new MethodVisitor(ASM9, super.visitMethod(access, name, descriptor, signature, exceptions)) {
// States:
// 0 - initial state
// 1 - ldc_w "Invalid characters in username"
// 2 - iconst_0
// 3 - anewarray java/lang/Object
// 4 - invokestatic org/apache/commons/lang3/Validate.validState:(ZLjava/lang/String;[Ljava/lang/Object;)V
int state = 0;
@Override
public void visitLdcInsn(Object value) {
if (state == 0 && "Invalid characters in username".equals(value)) {
state++;
}
super.visitLdcInsn(value);
}
@Override
public void visitInsn(int opcode) {
if (state == 1 && opcode == ICONST_0) {
state++;
}
super.visitInsn(opcode);
}
@Override
public void visitTypeInsn(int opcode, String type) {
if (state == 2 && opcode == ANEWARRAY && "java/lang/Object".equals(type)) {
state++;
}
super.visitTypeInsn(opcode, type);
}
@Override
public void visitMethodInsn(int opcode, String owner, String name, String descriptor, boolean isInterface) {
if (state == 3 &&
opcode == INVOKESTATIC &&
"org/apache/commons/lang3/Validate".equals(owner) &&
"validState".equals(name) &&
"(ZLjava/lang/String;[Ljava/lang/Object;)V".equals(descriptor)) {
context.markModified();
state++;
super.visitInsn(POP);
super.visitInsn(POP);
super.visitInsn(POP);
} else {
super.visitMethodInsn(opcode, owner, name, descriptor, isInterface);
}
}
};
}
});
}
@Override
public String toString() {
return "Username Character Checker Transformer";
}
}