improve WallFilter

This commit is contained in:
wenshao
2013-03-29 19:18:30 +08:00
parent f85701cb1c
commit 54e3ec9323
16 changed files with 479 additions and 7 deletions
@@ -24,6 +24,7 @@ import com.alibaba.druid.sql.ast.expr.SQLBetweenExpr;
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
import com.alibaba.druid.sql.ast.expr.SQLCaseExpr;
import com.alibaba.druid.sql.ast.expr.SQLCharExpr;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.ast.expr.SQLInListExpr;
import com.alibaba.druid.sql.ast.expr.SQLIntegerExpr;
import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr;
@@ -147,4 +148,8 @@ public class MySqlEvalVisitorImpl extends MySqlASTVisitorAdapter implements SQLE
public void registerFunction(String funcName, Function function) {
functions.put(funcName, function);
}
public boolean visit(SQLIdentifierExpr x) {
return SQLEvalVisitorUtils.visit(this, x);
}
}
@@ -23,6 +23,7 @@ import java.util.Map;
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
import com.alibaba.druid.sql.ast.expr.SQLCaseExpr;
import com.alibaba.druid.sql.ast.expr.SQLCharExpr;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.ast.expr.SQLInListExpr;
import com.alibaba.druid.sql.ast.expr.SQLIntegerExpr;
import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr;
@@ -134,4 +135,8 @@ public class OracleEvalVisitor extends OracleASTVisitorAdapter implements SQLEva
public void registerFunction(String funcName, Function function) {
functions.put(funcName, function);
}
public boolean visit(SQLIdentifierExpr x) {
return SQLEvalVisitorUtils.visit(this, x);
}
}
@@ -23,6 +23,7 @@ import java.util.Map;
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
import com.alibaba.druid.sql.ast.expr.SQLCaseExpr;
import com.alibaba.druid.sql.ast.expr.SQLCharExpr;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.ast.expr.SQLInListExpr;
import com.alibaba.druid.sql.ast.expr.SQLIntegerExpr;
import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr;
@@ -134,4 +135,8 @@ public class PGEvalVisitor extends PGASTVisitorAdapter implements SQLEvalVisitor
public void registerFunction(String funcName, Function function) {
functions.put(funcName, function);
}
public boolean visit(SQLIdentifierExpr x) {
return SQLEvalVisitorUtils.visit(this, x);
}
}
@@ -23,6 +23,7 @@ import java.util.Map;
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
import com.alibaba.druid.sql.ast.expr.SQLCaseExpr;
import com.alibaba.druid.sql.ast.expr.SQLCharExpr;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.ast.expr.SQLInListExpr;
import com.alibaba.druid.sql.ast.expr.SQLIntegerExpr;
import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr;
@@ -134,4 +135,8 @@ public class SQLServerEvalVisitor extends SQLServerASTVisitorAdapter implements
public void registerFunction(String funcName, Function function) {
functions.put(funcName, function);
}
public boolean visit(SQLIdentifierExpr x) {
return SQLEvalVisitorUtils.visit(this, x);
}
}
@@ -22,6 +22,7 @@ import com.alibaba.druid.sql.visitor.functions.Function;
public interface SQLEvalVisitor extends SQLASTVisitor {
public static final String EVAL_VALUE = "eval.value";
public static final String EVAL_EXPR = "eval.expr";
public static final Object EVAL_ERROR = new Object();
Function getFunction(String funcName);
@@ -23,6 +23,7 @@ import java.util.Map;
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
import com.alibaba.druid.sql.ast.expr.SQLCaseExpr;
import com.alibaba.druid.sql.ast.expr.SQLCharExpr;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.ast.expr.SQLInListExpr;
import com.alibaba.druid.sql.ast.expr.SQLIntegerExpr;
import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr;
@@ -128,4 +129,8 @@ public class SQLEvalVisitorImpl extends SQLASTVisitorAdapter implements SQLEvalV
public void registerFunction(String funcName, Function function) {
functions.put(funcName, function);
}
public boolean visit(SQLIdentifierExpr x) {
return SQLEvalVisitorUtils.visit(this, x);
}
}
@@ -15,6 +15,7 @@
*/
package com.alibaba.druid.sql.visitor;
import static com.alibaba.druid.sql.visitor.SQLEvalVisitor.EVAL_EXPR;
import static com.alibaba.druid.sql.visitor.SQLEvalVisitor.EVAL_VALUE;
import java.math.BigDecimal;
@@ -57,6 +58,7 @@ import com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerEvalVisitor;
import com.alibaba.druid.sql.visitor.functions.Ascii;
import com.alibaba.druid.sql.visitor.functions.Concat;
import com.alibaba.druid.sql.visitor.functions.Function;
import com.alibaba.druid.sql.visitor.functions.Hex;
import com.alibaba.druid.sql.visitor.functions.Instr;
import com.alibaba.druid.sql.visitor.functions.Lcase;
import com.alibaba.druid.sql.visitor.functions.Left;
@@ -66,6 +68,7 @@ import com.alibaba.druid.sql.visitor.functions.Reverse;
import com.alibaba.druid.sql.visitor.functions.Right;
import com.alibaba.druid.sql.visitor.functions.Trim;
import com.alibaba.druid.sql.visitor.functions.Ucase;
import com.alibaba.druid.sql.visitor.functions.Unhex;
import com.alibaba.druid.util.JdbcUtils;
import com.alibaba.druid.wall.spi.WallVisitorUtils;
import com.alibaba.druid.wall.spi.WallVisitorUtils.WallConditionContext;
@@ -158,6 +161,8 @@ public class SQLEvalVisitorUtils {
functions.put("upper", Ucase.instance);
functions.put("ucase", Lcase.instance);
functions.put("lower", Lcase.instance);
functions.put("hex", Hex.instance);
functions.put("unhex", Unhex.instance);
}
public static boolean visit(SQLEvalVisitor visitor, SQLMethodInvokeExpr x) {
@@ -709,11 +714,40 @@ public class SQLEvalVisitorUtils {
}
}
if (!left.getAttributes().containsKey(EVAL_VALUE)) {
boolean leftHasValue = left.getAttributes().containsKey(EVAL_VALUE);
boolean rightHasValue = right.getAttributes().containsKey(EVAL_VALUE);
if ((!leftHasValue) && !rightHasValue) {
SQLExpr leftEvalExpr = (SQLExpr) left.getAttribute(EVAL_EXPR);
SQLExpr rightEvalExpr = (SQLExpr) right.getAttribute(EVAL_EXPR);
if (leftEvalExpr.equals(rightEvalExpr)) {
switch (x.getOperator()) {
case Like:
case Equality:
case GreaterThanOrEqual:
case LessThanOrEqual:
case NotLessThan:
case NotGreaterThan:
x.putAttribute(EVAL_VALUE, Boolean.TRUE);
return false;
case NotEqual:
case NotLike:
case GreaterThan:
case LessThan:
x.putAttribute(EVAL_VALUE, Boolean.FALSE);
return false;
default:
break;
}
}
}
if (!leftHasValue) {
return false;
}
if (!right.getAttributes().containsKey(EVAL_VALUE)) {
if (!rightHasValue) {
return false;
}
@@ -1473,4 +1507,8 @@ public class SQLEvalVisitorUtils {
return Pattern.matches(regexpr, input);
}
public static boolean visit(SQLEvalVisitor visitor, SQLIdentifierExpr x) {
x.putAttribute(EVAL_EXPR, x);
return false;
}
}
@@ -15,19 +15,44 @@
*/
package com.alibaba.druid.sql.visitor.functions;
import static com.alibaba.druid.sql.visitor.SQLEvalVisitor.EVAL_VALUE;
import org.apache.xerces.impl.dv.util.HexBin;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr;
import com.alibaba.druid.sql.parser.ParserException;
import com.alibaba.druid.sql.visitor.SQLEvalVisitor;
public class Hex implements Function {
public final static Hex instance = new Hex();
public Object eval(SQLEvalVisitor visitor, SQLMethodInvokeExpr x) {
if(x.getParameters().size() != 1) {
if (x.getParameters().size() != 1) {
throw new ParserException("argument's != 1, " + x.getParameters().size());
}
return null;
SQLExpr param0 = x.getParameters().get(0);
param0.accept(visitor);
Object param0Value = param0.getAttributes().get(EVAL_VALUE);
if (param0Value == null) {
return SQLEvalVisitor.EVAL_ERROR;
}
if (param0Value instanceof String) {
byte[] bytes = ((String) param0Value).getBytes();
String result = HexBin.encode(bytes);
return result;
}
if (param0Value instanceof Number) {
long value = ((Number) param0Value).longValue();
String result = Long.toHexString(value).toUpperCase();
return result;
}
return SQLEvalVisitor.EVAL_ERROR;
}
}
@@ -0,0 +1,77 @@
/*
* Copyright 1999-2011 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.sql.visitor.functions;
import static com.alibaba.druid.sql.visitor.SQLEvalVisitor.EVAL_VALUE;
import static com.alibaba.druid.sql.visitor.SQLEvalVisitor.EVAL_EXPR;
import java.io.UnsupportedEncodingException;
import org.apache.xerces.impl.dv.util.HexBin;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr;
import com.alibaba.druid.sql.parser.ParserException;
import com.alibaba.druid.sql.visitor.SQLEvalVisitor;
public class Unhex implements Function {
public final static Unhex instance = new Unhex();
public Object eval(SQLEvalVisitor visitor, SQLMethodInvokeExpr x) {
if (x.getParameters().size() != 1) {
throw new ParserException("argument's != 1, " + x.getParameters().size());
}
SQLExpr param0 = x.getParameters().get(0);
if (param0 instanceof SQLMethodInvokeExpr) {
SQLMethodInvokeExpr paramMethodExpr = (SQLMethodInvokeExpr) param0;
if (paramMethodExpr.getMethodName().equalsIgnoreCase("hex")) {
SQLExpr subParamExpr = paramMethodExpr.getParameters().get(0);
subParamExpr.accept(visitor);
Object param0Value = subParamExpr.getAttributes().get(EVAL_VALUE);
if (param0Value == null) {
x.putAttribute(EVAL_EXPR, subParamExpr);
return SQLEvalVisitor.EVAL_ERROR;
}
return param0Value;
}
}
param0.accept(visitor);
Object param0Value = param0.getAttributes().get(EVAL_VALUE);
if (param0Value == null) {
return SQLEvalVisitor.EVAL_ERROR;
}
if (param0Value instanceof String) {
byte[] bytes = HexBin.decode((String) param0Value);
String result;
try {
result = new String(bytes, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException(e.getMessage(), e);
}
return result;
}
return SQLEvalVisitor.EVAL_ERROR;
}
}
@@ -0,0 +1,37 @@
/*
* Copyright 1999-2011 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.filter.wall;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.druid.wall.WallUtils;
/**
* SQLServerWallTest
*
* @author RaymondXiu
* @version 1.0, 2012-3-18
* @see
*/
public class MySqlWallTest27 extends TestCase {
public void test_false() throws Exception {
Assert.assertFalse(WallUtils.isValidateMySql(//
"select * from t where hex('abc') = '616263'")); //
}
}
@@ -0,0 +1,37 @@
/*
* Copyright 1999-2011 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.filter.wall;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.druid.wall.WallUtils;
/**
* SQLServerWallTest
*
* @author RaymondXiu
* @version 1.0, 2012-3-18
* @see
*/
public class MySqlWallTest28 extends TestCase {
public void test_false() throws Exception {
Assert.assertFalse(WallUtils.isValidateMySql(//
"select * from t where hex(123) = '7B'")); //
}
}
@@ -0,0 +1,37 @@
/*
* Copyright 1999-2011 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.filter.wall;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.druid.wall.WallUtils;
/**
* SQLServerWallTest
*
* @author RaymondXiu
* @version 1.0, 2012-3-18
* @see
*/
public class MySqlWallTest29 extends TestCase {
public void test_false() throws Exception {
Assert.assertFalse(WallUtils.isValidateMySql(//
"select * from t where UNHEX('4D7953514C') = 'MySQL'")); //
}
}
@@ -0,0 +1,37 @@
/*
* Copyright 1999-2011 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.filter.wall;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.druid.wall.WallUtils;
/**
* SQLServerWallTest
*
* @author RaymondXiu
* @version 1.0, 2012-3-18
* @see
*/
public class MySqlWallTest30 extends TestCase {
public void test_false() throws Exception {
Assert.assertFalse(WallUtils.isValidateMySql(//
"select * from t where UNHEX(HEX('MySQL')) = 'MySQL'")); //
}
}
@@ -0,0 +1,37 @@
/*
* Copyright 1999-2011 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.filter.wall;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.druid.wall.WallUtils;
/**
* SQLServerWallTest
*
* @author RaymondXiu
* @version 1.0, 2012-3-18
* @see
*/
public class MySqlWallTest31 extends TestCase {
public void test_false() throws Exception {
Assert.assertFalse(WallUtils.isValidateMySql(//
"select * from t where UNHEX(HEX(FNAME)) = FNAME")); //
}
}
@@ -0,0 +1,52 @@
/*
* Copyright 1999-2011 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.filter.wall;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.druid.wall.WallUtils;
/**
* SQLServerWallTest
*
* @author RaymondXiu
* @version 1.0, 2012-3-18
* @see
*/
public class MySqlWallTest32 extends TestCase {
public void test_false() throws Exception {
Assert.assertFalse(WallUtils.isValidateMySql(//
"select * from t where NOT(UNHEX(HEX(FNAME)) != FNAME)")); //
}
public void test_false_1() throws Exception {
Assert.assertFalse(WallUtils.isValidateMySql(//
"select * from t where NOT(UNHEX(HEX(FNAME)) > FNAME)")); //
}
public void test_false_2() throws Exception {
Assert.assertFalse(WallUtils.isValidateMySql(//
"select * from t where NOT(UNHEX(HEX(FNAME)) < FNAME)")); //
}
public void test_false_3() throws Exception {
Assert.assertFalse(WallUtils.isValidateMySql(//
"select * from t where NOT(UNHEX(HEX(FNAME)) NOT LIKE FNAME)")); //
}
}
@@ -0,0 +1,69 @@
/*
* Copyright 1999-2011 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.mysql;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import junit.framework.TestCase;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.util.JdbcUtils;
public class MySqlHexTest extends TestCase {
final int COUNT = 800;
private String jdbcUrl;
private String user;
private String password;
private String driverClass;
private DruidDataSource dataSource;
protected void setUp() throws Exception {
jdbcUrl = "jdbc:mysql://10.20.149.16:3306/circe0_test";
user = "circe";
password = "circe";
driverClass = "com.mysql.jdbc.Driver";
dataSource = new DruidDataSource();
dataSource.setDriverClassName(driverClass);
dataSource.setUrl(jdbcUrl);
dataSource.setPoolPreparedStatements(true);
dataSource.setUsername(user);
dataSource.setPassword(password);
}
public void test_0() throws Exception {
Connection conn = dataSource.getConnection();
String sql = "select hex('123'),hex(123)";
PreparedStatement stmt = conn.prepareStatement(sql);
ResultSet rs = stmt.executeQuery();
JdbcUtils.printResultSet(rs);
rs.close();
stmt.close();
conn.close();
}
}