File: RuntimeConfigurationRecord.cs

package info (click to toggle)
mono 6.12.0.199%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,296,836 kB
  • sloc: cs: 11,181,803; xml: 2,850,076; ansic: 699,709; cpp: 123,344; perl: 59,361; javascript: 30,841; asm: 21,853; makefile: 20,405; sh: 15,009; python: 4,839; pascal: 925; sql: 859; sed: 16; php: 1
file content (317 lines) | stat: -rw-r--r-- 15,284 bytes parent folder | download | duplicates (6)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
//------------------------------------------------------------------------------
// <copyright file="RuntimeConfigurationRecord.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//------------------------------------------------------------------------------

namespace System.Configuration {
    using System.Collections;
    using System.Collections.Specialized;
    using System.Globalization;
    using System.IO;
    using System.Reflection;
    using System.Security;
    using System.Security.Permissions;
    using System.Security.Policy;
    using System.Xml;
    using System.Net;
    using System.Configuration.Internal;
    using Assembly = System.Reflection.Assembly;
    using System.Diagnostics.CodeAnalysis;

    internal sealed class RuntimeConfigurationRecord : BaseConfigurationRecord {

        static internal IInternalConfigRecord Create(
                InternalConfigRoot          configRoot,
                IInternalConfigRecord       parent,
                string                      configPath) {

            RuntimeConfigurationRecord configRecord = new RuntimeConfigurationRecord();
            configRecord.Init(configRoot, (BaseConfigurationRecord) parent, configPath, null);
            return configRecord;
        }

        private RuntimeConfigurationRecord() {
        }

        static readonly SimpleBitVector32 RuntimeClassFlags = new SimpleBitVector32(
                  ClassSupportsChangeNotifications
                | ClassSupportsRefresh
                | ClassSupportsImpersonation
                | ClassSupportsRestrictedPermissions
                | ClassSupportsDelayedInit);

        override protected SimpleBitVector32 ClassFlags {
            get {
                return RuntimeClassFlags;
            }
        }

        // Create the factory that will evaluate configuration 
        override protected object CreateSectionFactory(FactoryRecord factoryRecord) {
            return new RuntimeConfigurationFactory(this, factoryRecord);
        }

        // parentConfig contains the config that we'd merge with.
        override protected object CreateSection(bool inputIsTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, SectionInput sectionInput, object parentConfig, ConfigXmlReader reader) {
            // Get the factory used to create a section.
            RuntimeConfigurationFactory factory = (RuntimeConfigurationFactory) factoryRecord.Factory;

            // Use the factory to create a section.
            object config = factory.CreateSection(inputIsTrusted, this, factoryRecord, sectionRecord, sectionInput, parentConfig, reader);

            return config;
        }

        override protected object UseParentResult(string configKey, object parentResult, SectionRecord sectionRecord) {
            return parentResult;
        }

        // Ignore user code on the stack
        [PermissionSet(SecurityAction.Assert, Unrestricted=true)]
        private object GetRuntimeObjectWithFullTrust(ConfigurationSection section) {
            return section.GetRuntimeObject();
        }

        [SuppressMessage("Microsoft.Security", "CA2107:ReviewDenyAndPermitOnlyUsage", Justification = "This PermitOnly is meant to protect unassuming handlers from malicious callers by undoing any asserts we have put on the stack.")]
        private object GetRuntimeObjectWithRestrictedPermissions(ConfigurationSection section) {
            // Run configuration section handlers as if user code was on the stack
            bool revertPermitOnly = false;

            try {
                PermissionSet permissionSet = GetRestrictedPermissions();
                if (permissionSet != null) {
                    permissionSet.PermitOnly();
                    revertPermitOnly = true;
                }

                return section.GetRuntimeObject();
            }
            finally {
                if (revertPermitOnly) {
                    CodeAccessPermission.RevertPermitOnly();
                }
            }
        }

        override protected object GetRuntimeObject(object result) {
            object runtimeObject;
            ConfigurationSection section = result as ConfigurationSection;
            if (section == null) {
                runtimeObject = result;
            }
            else {
                // Call into config section while impersonating process or UNC identity
                // so that the section could read files from disk if needed
                try {
                    using (Impersonate()) {
                        // If this configRecord is trusted, ignore user code on stack
                        if (_flags[IsTrusted]) {
                            runtimeObject = GetRuntimeObjectWithFullTrust(section);
                        }
                        else {
                            // Run configuration section handlers as if user code was on the stack
                            runtimeObject = GetRuntimeObjectWithRestrictedPermissions(section);
                        }
                    }
                }
                catch (Exception e) {
                    throw new ConfigurationErrorsException(SR.GetString(SR.Config_exception_in_config_section_handler, section.SectionInformation.SectionName), e);
                }
            }

            return runtimeObject;
        }

