improve sql wall

This commit is contained in:
wenshao
2013-08-28 16:47:58 +08:00
parent faee5b92b7
commit 670c376feb
9 changed files with 233 additions and 53 deletions
@@ -64,19 +64,19 @@ public class MySqlLexer extends Lexer {
super(input);
super.keywods = DEFAULT_MYSQL_KEYWORDS;
}
public void scanSharp() {
if (ch != '#') {
throw new ParserException("illegal stat");
}
if (charAt(pos + 1) == '{') {
scanVariable();
return;
}
Token lastToken = this.token;
scanChar();
mark = pos;
bufPos = 0;
@@ -102,10 +102,10 @@ public class MySqlLexer extends Lexer {
scanChar();
bufPos++;
}
stringVal = subString(mark, bufPos);
token = Token.LINE_COMMENT;
if (commentHandler != null && commentHandler.handle(lastToken, stringVal)) {
return;
}
@@ -315,7 +315,7 @@ public class MySqlLexer extends Lexer {
switch (ch) {
case '\0':
putChar('\0');
putChar(ch = '\0');
break;
case '\'':
putChar('\'');
@@ -345,20 +345,19 @@ public class MySqlLexer extends Lexer {
putChar(ch);
break;
}
scanChar();
}
if (ch == '\'') {
scanChar();
if (ch != '\'') {
token = LITERAL_CHARS;
break;
} else {
initBuff(bufPos);
arraycopy(mark + 1, buf, 0, bufPos);
hasSpecial = true;
putChar('\'');
continue;
} else {
if (ch == '\'') {
scanChar();
if (ch != '\'') {
token = LITERAL_CHARS;
break;
} else {
initBuff(bufPos);
arraycopy(mark + 1, buf, 0, bufPos);
hasSpecial = true;
putChar('\'');
continue;
}
}
}
@@ -438,7 +437,7 @@ public class MySqlLexer extends Lexer {
if (commentHandler != null && commentHandler.handle(lastToken, stringVal)) {
return;
}
if (!isAllowComment()) {
throw new NotAllowCommentException();
}
@@ -474,7 +473,7 @@ public class MySqlLexer extends Lexer {
stringVal = subString(mark, bufPos + 1);
token = Token.LINE_COMMENT;
if (commentHandler != null && commentHandler.handle(lastToken, stringVal)) {
return;
}
@@ -482,7 +481,7 @@ public class MySqlLexer extends Lexer {
if (!isAllowComment()) {
throw new NotAllowCommentException();
}
return;
}
}
@@ -1210,7 +1210,7 @@ public class SQLExprParser extends SQLParser {
if (lexer.token() == Token.ESCAPE) {
lexer.nextToken();
rightExp = expr();
rightExp = primary();
expr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.Escape, rightExp);
}
} else if (lexer.token() == (Token.NOT)) {
@@ -570,8 +570,12 @@ public class SQLEvalVisitorUtils {
public static boolean visit(SQLEvalVisitor visitor, SQLHexExpr x) {
String hex = x.getHex();
byte[] bytes = HexBin.decode(hex);
String val = new String(bytes);
x.putAttribute(EVAL_VALUE, val);
if (bytes == null) {
x.putAttribute(EVAL_VALUE, EVAL_ERROR);
} else {
String val = new String(bytes);
x.putAttribute(EVAL_VALUE, val);
}
return true;
}
@@ -807,9 +811,13 @@ public class SQLEvalVisitorUtils {
x.putAttribute(EVAL_VALUE, val);
break;
case NOT:
case Not:
x.putAttribute(EVAL_VALUE, !castToBoolean(val));
case Not: {
Boolean booleanVal = castToBoolean(val);
if (booleanVal != null) {
x.putAttribute(EVAL_VALUE, !booleanVal);
}
break;
}
case Plus:
x.putAttribute(EVAL_VALUE, val);
break;
@@ -1368,9 +1376,9 @@ public class SQLEvalVisitorUtils {
BigDecimal decimalA = castToDecimal(a);
BigDecimal decimalB = castToDecimal(b);
if (decimalB.scale() < decimalA.scale()) {
decimalB.setScale(decimalA.scale());
decimalB = decimalB.setScale(decimalA.scale());
}
return decimalA.divide(decimalB);
return decimalA.divide(decimalB, BigDecimal.ROUND_HALF_UP);
}
if (a instanceof Double || b instanceof Double) {
@@ -36,7 +36,9 @@ public class WallConfig implements WallConfigMBean {
private boolean createTableAllow = true;
private boolean dropTableAllow = true;
private boolean alterTableAllow = true;
private boolean conditionAndAlwayTrueAllow = false;
private boolean conditionDoubleConstAllow = false;
private boolean selectAllColumnAllow = true;
@@ -110,6 +112,14 @@ public class WallConfig implements WallConfigMBean {
}
public boolean isConditionDoubleConstAllow() {
return conditionDoubleConstAllow;
}
public void setConditionDoubleConstAllow(boolean conditionDoubleConstAllow) {
this.conditionDoubleConstAllow = conditionDoubleConstAllow;
}
public boolean isLimitZeroAllow() {
return limitZeroAllow;
}
@@ -164,7 +164,7 @@ public class WallVisitorUtils {
public static void check(WallVisitor visitor, SQLSelectItem x) {
SQLExpr expr = x.getExpr();
if (expr instanceof SQLVariantRefExpr) {
if ("@".equals(((SQLVariantRefExpr) expr).getName())) {
addViolation(visitor, ErrorCode.EVIL_NAME, "@ not allow", x);
@@ -223,14 +223,16 @@ public class WallVisitorUtils {
SQLExpr where = x.getWhere();
if (where != null) {
if (queryBlockFromIsNull(visitor, x, false)) {
addViolation(visitor, ErrorCode.EMPTY_QUERY_HAS_CONDITION, "empty select has condition", x);
}
where.setParent(x);
checkCondition(visitor, x.getWhere());
if (Boolean.TRUE == getConditionValue(visitor, where, visitor.getConfig().isSelectWhereAlwayTrueCheck())) {
Object whereValue = getConditionValue(visitor, where, visitor.getConfig().isSelectWhereAlwayTrueCheck());
if (queryBlockFromIsNull(visitor, x, false) && whereValue != null) {
addViolation(visitor, ErrorCode.EMPTY_QUERY_HAS_CONDITION, "empty select has condition", x);
}
if (Boolean.TRUE == whereValue) {
boolean isSimpleConstExpr = false;
SQLExpr first = getFirst(where);
@@ -672,22 +674,24 @@ public class WallVisitorUtils {
}
if (x.getOperator() == SQLBinaryOperator.BooleanAnd) {
if (rightResult != null && x.getLeft() instanceof SQLBinaryOpExpr) {
SQLBinaryOpExpr leftBinaryOpExpr = (SQLBinaryOpExpr) x.getLeft();
if (visitor != null && !visitor.getConfig().isConditionDoubleConstAllow()) {
if (rightResult != null && x.getLeft() instanceof SQLBinaryOpExpr) {
SQLBinaryOpExpr leftBinaryOpExpr = (SQLBinaryOpExpr) x.getLeft();
if (leftBinaryOpExpr.getOperator() != SQLBinaryOperator.BooleanAnd //
&& leftBinaryOpExpr.getOperator() != SQLBinaryOperator.BooleanOr //
&& leftResult != null //
&& visitor != null) {
addViolation(visitor, ErrorCode.DOUBLE_CONST_CONDITION, "double const condition", x);
}
if (leftBinaryOpExpr.getOperator() == SQLBinaryOperator.BooleanAnd //
|| leftBinaryOpExpr.getOperator() == SQLBinaryOperator.BooleanOr) {
Object leftRightVal = getValue(leftBinaryOpExpr.getRight());
if (leftRightVal != null) {
if (leftBinaryOpExpr.getOperator() != SQLBinaryOperator.BooleanAnd //
&& leftBinaryOpExpr.getOperator() != SQLBinaryOperator.BooleanOr //
&& leftResult != null //
&& visitor != null) {
addViolation(visitor, ErrorCode.DOUBLE_CONST_CONDITION, "double const condition", x);
}
if (leftBinaryOpExpr.getOperator() == SQLBinaryOperator.BooleanAnd //
|| leftBinaryOpExpr.getOperator() == SQLBinaryOperator.BooleanOr) {
Object leftRightVal = getValue(leftBinaryOpExpr.getRight());
if (leftRightVal != null) {
addViolation(visitor, ErrorCode.DOUBLE_CONST_CONDITION, "double const condition", x);
}
}
}
}