diff --git a/java/com_baidu_openrasp_v8_Context.cc b/java/com_baidu_openrasp_v8_Context.cc index 365c5c2..5b849f8 100644 --- a/java/com_baidu_openrasp_v8_Context.cc +++ b/java/com_baidu_openrasp_v8_Context.cc @@ -126,7 +126,6 @@ static void buffer_getter(v8::Local name, const v8::PropertyCallbackIn v8::Local CreateRequestContextTemplate(JNIEnv* env) { auto isolate = GetIsolate(env); - v8::HandleScope handle_scope(isolate); auto obj_templ = v8::ObjectTemplate::New(isolate); for (auto& key : stringKeys) { obj_templ->SetLazyDataProperty(NewV8String(isolate, key), string_getter); diff --git a/java/com_baidu_openrasp_v8_V8.cc b/java/com_baidu_openrasp_v8_V8.cc index a604ea4..84a69b4 100644 --- a/java/com_baidu_openrasp_v8_V8.cc +++ b/java/com_baidu_openrasp_v8_V8.cc @@ -30,7 +30,6 @@ JNIEXPORT jboolean JNICALL Java_com_baidu_openrasp_v8_V8_Initialize(JNIEnv* env, v8::V8::Initialize(); v8_class = V8Class(env); ctx_class = ContextClass(env); - stack_class = StackClass(env); isInitialized = true; } return isInitialized; diff --git a/java/header.h b/java/header.h index 2900653..5757ed2 100644 --- a/java/header.h +++ b/java/header.h @@ -38,10 +38,12 @@ class V8Class { auto ref = env->FindClass("com/baidu/openrasp/v8/V8"); cls = (jclass)env->NewGlobalRef(ref); env->DeleteLocalRef(ref); - plugin_log = env->GetStaticMethodID(cls, "PluginLog", "(Ljava/lang/String;)V"); + Log = env->GetStaticMethodID(cls, "Log", "(Ljava/lang/String;)V"); + GetStack = env->GetStaticMethodID(cls, "GetStack", "()[B"); } jclass cls; - jmethodID plugin_log; + jmethodID Log; + jmethodID GetStack; }; class ContextClass { @@ -61,19 +63,6 @@ class ContextClass { jmethodID getBuffer; }; -class StackClass { - public: - StackClass() = default; - StackClass(JNIEnv* env) { - auto ref = env->FindClass("com/baidu/openrasp/v8/Stack"); - cls = (jclass)env->NewGlobalRef(ref); - env->DeleteLocalRef(ref); - getStack = env->GetStaticMethodID(cls, "getStack", "()[B"); - } - jclass cls; - jmethodID getStack; -}; - inline JNIEnv* GetJNIEnv(openrasp::Isolate* isolate) { return reinterpret_cast(isolate->GetData()->custom_data); } @@ -86,7 +75,6 @@ typedef std::unique_ptr IsolatePtr; extern V8Class v8_class; extern ContextClass ctx_class; -extern StackClass stack_class; extern bool isInitialized; extern openrasp::Snapshot* snapshot; extern std::mutex mtx; diff --git a/java/src/main/java/com/baidu/openrasp/v8/Stack.java b/java/src/main/java/com/baidu/openrasp/v8/Stack.java deleted file mode 100644 index 8ae867a..0000000 --- a/java/src/main/java/com/baidu/openrasp/v8/Stack.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.baidu.openrasp.v8; - -public abstract class Stack { - - private static Stack instance = null; - - public static void setInstance(Stack instance) { - Stack.instance = instance; - } - - public static byte[] getStack() { - return Stack.instance != null ? Stack.instance.get() : null; - } - - public abstract byte[] get(); - -} \ No newline at end of file diff --git a/java/src/main/java/com/baidu/openrasp/v8/StackGetter.java b/java/src/main/java/com/baidu/openrasp/v8/StackGetter.java new file mode 100644 index 0000000..d1e762e --- /dev/null +++ b/java/src/main/java/com/baidu/openrasp/v8/StackGetter.java @@ -0,0 +1,5 @@ +package com.baidu.openrasp.v8; + +public interface StackGetter { + public byte[] get(); +} \ No newline at end of file diff --git a/java/src/main/java/com/baidu/openrasp/v8/V8.java b/java/src/main/java/com/baidu/openrasp/v8/V8.java index 99c8ac6..73d63b6 100644 --- a/java/src/main/java/com/baidu/openrasp/v8/V8.java +++ b/java/src/main/java/com/baidu/openrasp/v8/V8.java @@ -6,6 +6,8 @@ public class V8 { public static Logger logger = null; + public static StackGetter stackGetter = null; + public synchronized static native boolean Initialize(); public synchronized static native boolean Dispose(); @@ -25,13 +27,22 @@ public static void Load() throws Exception { NativeLoader.loadLibrary("openrasp_v8_java"); } - public static void PluginLog(String msg) { + public static void Log(String msg) { if (logger != null) { logger.log(msg.replaceAll("\n$", "")); } } - public static void SetPluginLogger(Logger logger) { + public static void SetLogger(Logger logger) { V8.logger = logger; } + + public static byte[] GetStack() { + return stackGetter != null ? stackGetter.get() : null; + } + + public static void SetStackGetter(StackGetter stackGetter) { + V8.stackGetter = stackGetter; + } + } diff --git a/java/src/test/java/com/baidu/openrasp/v8/V8Test.java b/java/src/test/java/com/baidu/openrasp/v8/V8Test.java index 8fdc365..a2cf387 100644 --- a/java/src/test/java/com/baidu/openrasp/v8/V8Test.java +++ b/java/src/test/java/com/baidu/openrasp/v8/V8Test.java @@ -21,7 +21,13 @@ public static void Initialize() throws Exception { new String[] { "path", "method", "url", "querystring", "protocol", "remoteAddr", "appBasePath", "requestId" }); Context.setObjectKeys(new String[] { "json", "server", "parameter", "header" }); Context.setBufferKeys(new String[] { "body" }); - Stack.setInstance(new Stack() { + V8.SetLogger(new Logger() { + @Override + public void log(String msg) { + System.out.println(msg); + } + }); + V8.SetStackGetter(new StackGetter() { @Override public byte[] get() { return "[1,2,3,4]".getBytes(); @@ -81,7 +87,7 @@ public void Check() throws Exception { @Test public void PluginLog() { - V8.SetPluginLogger(new Logger() { + V8.SetLogger(new Logger() { @Override public void log(String msg) { assertEquals(msg, "23333"); @@ -90,12 +96,12 @@ public void log(String msg) { List scripts = new ArrayList(); scripts.add(new String[] { "test.js", "console.log(23333)" }); assertTrue(V8.CreateSnapshot("{}", scripts.toArray())); - V8.SetPluginLogger(null); + V8.SetLogger(null); } @Test public void Context() { - V8.SetPluginLogger(new Logger() { + V8.SetLogger(new Logger() { @Override public void log(String msg) { assertEquals(msg, @@ -109,12 +115,12 @@ public void log(String msg) { String params = "{\"action\":\"ignore\"}"; byte[] result = V8.Check("request", params.getBytes(), params.getBytes().length, new ContextImpl(), true, 100); assertNull(result); - V8.SetPluginLogger(null); + V8.SetLogger(null); } @Test public void Unicode() throws Exception { - V8.SetPluginLogger(new Logger() { + V8.SetLogger(new Logger() { @Override public void log(String msg) { assertEquals(msg, "test δΈ­ζ–‡ & 😊"); diff --git a/java/utils.cc b/java/utils.cc index f48033e..54b3ce2 100644 --- a/java/utils.cc +++ b/java/utils.cc @@ -22,7 +22,6 @@ using namespace openrasp; V8Class v8_class; ContextClass ctx_class; -StackClass stack_class; bool isInitialized = false; Snapshot* snapshot = nullptr; std::mutex mtx; @@ -30,12 +29,12 @@ std::mutex mtx; void openrasp::plugin_info(Isolate* isolate, const std::string& message) { auto env = GetJNIEnv(isolate); auto msg = String2Jstring(env, message); - env->CallStaticVoidMethod(v8_class.cls, v8_class.plugin_log, msg); + env->CallStaticVoidMethod(v8_class.cls, v8_class.Log, msg); } v8::Local openrasp::get_stack(Isolate* isolate) { auto env = GetJNIEnv(isolate); - jbyteArray jbuf = reinterpret_cast(env->CallStaticObjectMethod(stack_class.cls, stack_class.getStack)); + jbyteArray jbuf = reinterpret_cast(env->CallStaticObjectMethod(v8_class.cls, v8_class.GetStack)); if (jbuf == nullptr) { return v8::Array::New(isolate, 0); } @@ -68,6 +67,7 @@ Isolate* GetIsolate(JNIEnv* env) { auto millis = std::chrono::duration_cast(duration).count(); isolate = Isolate::New(snapshot, millis); isolate_ptr.reset(isolate); + v8::HandleScope handle_scope(isolate); isolate->GetData()->request_context_templ.Reset(isolate, CreateRequestContextTemplate(env)); } }