Home > Articles > Security > Network Security

  • Print
  • + Share This
This chapter is from the book

Configuring ClickOnce Security Permissions

The permissions a ClickOnce application requires to run are determined by its application manifest. These permissions are populated by Visual Studio when you publish your application based on the project properties. You can configure these security permissions on the project properties editor's Security tab. You can also modify them to a certain degree after you have published from Visual Studio using the Mage SDK tools. I'll cover both approaches in this section.

Regardless of which tool you use, you have two choices at the top level—you can request full trust or partial trust. Full trust means that you do not want your application constrained by CAS in any way at runtime on the client. This corresponds to the unrestricted permission set in CAS. When you select this setting, your application code and any code it calls will not be restricted in any way based on CAS. Keep in mind that CAS is separate and distinct from user-based security. So depending on the users' rights, they may still be prevented from doing certain things, either by role-based security code in your application or by the operating system if they try to access something on the system through your application that they do not have Windows access control privileges to use. But as far as ClickOnce and CAS are concerned, if the application has full trust, it can do whatever it likes.

When you choose partial trust, you have to specifically select a set of permissions that you want to include in the requested permissions for the application. You can base this on one of the predefined zone-based permission sets, such as Local Intranet or Internet, or you can use a custom set of permissions to request the specific permissions that correspond to the operations and resources your application uses by design. The latter is a better approach from a security vulnerability perspective.

Whether you use Visual Studio or Mage, what you end up with is a specification inside your application manifest file that says to the runtime, "My application needs these permissions to run."

Configuring ClickOnce Security Permissions with Visual Studio

Figure 6.1 showed the Security tab of the project properties editor. This is where you configure the set of permissions that are placed in your application manifest at the time that you publish your application. Checking the Enable ClickOnce Security Settings checkbox makes the rest of the options available. This box will be checked automatically the first time you publish your application from Visual Studio.

Enabling ClickOnce security settings also affects the way your application runs in the debugger. Once enabled, each time you run your application, the selected security settings will be applied to the debug executable process, so your debug runtime environment will have the same security restrictions as your target environment. For example, if you select Local Intranet as the target zone for partial trust and make no modifications to the permissions list below the partial-trust selection, and then run a debug session and your code tries to do file I/O, you will get an exception in the debugger because the process will run with only the permissions for the Local Intranet zone. This is extremely helpful in debugging and fixing problems that would otherwise only occur in the deployed environment.

If you select partial trust, you can then select the target zone as Local Intranet, Internet, or Custom. Selecting either Local Intranet or Internet selects the permissions in the ClickOnce Security Permissions table to match the target zone. Once those permissions are selected, you can then customize the settings to something different than the defaults for that zone as needed, using the zone permissions as a starting point for a custom set of permissions. Remember that ClickOnce ignores any custom security policy code groups, so setting fine custom permissions through the ClickOnce partial trust settings are the only way to explicitly grant specific permissions in a partial-trust scenario.

So, for example, if you were going to deploy an application to the local Intranet, but the application needed to call a Web service on your network other than the one where the application is being deployed from, you would do the following.

  1. Check the Enable ClickOnce Security Settings checkbox in the project properties editor's Security tab (see Figure 6.4).
    Figure 6.4

    Figure 6.4 Adding Permissions to a Selected Zone

  2. Select This is a partial trust application.
  3. Select Local Intranet in the drop-down list labeled Zone your application will be installed from.
  4. Scroll down in the grid of permissions required by the application to find the WebPermission type.
  5. In the Setting column drop-down list, select Include.

After doing this, your application will request all of the permissions in the Local Intranet zone as well as the WebPermission permission with unrestricted access to the Web.

As mentioned, most permission types have a number of additional options that you can set to customize exactly what options in that permission type you need. WebPermission includes the ability to set a list of URLs that you will let your application either call out to or be called on. Unfortunately, the permissions editor in the Security tab for ClickOnce does not allow you to access all the options for all of the displayed permission types. WebPermission is an example. You can see in Figure 6.4 that the Properties button below the grid is disabled. If there are configurable properties for a selected permission type, this button will be enabled and will take you to a dialog that lets you edit the finer-grained options for that permission type. Figure 6.5 shows an example of one of these dialogs for the SecurityPermission type.

Figure 6.5

Figure 6.5 Options in the Permissions Settings Dialog

There are some permission types that are not shown in the grid at all. The only way to add these permission types is to either select full trust for the application (see Figure 6.4) or use the Mage tool to configure the permission type based on its XML declaration. See the next section for more information on how to do that.

