I am using klocwok to review my code. For the given line of code:
byte sigToVerify = new byte[sigFileInputStream.available()];
I am getting the following error report:
SV.DOS.ARRSIZE: Unvalidated user input
sigFileInputStream.available()
used for array size - attacker can specify a large number leading to high resource usage on the server and a DOS attack
Please help me resolve this issue.
Without more of your code snippet to go on, I would think that Klocwork is reporting a valid issue here. You should review the documentation provided for the SV.DOS.ARRSIZE checker, which explains why this is reported. On the Vulnerability and risk:
On the Mitigation and prevention:
Even the Java InputStream API docs (of which FileInputStream is a subclass) warn that using the return value of the
available()
method is a bad idea:An example of how to fix your code to avoid this would be to, as suggested above, validate the value returned by
available()
before using it to allocate the array:Note that 100000000 or 100MB for
sigToVerify
may still be way too large for your purposes, or it could be too small. You should determine the most sane value to use here based on what your code is trying to accomplish.