improve wall filter

This commit is contained in:
wenshao
2013-04-02 01:33:48 +08:00
parent 5f689f7aa5
commit ba45a857c4
18 changed files with 487 additions and 144 deletions
@@ -23,16 +23,14 @@ import com.alibaba.druid.wall.spi.OracleWallProvider;
/**
* @author wenshao
*
*/
public class WallDenySchemaTest extends TestCase {
public void testORACLE() throws Exception {
String sql = "SELECT F1, F2 FROM ALL_ALL_TABLES";
OracleWallProvider provider = new OracleWallProvider();
Assert.assertFalse(provider.checkValid(sql));
Assert.assertEquals(1, provider.getDenniedTableStat("ALL_ALL_TABLES").getDenyCount());
}
}
@@ -23,16 +23,14 @@ import com.alibaba.druid.wall.spi.OracleWallProvider;
/**
* @author wenshao
*
*/
public class WallDenyTableTest extends TestCase {
public void testORACLE() throws Exception {
String sql = "SELECT F1, F2 FROM SYS.ABC";
OracleWallProvider provider = new OracleWallProvider();
Assert.assertFalse(provider.checkValid(sql));
Assert.assertEquals(1, provider.getDenniedSchemaStat("SYS").getDenyCount());
}
}
@@ -25,16 +25,18 @@ import com.alibaba.druid.wall.spi.OracleWallProvider;
/**
* 这个场景,检测可疑的Having条件
*
* @author wenshao
*
*/
public class WallReadOnlyTest2 extends TestCase {
private WallConfig config = new WallConfig();
protected void setUp() throws Exception {
config.addReadOnlyTable("members");
}
private String sql = "SELECT F1, F2 members";
private String sql = "SELECT F1, F2 members";
private String insert_sql = "INSERT INTO members (FID, FNAME) VALUES (?, ?)";
private String update_sql = "UPDATE members SET FNAME = ? WHERe FID = ?";
private String delete_sql = "DELETE members WHERE FID = ?";
@@ -42,28 +44,20 @@ public class WallReadOnlyTest2 extends TestCase {
public void testMySql() throws Exception {
MySqlWallProvider provider = new MySqlWallProvider(config);
Assert.assertTrue(provider.checkValid(sql));
Assert.assertNull(provider.getDenniedTableStat("members"));
Assert.assertFalse(provider.checkValid(insert_sql));
Assert.assertFalse(provider.checkValid(update_sql));
Assert.assertFalse(provider.checkValid(delete_sql));
Assert.assertEquals(3, provider.getDenniedTableStat("members").getDenyCount());
Assert.assertEquals(3, provider.getDenniedTableStat("Members").getDenyCount());
}
public void testORACLE() throws Exception {
OracleWallProvider provider = new OracleWallProvider(config);
Assert.assertTrue(provider.checkValid(sql));
Assert.assertNull(provider.getDenniedTableStat("members"));
Assert.assertFalse(provider.checkValid(insert_sql));
Assert.assertFalse(provider.checkValid(update_sql));
Assert.assertFalse(provider.checkValid(delete_sql));
Assert.assertEquals(3, provider.getDenniedTableStat("members").getDenyCount());
Assert.assertEquals(3, provider.getDenniedTableStat("Members").getDenyCount());
}
}
@@ -0,0 +1,38 @@
package com.alibaba.druid.bvt.filter.wall;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.druid.wall.WallContext;
import com.alibaba.druid.wall.WallProvider;
import com.alibaba.druid.wall.WallTableStat;
import com.alibaba.druid.wall.spi.MySqlWallProvider;
public class WallStatTest_WhiteList extends TestCase {
protected void setUp() throws Exception {
WallContext.clearContext();
}
protected void tearDown() throws Exception {
WallContext.clearContext();
}
public void testMySql() throws Exception {
WallProvider provider = new MySqlWallProvider();
for (int i = 0; i < 1001; ++i) {
String sql = "select * from t where id = " + i;
Assert.assertTrue(provider.checkValid(sql));
}
WallTableStat tableStat = provider.getTableStat("t");
Assert.assertEquals(1001, tableStat.getSelectCount());
Assert.assertEquals(0, provider.getBlackListHitCount());
Assert.assertEquals(0, provider.getWhiteListHitCount());
Assert.assertEquals(1000, provider.getWhiteList().size());
Assert.assertEquals(1001, provider.getCheckCount());
}
}
@@ -0,0 +1,39 @@
package com.alibaba.druid.bvt.filter.wall;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.druid.wall.WallContext;
import com.alibaba.druid.wall.WallProvider;
import com.alibaba.druid.wall.WallTableStat;
import com.alibaba.druid.wall.spi.MySqlWallProvider;
public class WallStatTest_blackList_1 extends TestCase {
protected void setUp() throws Exception {
WallContext.clearContext();
}
protected void tearDown() throws Exception {
WallContext.clearContext();
}
public void testMySql() throws Exception {
WallProvider provider = new MySqlWallProvider();
for (int i = 0; i < 1001; ++i) {
String sql = "select * from t where id = " + i + " OR 1 = 1";
Assert.assertFalse(provider.checkValid(sql));
}
WallTableStat tableStat = provider.getTableStat("t");
Assert.assertEquals(1001, tableStat.getSelectCount());
Assert.assertEquals(0, provider.getBlackListHitCount());
Assert.assertEquals(0, provider.getWhiteListHitCount());
Assert.assertEquals(0, provider.getWhiteList().size());
Assert.assertEquals(1000, provider.getBlackList().size());
Assert.assertEquals(1001, provider.getCheckCount());
}
}
@@ -0,0 +1,48 @@
package com.alibaba.druid.bvt.filter.wall;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.druid.wall.WallContext;
import com.alibaba.druid.wall.WallFunctionStat;
import com.alibaba.druid.wall.WallProvider;
import com.alibaba.druid.wall.WallTableStat;
import com.alibaba.druid.wall.spi.MySqlWallProvider;
public class WallStatTest_function_stats extends TestCase {
private String sql = "select len(fname), len(fdesc) from t";
protected void setUp() throws Exception {
WallContext.clearContext();
}
protected void tearDown() throws Exception {
WallContext.clearContext();
}
public void testMySql() throws Exception {
WallProvider provider = new MySqlWallProvider();
Assert.assertTrue(provider.checkValid(sql));
{
WallTableStat tableStat = provider.getTableStat("t");
Assert.assertEquals(1, tableStat.getSelectCount());
}
{
WallFunctionStat functionStat = provider.getFunctionStat("len");
Assert.assertEquals(2, functionStat.getInvokeCount());
}
Assert.assertTrue(provider.checkValid(sql));
{
WallTableStat tableStat = provider.getTableStat("t");
Assert.assertEquals(2, tableStat.getSelectCount());
}
{
WallFunctionStat functionStat = provider.getFunctionStat("len");
Assert.assertEquals(4, functionStat.getInvokeCount());
}
}
}
@@ -18,6 +18,8 @@ package com.alibaba.druid.bvt.pool;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collections;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicLong;
@@ -34,6 +36,8 @@ import com.alibaba.druid.proxy.jdbc.ConnectionProxy;
import com.alibaba.druid.spring.IUserService;
import com.alibaba.druid.spring.User;
import com.alibaba.druid.stat.DruidDataSourceStatManager;
import com.alibaba.druid.stat.DruidStatService;
import com.alibaba.druid.support.json.JSONUtils;
public class SpringIbatisFilterTest extends TestCase {
@@ -101,6 +105,11 @@ public class SpringIbatisFilterTest extends TestCase {
conn.close();
}
Assert.assertEquals(1, DruidDataSourceStatManager.getInstance().getDataSourceList().size());
Map<String, Object> wallStats = DruidStatService.getInstance().getWallStatMap(Collections.<String, String>emptyMap());
System.out.println("wall-stats : " + JSONUtils.toJSONString(wallStats));
context.close();