If you do not want to run your debug sessions in the ClickOnce security zone selected, you can disable this capability by doing the following (see Figure 6.4).

  1. Open the project properties editor for your application.
  2. Select the Security tab.
  3. Check the Enable ClickOnce Security Settings checkbox if it is not already checked.
  4. Select This is a partial trust application if it is not already selected.
  5. Click on the Advanced button at the bottom of the Security tab. This brings up the Advanced Security Settings dialog shown in Figure 6.6.
    Figure 6.6

    Figure 6.6 Advanced Security Settings Dialog

  6. Uncheck the box labeled Debug this application with the selected permission set.

Notice that you also have the options in the Advanced Security Settings Dialog to do the following.

  • Grant the application access to its site of origin (selected by default)
  • Debug the application as if it were downloaded from a different URL

Granting the application access to its site of origin lets you expose a Web service from the same site that the application is launched from that the application calls for back-end services. You can also use this to download additional files on demand. By doing this, you do not need to ask for WebPermission specifically to make those calls. Debugging the application as if it were downloaded from a different URL lets you test and see what will happen with different security zones based on the URL—without needing to understand the exact logic that the runtime is using to evaluate the URL and match it against the location-based security zones.

The settings that you select in the Security tab are saved as part of your Visual Studio project file and will be used each time you publish your application from ClickOnce. The appropriate entries in the application manifest will be created when you publish.

Calculating Permissions with Visual Studio

At the bottom of the project properties editor's Security tab, there is a Calculate Permissions button (see Figure 6.4). If you click this button, Visual Studio will do a static analysis of your code, and every assembly that your code calls out to, in an attempt to determine what permissions your application will require to run. After you run the permissions calculator, it will configure the individual security permissions required for your application to include the permissions that it determined your application needs.

Using the permissions calculator is only appropriate if you plan to deploy your application under partial trust and are not sure what permissions your application requires based on its design. The thing to be aware of with the permissions calculator is that it makes a conservative estimate of what permissions your application will require. Based on my experience trying to use this tool, it always overestimates the permissions required by your application. In fact, it often grossly overestimates the permissions required.

As a result of this overestimation, you will be better off keeping track of what permissions your application needs based on its design and configuring only those permissions. Then test the application rigorously, running under the debugger with ClickOnce security enabled, to ensure you did not miss any required permissions.

If you use the permissions calculator to set the required permissions for your application, your application manifest will likely state that many more permissions are required than really are. This means you are removing some of the protections that running under partial trust brings you. However, running with a set of permissions determined by the permissions calculator under partial trust will still offer more protection to the client machine than running under full trust. So if you are unsure what permissions you need and don't want to jump the security requirements all the way to full trust as a result, go ahead and use the permissions calculator.

Configuring ClickOnce Security Permissions with Mage

It should be a fairly rare thing that you would change the permissions your application requests after you have published from Visual Studio. After all, the permissions required are determined by the code that executes, not based on administrative whims. However, if you find that you need to modify the set of permissions that your application requests without publishing a new version from Visual Studio, you have some ability to do so with the Mage tools.

Using the command line mage.exe tool, you can only set the security to one of the predefined zone levels of Internet, LocalIntranet, or FullTrust. You do this by running mage with a command line switch of –TrustLevel (or –tr for short). Because you are editing the manifest by doing this, you will also need to re-sign the manifest with a certificate. You do this with other command line options. The following example shows how to set the security zone to full trust and re-sign the manifest.

mage.exe -Update MyApp.exe.manifest -TrustLevel FullTrust -CertFile
MyCert.pfx -Password SomeSecretPwd

You can use the Mage UI tool, mageui.exe, to edit the permission settings in a dialog-based user interface. Start mageui.exe from a Visual Studio command prompt and open the application manifest that you want to edit. Select the Permissions Required category in the list on the left and you will see the view shown in Figure 6.7.

You can drop down the list of permission set types on the right in Figure 6.7 to select one of the predefined zones, including Internet, Local Intranet, and Full Trust. When you select one of these values, the Details pane below it will display the XML PermissionSet element that will be placed in the application manifest. Under this element, there will be individual IPermission elements for each permission you require, except in the case of Full Trust, which just sets the permission set to unrestricted. You can also select a value of Custom in the permission set type list to manually enter whatever settings you would like. This requires that you understand the full schema of the PermissionSet element and its child elements to determine what to put into the Details box. This schema is beyond the scope of this book to describe in detail, but follows a common convention with the way permission sets are defined for security configuration files. Consult the MSDN Library documentation for more information on manually creating XML permission set entries.

Figure 6.7

Figure 6.7 Setting Permissions with Mage UI

Note that using the Custom permission set type and figuring out the right XML elements and attributes to add is the only way to go beyond the permission sets and options that are exposed to you in Visual Studio. You could also write a custom tool to set these through the APIs exposed in the Microsoft.Build.Tasks.Deployment namespace, but that is not a trivial task either.

  • + Share This
  • 🔖 Save To Your Account