Skip to content

Commit

Permalink
Remove SQL Injection Filter of NativeRepository And Relative Refactor (
Browse files Browse the repository at this point in the history
  • Loading branch information
sim-wangyan committed Oct 15, 2020
1 parent 60dfd6e commit c3b0b3c
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
public interface UnsafeSyntaxFilter {

default String filter(String sql) {
return sql.replace("'", "''")
.replace(";", SqlScript.SPACE);
return sql.replace("'", "''");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
*/
public interface NativeSupport {

<T> boolean execute(Class<T> clzz, String sql);
boolean execute(String sql, Object...objs);

List<Map<String,Object>> list(String sql, List<Object> conditionList);
}
2 changes: 1 addition & 1 deletion sqli-core/src/main/java/io/xream/sqli/spi/JdbcHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public interface JdbcHelper extends BaseFinder, ResultMapFinder {

boolean remove(String sql, Object id);

boolean execute(String sql);
boolean execute(String sql,Object...objs);

<K> List<K> queryForPlainValueList(Class<K> clzz, String sql, Collection<Object> valueList, Dialect dialect);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,19 +230,8 @@ public <T> List<T> list(Criteria criteria) {
}


public <T> boolean execute(Class<T> clzz, String sql) {

Parsed parsed = Parser.get(clzz);
boolean b = dao.execute(clzz, sql);

if (!b)
return b;
if (isCacheEnabled(parsed)) {
String key = ParserUtil.getCacheKey(clzz, parsed);
cacheResolver.refresh(clzz, key);
}

return b;
public boolean execute(String sql, Object...objs) {
return dao.execute(sql,objs);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ List<Map<String,Object>> list(String sql,

<T> List<T> list(Criteria criteria);

@Deprecated
<T> boolean execute(Class<T> clzz, String sql);
boolean execute(String sql, Object...objs);

<T> T getOne(T conditionObj);

Expand Down
16 changes: 4 additions & 12 deletions sqli-repo/src/main/java/io/xream/sqli/repository/dao/DaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package io.xream.sqli.repository.dao;

import io.xream.sqli.annotation.X;
import io.xream.sqli.api.NativeRepository;
import io.xream.sqli.builder.*;
import io.xream.sqli.builder.internal.PageBuilderHelper;
import io.xream.sqli.converter.ObjectDataConverter;
Expand Down Expand Up @@ -175,8 +176,6 @@ public boolean createOrReplace(Object obj) {
@Override
public List<Map<String, Object>> list(String sql, List<Object> conditionList) {

sql = sqlBuilder.filter(sql);

return this.jdbcHelper.queryForResultMapList(sql, conditionList,null, null,this.dialect);
}

Expand Down Expand Up @@ -262,21 +261,14 @@ private long getCount(Class clz, String sql, Collection<Object> list) {

/**
*
* @param clzz
* @param
* @param sql
*/
@Deprecated
@Override
public boolean execute(Class clzz, String sql) {

Parsed parsed = Parser.get(clzz);

sql = sqlBuilder.filter(sql);
sql = SqlParserUtil.mapperForNative(sql, parsed);

SqliLoggerProxy.debug(clzz, sql);
public boolean execute(String sql, Object...objs) {

return this.jdbcHelper.execute(sql);
return this.jdbcHelper.execute(sql,objs);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ public void setNativeSupport(NativeSupport nativeSupport){
}

@Override
public <T> boolean execute(Class<T> clzz, String sql){
public boolean execute(String sql, Object...objs){
try {
return nativeSupport.execute(clzz, sql);
return nativeSupport.execute(sql, objs);
}catch (Exception e) {
if (e instanceof RuntimeException){
throw e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,35 +26,10 @@
*/
public final class SqlParserUtil {

public final static String COMMA = ",";
public final static String SPACE = " ";
public final static String SQL_KEYWORD_MARK = "`";


public static String mapperForNative(String sqlSegment, Parsed parsed) {

sqlSegment = mapper(sqlSegment,parsed);

if (parsed.isNoSpec())
return sqlSegment;

if (!sqlSegment.contains(COMMA))
return sqlSegment;

for (String property : parsed.getPropertyMapperMap().keySet()){//FIXME 解析之后, 替换,拼接
String key = SPACE+property+COMMA;
String value = SPACE+parsed.getMapper(property)+COMMA;
sqlSegment = sqlSegment.replaceAll(key, value);
}
for (String property : parsed.getPropertyMapperMap().keySet()){//FIXME 解析之后, 替换,拼接
String key = COMMA+property+COMMA;
String value = COMMA+parsed.getMapper(property)+COMMA;
sqlSegment = sqlSegment.replaceAll(key, value);
}
return sqlSegment;
}


public static String mapper(String sql, Parsed parsed) {

if (parsed.isNoSpec())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.xream.sqli.builder.DialectSupport;
import io.xream.sqli.core.NativeSupport;
import io.xream.sqli.core.RepositoryManagement;
import io.xream.sqli.exception.ParsingException;
import io.xream.sqli.parser.Parser;
import io.xream.sqli.repository.exception.UninitializedException;
import io.xream.sqli.repository.init.SqlInit;
Expand All @@ -37,22 +38,30 @@ public class InitializerListener {
private final static Logger logger = LoggerFactory.getLogger(InitializerListener.class);

private static InitializerListener instance;
private InitializerListener(){}

private InitializerListener() {
}

public static void onStarted(NativeSupport nativeSupport, DialectSupport dialect, SqlInit sqlInit) {

if (instance != null)
return;
instance = new InitializerListener();


for (BaseRepository repository : RepositoryManagement.REPOSITORY_LIST) {
if (repository.getClzz() == Void.class)
continue;
logger.info("Parsing {}" ,repository.getClzz());
Parser.get(repository.getClzz());
logger.info("Parsing {}", repository.getClzz());
try {
Parser.get(repository.getClzz());
} catch (Exception e) {
if (e instanceof ParsingException) {
throw new ParsingException(repository.getClzz() + ", " + e.getMessage());
}
}
}


boolean flag = false;
boolean isNotSupportTableSql = false;

Expand All @@ -65,12 +74,12 @@ public static void onStarted(NativeSupport nativeSupport, DialectSupport dialect
String createSql = sqlInit.tryToParse(clz);
String test = sqlInit.getSql(clz, SqlInit.CREATE);
if (SqliStringUtil.isNullOrEmpty(test)) {
logger.info("Failed to start sqli-repo, check Bean: {}",clz);
logger.info("Failed to start sqli-repo, check Bean: {}", clz);
throw new UninitializedException("Failed to start sqli-repo, check Bean: " + clz);
}

if (SqliStringUtil.isNotNull(createSql)) {
nativeSupport.execute(clz, createSql);
nativeSupport.execute(createSql);
}

} catch (Exception e) {
Expand All @@ -85,7 +94,7 @@ public static void onStarted(NativeSupport nativeSupport, DialectSupport dialect
logger.info("The dialect not support creating table, try to implement Dialect.buildTableSql(clzz, isTemporary)");
}

logger.info("sqli-repo " + (flag ? "still " : "") + "started" + (flag ? " OK, wtih some problem" : "" ) + "\n");
logger.info("sqli-repo " + (flag ? "still " : "") + "started" + (flag ? " OK, wtih some problem" : "") + "\n");

}
}

0 comments on commit c3b0b3c

Please sign in to comment.