Fail silently when plugins use reflection to add to dummy permissibles (#1117)

This commit is contained in:
Luck 2018-07-24 21:21:05 -07:00
parent c3cdf387c2
commit ae3adf23c8
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
2 changed files with 60 additions and 28 deletions

View File

@ -32,8 +32,14 @@ import org.bukkit.permissions.PermissionAttachmentInfo;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.function.BiFunction;
import java.util.function.Function;
public class DummyPermissibleBase extends PermissibleBase { public class DummyPermissibleBase extends PermissibleBase {
private static final Field ATTACHMENTS_FIELD; private static final Field ATTACHMENTS_FIELD;
@ -51,19 +57,6 @@ public class DummyPermissibleBase extends PermissibleBase {
} }
} }
public static void nullFields(PermissibleBase permissibleBase) {
try {
ATTACHMENTS_FIELD.set(permissibleBase, Collections.emptyList());
} catch (Exception e) {
// ignore
}
try {
PERMISSIONS_FIELD.set(permissibleBase, Collections.emptyMap());
} catch (Exception e) {
// ignore
}
}
public static void copyFields(PermissibleBase from, PermissibleBase to) { public static void copyFields(PermissibleBase from, PermissibleBase to) {
try { try {
ATTACHMENTS_FIELD.set(to, ATTACHMENTS_FIELD.get(from)); ATTACHMENTS_FIELD.set(to, ATTACHMENTS_FIELD.get(from));
@ -81,7 +74,31 @@ public class DummyPermissibleBase extends PermissibleBase {
private DummyPermissibleBase() { private DummyPermissibleBase() {
super(null); super(null);
nullFields(this);
// we want the singleton dummy attachment to be stateless
// the behaviour of this class is to fail silently, so we can't use Collections.emptyX
try {
ATTACHMENTS_FIELD.set(this, new ArrayList<PermissionAttachment>(){
@Override public boolean add(PermissionAttachment permissionAttachment) { return true; }
@Override public void add(int index, PermissionAttachment element) { }
@Override public boolean addAll(Collection<? extends PermissionAttachment> c) { return true; }
@Override public boolean addAll(int index, Collection<? extends PermissionAttachment> c) { return true; }
});
} catch (Exception e) {
// ignore
}
try {
PERMISSIONS_FIELD.set(this, new HashMap<String, PermissionAttachmentInfo>() {
@Override public PermissionAttachmentInfo put(String key, PermissionAttachmentInfo value) { return null; }
@Override public void putAll(Map<? extends String, ? extends PermissionAttachmentInfo> m) { }
@Override public PermissionAttachmentInfo putIfAbsent(String key, PermissionAttachmentInfo value) { return null; }
@Override public PermissionAttachmentInfo compute(String key, BiFunction<? super String, ? super PermissionAttachmentInfo, ? extends PermissionAttachmentInfo> remappingFunction) { return null; }
@Override public PermissionAttachmentInfo computeIfPresent(String key, BiFunction<? super String, ? super PermissionAttachmentInfo, ? extends PermissionAttachmentInfo> remappingFunction) { return null; }
@Override public PermissionAttachmentInfo computeIfAbsent(String key, Function<? super String, ? extends PermissionAttachmentInfo> mappingFunction) { return null; }
});
} catch (Exception e) {
// ignore
}
} }
@Override public boolean isOp() { return false; } @Override public boolean isOp() { return false; }

View File

@ -33,11 +33,15 @@ import cn.nukkit.plugin.Plugin;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.AbstractList; import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.RandomAccess; import java.util.RandomAccess;
import java.util.function.BiFunction;
import java.util.function.Function;
public class DummyPermissibleBase extends PermissibleBase { public class DummyPermissibleBase extends PermissibleBase {
private static final Field ATTACHMENTS_FIELD; private static final Field ATTACHMENTS_FIELD;
@ -55,19 +59,6 @@ public class DummyPermissibleBase extends PermissibleBase {
} }
} }
public static void nullFields(PermissibleBase permissibleBase) {
try {
ATTACHMENTS_FIELD.set(permissibleBase, EMPTY_LIST);
} catch (Exception e) {
// ignore
}
try {
PERMISSIONS_FIELD.set(permissibleBase, Collections.emptyMap());
} catch (Exception e) {
// ignore
}
}
public static void copyFields(PermissibleBase from, PermissibleBase to) { public static void copyFields(PermissibleBase from, PermissibleBase to) {
try { try {
ATTACHMENTS_FIELD.set(to, ATTACHMENTS_FIELD.get(from)); ATTACHMENTS_FIELD.set(to, ATTACHMENTS_FIELD.get(from));
@ -85,7 +76,31 @@ public class DummyPermissibleBase extends PermissibleBase {
private DummyPermissibleBase() { private DummyPermissibleBase() {
super(null); super(null);
nullFields(this);
// we want the singleton dummy attachment to be stateless
// the behaviour of this class is to fail silently, so we can't use Collections.emptyX
try {
ATTACHMENTS_FIELD.set(this, new ArrayList<PermissionAttachment>(){
@Override public boolean add(PermissionAttachment permissionAttachment) { return true; }
@Override public void add(int index, PermissionAttachment element) { }
@Override public boolean addAll(Collection<? extends PermissionAttachment> c) { return true; }
@Override public boolean addAll(int index, Collection<? extends PermissionAttachment> c) { return true; }
});
} catch (Exception e) {
// ignore
}
try {
PERMISSIONS_FIELD.set(this, new HashMap<String, PermissionAttachmentInfo>() {
@Override public PermissionAttachmentInfo put(String key, PermissionAttachmentInfo value) { return null; }
@Override public void putAll(Map<? extends String, ? extends PermissionAttachmentInfo> m) { }
@Override public PermissionAttachmentInfo putIfAbsent(String key, PermissionAttachmentInfo value) { return null; }
@Override public PermissionAttachmentInfo compute(String key, BiFunction<? super String, ? super PermissionAttachmentInfo, ? extends PermissionAttachmentInfo> remappingFunction) { return null; }
@Override public PermissionAttachmentInfo computeIfPresent(String key, BiFunction<? super String, ? super PermissionAttachmentInfo, ? extends PermissionAttachmentInfo> remappingFunction) { return null; }
@Override public PermissionAttachmentInfo computeIfAbsent(String key, Function<? super String, ? extends PermissionAttachmentInfo> mappingFunction) { return null; }
});
} catch (Exception e) {
// ignore
}
} }
@Override public boolean isOp() { return false; } @Override public boolean isOp() { return false; }