Medium Trust ASP.NET Applications

If you are an ISP offering shared hosting, or you are a developer deploying your app in a shared hosting environment, then there is no doubt you want to avoid running ASP.NET applications with full trust. Full trust is the default trust level for ASP.NET.

What is full trust? The runtime defines several trust levels we can use to constrain what an application can do. These trust level range from minimal trust, which is a highly restrictive level, to full trust, which has no restrictions at all. The recommended trust level for an ASP.NET application is right in the middle: medium trust (see the ASP.NET 2.0 Hosting Deployment Guide).

What is wrong with full trust? For starters, the AppDomain hosting the application is no longer a security boundary. Full trust allows native code to execute, and native code can poke around a process that is hosting multiple AppDomains to find or corrupt data from other applications. Full trust also leaves resource protection up to the operating system, which is a bad idea when all the applications are running with the same identity, and thus have equal access to files and registry keys.

For instance…

    1 string parentPath = Server.MapPath("~") + @"\..\";

    2 

    3 string[] webDirectories;

    4 webDirectories = Directory.GetDirectories(parentPath);

    5 

    6 foreach (string directory in webDirectories)

    7 {           

    8     string appDataPath = directory + @"\App_Data\";

    9 

   10     string[] appDataFiles;

   11     appDataFiles = Directory.GetFiles(appDataPath);

   12 

   13     foreach (string file in appDataFiles)

   14     {

   15         try

   16         {

   17             // goodbye, data

   18             File.Delete(file);

   19         }

   20         catch(Exception)

   21         {

   22             // eat it and go on

   23         }

   24     }           

   25 }

The above code tries to walk through the web sites on a server and destroy any files in the well known App_Data directories. Perhaps a database file will be in use and the runtime will throw an exception – that’s ok, we can try again later. The real problem here is that the code can even successfully retrieve a listing of files and directories outside of the root where the code executes.

Medium trust will place a number of restrictions on an application, including limiting an application’s file access to within the virtual directory where the application lives. If we run the above code under medium trust (see How To: Use Medium Trust in ASP.NET 2.0), the runtime will throw a System.Security.SecurityException exception on line 4. Line 4 is the where the code tries to get a list of directories one level above the application’s home directory.

In ASP.NET 2.0, Microsoft has made changes to make life easier for ISPs and developers who want to run code with the medium trust level. You can read more in the PAG document: Security Guidelines for ASP.NET 2.0.

posted on Friday, October 28, 2005 8:37 PM by scott

Comments

Friday, November 04, 2005 12:25 AM by Dinis Cruz

# List ISPs running ASP.NET in Partial Trust (versus Full Trust)

Hello

Do you know which ISPs are running their shared hosting environments in Partial Trust these days?

It seems that 1And1 is running their websites in partial trust (see http://faq.1and1.com/scripting_languages_supported/_net/15.html) although it is a bit non-ethical the fact that they don't talk about these limitations in their Asp.Net explanation page (http://order.1and1.com/xml/order/MsHostingDevNet">http://order.1and1.com/xml/order/MsHostingDevNet) which is linked from their hosting plan page (http://order.1and1.com/xml/order/MsHostingDev)

What other ISPs are doing this?

I googled a little bit at its seems that there are more ISPs doing this (when compared with a couple years ago) but nobody wants to publicly admit it.

Is there a list with this information?

Dinis Cruz
.Net Security Consultant
Owasp .Net Project
www.owasp.net
Friday, November 04, 2005 11:02 AM by scott

# re: Medium Trust ASP.NET Applications

I do not know of a list. It doesn't seem to be the type of "feature" that the ISPs are advertising.

Perhaps we should work on a list...
Tuesday, November 22, 2005 6:45 AM by David Burns

# re: Medium Trust ASP.NET Applications

This has been a real help reading your comments. The company hosting my asp.net application didn't tell me that they were running medium trust. My application ran really well locally but just won't retrieve any data from my the databse when run from the server as a website.

Is there any simple way around this? People need to order from my site so they have to be able to read/write to an orders database.

perhaps I need another ISP? Any recommendations?

Thanks.
Wednesday, November 23, 2005 6:11 AM by scott

# re: Medium Trust ASP.NET Applications

David:

What database are you using? Which version of the runtime are you using?
Sunday, January 22, 2006 9:45 AM by Rick

# re: Medium Trust ASP.NET Applications

It seems that medium trust is also blocking access to asp.net framework dll's like aspjpeg and others, how can I allow access to my clients applications without compromsing security?
Sunday, January 22, 2006 3:49 PM by scott

# re: Medium Trust ASP.NET Applications

Rick:

I'm not sure what aspjpeg is, it doesn't sound like a framework dll. Some assemblies require full trust, there is just no way to avoid the requirement. What you could do is wrap calls to the assembly with an assembly that allows partially trusted callers (search for the APTCA attribute set). There are some details here: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetsec/html/THCMCh09.asp
Tuesday, February 14, 2006 2:10 PM by Neel

# re: Medium Trust ASP.NET Applications

I have an asp .net application which uses remoting and works fine at my end. But many hosting providers will not aloowing that as it would need high trust. What should I do. What is the minimum grant required to support this case.
Sunday, February 19, 2006 7:06 PM by scott

# re: Medium Trust ASP.NET Applications

Neel: I'm not an expert on remoting. You might need to try one of the newsgroups or forums.
Thursday, October 12, 2006 7:35 AM by Matt Fausey

# re: Medium Trust ASP.NET Applications

Even when in Full Trust, your application is still limited by what the account can or cannot do. If your application is running under the ASPNET account, the shared hosting provider can still provide customized limitations. I believe this to be a better way because Medium Trust is too limiting.
Saturday, March 24, 2007 6:23 PM by A Ovest di Paperino

# Report del weekend

Report del
weekend
Saturday, March 24, 2007 6:48 PM by A Ovest di Paperino

# Report del weekend

Report del weekend