        [PermissionSet(SecurityAction.Assert, Unrestricted=true)]
        protected override string CallHostDecryptSection(string encryptedXml, ProtectedConfigurationProvider protectionProvider, ProtectedConfigurationSection protectedConfig) {
            // Decrypt should always succeed in runtime.  (VSWhidbey 429996)
            // Need to override in order to Assert before calling the base class method.
            return base.CallHostDecryptSection(encryptedXml, protectionProvider, protectedConfig);
        }

        private class RuntimeConfigurationFactory {
            ConstructorInfo                 _sectionCtor;
            IConfigurationSectionHandler    _sectionHandler;

            internal RuntimeConfigurationFactory(RuntimeConfigurationRecord configRecord, FactoryRecord factoryRecord) {
                // If the factory record was defined in a trusted config record, ignore user code on stack
                if (factoryRecord.IsFromTrustedConfigRecord) {
                    InitWithFullTrust(configRecord, factoryRecord);
                }
                else {
                    // Run configuration section handlers as if user code was on the stack
                    InitWithRestrictedPermissions(configRecord, factoryRecord);
                }
            }

            private void Init(RuntimeConfigurationRecord configRecord, FactoryRecord factoryRecord) {
                // Get the type of the factory
                Type type = TypeUtil.GetTypeWithReflectionPermission(configRecord.Host, factoryRecord.FactoryTypeName, true);

                // If the type is a ConfigurationSection, that's the type.
                if (typeof(ConfigurationSection).IsAssignableFrom(type)) {
                    _sectionCtor = TypeUtil.GetConstructorWithReflectionPermission(type, typeof(ConfigurationSection), true);
                }
                else {
                    // Note: in v1, IConfigurationSectionHandler is in effect a factory that has a Create method
                    // that creates the real section object.

                    // throws if type does not implement IConfigurationSectionHandler
                    TypeUtil.VerifyAssignableType(typeof(IConfigurationSectionHandler), type, true);

                    // Create an instance of the handler
                    _sectionHandler = (IConfigurationSectionHandler) TypeUtil.CreateInstanceWithReflectionPermission(type);
                }
            }

            // Ignore user code on the stack
            [PermissionSet(SecurityAction.Assert, Unrestricted=true)]
            private void InitWithFullTrust(RuntimeConfigurationRecord configRecord, FactoryRecord factoryRecord) {
                Init(configRecord, factoryRecord);
            }

            [SuppressMessage("Microsoft.Security", "CA2107:ReviewDenyAndPermitOnlyUsage", Justification = "This PermitOnly is meant to protect unassuming handlers from malicious callers by undoing any asserts we have put on the stack.")]
            private void InitWithRestrictedPermissions(RuntimeConfigurationRecord configRecord, FactoryRecord factoryRecord) {
                // Run configuration section handlers as if user code was on the stack
                bool revertPermitOnly = false;
                try {
                    PermissionSet permissionSet = configRecord.GetRestrictedPermissions();
                    if (permissionSet != null) {
                        permissionSet.PermitOnly();
                        revertPermitOnly = true;
                    }

                    Init(configRecord, factoryRecord);
                }
                finally {
                    if (revertPermitOnly) {
                        CodeAccessPermission.RevertPermitOnly();
                    }
                }
            }

            //
            // Throw an exception if an attribute within a legacy section is one of our
            // reserved locking attributes. We do not want admins to think they can lock
            // an attribute or element within a legacy section.
            //
            private static void CheckForLockAttributes(string sectionName, XmlNode xmlNode) {
                XmlAttributeCollection attributes = xmlNode.Attributes;
                if (attributes != null) {
                    foreach (XmlAttribute attribute in attributes) {
                        if (ConfigurationElement.IsLockAttributeName(attribute.Name)) {
                            throw new ConfigurationErrorsException(SR.GetString(SR.Config_element_locking_not_supported, sectionName), attribute);
                        }
                    }
                }

                foreach (XmlNode child in xmlNode.ChildNodes) {
                    if (xmlNode.NodeType == XmlNodeType.Element) {
                        CheckForLockAttributes(sectionName, child);
                    }
                }
            }

