2014-01-21 53 views
5

Fority掃描報告「路徑操作」的安全性問題在下面的代碼片段Fortify的路徑操作錯誤

String filePath = getFilePath(fileLocation, fileName); 
final File file = new File(filePath); 
LOGGER.info("Saving report at : " + filePath); 
BufferedWriter fileWriter = new BufferedWriter(new FileWriter(file)); 
fileWriter.write(fileContent); 

,所以我在fileLocation檢查列入黑名單的人物和投擲例外,仍然是Fortify的是拋出異常。

try { 
    String filePath = getFilePath(fileLocation, fileName); 
    if (isSecurePath(filePath)) { 
     final File file = new File(filePath); 
     LOGGER.info("Saving report at : " + filePath); 
     BufferedWriter fileWriter = new BufferedWriter(new FileWriter(file)); 
     fileWriter.write(fileContent); 
    } else { 
     throw new Exception("Security Issue. File Path has blacklisted characters"); 
    } 

} catch (final Exception e) { 
    LOGGER.error("Unable to prepare mail attachment : ", e); 
    message = "Mail cannot be send, Unable to prepare mail attachment"; 
} 


private boolean isSecurePath(String filePath) { 
    String[] blackListChars = {".."}; 
    return (StringUtils.indexOfAny(filePath, blackListChars)< 0); 
} 

我應該忽略掃描報告還是會對此進行正確的修復?

+0

瞧這也https://security.stackexchange.com/questions/103884/how-to-resolve-path-manipulation-error-given-by-fortify –

回答

1

首先SCA是一個靜態分析工具,所以無法檢查您的自定義驗證,以確定它是否工作正常與否,這是後話的動態工具,如WebInspect可以是專門做。其次,黑名單是保護任何東西的一種不好的方式,白名單是一種更安全的方法,而且你提到黑名單驗證的事實將會吸引攻擊者。這是因爲您必須考慮到每種可能的被攻擊方式,包括可能尚未發現的方式,因此在軟件發佈前很容易過時。

第三,這肯定是不夠的反對停止路徑操作,因爲你只佔人找相對路徑更具體地說當前目錄上述相對路徑。

您無法檢測到某人是否指定了完整路徑,或者是否有人轉到一個目錄,該目錄完全是一個到單獨目錄的符號鏈接,以及其他一些可能的替代攻擊。

理想情況下,你應該遵循SCA所示的建議,並有一個非常具體的允許路徑&文件名。如果這是不可能的,使用白名單技術,以指定唯一允許的字符,然後驗證指定它不是例如SMB共享或指定的完整路徑。如果它不根據用戶應指定的規範進行驗證,則拒絕它。

這樣做將擺脫這個問題本身,而是SCA可能仍會顯示在結果中的問題(這也是因爲VS動態分析靜態之間的差異)。這可以通過審覈它或者爲驗證問題的函數創建自定義清理規則來解決。