M
Mike Kraley
In my ASP.NET application, I'd like to set limits on the maximum size of an
uploaded file. Normally I'd just
set the maxRequestLength of the httpRuntime element in web.config. But in
this case, I have a few different
aspx pages and I want the limit set differently for each. Yes, I could put
each in its own folder, each with
its own web.config, but that is rather awkward for this application.
Alternatively, I could leave the limit set in web.config to the largest
limit, and then in the other pages,
do my own checking, throwing an error if the ContentLength was too large.
But if the goal here is preventing a DOS attack on my server by someone who
is uploading lots of giant files,
maybe this is too late. That is, by the time my code gets to run, maybe the
content is already all uploaded
and has consumed the server resources. I'd rather be able to stop things
earlier in the process.
Looking at some Reflector code, it appears that the method
Request.GetEntireRawContent is actually doing the
reading of the input stream, and this is called very early in page handling,
by the first reference to the
Form contents. But I'm not sure I'm reading this correctly. If I look at
Request.InputStream at PageLoad
time, it says that it is still at position 0. Does that mean that the
content really hasn't been streamed in
yet?
Also I wonder what I can trust. The simple thing is just to check
Request.ContentLength, but I assume that a
bad guy can just fake that to be a small number. Is the InputStream length a
real number that can be trusted?
Any suggestions would be appreciated.
uploaded file. Normally I'd just
set the maxRequestLength of the httpRuntime element in web.config. But in
this case, I have a few different
aspx pages and I want the limit set differently for each. Yes, I could put
each in its own folder, each with
its own web.config, but that is rather awkward for this application.
Alternatively, I could leave the limit set in web.config to the largest
limit, and then in the other pages,
do my own checking, throwing an error if the ContentLength was too large.
But if the goal here is preventing a DOS attack on my server by someone who
is uploading lots of giant files,
maybe this is too late. That is, by the time my code gets to run, maybe the
content is already all uploaded
and has consumed the server resources. I'd rather be able to stop things
earlier in the process.
Looking at some Reflector code, it appears that the method
Request.GetEntireRawContent is actually doing the
reading of the input stream, and this is called very early in page handling,
by the first reference to the
Form contents. But I'm not sure I'm reading this correctly. If I look at
Request.InputStream at PageLoad
time, it says that it is still at position 0. Does that mean that the
content really hasn't been streamed in
yet?
Also I wonder what I can trust. The simple thing is just to check
Request.ContentLength, but I assume that a
bad guy can just fake that to be a small number. Is the InputStream length a
real number that can be trusted?
Any suggestions would be appreciated.