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 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
|
//------------------------------------------------------------------------------
// <copyright file="ConfigurationErrorsException.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
namespace System.Configuration {
using System.Configuration.Internal;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Runtime.Serialization;
using System.Security;
using System.Security.Permissions;
using System.Xml;
using System.Runtime.Versioning;
using System.Diagnostics.CodeAnalysis;
[Serializable]
public class ConfigurationErrorsException : ConfigurationException {
// Constants
private const string HTTP_PREFIX = "http:";
private const string SERIALIZATION_PARAM_FILENAME = "firstFilename";
private const string SERIALIZATION_PARAM_LINE = "firstLine";
private const string SERIALIZATION_PARAM_ERROR_COUNT = "count";
private const string SERIALIZATION_PARAM_ERROR_DATA = "_errors";
private const string SERIALIZATION_PARAM_ERROR_TYPE = "_errors_type";
private string _firstFilename;
private int _firstLine;
private ConfigurationException[] _errors;
void Init(string filename, int line) {
HResult = HResults.Configuration;
// BaseConfigurationRecord.cs uses -1 as uninitialized line number.
if (line == -1) {
line = 0;
}
_firstFilename = filename;
_firstLine = line;
}
// The ConfigurationException class is obsolete, but we still need to derive from it and call the base ctor, so we
// just disable the obsoletion warning.
#pragma warning disable 0618
public ConfigurationErrorsException(string message, Exception inner, string filename, int line) : base(message, inner) {
#pragma warning restore 0618
Init(filename, line);
}
public ConfigurationErrorsException() :
this(null, null, null, 0) {}
public ConfigurationErrorsException(string message) :
this(message, null, null, 0) {}
public ConfigurationErrorsException(string message, Exception inner) :
this(message, inner, null, 0) {}
public ConfigurationErrorsException(string message, string filename, int line) :
this(message, null, filename, line) {}
public ConfigurationErrorsException(string message, XmlNode node) :
this(message, null, GetUnsafeFilename(node), GetLineNumber(node)) {}
public ConfigurationErrorsException(string message, Exception inner, XmlNode node) :
this(message, inner, GetUnsafeFilename(node), GetLineNumber(node)) {}
public ConfigurationErrorsException(string message, XmlReader reader) :
this(message, null, GetUnsafeFilename(reader), GetLineNumber(reader)) {}
public ConfigurationErrorsException(string message, Exception inner, XmlReader reader) :
this(message, inner, GetUnsafeFilename(reader), GetLineNumber(reader)) {}
internal ConfigurationErrorsException(string message, IConfigErrorInfo errorInfo) :
this(message, null, GetUnsafeConfigErrorInfoFilename(errorInfo), GetConfigErrorInfoLineNumber(errorInfo)) {}
internal ConfigurationErrorsException(string message, Exception inner, IConfigErrorInfo errorInfo) :
this(message, inner, GetUnsafeConfigErrorInfoFilename(errorInfo), GetConfigErrorInfoLineNumber(errorInfo)) {}
internal ConfigurationErrorsException(ConfigurationException e) :
this(GetBareMessage(e), GetInnerException(e), GetUnsafeFilename(e), GetLineNumber(e)) {}
[ResourceExposure(ResourceScope.None)]
internal ConfigurationErrorsException(ICollection<ConfigurationException> coll) :
this(GetFirstException(coll)) {
if (coll.Count > 1) {
_errors = new ConfigurationException[coll.Count];
coll.CopyTo(_errors, 0);
}
}
internal ConfigurationErrorsException(ArrayList coll) :
this((ConfigurationException) (coll.Count > 0 ? coll[0] : null)) {
if (coll.Count > 1) {
_errors = new ConfigurationException[coll.Count];
coll.CopyTo(_errors, 0);
foreach (object error in _errors) {
// force an illegal typecast exception if the object is not
// of the right type
ConfigurationException exception = (ConfigurationException) error;
}
}
}
static private ConfigurationException GetFirstException(ICollection<ConfigurationException> coll) {
foreach (ConfigurationException e in coll) {
return e;
}
return null;
}
static private string GetBareMessage(ConfigurationException e) {
if (e != null) {
return e.BareMessage;
}
return null;
}
static private Exception GetInnerException(ConfigurationException e) {
if (e != null) {
return e.InnerException;
}
return null;
}
//
// We assert PathDiscovery so that we get the full filename when calling ConfigurationException.Filename
//
[FileIOPermission(SecurityAction.Assert, AllFiles=FileIOPermissionAccess.PathDiscovery)]
[SuppressMessage("Microsoft.Security", "CA2106:SecureAsserts", Justification = "Our Filename property getter demands the appropriate permissions.")]
static private string GetUnsafeFilename(ConfigurationException e) {
if (e != null) {
return e.Filename;
}
return null;
}
static private int GetLineNumber(ConfigurationException e) {
if (e != null) {
return e.Line;
}
return 0;
}
// Serialization methods
protected ConfigurationErrorsException(SerializationInfo info, StreamingContext context) :
base(info, context) {
string firstFilename;
int firstLine;
int count;
string numPrefix;
string currentType;
Type currentExceptionType;
// Retrieve out members
firstFilename = info.GetString(SERIALIZATION_PARAM_FILENAME);
firstLine = info.GetInt32(SERIALIZATION_PARAM_LINE);
Init(firstFilename, firstLine);
// Retrieve errors for _errors object
count = info.GetInt32(SERIALIZATION_PARAM_ERROR_COUNT);
if (count != 0) {
_errors = new ConfigurationException[count];
for (int i = 0; i < count; i++) {
numPrefix = i.ToString(CultureInfo.InvariantCulture);
currentType = info.GetString(numPrefix + SERIALIZATION_PARAM_ERROR_TYPE);
currentExceptionType = Type.GetType(currentType, true);
// Only allow our exception types
if ( ( currentExceptionType != typeof( ConfigurationException ) ) &&
( currentExceptionType != typeof( ConfigurationErrorsException ) ) ) {
throw ExceptionUtil.UnexpectedError( "ConfigurationErrorsException" );
}
_errors[i] = (ConfigurationException)
info.GetValue(numPrefix + SERIALIZATION_PARAM_ERROR_DATA,
currentExceptionType);
}
}
}
[SecurityPermissionAttribute(SecurityAction.Demand,SerializationFormatter=true)]
public override void GetObjectData(SerializationInfo info, StreamingContext context) {
int subErrors = 0;
string numPrefix;
// call base implementation
base.GetObjectData(info, context);
// Serialize our members
info.AddValue(SERIALIZATION_PARAM_FILENAME, Filename);
info.AddValue(SERIALIZATION_PARAM_LINE, Line);
// Serialize rest of errors, along with count
// (since first error duplicates this error, only worry if
// there is more than one)
if ((_errors != null) &&
(_errors.Length > 1 )){
subErrors = _errors.Length;
for (int i = 0; i < _errors.Length; i++) {
numPrefix = i.ToString(CultureInfo.InvariantCulture);
info.AddValue(numPrefix + SERIALIZATION_PARAM_ERROR_DATA,
_errors[i]);
info.AddValue(numPrefix + SERIALIZATION_PARAM_ERROR_TYPE,
_errors[i].GetType());
}
}
info.AddValue(SERIALIZATION_PARAM_ERROR_COUNT, subErrors);
}
// The message includes the file/line number information.
// To get the message without the extra information, use BareMessage.
public override string Message {
get {
string file = Filename;
if (!string.IsNullOrEmpty(file)) {
if (Line != 0) {
return BareMessage + " (" + file + " line " + Line.ToString(CultureInfo.CurrentCulture) + ")";
}
else {
return BareMessage + " (" + file + ")";
}
}
else if (Line != 0) {
return BareMessage + " (line " + Line.ToString("G", CultureInfo.CurrentCulture) + ")";
}
else {
return BareMessage;
}
}
}
public override string BareMessage {
get {
return base.BareMessage;
}
}
public override string Filename {
get {
return SafeFilename(_firstFilename);
}
}
public override int Line {
get {
return _firstLine;
}
}
public ICollection Errors {
get {
if (_errors != null) {
return _errors;
}
else {
ConfigurationErrorsException e = new ConfigurationErrorsException(BareMessage, base.InnerException, _firstFilename, _firstLine);
ConfigurationException[] a = new ConfigurationException[] {e};
return a;
}
}
}
internal ICollection<ConfigurationException> ErrorsGeneric {
get {
return (ICollection<ConfigurationException>) this.Errors;
}
}
//
// Get file and linenumber from an XML Node in a DOM
//
public static int GetLineNumber(XmlNode node) {
return GetConfigErrorInfoLineNumber(node as IConfigErrorInfo);
}
public static string GetFilename(XmlNode node) {
return SafeFilename(GetUnsafeFilename(node));
}
private static string GetUnsafeFilename(XmlNode node) {
return GetUnsafeConfigErrorInfoFilename(node as IConfigErrorInfo);
}
//
// Get file and linenumber from an XML Reader
//
public static int GetLineNumber(XmlReader reader) {
return GetConfigErrorInfoLineNumber(reader as IConfigErrorInfo);
}
public static string GetFilename(XmlReader reader) {
return SafeFilename(GetUnsafeFilename(reader));
}
private static string GetUnsafeFilename(XmlReader reader) {
return GetUnsafeConfigErrorInfoFilename(reader as IConfigErrorInfo);
}
//
// Get file and linenumber from an IConfigErrorInfo
//
private static int GetConfigErrorInfoLineNumber(IConfigErrorInfo errorInfo) {
if (errorInfo != null) {
return errorInfo.LineNumber;
}
else {
return 0;
}
}
private static string GetUnsafeConfigErrorInfoFilename(IConfigErrorInfo errorInfo) {
if (errorInfo != null) {
return errorInfo.Filename;
}
else {
return null;
}
}
[FileIOPermission(SecurityAction.Assert, AllFiles = FileIOPermissionAccess.PathDiscovery)]
[SuppressMessage("Microsoft.Security", "CA2106:SecureAsserts", Justification = "This method simply extracts the filename, which isn't sensitive information.")]
private static string ExtractFileNameWithAssert(string filename) {
// This method can throw; callers should wrap in try / catch.
string fullPath = Path.GetFullPath(filename);
return Path.GetFileName(fullPath);
}
//
// Internal Helper to strip a full path to just filename.ext when caller
// does not have path discovery to the path (used for sane error handling).
//
internal static string SafeFilename(string filename) {
if (string.IsNullOrEmpty(filename)) {
return filename;
}
// configuration file can be an http URL in IE
if (StringUtil.StartsWithIgnoreCase(filename, HTTP_PREFIX)) {
return filename;
}
//
// If it is a relative path, return it as is.
// This could happen if the exception was constructed from the serialization constructor,
// and the caller did not have PathDiscoveryPermission for the file.
//
try {
if (!Path.IsPathRooted(filename)) {
return filename;
}
}
catch {
return null;
}
try {
// Confirm that it is a full path.
// GetFullPath will also Demand PathDiscovery for the resulting path
string fullPath = Path.GetFullPath(filename);
}
catch (SecurityException) {
// Get just the name of the file without the directory part.
try {
filename = ExtractFileNameWithAssert(filename);
}
catch {
filename = null;
}
}
catch {
filename = null;
}
return filename;
}
//
// Internal Helper to always strip a full path to just filename.ext.
//
internal static string AlwaysSafeFilename(string filename) {
if (string.IsNullOrEmpty(filename)) {
return filename;
}
// configuration file can be an http URL in IE
if (StringUtil.StartsWithIgnoreCase(filename, HTTP_PREFIX)) {
return filename;
}
//
// If it is a relative path, return it as is.
// This could happen if the exception was constructed from the serialization constructor,
// and the caller did not have PathDiscoveryPermission for the file.
//
try {
if (!Path.IsPathRooted(filename)) {
return filename;
}
}
catch {
return null;
}
// Get just the name of the file without the directory part.
try {
filename = ExtractFileNameWithAssert(filename);
}
catch {
filename = null;
}
return filename;
}
}
}
|