Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: expression injection RCE #1241

Merged
merged 1 commit into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
package org.dromara.hertzbeat.common.config;

import com.googlecode.aviator.AviatorEvaluator;
import com.googlecode.aviator.AviatorEvaluatorInstance;
import com.googlecode.aviator.Feature;
import com.googlecode.aviator.Options;
import com.googlecode.aviator.lexer.token.OperatorType;
import com.googlecode.aviator.runtime.function.AbstractFunction;
import com.googlecode.aviator.runtime.type.*;
Expand All @@ -42,13 +45,22 @@ public class AviatorConfiguration {

@Bean
public void configAviatorEvaluator() {
AviatorEvaluatorInstance instance = AviatorEvaluator.getInstance();

// 配置AviatorEvaluator使用LRU缓存编译后的表达式
AviatorEvaluator.getInstance()
instance
.useLRUExpressionCache(AVIATOR_LRU_CACHE_SIZE)
.addFunction(new StrEqualFunction());

// 配置Aviator语法特性集合
instance.setOption(Options.FEATURE_SET,
Feature.asSet(Feature.If,
Feature.Assignment,
Feature.Let,
Feature.StringInterpolation));

// 配置自定义aviator函数
AviatorEvaluator.getInstance().addOpFunction(OperatorType.BIT_OR, new AbstractFunction() {
instance.addOpFunction(OperatorType.BIT_OR, new AbstractFunction() {
@Override
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1,
final AviatorObject arg2) {
Expand All @@ -72,9 +84,9 @@ public String getName() {
}
});

AviatorEvaluator.getInstance().addFunction(new StrContainsFunction());
AviatorEvaluator.getInstance().addFunction(new ObjectExistsFunction());
AviatorEvaluator.getInstance().addFunction(new StrMatchesFunction());
instance.addFunction(new StrContainsFunction());
instance.addFunction(new ObjectExistsFunction());
instance.addFunction(new StrMatchesFunction());
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.dromara.hertzbeat.common.config;

import com.googlecode.aviator.AviatorEvaluator;
import com.googlecode.aviator.exception.UnsupportedFeatureException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -96,4 +97,21 @@ void testCustomStringFunctions() {
Boolean res13 = (Boolean) AviatorEvaluator.compile(expr10).execute(env);
Assertions.assertFalse(res13);
}

@Test
void testRCE() {
// test if 'new' syntax is disabled to prevent RCE
Assertions.assertThrows(UnsupportedFeatureException.class, () -> {
String expr1 = "let d = new java.util.Date();\n" +
"p(type(d));\n" +
"p(d);";
AviatorEvaluator.compile(expr1, true).execute();
});
// test allowed features
String expr2 = "let a = 0;\n" +
"if (\"#{a}\" == \"0\") { a = -1; }\n" +
"a == -1";
Boolean result = (Boolean) AviatorEvaluator.compile(expr2, true).execute();
Assertions.assertTrue(result);
}
}
Loading