Copilot commented on code in PR #16492:
URL: https://github.com/apache/dubbo/pull/16492#discussion_r4269033799
##########
dubbo-common/src/main/java/org/apache/dubbo/common/utils/SerializeSecurityConfigurator.java:
##########
@@ -348,6 +363,36 @@ private boolean checkClass(Class<?> clazz) {
return true;
}
+ private static Method resolveGetPermittedSubclasses() {
+ try {
+ return Class.class.getMethod("getPermittedSubclasses");
+ } catch (NoSuchMethodException e) {
+ // Running on Java 8/11: sealed classes do not exist, so there is
nothing to walk.
+ return null;
+ }
+ }
+
+ /**
+ * Returns the subtypes a sealed class permits, or an empty array when the
class is not sealed,
+ * the runtime predates sealed classes, or the attribute cannot be read.
+ *
+ * <p>Note that a Kotlin {@code sealed} declaration only carries the JVM
{@code
+ * PermittedSubclasses} attribute when compiled with {@code -jvm-target
17} or above; below that
+ * the sealed hierarchy exists only in {@code kotlin.Metadata} and is not
visible here.
+ */
+ private static Class<?>[] getPermittedSubclasses(Class<?> clazz) {
+ if (GET_PERMITTED_SUBCLASSES == null) {
+ return EMPTY_CLASS_ARRAY;
+ }
+ try {
+ Class<?>[] permittedSubclasses = (Class<?>[])
GET_PERMITTED_SUBCLASSES.invoke(clazz);
+ return permittedSubclasses == null ? EMPTY_CLASS_ARRAY :
permittedSubclasses;
+ } catch (Throwable t) {
+ // A permitted subtype that cannot be resolved must not stop the
rest of the walk.
Review Comment:
Catching `Throwable` here will also swallow serious `Error`s (e.g.,
`OutOfMemoryError`, `StackOverflowError`) and make failures much harder to
diagnose. Narrow this to the reflective/lookup-related exceptions you actually
expect (e.g., `ReflectiveOperationException`, `ClassCastException`,
`SecurityException`, possibly `LinkageError`/`TypeNotPresentException` if
observed) and consider logging at `DEBUG` when this fallback triggers so
operators can understand why sealed subtypes weren’t traversed.
##########
dubbo-common/src/test/java/org/apache/dubbo/common/utils/SerializeSecurityConfiguratorSealedTest.java:
##########
@@ -0,0 +1,138 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.dubbo.common.utils;
+
+import org.apache.dubbo.rpc.model.ApplicationModel;
+import org.apache.dubbo.rpc.model.FrameworkModel;
+import org.apache.dubbo.rpc.model.ModuleModel;
+
+import java.io.Serializable;
+import javassist.ClassPool;
+import javassist.CtClass;
+import javassist.LoaderClassPath;
+import javassist.Modifier;
+import javassist.bytecode.AttributeInfo;
+import javassist.bytecode.ClassFile;
+import javassist.bytecode.ConstPool;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.condition.EnabledForJreRange;
+import org.junit.jupiter.api.condition.JRE;
+
+/**
+ * Auto-trust must reach the subtypes a sealed type permits, because declaring
a sealed type in a
+ * service signature means the subtypes are what actually travel on the wire.
+ *
+ * <p>The sealed fixture is generated at runtime rather than written as
source: this module compiles
+ * at Java 8, where {@code sealed} does not exist.
+ */
+class SerializeSecurityConfiguratorSealedTest {
+
+ private static final int CLASS_FILE_JAVA_17 = 61;
+
+ /** Ordinary interface and implementation — the control for the sealed
case. */
+ public interface OpenParent extends Serializable {}
+
+ public static class OpenChild implements OpenParent {}
+
+ @Test
+ @EnabledForJreRange(min = JRE.JAVA_17)
+ void testSealedSubclassesAreAutoTrusted() throws Exception {
+ String pkg = getClass().getPackage().getName();
+ String parentName = pkg + ".GeneratedSealedParent";
+ String firstChildName = pkg + ".GeneratedSealedChildOne";
+ String secondChildName = pkg + ".GeneratedSealedChildTwo";
Review Comment:
These generated class names are constant, so re-running this test within the
same JVM (common in IDE “rerun”, some build setups, or flaky-test reruns) can
fail with a duplicate definition `LinkageError` when calling `toClass`. Use
unique names per run (e.g., suffix with a UUID/counter) and/or define them in
an isolated `ClassLoader` to avoid cross-run collisions.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]