diff --git a/modules/rhino/src/main/java/com/script/rhino/ProtectedNativeJavaClass.kt b/modules/rhino/src/main/java/com/script/rhino/ProtectedNativeJavaClass.kt new file mode 100644 index 000000000..73f111884 --- /dev/null +++ b/modules/rhino/src/main/java/com/script/rhino/ProtectedNativeJavaClass.kt @@ -0,0 +1,40 @@ +package com.script.rhino + +import org.mozilla.javascript.NativeJavaClass +import org.mozilla.javascript.Scriptable + +class ProtectedNativeJavaClass( + scope: Scriptable, + javaClass: Class<*>, + private val protectedName: HashSet +) : NativeJavaClass(scope, javaClass) { + + override fun has( + name: String, + start: Scriptable? + ): Boolean { + if (protectedName.contains(name)) { + return false + } + return super.has(name, start) + } + + override fun get(name: String, start: Scriptable?): Any? { + if (protectedName.contains(name)) { + return NOT_FOUND + } + return super.get(name, start) + } + + override fun put( + name: String, + start: Scriptable?, + value: Any? + ) { + if (protectedName.contains(name)) { + return + } + super.put(name, start, value) + } + +} diff --git a/modules/rhino/src/main/java/com/script/rhino/RhinoClassShutter.kt b/modules/rhino/src/main/java/com/script/rhino/RhinoClassShutter.kt index 5d9488a3e..dcdff226d 100644 --- a/modules/rhino/src/main/java/com/script/rhino/RhinoClassShutter.kt +++ b/modules/rhino/src/main/java/com/script/rhino/RhinoClassShutter.kt @@ -27,6 +27,8 @@ package com.script.rhino import android.os.Build import org.mozilla.javascript.ClassShutter import org.mozilla.javascript.Context +import org.mozilla.javascript.NativeJavaClass +import org.mozilla.javascript.Scriptable import java.io.ObjectInputStream import java.io.ObjectOutputStream import java.lang.reflect.Member @@ -51,6 +53,8 @@ object RhinoClassShutter : ClassShutter { "cn.hutool.core.lang.JarClassLoader", "cn.hutool.core.util.RuntimeUtil", "cn.hutool.core.util.ClassLoaderUtil", + "cn.hutool.core.util.ReflectUtil", + "cn.hutool.core.util.SerializeUtil", "org.mozilla.javascript.DefiningClassLoader", "java.lang.Runtime", "java.lang.ProcessBuilder", @@ -69,6 +73,7 @@ object RhinoClassShutter : ClassShutter { "java.nio.file.Files", "java.nio.file.FileSystems", "io.legado.app.data.AppDatabase", + "io.legado.app.data.AppDatabase_Impl", "io.legado.app.data.AppDatabaseKt", "io.legado.app.utils.ContextExtensionsKt", "android.content.Intent", @@ -77,6 +82,7 @@ object RhinoClassShutter : ClassShutter { "splitties.init.AppCtxKt", "android.app.ActivityThread", "android.app.AppGlobals", + "android.os.Looper", "okio.JvmSystemFileSystem", "okio.JvmFileHandle", "okio.NioSystemFileSystem", @@ -86,12 +92,18 @@ object RhinoClassShutter : ClassShutter { "android.system", "android.database", "androidx.sqlite.db", + "androidx.room", "cn.hutool.core.io", "dalvik.system", "java.nio.file", + "io.legado.app.data.dao", ) } + private val systemClassProtectedName by lazy { + hashSetOf("load", "loadLibrary", "exit") + } + fun visibleToScripts(obj: Any): Boolean { when (obj) { is ClassLoader, @@ -114,6 +126,16 @@ object RhinoClassShutter : ClassShutter { return visibleToScripts(obj.javaClass.name) } + fun wrapJavaClass(scope: Scriptable, javaClass: Class<*>): Scriptable { + return when (javaClass) { + System::class.java -> { + ProtectedNativeJavaClass(scope, javaClass, systemClassProtectedName) + } + + else -> NativeJavaClass(scope, javaClass) + } + } + override fun visibleToScripts(fullClassName: String): Boolean { if (!protectedClasses.contains(fullClassName)) { var className = fullClassName diff --git a/modules/rhino/src/main/java/com/script/rhino/RhinoWrapFactory.kt b/modules/rhino/src/main/java/com/script/rhino/RhinoWrapFactory.kt index 4f36a4792..56ec4ef21 100644 --- a/modules/rhino/src/main/java/com/script/rhino/RhinoWrapFactory.kt +++ b/modules/rhino/src/main/java/com/script/rhino/RhinoWrapFactory.kt @@ -55,4 +55,12 @@ object RhinoWrapFactory : WrapFactory() { return super.wrapAsJavaObject(cx, scope, javaObject, staticType) } + override fun wrapJavaClass( + cx: Context?, + scope: Scriptable, + javaClass: Class<*> + ): Scriptable? { + return RhinoClassShutter.wrapJavaClass(scope, javaClass) + } + } \ No newline at end of file