Max Toro
Max Toro

Reputation: 28608

Configuration vs. static properties, security concerns

I'm developing a class library and I need to provide a way to set configuration parameters. I can create a configuration section or I can expose static properties. My concern about static properties is security. What prevents a malicious component from making changes at runtime? For instance, in ASP.NET MVC you configure routes using a static property. Is this secure? Can a malicious component add/remove routes?


How would the "untrusted component" get in my application in the first place? NuGet for example. We don't know what's out there, who did it, and if it contains small bits of undesired state changes.

How would the "untrusted component" run? In ASP.NET all you need is PreApplicationStartMethodAttribute to run some code when the application is starting.

Upvotes: 2

Views: 1153

Answers (5)

kochobay
kochobay

Reputation: 392

I generally use Config file and Static variables together. I define static variable as private, and i make only "get" method to expose value. so it is can not be changed out of class.

I create a class to handle configuration implementing "IConfigurationSectionHandler" interface. My implementation is for ASP.NET Web applications.

Step 1: Create a section in web.config file to process later.

<configuration>
    <configSections>
        <section name="XXXConfiguration" type="Company.XXXConfiguration, Company"/>
        ...
    </configSections>

    <XXXConfiguration>
        <Variable>Value to set static variable</Variable>
    </XXXConfiguration>

    ...

<configuration>

Step 2: Create a class to handle previous configuration section.

using System;
using System.Collections;
using System.Collections.Specialized;
using System.Xml;
using System.Configuration;
namespace Company{
    public class XXXConfiguration : IConfigurationSectionHandler
    {
        /// <summary>
        /// Initializes a new instance of LoggingConfiguration class.
        /// </summary>
        public XXXConfiguration() {}

        private static string _variable;

        public static string Variable
        {
            get {return XXXConfiguration._variable; }
        }

        public object Create(object parent, object configContext, XmlNode section)
        {
            // process config section node
            XXXConfiguration._variable = section.SelectSingleNode("./Variable").InnerText;

            return null;
        }

    }
}

Step 3: Use GetSection method of System.Configuration.ConfigurationManager at startup of application. In Global.asax

void Application_Start(object sender, EventArgs e) 
{
      // Code that runs on application startup
      System.Configuration.ConfigurationManager.GetSection("LoggingConfiguration");
      ...
}   

Upvotes: 0

Jon Hanna
Jon Hanna

Reputation: 113312

As the previous answers note, there isn't really much of a difference here.

