11.07.2015 Views

Improving Web Application Security: Threats and - CGISecurity

Improving Web Application Security: Threats and - CGISecurity

Improving Web Application Security: Threats and - CGISecurity

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

826 <strong>Improving</strong> <strong>Web</strong> <strong>Application</strong> <strong>Security</strong>: <strong>Threats</strong> <strong>and</strong> Countermeasures7. Add the following public method so that it reads from a specified file.public static string ReadFile(string filename){byte[] fileBytes = null;long fileSize = -1;Stream fileStream = null;try{if(null == filename){throw new ArgumentException("Missing filename");}// Canonicalize <strong>and</strong> validate the supplied filename// GetFullPath:// - Checks for invalid characters (defined by Path.InvalidPathChars)// - Checks for Win32 non file-type device names including// physical drives, parallel <strong>and</strong> serial ports, pipes, mail slots,// <strong>and</strong> so on// - Normalizes the file pathfilename = Path.GetFullPath(filename);fileStream = File.OpenRead(filename);if(!fileStream.CanRead){throw new Exception("Unable to read from file.");}fileSize = fileStream.Length;fileBytes = new byte[fileSize];fileStream.Read(fileBytes, 0, Convert.ToInt32(fileSize));return Encoding.ASCII.GetString(fileBytes);}catch (Exception ex){throw ex;}finally{if (null != fileStream)fileStream.Close();}}

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!