Horis 1 год назад
Родитель
Сommit
2e7b878f1a

+ 4 - 0
modules/rhino/src/main/java/com/script/AbstractScriptEngine.kt

@@ -54,6 +54,10 @@ abstract class AbstractScriptEngine(val bindings: Bindings? = null) : ScriptEngi
         return getBindings(ENGINE_SCOPE)?.get(key)
     }
 
+    override fun eval(reader: Reader, scope: Scriptable): Any? {
+        return eval(reader, scope, null)
+    }
+
     override suspend fun evalSuspend(script: String, scope: Scriptable): Any? {
         return this.evalSuspend(StringReader(script), scope)
     }

+ 1 - 0
modules/rhino/src/main/java/com/script/rhino/RhinoContext.kt

@@ -9,6 +9,7 @@ import kotlin.coroutines.CoroutineContext
 class RhinoContext(factory: ContextFactory) : Context(factory) {
 
     var coroutineContext: CoroutineContext? = null
+    var allowScriptRun = false
 
     @Throws(RhinoInterruptError::class)
     fun ensureActive() {

+ 11 - 28
modules/rhino/src/main/java/com/script/rhino/RhinoScriptEngine.kt

@@ -86,32 +86,6 @@ object RhinoScriptEngine : AbstractScriptEngine(), Invocable, Compilable {
         return eval(js, bindings)
     }
 
-    @Throws(ScriptException::class)
-    override fun eval(reader: Reader, scope: Scriptable): Any? {
-        val cx = Context.enter()
-        val ret: Any?
-        try {
-            var filename = this["javax.script.filename"] as? String
-            filename = filename ?: "<Unknown source>"
-            ret = cx.evaluateReader(scope, reader, filename, 1, null)
-        } catch (re: RhinoException) {
-            val line = if (re.lineNumber() == 0) -1 else re.lineNumber()
-            val msg: String = if (re is JavaScriptException) {
-                re.value.toString()
-            } else {
-                re.toString()
-            }
-            val se = ScriptException(msg, re.sourceName(), line)
-            se.initCause(re)
-            throw se
-        } catch (var14: IOException) {
-            throw ScriptException(var14)
-        } finally {
-            Context.exit()
-        }
-        return unwrapReturnValue(ret)
-    }
-
     override fun eval(
         reader: Reader,
         scope: Scriptable,
@@ -120,6 +94,7 @@ object RhinoScriptEngine : AbstractScriptEngine(), Invocable, Compilable {
         val cx = Context.enter() as RhinoContext
         val previousCoroutineContext = cx.coroutineContext
         cx.coroutineContext = coroutineContext
+        cx.allowScriptRun = true
         val ret: Any?
         try {
             var filename = this["javax.script.filename"] as? String
@@ -139,6 +114,7 @@ object RhinoScriptEngine : AbstractScriptEngine(), Invocable, Compilable {
             throw ScriptException(var14)
         } finally {
             cx.coroutineContext = previousCoroutineContext
+            cx.allowScriptRun = false
             Context.exit()
         }
         return unwrapReturnValue(ret)
@@ -146,9 +122,10 @@ object RhinoScriptEngine : AbstractScriptEngine(), Invocable, Compilable {
 
     @Throws(ContinuationPending::class)
     override suspend fun evalSuspend(reader: Reader, scope: Scriptable): Any? {
-        val cx = Context.enter()
+        val cx = Context.enter() as RhinoContext
         var ret: Any?
         withContext(VMBridgeReflect.contextLocal.asContextElement()) {
+            cx.allowScriptRun = true
             try {
                 var filename = this@RhinoScriptEngine["javax.script.filename"] as? String
                 filename = filename ?: "<Unknown source>"
@@ -186,6 +163,7 @@ object RhinoScriptEngine : AbstractScriptEngine(), Invocable, Compilable {
             } catch (var14: IOException) {
                 throw ScriptException(var14)
             } finally {
+                cx.allowScriptRun = false
                 Context.exit()
             }
         }
@@ -395,7 +373,12 @@ object RhinoScriptEngine : AbstractScriptEngine(), Invocable, Compilable {
                 args: Array<Any>
             ): Any? {
                 try {
-                    (cx as RhinoContext).ensureActive()
+                    if (cx is RhinoContext) {
+                        if (!cx.allowScriptRun) {
+                            error("Not allow run script in unauthorized way.")
+                        }
+                        cx.ensureActive()
+                    }
                     return super.doTopCall(callable, cx, scope, thisObj, args)
                 } catch (e: RhinoInterruptError) {
                     throw e.cause

+ 1 - 0
modules/rhino/src/main/java/com/script/rhino/RhinoWrapFactory.kt

@@ -55,6 +55,7 @@ object RhinoWrapFactory : WrapFactory() {
             is ClassLoader,
             is Class<*>,
             is Member,
+            is Context,
             is android.content.Context -> {
                 null
             }