diff --git a/src/main/java/com/alibaba/druid/wall/spi/MySqlWallVisitor.java b/src/main/java/com/alibaba/druid/wall/spi/MySqlWallVisitor.java index 1aa506e897..4fb1db7d76 100644 --- a/src/main/java/com/alibaba/druid/wall/spi/MySqlWallVisitor.java +++ b/src/main/java/com/alibaba/druid/wall/spi/MySqlWallVisitor.java @@ -63,7 +63,6 @@ import com.alibaba.druid.wall.WallContext; import com.alibaba.druid.wall.WallProvider; import com.alibaba.druid.wall.WallSqlTableStat; import com.alibaba.druid.wall.WallVisitor; -import com.alibaba.druid.wall.spi.WallVisitorUtils.WallConditionContext; import com.alibaba.druid.wall.violation.ErrorCode; import com.alibaba.druid.wall.violation.IllegalSQLObjectViolation; @@ -178,13 +177,13 @@ public class MySqlWallVisitor extends MySqlASTVisitorAdapter implements WallVisi public boolean visit(Limit x) { if (x.getRowCount() instanceof SQLNumericLiteralExpr) { WallContext context = WallContext.current(); - + int rowCount = ((SQLNumericLiteralExpr) x.getRowCount()).getNumber().intValue(); if (rowCount == 0) { if (context != null) { context.incrementWarnnings(); } - + if (!provider.getConfig().isLimitZeroAllow()) { this.getViolations().add(new IllegalSQLObjectViolation(ErrorCode.LIMIT_ZERO, "limit row 0", this.toSQL(x))); @@ -209,8 +208,21 @@ public class MySqlWallVisitor extends MySqlASTVisitorAdapter implements WallVisi if (!checkVar(x.getParent(), x.getName())) { boolean isTop = WallVisitorUtils.isTopNoneFromSelect(this, x); if (!isTop) { - violations.add(new IllegalSQLObjectViolation(ErrorCode.VARIANT_DENY, "variable not allow : " - + x.getName(), toSQL(x))); + boolean allow = true; + if (isDeny(varName)) { + allow = false; + } + + if (allow) { + if (WallVisitorUtils.isWhereOrHaving(x)) { + allow = false; + } + } + + if (!allow) { + violations.add(new IllegalSQLObjectViolation(ErrorCode.VARIANT_DENY, "variable not allow : " + + x.getName(), toSQL(x))); + } } } return false; @@ -248,6 +260,14 @@ public class MySqlWallVisitor extends MySqlASTVisitorAdapter implements WallVisi return false; } + + public boolean isDeny(String varName) { + if (varName.startsWith("@@")) { + varName = varName.substring(2); + } + + return config.getDenyVariants().contains(varName); + } public boolean visit(SQLVariantRefExpr x) { String varName = x.getName(); @@ -256,10 +276,23 @@ public class MySqlWallVisitor extends MySqlASTVisitorAdapter implements WallVisi } if (varName.startsWith("@@") && !checkVar(x.getParent(), x.getName())) { - boolean noneCondition = WallVisitorUtils.getWallConditionContext() == null; - if (!noneCondition) { - violations.add(new IllegalSQLObjectViolation(ErrorCode.VARIANT_DENY, "variable not allow : " - + x.getName(), toSQL(x))); + boolean isTop = WallVisitorUtils.isTopNoneFromSelect(this, x); + if (!isTop) { + boolean allow = true; + if (isDeny(varName)) { + allow = false; + } + + if (allow) { + if (WallVisitorUtils.isWhereOrHaving(x)) { + allow = false; + } + } + + if (!allow) { + violations.add(new IllegalSQLObjectViolation(ErrorCode.VARIANT_DENY, "variable not allow : " + + x.getName(), toSQL(x))); + } } } diff --git a/src/main/java/com/alibaba/druid/wall/spi/WallVisitorUtils.java b/src/main/java/com/alibaba/druid/wall/spi/WallVisitorUtils.java index 6ae42251d6..8a0d28374e 100644 --- a/src/main/java/com/alibaba/druid/wall/spi/WallVisitorUtils.java +++ b/src/main/java/com/alibaba/druid/wall/spi/WallVisitorUtils.java @@ -60,6 +60,7 @@ import com.alibaba.druid.sql.ast.statement.SQLInsertStatement; import com.alibaba.druid.sql.ast.statement.SQLJoinTableSource; import com.alibaba.druid.sql.ast.statement.SQLRollbackStatement; import com.alibaba.druid.sql.ast.statement.SQLSelect; +import com.alibaba.druid.sql.ast.statement.SQLSelectGroupByClause; import com.alibaba.druid.sql.ast.statement.SQLSelectItem; import com.alibaba.druid.sql.ast.statement.SQLSelectQuery; import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock; @@ -622,6 +623,40 @@ public class WallVisitorUtils { return false; } + public static boolean isWhereOrHaving(SQLObject x) { + if (x == null) { + return false; + } + + for (;;) { + SQLObject parent = x.getParent(); + + if (parent == null) { + return false; + } + + if (parent instanceof SQLSelectQueryBlock) { + SQLSelectQueryBlock query = (SQLSelectQueryBlock) parent; + if (query.getWhere() == x) { + return true; + } else { + return false; + } + } + + if (parent instanceof SQLSelectGroupByClause) { + SQLSelectGroupByClause groupBy = (SQLSelectGroupByClause) parent; + if (x == groupBy.getHaving()) { + return true; + } else { + return false; + } + } + + x = parent; + } + } + public static class WallConditionContext { private boolean partAlwayrue; diff --git a/src/main/resources/META-INF/druid/wall/mysql/deny-variant.txt b/src/main/resources/META-INF/druid/wall/mysql/deny-variant.txt new file mode 100644 index 0000000000..9c0c1da30a --- /dev/null +++ b/src/main/resources/META-INF/druid/wall/mysql/deny-variant.txt @@ -0,0 +1,3 @@ +basedir +version_compile_os +version \ No newline at end of file diff --git a/src/test/java/com/alibaba/druid/bvt/filter/wall/MySqlWallPermitVariantTest.java b/src/test/java/com/alibaba/druid/bvt/filter/wall/MySqlWallPermitVariantTest.java index 358506609e..bd48b1b3bf 100644 --- a/src/test/java/com/alibaba/druid/bvt/filter/wall/MySqlWallPermitVariantTest.java +++ b/src/test/java/com/alibaba/druid/bvt/filter/wall/MySqlWallPermitVariantTest.java @@ -21,6 +21,7 @@ import org.junit.Assert; import com.alibaba.druid.wall.WallConfig; import com.alibaba.druid.wall.WallUtils; +import com.alibaba.druid.wall.spi.MySqlWallProvider; /** * 这个场景测试访问Oracle系统对象 @@ -30,16 +31,16 @@ import com.alibaba.druid.wall.WallUtils; public class MySqlWallPermitVariantTest extends TestCase { public void test_allow() throws Exception { - WallConfig config = new WallConfig(); - config.setVariantCheck(false); + MySqlWallProvider provider = new MySqlWallProvider(); + provider.getConfig().setVariantCheck(false); - Assert.assertTrue(WallUtils.isValidateMySql("select @@version_compile_os FROM X", config)); + Assert.assertTrue(provider.checkValid("select @@version_compile_os FROM X")); } public void test_not_allow() throws Exception { - WallConfig config = new WallConfig(); - config.setVariantCheck(true); + MySqlWallProvider provider = new MySqlWallProvider(); + provider.getConfig().setVariantCheck(true); - Assert.assertFalse(WallUtils.isValidateMySql("select @@version_compile_os FROM X", config)); + Assert.assertFalse(provider.checkValid("select @@version_compile_os FROM X")); } } diff --git a/src/test/java/com/alibaba/druid/bvt/filter/wall/MySqlWallTest72.java b/src/test/java/com/alibaba/druid/bvt/filter/wall/MySqlWallTest72.java index c2ec53d1e1..943edca932 100644 --- a/src/test/java/com/alibaba/druid/bvt/filter/wall/MySqlWallTest72.java +++ b/src/test/java/com/alibaba/druid/bvt/filter/wall/MySqlWallTest72.java @@ -37,7 +37,7 @@ public class MySqlWallTest72 extends TestCase { provider.getConfig().setCommentAllow(true); Assert.assertFalse(provider.checkValid(// - "select * from t /**/ where 1=1")); + "select * from t /**/ where fid = 123 AND 1=1")); Assert.assertEquals(1, provider.getTableStats().size()); } diff --git a/src/test/java/com/alibaba/druid/bvt/pool/ConfigErrorTest3.java b/src/test/java/com/alibaba/druid/bvt/pool/ConfigErrorTest3.java index c96d1e9464..5d839684ba 100644 --- a/src/test/java/com/alibaba/druid/bvt/pool/ConfigErrorTest3.java +++ b/src/test/java/com/alibaba/druid/bvt/pool/ConfigErrorTest3.java @@ -17,9 +17,10 @@ package com.alibaba.druid.bvt.pool; import java.lang.reflect.Field; -import junit.framework.Assert; import junit.framework.TestCase; +import org.junit.Assert; + import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.support.logging.Log; @@ -50,6 +51,6 @@ public class ConfigErrorTest3 extends TestCase { Assert.assertEquals(0, LOG.getErrorCount()); dataSource.init(); - Assert.assertEquals(1, LOG.getErrorCount()); + Assert.assertEquals(0, LOG.getErrorCount()); } } diff --git a/src/test/java/com/alibaba/druid/bvt/pool/exception/OracleExceptionSorterTest_concurrent.java b/src/test/java/com/alibaba/druid/bvt/pool/exception/OracleExceptionSorterTest_concurrent.java new file mode 100644 index 0000000000..884e8ea066 --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/pool/exception/OracleExceptionSorterTest_concurrent.java @@ -0,0 +1,105 @@ +package com.alibaba.druid.bvt.pool.exception; + +import java.sql.Connection; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.concurrent.CountDownLatch; + +import junit.framework.TestCase; + +import org.junit.Assert; + +import com.alibaba.druid.mock.MockConnection; +import com.alibaba.druid.pool.DruidDataSource; +import com.alibaba.druid.pool.vendor.OracleExceptionSorter; +import com.alibaba.druid.stat.DruidDataSourceStatManager; +import com.alibaba.druid.stat.JdbcStatManager; +import com.alibaba.druid.test.util.OracleMockDriver; +import com.alibaba.druid.util.JdbcUtils; + +public class OracleExceptionSorterTest_concurrent extends TestCase { + + private DruidDataSource dataSource; + + protected void setUp() throws Exception { + Assert.assertEquals(0, JdbcStatManager.getInstance().getSqlList().size()); + + dataSource = new DruidDataSource(); + + dataSource.setExceptionSorter(new OracleExceptionSorter()); + + dataSource.setDriver(new OracleMockDriver()); + dataSource.setUrl("jdbc:mock:xxx"); + dataSource.setPoolPreparedStatements(true); + dataSource.setMaxOpenPreparedStatements(100); + dataSource.setMaxActive(1); + dataSource.setMaxWait(1000 * 100); + } + + @Override + protected void tearDown() throws Exception { + JdbcUtils.close(dataSource); + Assert.assertEquals(0, DruidDataSourceStatManager.getInstance().getDataSourceList().size()); + } + + public void test_connect() throws Exception { + final CountDownLatch latch_0 = new CountDownLatch(1); + + Thread errorThread = new Thread() { + + public void run() { + try { + Connection conn = dataSource.getConnection(); + + latch_0.countDown(); + + MockConnection mockConn = conn.unwrap(MockConnection.class); + Assert.assertNotNull(mockConn); + + SQLException exception = new SQLException("xx", "xxx", 28); + mockConn.setError(exception); + + try { + conn.createStatement(); + } catch (SQLException ex) { + // ex.printStackTrace(); + } + conn.close(); + } catch (Exception error) { + error.printStackTrace(); + } + } + }; + errorThread.start(); + + final CountDownLatch workLatch = new CountDownLatch(2); + final CountDownLatch workCompleteLatch = new CountDownLatch(2); + for (int i = 0; i < 2; ++i) { + Thread thread = new Thread() { + + public void run() { + try { + for (int i = 0; i < 1000; ++i) { + workLatch.countDown(); + Connection conn = dataSource.getConnection(); + conn.close(); + } + } catch (Exception ex) { + ex.printStackTrace(); + } finally { + workCompleteLatch.countDown(); + } + } + }; + thread.start(); + } + workLatch.await(); + + latch_0.countDown(); + + workCompleteLatch.await(); + + Assert.assertEquals(2001, dataSource.getConnectCount()); + } + +} diff --git a/src/test/java/com/alibaba/druid/pool/oracle/DruidDataSourceTest_oracle3.java b/src/test/java/com/alibaba/druid/pool/oracle/DruidDataSourceTest_oracle3.java new file mode 100644 index 0000000000..4513b7bcff --- /dev/null +++ b/src/test/java/com/alibaba/druid/pool/oracle/DruidDataSourceTest_oracle3.java @@ -0,0 +1,87 @@ +package com.alibaba.druid.pool.oracle; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.Statement; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import com.alibaba.druid.pool.DruidDataSource; +import com.alibaba.druid.pool.vendor.OracleValidConnectionChecker; + +/** + * 这个场景测试minIdle > maxActive + * + * @author wenshao + */ +public class DruidDataSourceTest_oracle3 extends TestCase { + + private DruidDataSource dataSource; + + protected void setUp() throws Exception { + dataSource = new DruidDataSource(); + dataSource.setUrl("jdbc:oracle:thin:@192.168.122.184:1521:ORCL"); + dataSource.setUsername("system"); + dataSource.setPassword("druid"); + dataSource.setInitialSize(1); + dataSource.setMaxActive(14); + dataSource.setMinIdle(1); + dataSource.setMinEvictableIdleTimeMillis(300 * 1000); // 300 / 10 + dataSource.setTimeBetweenEvictionRunsMillis(180 * 1000); // 180 / 10 + dataSource.setTestWhileIdle(false); + dataSource.setTestOnBorrow(false); + dataSource.setValidationQuery("SELECT 1 FROM DUAL"); + dataSource.setFilters("stat,log4j"); + } + + protected void tearDown() throws Exception { + dataSource.close(); + } + + public void test_error() throws Exception { + dataSource.init(); + Assert.assertTrue(dataSource.isOracle()); + Assert.assertTrue(dataSource.getValidConnectionChecker() instanceof OracleValidConnectionChecker); + + try { + Connection conn = dataSource.getConnection(); + Statement stmt = conn.createStatement(); + ResultSet rs = stmt.executeQuery("SELECT 1 FROM DUAL"); + while (rs.next()) { + + } + rs.close(); + stmt.close(); + conn.close(); + } catch (Exception e) { + e.printStackTrace(); + } + try { + Connection conn = dataSource.getConnection(); + Statement stmt = conn.createStatement(); + ResultSet rs = stmt.executeQuery("SELECT 1 FROM DUAL"); + while (rs.next()) { + + } + rs.close(); + stmt.close(); + conn.close(); + } catch (Exception e) { + e.printStackTrace(); + } + try { + Connection conn = dataSource.getConnection(); + Statement stmt = conn.createStatement(); + ResultSet rs = stmt.executeQuery("SELECT 1 FROM DUAL"); + while (rs.next()) { + + } + rs.close(); + stmt.close(); + conn.close(); + } catch (Exception e) { + e.printStackTrace(); + } + } +}