美文网首页Java 杂谈
Druid 的 WallFilter 抛出 sql inject

Druid 的 WallFilter 抛出 sql inject

作者: deniro | 来源:发表于2019-07-21 14:51 被阅读1次

    1 现象

    查询某个模块数据时,抛出以下异常:

    Caused by: java.sql.SQLException: sql injection violation, comment not allow : select count(*) FROM sys_x a WHERE 1=1 --澶囨敞 AND a.organization_id NOT IN( SELECT b.descendant_id FROM sys_y b WHERE b.path_length!=0) 
        at com.alibaba.druid.wall.WallFilter.checkInternal(WallFilter.java:800)
        at com.alibaba.druid.wall.WallFilter.connection_prepareStatement(WallFilter.java:251)
        at com.alibaba.druid.filter.FilterChainImpl.connection_prepareStatement(FilterChainImpl.java:473)
        at com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl.prepareStatement(ConnectionProxyImpl.java:342)
        at com.alibaba.druid.pool.DruidPooledConnection.prepareStatement(DruidPooledConnection.java:349)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy$TransactionAwareInvocationHandler.invoke(TransactionAwareDataSourceProxy.java:239)
        at com.sun.proxy.$Proxy23.prepareStatement(Unknown Source)
        at org.springframework.jdbc.core.PreparedStatementCreatorFactory$PreparedStatementCreatorImpl.createPreparedStatement(PreparedStatementCreatorFactory.java:245)
        at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:583)
        ... 59 more
    

    2 原因

    1. 在数据源配置时,加上了 Druid 的 wall 过滤器。而它默认的拦截策略是,不允许 SQL 中带有备注。
    2. 在该条 SQL 语句中,果然加了备注。

    3 解决

    1. 如果是新项目,SQL 语句较少,那么可以去除语句中的备注。
    2. 如果是老项目,那么就必须对 Druid 的 wall 过滤器进行配置,打开项目的 XML 配置文件,配置 druid 拦截过滤器,允许 SQL 语句中存在注释:
    <!--配置 druid 拦截过滤器-->
    <bean id="wall-filter-config" class="com.alibaba.druid.wall.WallConfig" init-method="init">
        <!-- 是否允许语句中存在注释-->
        <property name="commentAllow" value="true" />
    </bean>
    <bean id="wall-filter" class="com.alibaba.druid.wall.WallFilter">
        <property name="config" ref="wall-filter-config" />
    </bean>
    

    然后在 Druid 数据源配置中,加入 Druid 拦截过滤器:

    <!--druid 数据源-->
    <bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
        ...
        <!--配置 Druid 过滤器-->
        <property name="proxyFilters">
            <list>
                    ...
                <ref bean="wall-filter"/>
            </list>
        </property>
            ...
    </bean>
    

    重启应用,看看问题是不是已经解决啦O(∩_∩)O哈哈~

    相关文章

      网友评论

        本文标题:Druid 的 WallFilter 抛出 sql inject

        本文链接:https://www.haomeiwen.com/subject/wowmlctx.html