Skip to content

Commit

Permalink
refactor(java): update Logger and StackGetter
Browse files Browse the repository at this point in the history
  • Loading branch information
spacelan committed Jun 26, 2019
1 parent a1097ce commit 062ed88
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 46 deletions.
1 change: 0 additions & 1 deletion java/com_baidu_openrasp_v8_Context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ static void buffer_getter(v8::Local<v8::Name> name, const v8::PropertyCallbackIn

v8::Local<v8::ObjectTemplate> 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);
Expand Down
1 change: 0 additions & 1 deletion java/com_baidu_openrasp_v8_V8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
20 changes: 4 additions & 16 deletions java/header.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<JNIEnv*>(isolate->GetData()->custom_data);
}
Expand All @@ -86,7 +75,6 @@ typedef std::unique_ptr<openrasp::Isolate, IsolateDeleter> IsolatePtr;

extern V8Class v8_class;
extern ContextClass ctx_class;
extern StackClass stack_class;
extern bool isInitialized;
extern openrasp::Snapshot* snapshot;
extern std::mutex mtx;
17 changes: 0 additions & 17 deletions java/src/main/java/com/baidu/openrasp/v8/Stack.java

This file was deleted.

5 changes: 5 additions & 0 deletions java/src/main/java/com/baidu/openrasp/v8/StackGetter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.baidu.openrasp.v8;

public interface StackGetter {
public byte[] get();
}
15 changes: 13 additions & 2 deletions java/src/main/java/com/baidu/openrasp/v8/V8.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
}

}
18 changes: 12 additions & 6 deletions java/src/test/java/com/baidu/openrasp/v8/V8Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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");
Expand All @@ -90,12 +96,12 @@ public void log(String msg) {
List<String[]> scripts = new ArrayList<String[]>();
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,
Expand All @@ -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 中文 & 😊");
Expand Down
6 changes: 3 additions & 3 deletions java/utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,19 @@ using namespace openrasp;

V8Class v8_class;
ContextClass ctx_class;
StackClass stack_class;
bool isInitialized = false;
Snapshot* snapshot = nullptr;
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<v8::Array> openrasp::get_stack(Isolate* isolate) {
auto env = GetJNIEnv(isolate);
jbyteArray jbuf = reinterpret_cast<jbyteArray>(env->CallStaticObjectMethod(stack_class.cls, stack_class.getStack));
jbyteArray jbuf = reinterpret_cast<jbyteArray>(env->CallStaticObjectMethod(v8_class.cls, v8_class.GetStack));
if (jbuf == nullptr) {
return v8::Array::New(isolate, 0);
}
Expand Down Expand Up @@ -68,6 +67,7 @@ Isolate* GetIsolate(JNIEnv* env) {
auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(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));
}
}
Expand Down

0 comments on commit 062ed88

Please sign in to comment.