            private object CreateSectionImpl(
                    RuntimeConfigurationRecord configRecord, FactoryRecord factoryRecord, SectionRecord sectionRecord, 
                    SectionInput sectionInput, object parentConfig, ConfigXmlReader reader) {

                object config;

                if (_sectionCtor != null) {
                    ConfigurationSection configSection = (ConfigurationSection) TypeUtil.InvokeCtorWithReflectionPermission(_sectionCtor);

                    configSection.SectionInformation.SetRuntimeConfigurationInformation(configRecord, factoryRecord, sectionRecord);

                    configSection.CallInit();

                    ConfigurationSection parentSection = (ConfigurationSection) parentConfig;
                    configSection.Reset(parentSection);

                    if (reader != null) {
                        configSection.DeserializeSection(reader);
                    }

                    if (configRecord != null && sectionInput != null && sectionInput.ConfigBuilder != null) {
                        configSection = configRecord.CallHostProcessConfigurationSection(configSection, sectionInput.ConfigBuilder);
                    }

                    // throw if there are any cached errors
                    ConfigurationErrorsException errors = configSection.GetErrors();
                    if (errors != null) {
                        throw errors;
                    }

                    // don't allow changes to sections at runtime
                    configSection.SetReadOnly();

                    // reset the modified bit
                    configSection.ResetModified();

                    config = configSection;
                }
                else {
                    if (reader != null) {
                        XmlNode xmlNode = ErrorInfoXmlDocument.CreateSectionXmlNode(reader);

                        CheckForLockAttributes(factoryRecord.ConfigKey, xmlNode);

                        // In v1, our old section handler expects a context that contains the virtualPath from the configPath
                        object configContext = configRecord.Host.CreateDeprecatedConfigContext(configRecord.ConfigPath);

                        config = _sectionHandler.Create(parentConfig, configContext, xmlNode);
                    }
                    else {
                        config = null;
                    }
                }

                return config;
            }

            // Ignore user code on the stack
            [PermissionSet(SecurityAction.Assert, Unrestricted=true)]
            private object CreateSectionWithFullTrust(
                    RuntimeConfigurationRecord configRecord, FactoryRecord factoryRecord, SectionRecord sectionRecord, 
                    SectionInput sectionInput, object parentConfig, ConfigXmlReader reader) {

                        return CreateSectionImpl(configRecord, factoryRecord, sectionRecord, sectionInput, parentConfig, reader);
            }

            [SuppressMessage("Microsoft.Security", "CA2107:ReviewDenyAndPermitOnlyUsage", Justification = "This PermitOnly is meant to protect unassuming handlers from malicious callers by undoing any asserts we have put on the stack.")]
            private object CreateSectionWithRestrictedPermissions(
                    RuntimeConfigurationRecord configRecord, FactoryRecord factoryRecord, SectionRecord sectionRecord,
                    SectionInput sectionInput, object parentConfig, ConfigXmlReader reader) {

                // run configuration section handlers as if user code was on the stack
                bool revertPermitOnly = false;
                try {
                    PermissionSet permissionSet = configRecord.GetRestrictedPermissions();
                    if (permissionSet != null) {
                        permissionSet.PermitOnly();
                        revertPermitOnly = true;
                    }

                    return CreateSectionImpl(configRecord, factoryRecord, sectionRecord, sectionInput, parentConfig, reader);
                }
                finally {
                    if (revertPermitOnly) {
                        CodeAccessPermission.RevertPermitOnly();
                    }
                }
            }

            internal object CreateSection(bool inputIsTrusted, RuntimeConfigurationRecord configRecord, 
                    FactoryRecord factoryRecord, SectionRecord sectionRecord, SectionInput sectionInput, object parentConfig, ConfigXmlReader reader) {

                if (inputIsTrusted) {
                    return CreateSectionWithFullTrust(configRecord, factoryRecord, sectionRecord, sectionInput, parentConfig, reader);
                }
                else {
                    return CreateSectionWithRestrictedPermissions(configRecord, factoryRecord, sectionRecord, sectionInput, parentConfig, reader);
                }
            }
        }
    }
}