mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
refactor
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
package com.alibaba.druid.filter.wall;
|
||||
|
||||
import static com.alibaba.druid.filter.wall.spi.WallVisitorUtils.loadResource;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
@@ -18,12 +20,39 @@ public class WallConfig {
|
||||
private boolean insertAllow = true;
|
||||
private boolean mergeAllow = true;
|
||||
|
||||
private boolean shcemaCheck = true;
|
||||
private boolean tableCheck = true;
|
||||
private boolean functionCheck = true;
|
||||
|
||||
protected final Set<String> permitFunctions = new HashSet<String>();
|
||||
protected final Set<String> permitTables = new HashSet<String>();
|
||||
protected final Set<String> permitSchemas = new HashSet<String>();
|
||||
protected final Set<String> permitNames = new HashSet<String>();
|
||||
protected final Set<String> permitObjects = new HashSet<String>();
|
||||
|
||||
public void loadDefault(String dir) {
|
||||
if (dir.endsWith("/")) {
|
||||
dir = dir.substring(0, dir.length() - 1);
|
||||
}
|
||||
loadResource(getPermitNames(), dir + "/permit-name-default.txt");
|
||||
loadResource(getPermitSchemas(), dir + "/permit-schema-default.txt");
|
||||
loadResource(getPermitFunctions(), dir + "/permit-function-default.txt");
|
||||
loadResource(getPermitTables(), dir + "/permit-table-default.txt");
|
||||
loadResource(getPermitObjects(), dir + "/permit-object-default.txt");
|
||||
}
|
||||
|
||||
public void loadExtend(String dir) {
|
||||
if (dir.endsWith("/")) {
|
||||
dir = dir.substring(0, dir.length() - 1);
|
||||
}
|
||||
|
||||
loadResource(getPermitNames(), dir + "permit-name.txt");
|
||||
loadResource(getPermitSchemas(), dir + "permit-schema.txt");
|
||||
loadResource(getPermitFunctions(), dir + "permit-function.txt");
|
||||
loadResource(getPermitTables(), dir + "permit-table.txt");
|
||||
loadResource(getPermitObjects(), dir + "permit-object.txt");
|
||||
}
|
||||
|
||||
public boolean isSelelctAllow() {
|
||||
return selelctAllow;
|
||||
}
|
||||
@@ -96,6 +125,30 @@ public class WallConfig {
|
||||
this.mergeAllow = mergeAllow;
|
||||
}
|
||||
|
||||
public boolean isShcemaCheck() {
|
||||
return shcemaCheck;
|
||||
}
|
||||
|
||||
public void setShcemaCheck(boolean shcemaCheck) {
|
||||
this.shcemaCheck = shcemaCheck;
|
||||
}
|
||||
|
||||
public boolean isTableCheck() {
|
||||
return tableCheck;
|
||||
}
|
||||
|
||||
public void setTableCheck(boolean tableCheck) {
|
||||
this.tableCheck = tableCheck;
|
||||
}
|
||||
|
||||
public boolean isFunctionCheck() {
|
||||
return functionCheck;
|
||||
}
|
||||
|
||||
public void setFunctionCheck(boolean functionCheck) {
|
||||
this.functionCheck = functionCheck;
|
||||
}
|
||||
|
||||
public Set<String> getPermitFunctions() {
|
||||
return permitFunctions;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package com.alibaba.druid.filter.wall.spi;
|
||||
|
||||
import static com.alibaba.druid.filter.wall.spi.WallVisitorUtils.loadResource;
|
||||
|
||||
import com.alibaba.druid.filter.wall.WallConfig;
|
||||
import com.alibaba.druid.filter.wall.WallProvider;
|
||||
import com.alibaba.druid.filter.wall.WallVisitor;
|
||||
@@ -22,30 +20,14 @@ public class MySqlWallProvider extends WallProvider {
|
||||
super(config);
|
||||
|
||||
if (loadDefault) {
|
||||
loadDefault();
|
||||
config.loadDefault("META-INF/druid/wall/mysql");
|
||||
}
|
||||
|
||||
if (loadExtend) {
|
||||
loadExtend();
|
||||
config.loadExtend("META-INF/druid/wall/mysql");
|
||||
}
|
||||
}
|
||||
|
||||
public void loadExtend() {
|
||||
loadResource(config.getPermitNames(), "META-INF/druid/wall/mysql/permit-name.txt");
|
||||
loadResource(config.getPermitSchemas(), "META-INF/druid/wall/mysql/permit-schema.txt");
|
||||
loadResource(config.getPermitFunctions(), "META-INF/druid/wall/mysql/permit-function.txt");
|
||||
loadResource(config.getPermitTables(), "META-INF/druid/wall/mysql/permit-table.txt");
|
||||
loadResource(config.getPermitObjects(), "META-INF/druid/wall/mysql/permit-object.txt");
|
||||
}
|
||||
|
||||
public void loadDefault() {
|
||||
loadResource(config.getPermitNames(), "META-INF/druid/wall/mysql/permit-name-default.txt");
|
||||
loadResource(config.getPermitSchemas(), "META-INF/druid/wall/mysql/permit-schema-default.txt");
|
||||
loadResource(config.getPermitFunctions(), "META-INF/druid/wall/mysql/permit-function-default.txt");
|
||||
loadResource(config.getPermitTables(), "META-INF/druid/wall/mysql/permit-table-default.txt");
|
||||
loadResource(config.getPermitObjects(), "META-INF/druid/wall/mysql/permit-object-default.txt");
|
||||
}
|
||||
|
||||
@Override
|
||||
public SQLStatementParser createParser(String sql) {
|
||||
return new MySqlStatementParser(sql);
|
||||
|
||||
@@ -26,6 +26,7 @@ import com.alibaba.druid.sql.ast.statement.SQLUnionQuery;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLUpdateStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlOutFileExpr;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlDeleteStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectGroupBy;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock.Limit;
|
||||
@@ -77,30 +78,64 @@ public class MySqlWallVisitor extends MySqlASTVisitorAdapter implements WallVisi
|
||||
}
|
||||
|
||||
public boolean visit(SQLSelectGroupByClause x) {
|
||||
WallVisitorUtils.checkCondition(this, x.getHaving());
|
||||
WallVisitorUtils.checkHaving(this, x.getHaving());
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean visit(MySqlSelectGroupBy x) {
|
||||
WallVisitorUtils.checkCondition(this, x.getHaving());
|
||||
WallVisitorUtils.checkHaving(this, x.getHaving());
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(MySqlDeleteStatement x) {
|
||||
WallVisitorUtils.checkCondition(this, x.getWhere());
|
||||
return true;
|
||||
return visit((SQLDeleteStatement) x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLDeleteStatement x) {
|
||||
WallVisitorUtils.checkCondition(this, x.getWhere());
|
||||
if (!config.isDeleteAllow()) {
|
||||
this.getViolations().add(new IllegalSQLObjectViolation(this.toSQL(x)));
|
||||
return false;
|
||||
}
|
||||
|
||||
WallVisitorUtils.checkDeleteCondition(this, x.getWhere());
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLUpdateStatement x) {
|
||||
WallVisitorUtils.checkCondition(this, x.getWhere());
|
||||
if (!config.isUpdateAllow()) {
|
||||
this.getViolations().add(new IllegalSQLObjectViolation(this.toSQL(x)));
|
||||
return false;
|
||||
}
|
||||
|
||||
WallVisitorUtils.checkUpdateCondition(this, x.getWhere());
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(MySqlInsertStatement x) {
|
||||
return visit((SQLInsertStatement) x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLInsertStatement x) {
|
||||
if (!config.isInsertAllow()) {
|
||||
this.getViolations().add(new IllegalSQLObjectViolation(this.toSQL(x)));
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLSelectStatement x) {
|
||||
if (!config.isSelelctAllow()) {
|
||||
this.getViolations().add(new IllegalSQLObjectViolation(this.toSQL(x)));
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package com.alibaba.druid.filter.wall.spi;
|
||||
|
||||
import static com.alibaba.druid.filter.wall.spi.WallVisitorUtils.loadResource;
|
||||
|
||||
import com.alibaba.druid.filter.wall.WallConfig;
|
||||
import com.alibaba.druid.filter.wall.WallProvider;
|
||||
import com.alibaba.druid.filter.wall.WallVisitor;
|
||||
@@ -22,29 +20,14 @@ public class OracleWallProvider extends WallProvider {
|
||||
super(config);
|
||||
|
||||
if (loadDefault) {
|
||||
loadDefault();
|
||||
config.loadDefault("META-INF/druid/wall/oracle");
|
||||
}
|
||||
|
||||
if (loadExtend) {
|
||||
loadExtend();
|
||||
config.loadExtend("META-INF/druid/wall/oracle");
|
||||
}
|
||||
}
|
||||
|
||||
public void loadExtend() {
|
||||
loadResource(config.getPermitNames(), "META-INF/druid/wall/oracle/permit-name.txt");
|
||||
loadResource(config.getPermitSchemas(), "META-INF/druid/wall/oracle/permit-schema.txt");
|
||||
loadResource(config.getPermitFunctions(), "META-INF/druid/wall/oracle/permit-function.txt");
|
||||
loadResource(config.getPermitTables(), "META-INF/druid/wall/oracle/permit-table.txt");
|
||||
loadResource(config.getPermitObjects(), "META-INF/druid/wall/oracle/permit-object.txt");
|
||||
}
|
||||
|
||||
public void loadDefault() {
|
||||
loadResource(config.getPermitNames(), "META-INF/druid/wall/oracle/permit-name-default.txt");
|
||||
loadResource(config.getPermitSchemas(), "META-INF/druid/wall/oracle/permit-schema-default.txt");
|
||||
loadResource(config.getPermitFunctions(), "META-INF/druid/wall/oracle/permit-function-default.txt");
|
||||
loadResource(config.getPermitTables(), "META-INF/druid/wall/oracle/permit-table-default.txt");
|
||||
loadResource(config.getPermitObjects(), "META-INF/druid/wall/oracle/permit-object-default.txt");
|
||||
}
|
||||
|
||||
@Override
|
||||
public SQLStatementParser createParser(String sql) {
|
||||
|
||||
@@ -25,6 +25,7 @@ import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLUnionQuery;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLUpdateStatement;
|
||||
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleDeleteStatement;
|
||||
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleInsertStatement;
|
||||
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleMergeStatement;
|
||||
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleMultiInsertStatement;
|
||||
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectQueryBlock;
|
||||
@@ -92,7 +93,7 @@ public class OracleWallVisitor extends OracleASTVIsitorAdapter implements WallVi
|
||||
}
|
||||
|
||||
public boolean visit(SQLSelectGroupByClause x) {
|
||||
WallVisitorUtils.checkCondition(this, x.getHaving());
|
||||
WallVisitorUtils.checkHaving(this, x.getHaving());
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -119,30 +120,6 @@ public class OracleWallVisitor extends OracleASTVIsitorAdapter implements WallVi
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(OracleDeleteStatement x) {
|
||||
WallVisitorUtils.checkCondition(this, x.getWhere());
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLDeleteStatement x) {
|
||||
WallVisitorUtils.checkCondition(this, x.getWhere());
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLUpdateStatement x) {
|
||||
WallVisitorUtils.checkCondition(this, x.getWhere());
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(OracleUpdateStatement x) {
|
||||
WallVisitorUtils.checkCondition(this, x.getWhere());
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toSQL(SQLObject obj) {
|
||||
return SQLUtils.toOracleString(obj);
|
||||
@@ -182,4 +159,70 @@ public class OracleWallVisitor extends OracleASTVIsitorAdapter implements WallVi
|
||||
return config.getPermitObjects().contains(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLSelectStatement x) {
|
||||
if (!config.isSelelctAllow()) {
|
||||
this.getViolations().add(new IllegalSQLObjectViolation(this.toSQL(x)));
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(OracleInsertStatement x) {
|
||||
return visit((SQLInsertStatement) x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLInsertStatement x) {
|
||||
if (!config.isInsertAllow()) {
|
||||
this.getViolations().add(new IllegalSQLObjectViolation(this.toSQL(x)));
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(OracleMultiInsertStatement x) {
|
||||
if (!config.isInsertAllow()) {
|
||||
this.getViolations().add(new IllegalSQLObjectViolation(this.toSQL(x)));
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(OracleDeleteStatement x) {
|
||||
return visit((SQLDeleteStatement) x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLDeleteStatement x) {
|
||||
if (!config.isDeleteAllow()) {
|
||||
this.getViolations().add(new IllegalSQLObjectViolation(this.toSQL(x)));
|
||||
return false;
|
||||
}
|
||||
|
||||
WallVisitorUtils.checkDeleteCondition(this, x.getWhere());
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(OracleUpdateStatement x) {
|
||||
return visit((SQLUpdateStatement) x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLUpdateStatement x) {
|
||||
if (!config.isUpdateAllow()) {
|
||||
this.getViolations().add(new IllegalSQLObjectViolation(this.toSQL(x)));
|
||||
return false;
|
||||
}
|
||||
|
||||
WallVisitorUtils.checkUpdateCondition(this, x.getWhere());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,11 +78,43 @@ public class WallVisitorUtils {
|
||||
}
|
||||
}
|
||||
|
||||
public static void checkCondition(WallVisitor visitor, SQLExpr x) {
|
||||
public static void checkHaving(WallVisitor visitor, SQLExpr x) {
|
||||
if (x == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!visitor.getConfig().isSelectHavingAlwayTrueCheck()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (Boolean.TRUE == getValue(x)) {
|
||||
visitor.getViolations().add(new IllegalSQLObjectViolation(SQLUtils.toSQLString(x)));
|
||||
}
|
||||
}
|
||||
|
||||
public static void checkDeleteCondition(WallVisitor visitor, SQLExpr x) {
|
||||
if (x == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!visitor.getConfig().isDeleteWhereAlwayTrueCheck()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (Boolean.TRUE == getValue(x)) {
|
||||
visitor.getViolations().add(new IllegalSQLObjectViolation(SQLUtils.toSQLString(x)));
|
||||
}
|
||||
}
|
||||
|
||||
public static void checkUpdateCondition(WallVisitor visitor, SQLExpr x) {
|
||||
if (x == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!visitor.getConfig().isUpdateWhereAlayTrueCheck()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (Boolean.TRUE == getValue(x)) {
|
||||
visitor.getViolations().add(new IllegalSQLObjectViolation(SQLUtils.toSQLString(x)));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user