[英]Header Manipulation issue with HP Fortify in HTTP response [java]
我正在嘗試修復此代碼的 HP Fortify Scan 返回的“標題操作”問題。 我不知道文件在上傳期間是否已經過驗證(我認為沒有)。 我嘗試使用正則表達式來驗證文件名,但沒有成功。 任何人都可以幫助我嗎?
b = uploadedFiles.getFilecontent().getBytes(1,
uploadedFiles.getFilesize().intValue());
if (b != null) {
response.reset();
String fileName = uploadedFiles.getFilename();
String header = "attachment; filename=\"" + fileName + "\"";
String contentType = uploadedFiles.getFilecontenttype();
response.setContentType(uploadedFiles.getFilecontenttype());
response.addHeader("Content-Transfer-Encoding", "Binary");
response.addHeader("Cache-Control", "must-revalidate, private");
response.setContentLength(b.length);
FileCopyUtils.copy(b, response.getOutputStream());
response.getOutputStream().flush();
response.getOutputStream().close();
}
我嘗試了什么:
String fileName = uploadedFiles.getFilename();
String regex = "[a-zA-Z._ ]*";
if (b != null && fileName.matches(regex)) {
response.reset();
// String fileName = uploadedFiles.getFilename();
String header = "attachment; filename=\"" + fileName + "\"";
String contentType = uploadedFiles.getFilecontenttype();
response.setContentType(uploadedFiles.getFilecontenttype());
response.addHeader("Content-Transfer-Encoding", "Binary");
response.addHeader("Cache-Control", "must-revalidate, private");
response.setHeader("Content-Disposition", header);
response.setContentLength(b.length);
FileCopyUtils.copy(b, response.getOutputStream());
response.getOutputStream().flush();
response.getOutputStream().close();
}
您應該使用一種方法來過濾敏感信息
response.setHeader("Content-Disposition", header)
只是使用
fileName.matches(regex)
太簡單了。
String contentType = uploadedFiles.getFilecontenttype();
response.setContentType(uploadedFiles.getFilecontenttype());
首先,您可以在此處修復冗余。 其次,問題可能來自您沒有嘗試驗證內容類型這一事實。 如果內容類型已更改並且與文件不匹配怎么辦? 每個用戶輸入都應該經過清理和/或與您實際期望的內容白名單進行比較。
編輯: filename
同上。 清理此字段
在這種情況下,您可以使用此方法驗證標頭值文件名
//Header manipulation
公共靜態字符串 validateHeaders(字符串標頭)拋出 UnsupportedEncodingException{
String filename = new String(header.getBytes("UTF-8"), "ISO-8859-1");
String regex = "[`~!@#$%^&*()\\+\\=\\{}|:\"?><\\/r\\/n]";
Pattern pa = Pattern.compile(regex);
Matcher ma = pa.matcher(filename);
if(ma.find()){
filename = ma.replaceAll("");
}
return filename;
}
字符串頭=“附件;文件名=””+ validateHeaders(文件名)+“””;
通過使用 RestTemplate 並使用 HttpHeader 作為授權標頭,下面的代碼能夠解決標頭操作問題。
import org.apache.commons.lang3.StringUtils;
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headersNew = new HttpHeaders();
//Below 2 Line solve the problem
String sanitizedToken = StringUtils.normalizeSpace(yourJwtToken);
headersNew.setBearerAuth(sanitizedToken); //This line where fortify reporting issue earlier
String url ="Your url";
HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<>(null, headersNew);
ResponseEntity<Object> response =
restTemplate.exchange(url,
HttpMethod.GET,
entity,
Object.class);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.