I have an HTTP handler that I created to update information in a local SQL Express database.
I realized that the user can use the relative paths of the URI "/../../file.zip" as a query string and can upload files outside the restricted area.
The site is not yet online, so this is not a security problem right now, but I would really like to prevent such things.
I added a simple string.replace line that removes any ".." from the input request.
Is there anything else I have to do here to protect this?
public void ProcessRequest(HttpContext context) { string filesPath = "C:/Downloads/"; string fileName = context.Request.QueryString["filename"]; fileName = fileName.Replace("'", "''").Replace("..", "").Replace("/", "").Replace("\\", ""); if (!string.IsNullOrEmpty(fileName) && File.Exists(filesPath + fileName)) { context.Response.ContentType = "application/octet-stream"; context.Response.AddHeader("Content-Disposition", string.Format("attachment; filename=\"{0}\"", fileName)); context.Response.WriteFile(filesPath + fileName);
source share