Malicious code could set a static property, and malicious code could change a configuration file. The latter is probably a bit easier to figure out from the outside, and can be done no matter what way the code is run (it wouldn't have to be .NET, wouldn't have to be run in your app domain, and indeed wouldn't have to be code, should someone gain the ability to change the file manually), so there's a bit of a security advantage in the use of a static property, though it's a rather bogus one considering that we may well have just moved the issue around a bit, since the calling code could very well be using configuration itself to decide what to set the properties to!

There's a third possibility, which is that you have an instance with instance members that set the properties, and it's the calling code that makes that instance static. This might be completely irrelevant to what you are doing, but it can be worth considering cases where someone might want to have your code running with two sets of configuration parameters in the same app domain. As a matter of security, it is much the same as the matter of static members, except that it could affect serialisation concerns.

So, so far there's the disadvantage of configuration files in that they can be attacked by code completely separate to yours, but with the noted caveat that the information might end up in a configuration file somewhere else anyway.

Whichever approach you take, the safety of access comes down to the way that you load in partially-trusted code.

The code should be loaded into its own app domain, and the security on that app domain set appropriately to how well it can be trusted. If at all possible, it shouldn't be your library that is doing so, but left to the calling code to decide upon the policies to be set by any partially-trusted code it loads in. Of course, if it's inherent to your libraries purpose that it loads in partially-trusted code, then it must do so, but generally it should remain agnostic as to whether the code is fully or partially trusted except in demanding certain permissions when appropriate. If it is up to your library to load in this code, then you will need to decide upon the appropriate permissions to give the app domain. Really, this should be the lowest amount of permission where it is still possible to do the job it was loaded in for. This would presumably not include FileIOPermission, hence preventing it from writing to a config file.

Now, whether your library or the calling code has loaded the partially trusted code, you need to consider what permissions are necessary on your exposed classes and their members. This covers the static setter properties, but would still be necessary if you took the config-file approach given that your scenario still involves that there is partially-trusted code accessing your library.

In some cases, the methods won't need any more protection, because they inherently have it due to what they do. For example, if you try to access a file but the calling code does not have permission to do so, then your code will fail with a security exception that will be passed up to the calling code. Indeed, you may have to do the opposite and take measures to allow the partially-trusted code to call your method (if you access a file in a way that is safe because the caller cannot affect which file is accessed or how, you may want to Assert file-access permissions at that point).

In other cases, you may need to add protection because calling code won't do anything that immediately attempts a security-restricted operation but which may cause trusted code to behave in an inappropriate manner. For example, if your code stores paths that are used by later operations, then essentially calling that code allows for file access to happen in a particular way. E.g.:

public string TempFilePath{get;set;}
public void WriteTempData(string data)
{
  using(sw = new StreamWriter(TempFilePath, true))
    sw.Write(data);
}

Here if malicious code set TempDirPath it could cause a later call by trusted code to WriteTempData to damage an important file by over-writing it. An obvious approach here is to call Demand on an appropriate FileIOPermission object, so that the only code that could set it would be code that was already trusted to write to arbitrary locations anyway (this could of course be combined by both restricting the possible values for TempDirPath and demanding the ability to write within the set of locations that allowed).

You can also demand certain unions of permission, and of course create your own permissions, though using unions of those defined by the framework has an advantage of better fitting in with existing code.

Upvotes: 2

Ran
Ran

Reputation: 6159

When you consider something as a security threat, you should also think about from whom you are trying to protect.

In order for "malicious code" to alter the values of your static properties, this code would need to be loaded into your AppDomain and run. Now think that a malicious attacker has managed to get his code to run in your AppDomain - are your static properties really your major concern? Such an attacker can probably do a lot worst.

Unless you have a scenario where you need to load an assembly/code originating from external untrusted sources, I think you don't really need to defend against your user accessing your properties (Not from security perspective anyway - usability is another thing).

EDIT - about external untrusted code

I still think this is not really your concern. If I understand correctly, you are developing and providing a library, to be used by some 3rd party in their application.

If the application owner decided to take some external library which he does not trust, add it to his application, and allow it to run, then this is not your concern, it is the application owner's concern.

In this scenario, everything I said above still applies. The malicious code can do much worse then setting your properties. It can mess with memory, corrupt data, flood the thread pool, or even easily crash the AppDomain.

The point is, if you don't own the application because you are only providing a class library, you don't need to defend from code running inside the AppDomain where you classes are loaded.

Note: Re. NuGet, I wouldn't be too worried about that. NuGet is sort of a static tool. If I understand correctly, it doesn't do things in runtime such as downloading code and running it. It is only used in design time to download binaries, add references, and possibly add code. I think it's perfectly reasonable to assume that an application owner that uses NuGet to download a package will do his due diligence to ensure that the package is safe. And he has to do it only once, during development.

Upvotes: 10

Jahan Zinedine
Jahan Zinedine

Reputation: 14874

For sure, it can be changed by underlying classes which provide those abstractions, even in case of being defined as private members.

Think of a security interceptor that provision every request against defined privileges of authenticated or anonymous users.

Upvotes: 1

Reed Copsey
Reed Copsey

Reputation: 564531

What prevents a malicious component from making changes at runtime?

This depends on the definition of "malicious component". Configuration is really intended to allow changes at runtime.

If you handle this via code (whether static or instance properties, etc), you do have the distinct advantage of controlling the allowable settings directly, as your property setter can control this however you wish. You could also add some form of security, if your application requires it, as you'd control the way this was set.

With a configuration section, your only control would be in reading the values - you couldn't control the writing, but instead would have to validate settings on read.

Upvotes: 1

Related Questions