CVE-2020-5421 的修复方法说明

原因分析

CVE-2020-5421 的漏洞是在修复CVE-2015-5211时,留下的一个漏洞。在对url做过滤查找文件名称前,先针对性的处理了“;jsessionid=xxxx; 在发现”;jsessionId=“开始到下一个分号结束的部分内不检查是否存在文件名称,而漏洞就可以通过”;jsessionid=ssddfeff&setup.bat"这样的方式存在了。

 protected String determineEncoding(HttpServletRequest request) {
     * @return the updated URI string
     */
    public String removeSemicolonContent(String requestUri) {
        return (this.removeSemicolonContent ?
                removeSemicolonContentInternal(requestUri) : removeJsessionid(requestUri));
        return (this.removeSemicolonContent ? removeSemicolonContentInternal(requestUri) : requestUri);
    }

问题出在里面混杂的removeJsessionid 这个处理,因为它做了如下的处理

    private String removeJsessionid(String requestUri) {    
        int startIndex = requestUri.toLowerCase().indexOf(";jsessionid=");  
        if (startIndex != -1) { 
            int endIndex = requestUri.indexOf(';', startIndex + 12);    
            String start = requestUri.substring(0, startIndex); 
            requestUri = (endIndex != -1) ? start + requestUri.substring(endIndex) : start; 
        }   
        return requestUri;  
    }

修改方法说明

因此修改时去掉了对jsessionid关键字的处理,改为对路径中的内容始终全部检查,也就是去掉这个化蛇添足的步骤。

public String removeSemicolonContent(String requestUri) {
    return (this.removeSemicolonContent ?  
        removeSemicolonContentInternal(requestUri) : requestUri);
        
 }

单元测试中修改后的测试用例

@Test
    public void getRequestKeepSemicolonContent() throws 
        ....
        //  **针对CVE-2020-5421增加的测试**
        helper.getRequestUri(request)); 
        assertEquals("/foo;jsessionid=c0o7fszeb1", helper.getRequestUri(request)); 

commit:2f75212eb667a30fe2fa9b5aca8f22d5e255821f 中还补充了对Maxtrix variable 的处理,这里又跳过了对jessionid部分的处理,但这是为了避免在解析出的矩阵变量中混入了jsessionid,所以这次提交的名称是Avoid unnecessary parsing of path params,其实里面是有两个目的的。

附:CVE-2015-5211 的修复

在修复CVE-2015-5211的代码中所做的处理是:解析url中的最后一节(最后一个'/'之后的内容'),如果存在文件名称,取得其扩展名。如果不是如下的白名单中的类型,就在头信息中设置:

"Content-Disposition: inline;filename=f.txt"

让其以固定的文件名称f.txt下载,以避免出现不受控制的文件类型。

文件类型白名单:"txt", "text", "json", "xml", "atom", "rss", "png", "jpe", "jpeg", "jpg", "gif", "wbmp", "bmp"

[前一篇]CVE-2020-5421 的复现和检测方法探寻
[后一篇]对Spring framwrok 3.1.1 修复RDF缺陷(CVE-2015-5211及CVE-2020-5421)的全过程

参考

Spring-framework 对应CVE-2020-5421的修改
Spring-framework 对应CVE-2015-5211的修改

你可能感兴趣的:(CVE-2020-5421 的修复方法说明)