Using the Security System for Defensive Coding

Darren Neimke points out that you can often run into trouble by not checking variables past in by users.  You can view his scenario here.

Now, while I support parameter checking, there is also a feature of the .NET Framework that would give him the same functionality as parameter checking in a much safer fasion.  The FileIOPermission object can be used to deny access to all but selected paths.  So here goes:

private void MyForm_Load( ... ) {
    string niceFile = @"..\File.txt";
    string path = @"c:\SafeScripts\";
    string fullPath = System.IO.Path.Combine(path, niceFile);
    label1.Text = ReadFromFile(fullPath, safePath);
}

private string ReadFromFile(string fileName, safePath) {
    string retVal = null;
   
    FileIOPermission ioPerm = new FileIOPermission(FileIOPermissionAccess.Read, safePath);
    try {
        ioPerm.PermitOnly();
        using(StreamReader sr = new StreamReader(File.OpenRead(fileName))) {
            retVal = sr.ReadToEnd();
            sr.Close();
        }
    } catch {
        retVal = "An error occurred accessing file: " + fileName;
    } finally {
        ioPerm.RevertPermitOnly();
    }
   
    return retVal;
}

I love making use of the various permissions whenever possible.  It makes me feel like my application is going to be secured not only by me, but months of testing and security reviews Microsoft has done as well.  If a new form of hack is found that mangles the path name, I can then rely on the MS security fix process to ensure my application gets access to updated protections as well.

Published Monday, January 19, 2004 6:57 PM by Justin Rogers
Filed under:

Comments

Tuesday, January 20, 2004 4:49 AM by Darren Neimke

# re: Using the Security System for Defensive Coding

Thanks for the heads-up Justin :)
Thursday, February 12, 2004 6:06 PM by stefan demetz

# re: Using the Security System for Defensive Coding

Friday, February 13, 2004 10:03 PM by TrackBack

# PermitOnly is excellent for security, but can be a bane to programming..., enter PermissionSet

Friday, November 28, 2008 6:51 AM by fabiola-fm

# re: Using the Security System for Defensive Coding

<a href= http://fasster.angelfire.com >baltimore and convention center and headquarters</a> <a href= http://gertui.angelfire.com >nasdaq 100 tennis tournament</a>

Friday, November 28, 2008 6:59 PM by fabiola-mv

# re: Using the Security System for Defensive Coding

<a href= http://chkola.angelfire.com >avlastkey</a> <a href= http://bustersw.angelfire.com >how to start a strawberry patch in alabama</a>

Saturday, November 29, 2008 12:02 AM by fabiola-gn

# re: Using the Security System for Defensive Coding

<a href= http://kustur.angelfire.com >dad vail regatta</a> <a href= http://trututa.angelfire.com >ratings apartments eagle ridge alabama</a>

Friday, December 26, 2008 11:03 AM by Albina-he

# re: Using the Security System for Defensive Coding

<a href= membres.lycos.fr/maffals >genetic disorters</a>

Leave a Comment

(required) 
(required) 
(optional)
(required)