我们的应用程序报告了以下安全漏洞 -
The call to readLine() at xyz.java line 119 might allow an attacker to crash the program or otherwise make it unavailable to legitimate users.
有漏洞的代码
评论行报告 -
BufferedReader reader = new BufferedReader(new InputStreamReader(
httpConnection.getInputStream()));
String inputLine;
StringBuffer okResponse = new StringBuffer();
while ((inputLine = reader.readLine()) != null) { //readLine() on this line has been reported
okResponse.append(inputLine);
}
reader.close();
return okResponse.toString();
同样的补救措施 -
Validate user input to ensure that it will not cause inappropriate resource utilization.
但是,尚不清楚究竟可以验证什么。有什么指点吗?
最佳答案
理论上,攻击者可以通过 httpConnection 向您发送无限量的数据。由于您尝试使用所有这些,它可能会使您的应用程序崩溃 (OutOfMemory)。
我假设您对 okResponse
有一定的格式和长度,
所以你最好检查一下。
请务必阅读 Subhas
的答案,链接由 Luis Muñoz
( Most Robust way of reading a file or stream using Java (to prevent DoS attacks) ),
其中包含更多用于从流中读取内容的实现细节。
另一个问题可能是攻击只是保持连接打开而不发送任何数据。我假设应该有一个超时来在某个时候切断连接,否则线程可能会永远被阻塞。
编辑:
由于您的代码没有明确包含它,您还应该添加一个 try ... finally ...
block 以确保正确关闭资源。
关于java - 如何解决这种拒绝服务问题的可能性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56987837/