mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
支持DoPrivileged
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package com.alibaba.druid.wall;
|
||||
|
||||
import java.security.PrivilegedAction;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
@@ -18,17 +19,19 @@ import com.alibaba.druid.wall.violation.SyntaxErrorViolation;
|
||||
public abstract class WallProvider {
|
||||
|
||||
// Dummy value to associate with an Object in the backing Map
|
||||
private static final Object PRESENT = new Object();
|
||||
private static final Object PRESENT = new Object();
|
||||
|
||||
private LinkedHashMap<String, Object> whiteList;
|
||||
private LinkedHashMap<String, Object> whiteList;
|
||||
|
||||
private int whileListMaxSize = 1024;
|
||||
private int whileListMaxSize = 1024;
|
||||
|
||||
private int whiteSqlMaxLength = 1024; // 1k
|
||||
private int whiteSqlMaxLength = 1024; // 1k
|
||||
|
||||
protected final WallConfig config;
|
||||
protected final WallConfig config;
|
||||
|
||||
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
|
||||
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
|
||||
|
||||
private static final ThreadLocal<Boolean> privileged = new ThreadLocal<Boolean>();
|
||||
|
||||
public WallProvider(WallConfig config){
|
||||
this.config = config;
|
||||
@@ -50,7 +53,7 @@ public abstract class WallProvider {
|
||||
lock.writeLock().unlock();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public Set<String> getWhiteList() {
|
||||
Set<String> hashSet = new HashSet<String>();
|
||||
lock.readLock().lock();
|
||||
@@ -61,10 +64,10 @@ public abstract class WallProvider {
|
||||
} finally {
|
||||
lock.readLock().unlock();
|
||||
}
|
||||
|
||||
|
||||
return hashSet;
|
||||
}
|
||||
|
||||
|
||||
public void clearCache() {
|
||||
lock.writeLock().lock();
|
||||
try {
|
||||
@@ -98,6 +101,10 @@ public abstract class WallProvider {
|
||||
}
|
||||
|
||||
public List<Violation> check(String sql) {
|
||||
if (privileged.get() == Boolean.TRUE) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
// first step, check whiteList
|
||||
boolean isWhite = whiteContains(sql);
|
||||
if (isWhite) {
|
||||
@@ -145,4 +152,12 @@ public abstract class WallProvider {
|
||||
return visitor.getViolations();
|
||||
}
|
||||
|
||||
public static <T> T doPrivileged(PrivilegedAction<T> action) {
|
||||
privileged.set(Boolean.TRUE);
|
||||
try {
|
||||
return action.run();
|
||||
} finally {
|
||||
privileged.set(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.alibaba.druid.bvt.filter.wall;
|
||||
|
||||
import java.security.PrivilegedAction;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.wall.WallProvider;
|
||||
import com.alibaba.druid.wall.WallUtils;
|
||||
|
||||
public class DoPrivilegedTest extends TestCase {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
Assert.assertFalse(WallUtils.isValidateMySql("select @@version_compile_os"));
|
||||
}
|
||||
|
||||
public void test_1() throws Exception {
|
||||
WallProvider.doPrivileged(new PrivilegedAction<Object>() {
|
||||
@Override
|
||||
public Object run() {
|
||||
Assert.assertTrue(WallUtils.isValidateMySql("select @@version_compile_os"));
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.alibaba.druid.sql;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLBinaryOperator;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIntegerExpr;
|
||||
import com.alibaba.druid.sql.parser.SQLExprParser;
|
||||
|
||||
|
||||
public class SQLExprParserTest extends TestCase {
|
||||
public void test_binary() throws Exception {
|
||||
SQLExprParser exprParser = new SQLExprParser("AGE > 5");
|
||||
SQLBinaryOpExpr binaryOpExpr = (SQLBinaryOpExpr) exprParser.expr();
|
||||
|
||||
Assert.assertEquals(SQLBinaryOperator.GreaterThan, binaryOpExpr.getOperator());
|
||||
|
||||
SQLIdentifierExpr left = (SQLIdentifierExpr) binaryOpExpr.getLeft();
|
||||
SQLIntegerExpr right = (SQLIntegerExpr) binaryOpExpr.getRight();
|
||||
|
||||
Assert.assertEquals("AGE", left.getName());
|
||||
Assert.assertEquals(5, right.getNumber().intValue());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user