File: PartialTrustSandbox.cs

package info (click to toggle)
mono-reference-assemblies 3.12.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 604,240 kB
  • ctags: 625,505
  • sloc: cs: 3,967,741; xml: 2,793,081; ansic: 418,042; java: 60,435; sh: 14,833; makefile: 11,576; sql: 7,956; perl: 1,467; cpp: 1,446; yacc: 1,203; python: 598; asm: 422; sed: 16; php: 1
file content (126 lines) | stat: -rw-r--r-- 5,732 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.

namespace System.Data.Entity
{
    using System.IO;
    using System.Runtime.InteropServices;
    using System.Security;
    using System.Security.Permissions;

    /// <summary>
    ///     Represents a partial trust sandbox
    /// </summary>
    public class PartialTrustSandbox : IDisposable
    {
        private static readonly PartialTrustSandbox _default = new PartialTrustSandbox("Default Partial Trust Sandbox");
        private AppDomain _domain;

        /// <summary>
        ///     Constructs a new partial trust sandbox
        /// </summary>
        /// <param name="grantReflectionPermission"> Specify true to grant unrestricted reflection permission </param>
        /// <param name="configurationFile"> Specify an alternate configuration file for the AppDoman. By default, the calling domain's will be used </param>
        /// <remarks>
        ///     If you do not need any special configuration, use the <see cref="Default" /> instance.
        /// </remarks>
        public PartialTrustSandbox(bool grantReflectionPermission = false, string configurationFile = null)
            : this("Partial Trust Sandbox " + Guid.NewGuid(), grantReflectionPermission, configurationFile)
        {
        }

        protected PartialTrustSandbox(string domainName, bool grantReflectionPermission = false, string configurationFile = null)
        {
            var securityConfig = Path.Combine(
                RuntimeEnvironment.GetRuntimeDirectory(), "CONFIG",
                "web_mediumtrust.config");
            var permissionXml = File.ReadAllText(securityConfig).Replace("$AppDir$", Environment.CurrentDirectory);

            // ASP.NET's configuration files still use the full policy levels rather than just permission sets,
            // so we can either write a lot of code to parse them ourselves, or we can use a deprecated API to
            // load them.
#pragma warning disable 0618
            var grantSet =
                SecurityManager.LoadPolicyLevelFromString(permissionXml, PolicyLevelType.AppDomain).
                    GetNamedPermissionSet("ASP.Net");
#pragma warning restore 0618

            if (grantReflectionPermission)
            {
                grantSet.AddPermission(new ReflectionPermission(PermissionState.Unrestricted));
            }

            var info = new AppDomainSetup
                           {
                               ApplicationBase = Environment.CurrentDirectory,
                               PartialTrustVisibleAssemblies = new[]
                                                                   {
                                                                       // Add conditional APTCA assemblies that you need to access in partial trust here.
                                                                       // Do NOT add System.Web here since at least one test relies on it not being treated as conditionally APTCA.
                                                                       "System.ComponentModel.DataAnnotations, PublicKey=0024000004800000940000000602000000240000525341310004000001000100b5fc90e7027f67871e773a8fde8938c81dd402ba65b9201d60593e96c492651e889cc13f1415ebb53fac1131ae0bd333c5ee6021672d9718ea31a8aebd0da0072f25d87dba6fc90ffd598ed4da35e44c398c454307e8e33b8426143daec9f596836f97c8f74750e5975c64e2189f45def46b2a2b1247adc3652bf5c308055da9"
                                                                   }
                           };

            info.ConfigurationFile = configurationFile == null
                                         ? AppDomain.CurrentDomain.SetupInformation.ConfigurationFile
                                         : configurationFile;

            _domain = AppDomain.CreateDomain(domainName, null, info, grantSet, null);
        }

        ~PartialTrustSandbox()
        {
            Dispose(false);
        }

        public static PartialTrustSandbox Default
        {
            get { return _default; }
        }

        /// <summary>
        ///     Creates a new instance of the specified type in the partial trust sandbox and returns a proxy to it.
        /// </summary>
        /// <typeparam name="T"> The type of object to create </typeparam>
        /// <returns> A proxy to the instance created in the partial trust sandbox </returns>
        public T CreateInstance<T>()
        {
            return (T)CreateInstance(typeof(T));
        }

        /// <summary>
        ///     Creates a new instance of the specified type in the partial trust sandbox and returns a proxy to it.
        /// </summary>
        /// <param name="type"> The type of object to create </param>
        /// <returns> A proxy to the instance created in the partial trust sandbox </returns>
        public object CreateInstance(Type type)
        {
            HandleDisposed();

            return _domain.CreateInstanceAndUnwrap(type.Assembly.FullName, type.FullName);
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing
                && _domain != null)
            {
                AppDomain.Unload(_domain);
                _domain = null;
            }
        }

        private void HandleDisposed()
        {
            if (_domain == null)
            {
                throw new ObjectDisposedException(null);
            }
        }
    }
}