mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
refactor
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
package com.alibaba.druid.wall;
|
||||
|
||||
public class IllegalSQLObjectViolation implements Violation {
|
||||
|
||||
private String sqlPart;
|
||||
|
||||
public IllegalSQLObjectViolation(){
|
||||
|
||||
}
|
||||
|
||||
public IllegalSQLObjectViolation(String condition){
|
||||
this.sqlPart = condition;
|
||||
}
|
||||
|
||||
public String getSqlPart() {
|
||||
return sqlPart;
|
||||
}
|
||||
|
||||
public void setSqlPart(String sqlPart) {
|
||||
this.sqlPart = sqlPart;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return this.sqlPart;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.alibaba.druid.wall;
|
||||
|
||||
public interface Violation {
|
||||
|
||||
String toString();
|
||||
}
|
||||
@@ -0,0 +1,285 @@
|
||||
package com.alibaba.druid.wall;
|
||||
|
||||
import static com.alibaba.druid.wall.spi.WallVisitorUtils.loadResource;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import com.alibaba.druid.wall.spi.WallVisitorUtils;
|
||||
|
||||
public class WallConfig {
|
||||
|
||||
private boolean noneBaseStatementAllow = false;
|
||||
|
||||
private boolean selelctAllow = true;
|
||||
private boolean selectIntoAllow = true;
|
||||
private boolean selectIntoOutfileAllow = false;
|
||||
private boolean selectWhereAlwayTrueCheck = true;
|
||||
private boolean selectHavingAlwayTrueCheck = true;
|
||||
private boolean selectUnionCheck = true;
|
||||
|
||||
private boolean deleteAllow = true;
|
||||
private boolean deleteWhereAlwayTrueCheck = true;
|
||||
|
||||
private boolean updateAllow = true;
|
||||
private boolean updateWhereAlayTrueCheck = true;
|
||||
|
||||
private boolean insertAllow = true;
|
||||
private boolean mergeAllow = true;
|
||||
|
||||
private boolean truncateAllow = false;
|
||||
|
||||
private boolean schemaCheck = true;
|
||||
private boolean tableCheck = true;
|
||||
private boolean functionCheck = true;
|
||||
private boolean objectCheck = true;
|
||||
private boolean variantCheck = 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> permitVariants = new HashSet<String>();
|
||||
protected final Set<String> permitObjects = new HashSet<String>();
|
||||
|
||||
private String dir;
|
||||
|
||||
private boolean inited;
|
||||
|
||||
public WallConfig(){
|
||||
|
||||
}
|
||||
|
||||
public boolean isInited() {
|
||||
return inited;
|
||||
}
|
||||
|
||||
public WallConfig(String dir){
|
||||
this.dir = dir;
|
||||
this.init();
|
||||
}
|
||||
|
||||
public String getDir() {
|
||||
return dir;
|
||||
}
|
||||
|
||||
public void setDir(String dir) {
|
||||
this.dir = dir;
|
||||
}
|
||||
|
||||
public void init() {
|
||||
loadConfig(dir);
|
||||
}
|
||||
|
||||
public void loadConfig(String dir) {
|
||||
if (dir.endsWith("/")) {
|
||||
dir = dir.substring(0, dir.length() - 1);
|
||||
}
|
||||
|
||||
loadResource(getPermitVariants(), dir + "/permit-variant.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 isNoneBaseStatementAllow() {
|
||||
return noneBaseStatementAllow;
|
||||
}
|
||||
|
||||
public void setNoneBaseStatementAllow(boolean noneBaseStatementAllow) {
|
||||
this.noneBaseStatementAllow = noneBaseStatementAllow;
|
||||
}
|
||||
|
||||
public boolean isTruncateAllow() {
|
||||
return truncateAllow;
|
||||
}
|
||||
|
||||
public void setTruncateAllow(boolean truncateAllow) {
|
||||
this.truncateAllow = truncateAllow;
|
||||
}
|
||||
|
||||
public boolean isSelelctAllow() {
|
||||
return selelctAllow;
|
||||
}
|
||||
|
||||
public void setSelelctAllow(boolean selelctAllow) {
|
||||
this.selelctAllow = selelctAllow;
|
||||
}
|
||||
|
||||
public boolean isSelectIntoAllow() {
|
||||
return selectIntoAllow;
|
||||
}
|
||||
|
||||
public void setSelectIntoAllow(boolean selectIntoAllow) {
|
||||
this.selectIntoAllow = selectIntoAllow;
|
||||
}
|
||||
|
||||
public boolean isSelectIntoOutfileAllow() {
|
||||
return selectIntoOutfileAllow;
|
||||
}
|
||||
|
||||
public void setSelectIntoOutfileAllow(boolean selectIntoOutfileAllow) {
|
||||
this.selectIntoOutfileAllow = selectIntoOutfileAllow;
|
||||
}
|
||||
|
||||
public boolean isSelectUnionCheck() {
|
||||
return selectUnionCheck;
|
||||
}
|
||||
|
||||
public void setSelectUnionCheck(boolean selectUnionCheck) {
|
||||
this.selectUnionCheck = selectUnionCheck;
|
||||
}
|
||||
|
||||
public boolean isSelectWhereAlwayTrueCheck() {
|
||||
return selectWhereAlwayTrueCheck;
|
||||
}
|
||||
|
||||
public void setSelectWhereAlwayTrueCheck(boolean selectWhereAlwayTrueCheck) {
|
||||
this.selectWhereAlwayTrueCheck = selectWhereAlwayTrueCheck;
|
||||
}
|
||||
|
||||
public boolean isSelectHavingAlwayTrueCheck() {
|
||||
return selectHavingAlwayTrueCheck;
|
||||
}
|
||||
|
||||
public void setSelectHavingAlwayTrueCheck(boolean selectHavingAlwayTrueCheck) {
|
||||
this.selectHavingAlwayTrueCheck = selectHavingAlwayTrueCheck;
|
||||
}
|
||||
|
||||
public boolean isDeleteAllow() {
|
||||
return deleteAllow;
|
||||
}
|
||||
|
||||
public void setDeleteAllow(boolean deleteAllow) {
|
||||
this.deleteAllow = deleteAllow;
|
||||
}
|
||||
|
||||
public boolean isDeleteWhereAlwayTrueCheck() {
|
||||
return deleteWhereAlwayTrueCheck;
|
||||
}
|
||||
|
||||
public void setDeleteWhereAlwayTrueCheck(boolean deleteWhereAlwayTrueCheck) {
|
||||
this.deleteWhereAlwayTrueCheck = deleteWhereAlwayTrueCheck;
|
||||
}
|
||||
|
||||
public boolean isUpdateAllow() {
|
||||
return updateAllow;
|
||||
}
|
||||
|
||||
public void setUpdateAllow(boolean updateAllow) {
|
||||
this.updateAllow = updateAllow;
|
||||
}
|
||||
|
||||
public boolean isUpdateWhereAlayTrueCheck() {
|
||||
return updateWhereAlayTrueCheck;
|
||||
}
|
||||
|
||||
public void setUpdateWhereAlayTrueCheck(boolean updateWhereAlayTrueCheck) {
|
||||
this.updateWhereAlayTrueCheck = updateWhereAlayTrueCheck;
|
||||
}
|
||||
|
||||
public boolean isInsertAllow() {
|
||||
return insertAllow;
|
||||
}
|
||||
|
||||
public void setInsertAllow(boolean insertAllow) {
|
||||
this.insertAllow = insertAllow;
|
||||
}
|
||||
|
||||
public boolean isMergeAllow() {
|
||||
return mergeAllow;
|
||||
}
|
||||
|
||||
public void setMergeAllow(boolean mergeAllow) {
|
||||
this.mergeAllow = mergeAllow;
|
||||
}
|
||||
|
||||
public boolean isSchemaCheck() {
|
||||
return schemaCheck;
|
||||
}
|
||||
|
||||
public void setSchemaCheck(boolean schemaCheck) {
|
||||
this.schemaCheck = schemaCheck;
|
||||
}
|
||||
|
||||
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 boolean isVariantCheck() {
|
||||
return variantCheck;
|
||||
}
|
||||
|
||||
public void setVariantCheck(boolean variantCheck) {
|
||||
this.variantCheck = variantCheck;
|
||||
}
|
||||
|
||||
public boolean isObjectCheck() {
|
||||
return objectCheck;
|
||||
}
|
||||
|
||||
public void setObjectCheck(boolean objectCheck) {
|
||||
this.objectCheck = objectCheck;
|
||||
}
|
||||
|
||||
// ///////////////////
|
||||
|
||||
public Set<String> getPermitFunctions() {
|
||||
return permitFunctions;
|
||||
}
|
||||
|
||||
public Set<String> getPermitTables() {
|
||||
return permitTables;
|
||||
}
|
||||
|
||||
public Set<String> getPermitSchemas() {
|
||||
return permitSchemas;
|
||||
}
|
||||
|
||||
public Set<String> getPermitVariants() {
|
||||
return permitVariants;
|
||||
}
|
||||
|
||||
public Set<String> getPermitObjects() {
|
||||
return permitObjects;
|
||||
}
|
||||
|
||||
public boolean isPermitObjects(String name) {
|
||||
if (!objectCheck) {
|
||||
return false;
|
||||
}
|
||||
|
||||
name = WallVisitorUtils.form(name);
|
||||
return permitObjects.contains(name);
|
||||
}
|
||||
|
||||
public boolean isPermitSchema(String name) {
|
||||
if (!schemaCheck) {
|
||||
return false;
|
||||
}
|
||||
|
||||
name = WallVisitorUtils.form(name);
|
||||
return this.permitSchemas.contains(name);
|
||||
}
|
||||
|
||||
public boolean isPermitFunction(String name) {
|
||||
if (!functionCheck) {
|
||||
return false;
|
||||
}
|
||||
|
||||
name = WallVisitorUtils.form(name);
|
||||
return this.permitFunctions.contains(name);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,265 @@
|
||||
package com.alibaba.druid.wall;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
import com.alibaba.druid.DruidRuntimeException;
|
||||
import com.alibaba.druid.filter.FilterAdapter;
|
||||
import com.alibaba.druid.filter.FilterChain;
|
||||
import com.alibaba.druid.logging.Log;
|
||||
import com.alibaba.druid.logging.LogFactory;
|
||||
import com.alibaba.druid.proxy.jdbc.CallableStatementProxy;
|
||||
import com.alibaba.druid.proxy.jdbc.ConnectionProxy;
|
||||
import com.alibaba.druid.proxy.jdbc.DataSourceProxy;
|
||||
import com.alibaba.druid.proxy.jdbc.PreparedStatementProxy;
|
||||
import com.alibaba.druid.proxy.jdbc.ResultSetProxy;
|
||||
import com.alibaba.druid.proxy.jdbc.StatementProxy;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
import com.alibaba.druid.wall.spi.MySqlWallProvider;
|
||||
import com.alibaba.druid.wall.spi.OracleWallProvider;
|
||||
import com.alibaba.druid.wall.spi.SQLServerProvider;
|
||||
|
||||
public class WallFilter extends FilterAdapter {
|
||||
|
||||
private final static Log LOG = LogFactory.getLog(WallFilter.class);
|
||||
|
||||
private boolean inited = false;
|
||||
|
||||
private WallProvider provider;
|
||||
|
||||
private String dbType;
|
||||
|
||||
private WallConfig config;
|
||||
|
||||
private boolean logViolation = false;
|
||||
private boolean throwException = true;
|
||||
|
||||
@Override
|
||||
public void init(DataSourceProxy dataSource) {
|
||||
this.dataSource = dataSource;
|
||||
|
||||
String dbType = this.dbType;
|
||||
|
||||
if (this.dbType == null || this.dbType.trim().length() == 0) {
|
||||
this.dbType = dataSource.getDbType();
|
||||
}
|
||||
|
||||
if (JdbcUtils.MYSQL.equals(dbType)) {
|
||||
provider = new MySqlWallProvider(config);
|
||||
|
||||
if (config == null) {
|
||||
config = new WallConfig(MySqlWallProvider.DEFAULT_CONFIG_DIR);
|
||||
}
|
||||
} else if (JdbcUtils.ORACLE.equals(dbType)) {
|
||||
provider = new OracleWallProvider(config);
|
||||
|
||||
if (config == null) {
|
||||
config = new WallConfig(OracleWallProvider.DEFAULT_CONFIG_DIR);
|
||||
}
|
||||
} else if (JdbcUtils.SQL_SERVER.equals(dbType)) {
|
||||
provider = new SQLServerProvider(config);
|
||||
|
||||
if (config == null) {
|
||||
config = new WallConfig(SQLServerProvider.DEFAULT_CONFIG_DIR);
|
||||
}
|
||||
} else {
|
||||
throw new IllegalStateException("dbType not support : " + dbType);
|
||||
}
|
||||
|
||||
this.inited = true;
|
||||
}
|
||||
|
||||
public String getDbType() {
|
||||
return dbType;
|
||||
}
|
||||
|
||||
public void setDbType(String dbType) {
|
||||
this.dbType = dbType;
|
||||
}
|
||||
|
||||
public boolean isLogViolation() {
|
||||
return logViolation;
|
||||
}
|
||||
|
||||
public void setLogViolation(boolean logViolation) {
|
||||
this.logViolation = logViolation;
|
||||
}
|
||||
|
||||
public boolean isThrowException() {
|
||||
return throwException;
|
||||
}
|
||||
|
||||
public void setThrowException(boolean throwException) {
|
||||
this.throwException = throwException;
|
||||
}
|
||||
|
||||
public WallProvider getProvider() {
|
||||
return provider;
|
||||
}
|
||||
|
||||
public WallConfig getConfig() {
|
||||
return config;
|
||||
}
|
||||
|
||||
public void setConfig(WallConfig config) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
public void checkInit() {
|
||||
if (inited) {
|
||||
throw new DruidRuntimeException("wall filter is inited");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ConnectionProxy connection_connect(FilterChain chain, Properties info) throws SQLException {
|
||||
return chain.connection_connect(info);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PreparedStatementProxy connection_prepareStatement(FilterChain chain, ConnectionProxy connection, String sql)
|
||||
throws SQLException {
|
||||
check(connection, sql);
|
||||
return chain.connection_prepareStatement(connection, sql);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PreparedStatementProxy connection_prepareStatement(FilterChain chain, ConnectionProxy connection,
|
||||
String sql, int autoGeneratedKeys) throws SQLException {
|
||||
check(connection, sql);
|
||||
return chain.connection_prepareStatement(connection, sql, autoGeneratedKeys);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PreparedStatementProxy connection_prepareStatement(FilterChain chain, ConnectionProxy connection,
|
||||
String sql, int resultSetType, int resultSetConcurrency)
|
||||
throws SQLException {
|
||||
check(connection, sql);
|
||||
return chain.connection_prepareStatement(connection, sql, resultSetType, resultSetConcurrency);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PreparedStatementProxy connection_prepareStatement(FilterChain chain, ConnectionProxy connection,
|
||||
String sql, int resultSetType, int resultSetConcurrency,
|
||||
int resultSetHoldability) throws SQLException {
|
||||
check(connection, sql);
|
||||
return chain.connection_prepareStatement(connection, sql, resultSetType, resultSetConcurrency,
|
||||
resultSetHoldability);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PreparedStatementProxy connection_prepareStatement(FilterChain chain, ConnectionProxy connection,
|
||||
String sql, int[] columnIndexes) throws SQLException {
|
||||
check(connection, sql);
|
||||
return chain.connection_prepareStatement(connection, sql, columnIndexes);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PreparedStatementProxy connection_prepareStatement(FilterChain chain, ConnectionProxy connection,
|
||||
String sql, String[] columnNames) throws SQLException {
|
||||
check(connection, sql);
|
||||
return chain.connection_prepareStatement(connection, sql, columnNames);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CallableStatementProxy connection_prepareCall(FilterChain chain, ConnectionProxy connection, String sql)
|
||||
throws SQLException {
|
||||
check(connection, sql);
|
||||
return chain.connection_prepareCall(connection, sql);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CallableStatementProxy connection_prepareCall(FilterChain chain, ConnectionProxy connection, String sql,
|
||||
int resultSetType, int resultSetConcurrency)
|
||||
throws SQLException {
|
||||
check(connection, sql);
|
||||
return chain.connection_prepareCall(connection, sql, resultSetType, resultSetConcurrency);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CallableStatementProxy connection_prepareCall(FilterChain chain, ConnectionProxy connection, String sql,
|
||||
int resultSetType, int resultSetConcurrency,
|
||||
int resultSetHoldability) throws SQLException {
|
||||
check(connection, sql);
|
||||
return chain.connection_prepareCall(connection, sql, resultSetType, resultSetConcurrency, resultSetHoldability);
|
||||
}
|
||||
|
||||
// //////////////
|
||||
|
||||
@Override
|
||||
public boolean statement_execute(FilterChain chain, StatementProxy statement, String sql) throws SQLException {
|
||||
check(statement.getConnectionProxy(), sql);
|
||||
return chain.statement_execute(statement, sql);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean statement_execute(FilterChain chain, StatementProxy statement, String sql, int autoGeneratedKeys)
|
||||
throws SQLException {
|
||||
check(statement.getConnectionProxy(), sql);
|
||||
return chain.statement_execute(statement, sql, autoGeneratedKeys);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean statement_execute(FilterChain chain, StatementProxy statement, String sql, int columnIndexes[])
|
||||
throws SQLException {
|
||||
check(statement.getConnectionProxy(), sql);
|
||||
return chain.statement_execute(statement, sql, columnIndexes);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean statement_execute(FilterChain chain, StatementProxy statement, String sql, String columnNames[])
|
||||
throws SQLException {
|
||||
check(statement.getConnectionProxy(), sql);
|
||||
return chain.statement_execute(statement, sql, columnNames);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResultSetProxy statement_executeQuery(FilterChain chain, StatementProxy statement, String sql)
|
||||
throws SQLException {
|
||||
check(statement.getConnectionProxy(), sql);
|
||||
return chain.statement_executeQuery(statement, sql);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int statement_executeUpdate(FilterChain chain, StatementProxy statement, String sql) throws SQLException {
|
||||
check(statement.getConnectionProxy(), sql);
|
||||
return chain.statement_executeUpdate(statement, sql);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int statement_executeUpdate(FilterChain chain, StatementProxy statement, String sql, int autoGeneratedKeys)
|
||||
throws SQLException {
|
||||
check(statement.getConnectionProxy(), sql);
|
||||
return chain.statement_executeUpdate(statement, sql, autoGeneratedKeys);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int statement_executeUpdate(FilterChain chain, StatementProxy statement, String sql, int columnIndexes[])
|
||||
throws SQLException {
|
||||
check(statement.getConnectionProxy(), sql);
|
||||
return chain.statement_executeUpdate(statement, sql, columnIndexes);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int statement_executeUpdate(FilterChain chain, StatementProxy statement, String sql, String columnNames[])
|
||||
throws SQLException {
|
||||
check(statement.getConnectionProxy(), sql);
|
||||
return chain.statement_executeUpdate(statement, sql, columnNames);
|
||||
}
|
||||
|
||||
public void check(ConnectionProxy connection, String sql) throws SQLException {
|
||||
List<Violation> violations = provider.check(sql);
|
||||
|
||||
if (violations.size() > 0) {
|
||||
if (isLogViolation()) {
|
||||
LOG.error("sql injection violation : " + sql);
|
||||
}
|
||||
|
||||
if (throwException) {
|
||||
throw new SQLException("sql injection violation : " + sql);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
package com.alibaba.druid.wall;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.parser.ParserException;
|
||||
import com.alibaba.druid.sql.parser.SQLStatementParser;
|
||||
import com.alibaba.druid.sql.parser.Token;
|
||||
|
||||
public abstract class WallProvider {
|
||||
|
||||
// Dummy value to associate with an Object in the backing Map
|
||||
private static final Object PRESENT = new Object();
|
||||
|
||||
private LinkedHashMap<String, Object> whiteList;
|
||||
|
||||
private int whileListMaxSize = 1024;
|
||||
|
||||
private int whiteSqlMaxLength = 1024; // 1k
|
||||
|
||||
protected final WallConfig config;
|
||||
|
||||
public WallProvider(WallConfig config){
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
public WallConfig getConfig() {
|
||||
return config;
|
||||
}
|
||||
|
||||
public synchronized void addWhiteSql(String sql) {
|
||||
if (whiteList == null) {
|
||||
whiteList = new LinkedHashMap<String, Object>(whileListMaxSize, 0.75f, true);
|
||||
}
|
||||
|
||||
whiteList.put(sql, PRESENT);
|
||||
}
|
||||
|
||||
public synchronized boolean whiteContains(String sql) {
|
||||
if (whiteList == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return whiteList.get(sql) != null;
|
||||
}
|
||||
|
||||
public abstract SQLStatementParser createParser(String sql);
|
||||
|
||||
public abstract WallVisitor createWallVisitor();
|
||||
|
||||
public boolean checkValid(String sql) {
|
||||
return check(sql).size() == 0;
|
||||
}
|
||||
|
||||
public List<Violation> check(String sql) {
|
||||
// first step, check whiteList
|
||||
boolean isWhite = whiteContains(sql);
|
||||
if (isWhite) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
SQLStatementParser parser = createParser(sql);
|
||||
parser.getLexer().setAllowComment(false); // permit comment
|
||||
|
||||
List<SQLStatement> statementList = new ArrayList<SQLStatement>();
|
||||
|
||||
try {
|
||||
parser.parseStatementList(statementList);
|
||||
} catch (ParserException e) {
|
||||
return Collections.<Violation> singletonList(new IllegalSQLObjectViolation(sql));
|
||||
}
|
||||
|
||||
if (parser.getLexer().token() != Token.EOF) {
|
||||
return Collections.<Violation> singletonList(new IllegalSQLObjectViolation(sql));
|
||||
}
|
||||
|
||||
if (statementList.size() > 1) {
|
||||
return Collections.<Violation> singletonList(new IllegalSQLObjectViolation(sql));
|
||||
}
|
||||
|
||||
SQLStatement stmt = statementList.get(0);
|
||||
|
||||
WallVisitor visitor = createWallVisitor();
|
||||
|
||||
try {
|
||||
stmt.accept(visitor);
|
||||
} catch (ParserException e) {
|
||||
return Collections.<Violation> singletonList(new IllegalSQLObjectViolation(sql));
|
||||
}
|
||||
|
||||
if (visitor.getViolations().size() == 0) {
|
||||
if (sql.length() < whiteSqlMaxLength) {
|
||||
this.addWhiteSql(sql);
|
||||
}
|
||||
}
|
||||
|
||||
return visitor.getViolations();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.alibaba.druid.wall;
|
||||
|
||||
|
||||
public class WallRuntimeException extends RuntimeException {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public WallRuntimeException(){
|
||||
super();
|
||||
}
|
||||
|
||||
public WallRuntimeException(String reason, Throwable cause){
|
||||
super(reason, cause);
|
||||
}
|
||||
|
||||
public WallRuntimeException(String reason){
|
||||
super(reason);
|
||||
}
|
||||
|
||||
public WallRuntimeException(Throwable cause){
|
||||
super(cause);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.alibaba.druid.wall;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class WallSQLException extends SQLException {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public WallSQLException(){
|
||||
super();
|
||||
}
|
||||
|
||||
public WallSQLException(String reason, Throwable cause){
|
||||
super(reason, cause);
|
||||
}
|
||||
|
||||
public WallSQLException(String reason){
|
||||
super(reason);
|
||||
}
|
||||
|
||||
public WallSQLException(Throwable cause){
|
||||
super(cause);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.alibaba.druid.wall;
|
||||
|
||||
import com.alibaba.druid.wall.spi.MySqlWallProvider;
|
||||
import com.alibaba.druid.wall.spi.OracleWallProvider;
|
||||
import com.alibaba.druid.wall.spi.SQLServerProvider;
|
||||
|
||||
public class WallUtils {
|
||||
|
||||
public static boolean isValidateMySql(String sql) {
|
||||
MySqlWallProvider provider = new MySqlWallProvider();
|
||||
return provider.checkValid(sql);
|
||||
}
|
||||
|
||||
public static boolean isValidateMySql(String sql, WallConfig config) {
|
||||
MySqlWallProvider provider = new MySqlWallProvider(config);
|
||||
return provider.checkValid(sql);
|
||||
}
|
||||
|
||||
public static boolean isValidateOracle(String sql) {
|
||||
OracleWallProvider provider = new OracleWallProvider();
|
||||
return provider.checkValid(sql);
|
||||
}
|
||||
|
||||
public static boolean isValidateOracle(String sql, WallConfig config) {
|
||||
OracleWallProvider provider = new OracleWallProvider(config);
|
||||
return provider.checkValid(sql);
|
||||
}
|
||||
|
||||
public static boolean isValidateSqlServer(String sql) {
|
||||
SQLServerProvider provider = new SQLServerProvider();
|
||||
return provider.checkValid(sql);
|
||||
}
|
||||
|
||||
public static boolean isValidateSqlServer(String sql, WallConfig config) {
|
||||
SQLServerProvider provider = new SQLServerProvider(config);
|
||||
return provider.checkValid(sql);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.alibaba.druid.wall;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLObject;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
|
||||
public interface WallVisitor extends SQLASTVisitor {
|
||||
|
||||
WallConfig getConfig();
|
||||
|
||||
List<Violation> getViolations();
|
||||
|
||||
boolean isPermitTable(String name);
|
||||
|
||||
String toSQL(SQLObject obj);
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.alibaba.druid.wall.spi;
|
||||
|
||||
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
|
||||
import com.alibaba.druid.sql.parser.SQLStatementParser;
|
||||
import com.alibaba.druid.wall.WallConfig;
|
||||
import com.alibaba.druid.wall.WallProvider;
|
||||
import com.alibaba.druid.wall.WallVisitor;
|
||||
|
||||
public class MySqlWallProvider extends WallProvider {
|
||||
|
||||
public final static String DEFAULT_CONFIG_DIR = "META-INF/druid/wall/mysql";
|
||||
|
||||
public MySqlWallProvider(){
|
||||
this(new WallConfig(DEFAULT_CONFIG_DIR));
|
||||
}
|
||||
|
||||
public MySqlWallProvider(WallConfig config){
|
||||
super(config);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SQLStatementParser createParser(String sql) {
|
||||
return new MySqlStatementParser(sql);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WallVisitor createWallVisitor() {
|
||||
return new MySqlWallVisitor(config);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,226 @@
|
||||
package com.alibaba.druid.wall.spi;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLName;
|
||||
import com.alibaba.druid.sql.ast.SQLObject;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDeleteStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLInsertStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectGroupByClause;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLTruncateStatement;
|
||||
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;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter;
|
||||
import com.alibaba.druid.wall.IllegalSQLObjectViolation;
|
||||
import com.alibaba.druid.wall.Violation;
|
||||
import com.alibaba.druid.wall.WallConfig;
|
||||
import com.alibaba.druid.wall.WallVisitor;
|
||||
|
||||
public class MySqlWallVisitor extends MySqlASTVisitorAdapter implements WallVisitor, MySqlASTVisitor {
|
||||
|
||||
private final WallConfig config;
|
||||
private final List<Violation> violations = new ArrayList<Violation>();
|
||||
|
||||
public MySqlWallVisitor(WallConfig config){
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
public WallConfig getConfig() {
|
||||
return config;
|
||||
}
|
||||
|
||||
public List<Violation> getViolations() {
|
||||
return violations;
|
||||
}
|
||||
|
||||
public boolean visit(SQLPropertyExpr x) {
|
||||
WallVisitorUtils.check(this, x);
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean visit(SQLBinaryOpExpr x) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLSelectQueryBlock x) {
|
||||
WallVisitorUtils.checkSelelct(this, x);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(MySqlSelectQueryBlock x) {
|
||||
WallVisitorUtils.checkSelelct(this, x);
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean visit(SQLSelectGroupByClause x) {
|
||||
WallVisitorUtils.checkHaving(this, x.getHaving());
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean visit(MySqlSelectGroupBy x) {
|
||||
WallVisitorUtils.checkHaving(this, x.getHaving());
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(MySqlDeleteStatement x) {
|
||||
return visit((SQLDeleteStatement) x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLDeleteStatement x) {
|
||||
WallVisitorUtils.checkDelete(this, x);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLUpdateStatement x) {
|
||||
WallVisitorUtils.checkUpdate(this, x);
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(Limit x) {
|
||||
if (x.getRowCount() instanceof SQLNumericLiteralExpr) {
|
||||
int rowCount = ((SQLNumericLiteralExpr) x.getRowCount()).getNumber().intValue();
|
||||
if (rowCount == 0) {
|
||||
this.getViolations().add(new IllegalSQLObjectViolation(this.toSQL(x)));
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean visit(SQLVariantRefExpr x) {
|
||||
String varName = x.getName();
|
||||
if (varName == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (config.isVariantCheck() && varName.startsWith("@@")) {
|
||||
violations.add(new IllegalSQLObjectViolation(toSQL(x)));
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLMethodInvokeExpr x) {
|
||||
WallVisitorUtils.checkFunction(this, x);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean visit(SQLExprTableSource x) {
|
||||
WallVisitorUtils.check(this, x);
|
||||
|
||||
if (x.getExpr() instanceof SQLName) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(MySqlOutFileExpr x) {
|
||||
if (!config.isSelectIntoOutfileAllow()) {
|
||||
violations.add(new IllegalSQLObjectViolation(toSQL(x)));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLUnionQuery x) {
|
||||
WallVisitorUtils.checkUnion(this, x);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toSQL(SQLObject obj) {
|
||||
return SQLUtils.toMySqlString(obj);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPermitTable(String name) {
|
||||
if (!config.isTableCheck()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
name = WallVisitorUtils.form(name);
|
||||
return config.getPermitTables().contains(name);
|
||||
}
|
||||
|
||||
public void preVisit(SQLObject x) {
|
||||
if (!(x instanceof SQLStatement)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (config.isNoneBaseStatementAllow()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (x instanceof SQLInsertStatement) {
|
||||
|
||||
} else if (x instanceof SQLSelectStatement) {
|
||||
|
||||
} else if (x instanceof SQLDeleteStatement) {
|
||||
|
||||
} else if (x instanceof SQLUpdateStatement) {
|
||||
|
||||
} else if (x instanceof SQLTruncateStatement) {
|
||||
if (!config.isTruncateAllow()) {
|
||||
violations.add(new IllegalSQLObjectViolation(toSQL(x)));
|
||||
}
|
||||
} else {
|
||||
violations.add(new IllegalSQLObjectViolation(toSQL(x)));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.alibaba.druid.wall.spi;
|
||||
|
||||
import com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser;
|
||||
import com.alibaba.druid.sql.parser.SQLStatementParser;
|
||||
import com.alibaba.druid.wall.WallConfig;
|
||||
import com.alibaba.druid.wall.WallProvider;
|
||||
import com.alibaba.druid.wall.WallVisitor;
|
||||
|
||||
public class OracleWallProvider extends WallProvider {
|
||||
|
||||
public final static String DEFAULT_CONFIG_DIR = "META-INF/druid/wall/oracle";
|
||||
|
||||
public OracleWallProvider(){
|
||||
this(new WallConfig(DEFAULT_CONFIG_DIR));
|
||||
}
|
||||
|
||||
public OracleWallProvider(WallConfig config){
|
||||
super(config);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SQLStatementParser createParser(String sql) {
|
||||
return new OracleStatementParser(sql);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WallVisitor createWallVisitor() {
|
||||
return new OracleWallVisitor(config);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,222 @@
|
||||
package com.alibaba.druid.wall.spi;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLName;
|
||||
import com.alibaba.druid.sql.ast.SQLObject;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDeleteStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLInsertStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectGroupByClause;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLTruncateStatement;
|
||||
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;
|
||||
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectTableReference;
|
||||
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleUpdateStatement;
|
||||
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleASTVIsitorAdapter;
|
||||
import com.alibaba.druid.wall.IllegalSQLObjectViolation;
|
||||
import com.alibaba.druid.wall.Violation;
|
||||
import com.alibaba.druid.wall.WallConfig;
|
||||
import com.alibaba.druid.wall.WallVisitor;
|
||||
|
||||
public class OracleWallVisitor extends OracleASTVIsitorAdapter implements WallVisitor {
|
||||
|
||||
private final WallConfig config;
|
||||
private final List<Violation> violations = new ArrayList<Violation>();
|
||||
|
||||
public OracleWallVisitor(WallConfig config){
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
public WallConfig getConfig() {
|
||||
return config;
|
||||
}
|
||||
|
||||
public List<Violation> getViolations() {
|
||||
return violations;
|
||||
}
|
||||
|
||||
public boolean visit(SQLIdentifierExpr x) {
|
||||
String name = x.getName();
|
||||
name = WallVisitorUtils.form(name);
|
||||
if (config.isVariantCheck() && config.getPermitVariants().contains(name)) {
|
||||
getViolations().add(new IllegalSQLObjectViolation(toSQL(x)));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean visit(SQLPropertyExpr x) {
|
||||
WallVisitorUtils.check(this, x);
|
||||
return true;
|
||||
}
|
||||
|
||||
// executeQuery
|
||||
public boolean visit(SQLBinaryOpExpr x) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLMethodInvokeExpr x) {
|
||||
WallVisitorUtils.checkFunction(this, x);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean visit(OracleSelectTableReference x) {
|
||||
WallVisitorUtils.check(this, x);
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean visit(SQLExprTableSource x) {
|
||||
WallVisitorUtils.check(this, x);
|
||||
|
||||
if (x.getExpr() instanceof SQLName) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean visit(SQLSelectGroupByClause x) {
|
||||
WallVisitorUtils.checkHaving(this, x.getHaving());
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLSelectQueryBlock x) {
|
||||
WallVisitorUtils.checkSelelct(this, x);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(OracleSelectQueryBlock x) {
|
||||
WallVisitorUtils.checkSelelct(this, x);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLUnionQuery x) {
|
||||
WallVisitorUtils.checkUnion(this, x);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toSQL(SQLObject obj) {
|
||||
return SQLUtils.toOracleString(obj);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPermitTable(String name) {
|
||||
if (!config.isTableCheck()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
name = WallVisitorUtils.form(name);
|
||||
if (name.startsWith("v$") || name.startsWith("v_$")) {
|
||||
return true;
|
||||
}
|
||||
return config.getPermitTables().contains(name);
|
||||
}
|
||||
|
||||
public void preVisit(SQLObject x) {
|
||||
if (!(x instanceof SQLStatement)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (config.isNoneBaseStatementAllow()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (x instanceof SQLInsertStatement) {
|
||||
|
||||
} else if (x instanceof SQLSelectStatement) {
|
||||
|
||||
} else if (x instanceof SQLDeleteStatement) {
|
||||
|
||||
} else if (x instanceof SQLUpdateStatement) {
|
||||
} else if (x instanceof OracleMultiInsertStatement) {
|
||||
} else if (x instanceof OracleMergeStatement) {
|
||||
} else if (x instanceof SQLTruncateStatement) {
|
||||
if (!config.isTruncateAllow()) {
|
||||
violations.add(new IllegalSQLObjectViolation(toSQL(x)));
|
||||
}
|
||||
} else {
|
||||
violations.add(new IllegalSQLObjectViolation(toSQL(x)));
|
||||
}
|
||||
}
|
||||
|
||||
@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) {
|
||||
WallVisitorUtils.checkDelete(this, x);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(OracleUpdateStatement x) {
|
||||
return visit((SQLUpdateStatement) x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLUpdateStatement x) {
|
||||
WallVisitorUtils.checkUpdate(this, x);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.alibaba.druid.wall.spi;
|
||||
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser;
|
||||
import com.alibaba.druid.sql.parser.SQLStatementParser;
|
||||
import com.alibaba.druid.wall.WallConfig;
|
||||
import com.alibaba.druid.wall.WallProvider;
|
||||
import com.alibaba.druid.wall.WallVisitor;
|
||||
|
||||
/**
|
||||
* SQLServerProvider
|
||||
*
|
||||
* @author RaymondXiu
|
||||
* @version 1.0, 2012-3-17
|
||||
* @see
|
||||
*/
|
||||
public class SQLServerProvider extends WallProvider {
|
||||
|
||||
public final static String DEFAULT_CONFIG_DIR = "META-INF/druid/wall/sqlserver";
|
||||
|
||||
/**
|
||||
* @param config
|
||||
*/
|
||||
public SQLServerProvider() {
|
||||
this(new WallConfig(DEFAULT_CONFIG_DIR));
|
||||
}
|
||||
|
||||
|
||||
public SQLServerProvider(WallConfig config){
|
||||
super(config);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SQLStatementParser createParser(String sql) {
|
||||
return new SQLServerStatementParser(sql);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WallVisitor createWallVisitor() {
|
||||
return new SQLServerWallVisitor(config);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,181 @@
|
||||
package com.alibaba.druid.wall.spi;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLName;
|
||||
import com.alibaba.druid.sql.ast.SQLObject;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDeleteStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLInsertStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectGroupByClause;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLTruncateStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLUnionQuery;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLUpdateStatement;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerASTVisitor;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerASTVisitorAdapter;
|
||||
import com.alibaba.druid.wall.IllegalSQLObjectViolation;
|
||||
import com.alibaba.druid.wall.Violation;
|
||||
import com.alibaba.druid.wall.WallConfig;
|
||||
import com.alibaba.druid.wall.WallVisitor;
|
||||
|
||||
|
||||
public class SQLServerWallVisitor extends SQLServerASTVisitorAdapter implements WallVisitor, SQLServerASTVisitor{
|
||||
|
||||
private final WallConfig config;
|
||||
private final List<Violation> violations = new ArrayList<Violation>();
|
||||
|
||||
/**
|
||||
* @param config
|
||||
*/
|
||||
public SQLServerWallVisitor(WallConfig config) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WallConfig getConfig() {
|
||||
return this.config;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Violation> getViolations() {
|
||||
return violations;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPermitTable(String name) {
|
||||
if(!config.isTableCheck()){
|
||||
return false;
|
||||
}
|
||||
|
||||
name = WallVisitorUtils.form(name);
|
||||
return config.getPermitTables().contains(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toSQL(SQLObject obj) {
|
||||
return SQLUtils.toMySqlString(obj);
|
||||
}
|
||||
|
||||
|
||||
public boolean visit(SQLIdentifierExpr x) {
|
||||
String name = x.getName();
|
||||
name = WallVisitorUtils.form(name);
|
||||
if (config.isVariantCheck() && config.getPermitVariants().contains(name)) {
|
||||
getViolations().add(new IllegalSQLObjectViolation(toSQL(x)));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean visit(SQLPropertyExpr x) {
|
||||
WallVisitorUtils.check(this, x);
|
||||
return true;
|
||||
}
|
||||
|
||||
// executeQuery
|
||||
public boolean visit(SQLBinaryOpExpr x) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLMethodInvokeExpr x) {
|
||||
WallVisitorUtils.checkFunction(this, x);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean visit(SQLExprTableSource x) {
|
||||
WallVisitorUtils.check(this, x);
|
||||
|
||||
if (x.getExpr() instanceof SQLName) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean visit(SQLSelectGroupByClause x) {
|
||||
WallVisitorUtils.checkHaving(this, x.getHaving());
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLSelectQueryBlock x) {
|
||||
WallVisitorUtils.checkSelelct(this, x);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLUnionQuery x) {
|
||||
WallVisitorUtils.checkUnion(this, x);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void preVisit(SQLObject x) {
|
||||
if (!(x instanceof SQLStatement)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (config.isNoneBaseStatementAllow()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (x instanceof SQLInsertStatement) {
|
||||
|
||||
} else if (x instanceof SQLSelectStatement) {
|
||||
|
||||
} else if (x instanceof SQLDeleteStatement) {
|
||||
|
||||
} else if (x instanceof SQLUpdateStatement) {
|
||||
} else if (x instanceof SQLTruncateStatement) {
|
||||
if (!config.isTruncateAllow()) {
|
||||
violations.add(new IllegalSQLObjectViolation(toSQL(x)));
|
||||
}
|
||||
} else {
|
||||
violations.add(new IllegalSQLObjectViolation(toSQL(x)));
|
||||
}
|
||||
}
|
||||
|
||||
@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(SQLInsertStatement x) {
|
||||
if (!config.isInsertAllow()) {
|
||||
this.getViolations().add(new IllegalSQLObjectViolation(this.toSQL(x)));
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLDeleteStatement x) {
|
||||
WallVisitorUtils.checkDelete(this, x);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLUpdateStatement x) {
|
||||
WallVisitorUtils.checkUpdate(this, x);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,555 @@
|
||||
package com.alibaba.druid.wall.spi;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.URL;
|
||||
import java.util.Enumeration;
|
||||
import java.util.Set;
|
||||
|
||||
import com.alibaba.druid.logging.Log;
|
||||
import com.alibaba.druid.logging.LogFactory;
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLName;
|
||||
import com.alibaba.druid.sql.ast.SQLObject;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLAggregateExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLBinaryOperator;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLCharExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIntegerExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLNCharExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLNotExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLNullExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLQueryExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDeleteStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelect;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectItem;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectQuery;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLTableSource;
|
||||
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.MySqlBooleanExpr;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
import com.alibaba.druid.wall.IllegalSQLObjectViolation;
|
||||
import com.alibaba.druid.wall.WallVisitor;
|
||||
|
||||
public class WallVisitorUtils {
|
||||
|
||||
private final static Log LOG = LogFactory.getLog(WallVisitorUtils.class);
|
||||
|
||||
public static void check(WallVisitor visitor, SQLPropertyExpr x) {
|
||||
if (x.getOwner() instanceof SQLIdentifierExpr) {
|
||||
String owner = x.getOwner().toString();
|
||||
owner = WallVisitorUtils.form(owner);
|
||||
if (visitor.getConfig().isPermitObjects(owner)) {
|
||||
addViolation(visitor, x);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void checkSelelct(WallVisitor visitor, SQLSelectQueryBlock x) {
|
||||
if (!visitor.getConfig().isSelectIntoAllow() && x.getInto() != null) {
|
||||
addViolation(visitor, x);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!visitor.getConfig().isSelectWhereAlwayTrueCheck()) {
|
||||
return;
|
||||
}
|
||||
|
||||
SQLExpr where = x.getWhere();
|
||||
if (where != null && Boolean.TRUE == getValue(where)) {
|
||||
if (where instanceof SQLBinaryOpExpr) {
|
||||
SQLBinaryOpExpr binaryOpExpr = (SQLBinaryOpExpr) where;
|
||||
if (binaryOpExpr.getOperator() == SQLBinaryOperator.Equality
|
||||
|| binaryOpExpr.getOperator() == SQLBinaryOperator.NotEqual) {
|
||||
if (binaryOpExpr.getLeft() instanceof SQLIntegerExpr
|
||||
&& binaryOpExpr.getRight() instanceof SQLIntegerExpr) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
addViolation(visitor, x);
|
||||
}
|
||||
}
|
||||
|
||||
public static void checkHaving(WallVisitor visitor, SQLExpr x) {
|
||||
if (x == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!visitor.getConfig().isSelectHavingAlwayTrueCheck()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (Boolean.TRUE == getValue(x)) {
|
||||
addViolation(visitor, x);
|
||||
}
|
||||
}
|
||||
|
||||
public static void checkDelete(WallVisitor visitor, SQLDeleteStatement x) {
|
||||
if (!visitor.getConfig().isDeleteAllow()) {
|
||||
addViolation(visitor, x);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!visitor.getConfig().isDeleteWhereAlwayTrueCheck()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (x.getWhere() == null || Boolean.TRUE == getValue(x.getWhere())) {
|
||||
addViolation(visitor, x);
|
||||
}
|
||||
}
|
||||
|
||||
public static void checkUpdate(WallVisitor visitor, SQLUpdateStatement x) {
|
||||
if (!visitor.getConfig().isUpdateAllow()) {
|
||||
addViolation(visitor, x);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!visitor.getConfig().isUpdateWhereAlayTrueCheck()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (x.getWhere() == null || Boolean.TRUE == getValue(x.getWhere())) {
|
||||
addViolation(visitor, x);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
public static Object getValue(SQLBinaryOpExpr x) {
|
||||
x.getLeft().setParent(x);
|
||||
x.getRight().setParent(x);
|
||||
Object leftResult = getValue(x.getLeft());
|
||||
Object rightResult = getValue(x.getRight());
|
||||
|
||||
if (x.getOperator() == SQLBinaryOperator.BooleanOr) {
|
||||
if (Boolean.TRUE == leftResult || Boolean.TRUE == rightResult) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (x.getOperator() == SQLBinaryOperator.BooleanAnd) {
|
||||
if (Boolean.FALSE == leftResult || Boolean.FALSE == rightResult) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (Boolean.TRUE == leftResult && Boolean.TRUE == rightResult) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (x.getOperator() == SQLBinaryOperator.Like) {
|
||||
if (x.getRight() instanceof SQLCharExpr) {
|
||||
String text = ((SQLCharExpr) x.getRight()).getText();
|
||||
|
||||
if (text.length() >= 0) {
|
||||
for (char ch : text.toCharArray()) {
|
||||
if (ch != '%') {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if (leftResult == null || rightResult == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (x.getOperator() == SQLBinaryOperator.Equality) {
|
||||
if (x.getLeft() instanceof SQLNullExpr && x.getRight() instanceof SQLNullExpr) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return leftResult.equals(rightResult);
|
||||
}
|
||||
|
||||
if (x.getOperator() == SQLBinaryOperator.NotEqual || x.getOperator() == SQLBinaryOperator.LessThanOrGreater) {
|
||||
if (x.getLeft() instanceof SQLNullExpr && x.getRight() instanceof SQLNullExpr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (leftResult == null || rightResult == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return !leftResult.equals(rightResult);
|
||||
}
|
||||
|
||||
if (x.getOperator() == SQLBinaryOperator.GreaterThan) {
|
||||
if (x.getLeft() instanceof SQLNullExpr && x.getRight() instanceof SQLNullExpr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (leftResult == null || rightResult == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (leftResult instanceof Comparable) {
|
||||
return (((Comparable) leftResult).compareTo(rightResult) > 0);
|
||||
}
|
||||
}
|
||||
|
||||
if (x.getOperator() == SQLBinaryOperator.GreaterThanOrEqual) {
|
||||
if (x.getLeft() instanceof SQLNullExpr && x.getRight() instanceof SQLNullExpr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (leftResult == null || rightResult == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (leftResult instanceof Comparable) {
|
||||
return ((Comparable) leftResult).compareTo(rightResult) >= 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (x.getOperator() == SQLBinaryOperator.LessThan) {
|
||||
if (x.getLeft() instanceof SQLNullExpr && x.getRight() instanceof SQLNullExpr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (leftResult == null || rightResult == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (leftResult instanceof Comparable) {
|
||||
return (((Comparable) leftResult).compareTo(rightResult) < 0);
|
||||
}
|
||||
}
|
||||
|
||||
if (x.getOperator() == SQLBinaryOperator.LessThanOrEqual) {
|
||||
if (x.getLeft() instanceof SQLNullExpr && x.getRight() instanceof SQLNullExpr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (leftResult == null || rightResult == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (leftResult instanceof Comparable) {
|
||||
return ((Comparable) leftResult).compareTo(rightResult) <= 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (x.getOperator() == SQLBinaryOperator.Concat) {
|
||||
return leftResult.toString() + rightResult.toString();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Object getValue(SQLExpr x) {
|
||||
if (x instanceof SQLBinaryOpExpr) {
|
||||
return getValue((SQLBinaryOpExpr) x);
|
||||
}
|
||||
|
||||
if (x instanceof MySqlBooleanExpr) {
|
||||
return ((MySqlBooleanExpr) x).getValue();
|
||||
}
|
||||
|
||||
if (x instanceof SQLNumericLiteralExpr) {
|
||||
return ((SQLNumericLiteralExpr) x).getNumber();
|
||||
}
|
||||
|
||||
if (x instanceof SQLCharExpr) {
|
||||
return ((SQLCharExpr) x).getText();
|
||||
}
|
||||
|
||||
if (x instanceof SQLNCharExpr) {
|
||||
return ((SQLNCharExpr) x).getText();
|
||||
}
|
||||
|
||||
if (x instanceof SQLNotExpr) {
|
||||
Object result = getValue(((SQLNotExpr) x).getExpr());
|
||||
if (result != null && result instanceof Boolean) {
|
||||
return !((Boolean) result).booleanValue();
|
||||
}
|
||||
}
|
||||
|
||||
if (x instanceof SQLQueryExpr) {
|
||||
if (isSimpleCountTableSource(((SQLQueryExpr) x).getSubQuery())) {
|
||||
return Integer.valueOf(1);
|
||||
}
|
||||
}
|
||||
|
||||
if (x instanceof SQLMethodInvokeExpr) {
|
||||
return getValue((SQLMethodInvokeExpr) x);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Object getValue(SQLMethodInvokeExpr x) {
|
||||
String methodName = x.getMethodName();
|
||||
if ("len".equalsIgnoreCase(methodName) || "length".equalsIgnoreCase(methodName)) {
|
||||
Object firstValue = null;
|
||||
if (x.getParameters().size() > 0) {
|
||||
firstValue = (getValue(x.getParameters().get(0)));
|
||||
}
|
||||
|
||||
if (firstValue instanceof String) {
|
||||
return ((String) firstValue).length();
|
||||
}
|
||||
}
|
||||
|
||||
if ("if".equalsIgnoreCase(methodName) && x.getParameters().size() == 3) {
|
||||
SQLExpr first = x.getParameters().get(0);
|
||||
Object firstResult = getValue(first);
|
||||
|
||||
if (Boolean.TRUE == firstResult) {
|
||||
return getValue(x.getParameters().get(1));
|
||||
}
|
||||
|
||||
if (Boolean.FALSE == firstResult) {
|
||||
getValue(x.getParameters().get(2));
|
||||
}
|
||||
}
|
||||
|
||||
if ("chr".equalsIgnoreCase(methodName) && x.getParameters().size() == 1) {
|
||||
SQLExpr first = x.getParameters().get(0);
|
||||
Object firstResult = getValue(first);
|
||||
if (firstResult instanceof Number) {
|
||||
int intValue = ((Number) firstResult).intValue();
|
||||
char ch = (char) intValue;
|
||||
|
||||
return "" + ch;
|
||||
}
|
||||
}
|
||||
|
||||
if ("concat".equalsIgnoreCase(methodName)) {
|
||||
StringBuffer buf = new StringBuffer();
|
||||
for (SQLExpr expr : x.getParameters()) {
|
||||
Object value = getValue(expr);
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
buf.append(value.toString());
|
||||
}
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static boolean isSimpleCountTableSource(SQLTableSource tableSource) {
|
||||
if (!(tableSource instanceof SQLSubqueryTableSource)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
SQLSubqueryTableSource subQuery = (SQLSubqueryTableSource) tableSource;
|
||||
|
||||
return isSimpleCountTableSource(subQuery.getSelect());
|
||||
}
|
||||
|
||||
public static boolean isSimpleCountTableSource(SQLSelect select) {
|
||||
SQLSelectQuery query = select.getQuery();
|
||||
|
||||
if (query instanceof SQLSelectQueryBlock) {
|
||||
SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) query;
|
||||
|
||||
boolean allawTrueWhere = false;
|
||||
|
||||
if (queryBlock.getWhere() == null) {
|
||||
allawTrueWhere = true;
|
||||
} else {
|
||||
Object whereValue = getValue(queryBlock.getWhere());
|
||||
if (whereValue == Boolean.TRUE) {
|
||||
allawTrueWhere = true;
|
||||
} else if (whereValue == Boolean.FALSE) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
boolean simpleCount = false;
|
||||
if (queryBlock.getSelectList().size() == 1) {
|
||||
SQLExpr selectItemExpr = queryBlock.getSelectList().get(0).getExpr();
|
||||
if (selectItemExpr instanceof SQLAggregateExpr) {
|
||||
if (((SQLAggregateExpr) selectItemExpr).getMethodName().equalsIgnoreCase("COUNT")) {
|
||||
simpleCount = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (allawTrueWhere && simpleCount) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static void checkFunction(WallVisitor visitor, SQLMethodInvokeExpr x) {
|
||||
if (x.getOwner() instanceof SQLIdentifierExpr) {
|
||||
String owner = x.getOwner().toString();
|
||||
owner = WallVisitorUtils.form(owner);
|
||||
if (visitor.getConfig().isPermitObjects(owner)) {
|
||||
addViolation(visitor, x);
|
||||
}
|
||||
}
|
||||
|
||||
if (!visitor.getConfig().isFunctionCheck()) {
|
||||
return;
|
||||
}
|
||||
|
||||
String methodName = x.getMethodName();
|
||||
|
||||
if (visitor.getConfig().isPermitFunction(methodName.toLowerCase())) {
|
||||
addViolation(visitor, x);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void check(WallVisitor visitor, SQLExprTableSource x) {
|
||||
SQLExpr expr = x.getExpr();
|
||||
|
||||
String tableName = null;
|
||||
if (expr instanceof SQLPropertyExpr) {
|
||||
SQLPropertyExpr propExpr = (SQLPropertyExpr) expr;
|
||||
|
||||
if (propExpr.getOwner() instanceof SQLIdentifierExpr) {
|
||||
String ownerName = ((SQLIdentifierExpr) propExpr.getOwner()).getName();
|
||||
|
||||
ownerName = form(ownerName);
|
||||
|
||||
if (visitor.getConfig().getPermitSchemas().contains(ownerName.toLowerCase())) {
|
||||
addViolation(visitor, x);
|
||||
}
|
||||
}
|
||||
|
||||
tableName = propExpr.getName();
|
||||
}
|
||||
|
||||
if (expr instanceof SQLIdentifierExpr) {
|
||||
tableName = ((SQLIdentifierExpr) expr).getName();
|
||||
}
|
||||
|
||||
if (tableName != null) {
|
||||
tableName = form(tableName);
|
||||
if (visitor.isPermitTable(tableName)) {
|
||||
addViolation(visitor, x);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void addViolation(WallVisitor visitor, SQLObject x) {
|
||||
visitor.getViolations().add(new IllegalSQLObjectViolation(visitor.toSQL(x)));
|
||||
}
|
||||
|
||||
public static void checkUnion(WallVisitor visitor, SQLUnionQuery x) {
|
||||
if (!visitor.getConfig().isSelectUnionCheck()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (WallVisitorUtils.queryBlockFromIsNull(x.getLeft()) || WallVisitorUtils.queryBlockFromIsNull(x.getRight())) {
|
||||
addViolation(visitor, x);
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean queryBlockFromIsNull(SQLSelectQuery query) {
|
||||
if (query instanceof SQLSelectQueryBlock) {
|
||||
SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) query;
|
||||
SQLTableSource from = queryBlock.getFrom();
|
||||
|
||||
if (from == null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (from instanceof SQLExprTableSource) {
|
||||
SQLExpr fromExpr = ((SQLExprTableSource) from).getExpr();
|
||||
if (fromExpr instanceof SQLName) {
|
||||
String name = fromExpr.toString();
|
||||
|
||||
name = form(name);
|
||||
|
||||
if (name.equalsIgnoreCase("DUAL")) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (queryBlock.getSelectList().size() == 1
|
||||
&& queryBlock.getSelectList().get(0).getExpr() instanceof SQLAllColumnExpr) {
|
||||
if (from instanceof SQLSubqueryTableSource) {
|
||||
SQLSelectQuery subQuery = ((SQLSubqueryTableSource) from).getSelect().getQuery();
|
||||
if (queryBlockFromIsNull(subQuery)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
boolean allIsConst = true;
|
||||
for (SQLSelectItem item : queryBlock.getSelectList()) {
|
||||
if (getValue(item.getExpr()) == null) {
|
||||
allIsConst = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (allIsConst) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static String form(String name) {
|
||||
if (name.startsWith("\"") && name.endsWith("\"")) {
|
||||
name = name.substring(1, name.length() - 1);
|
||||
}
|
||||
if (name.startsWith("`") && name.endsWith("`")) {
|
||||
name = name.substring(1, name.length() - 1);
|
||||
}
|
||||
|
||||
name = name.toLowerCase();
|
||||
return name;
|
||||
}
|
||||
|
||||
public static void loadResource(Set<String> names, String resource) {
|
||||
try {
|
||||
Enumeration<URL> e = Thread.currentThread().getContextClassLoader().getResources(resource);
|
||||
while (e.hasMoreElements()) {
|
||||
URL url = e.nextElement();
|
||||
InputStream in = null;
|
||||
BufferedReader reader = null;
|
||||
try {
|
||||
in = url.openStream();
|
||||
reader = new BufferedReader(new InputStreamReader(in));
|
||||
for (;;) {
|
||||
String line = reader.readLine();
|
||||
if (line == null) {
|
||||
break;
|
||||
}
|
||||
line = line.trim();
|
||||
if (line.length() > 0) {
|
||||
line = line.toLowerCase();
|
||||
names.add(line);
|
||||
}
|
||||
}
|
||||
|
||||
url.openStream();
|
||||
} finally {
|
||||
JdbcUtils.close(reader);
|
||||
JdbcUtils.close(in);
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
LOG.error("load oracle permit tables errror", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user