PartialTrustValidationBehavior.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel.Dispatcher
  5. {
  6. using System.ServiceModel.Channels;
  7. using System.ServiceModel;
  8. using System.ServiceModel.Description;
  9. using System.Collections.ObjectModel;
  10. using System.Collections.Generic;
  11. using System.Xml;
  12. using System.Security;
  13. using System.Security.Permissions;
  14. using System.ServiceModel.MsmqIntegration;
  15. using System.Runtime;
  16. class PartialTrustValidationBehavior : IServiceBehavior, IEndpointBehavior
  17. {
  18. static PartialTrustValidationBehavior instance = null;
  19. internal static PartialTrustValidationBehavior Instance
  20. {
  21. get
  22. {
  23. // no need to synchronize -- it's ok if two are created
  24. if (instance == null)
  25. {
  26. instance = new PartialTrustValidationBehavior();
  27. }
  28. return instance;
  29. }
  30. }
  31. void ValidateEndpoint(ServiceEndpoint endpoint)
  32. {
  33. Binding binding = endpoint.Binding;
  34. if (binding != null)
  35. {
  36. new BindingValidator(endpoint.Binding).Validate();
  37. }
  38. }
  39. #region IEndpointBehavior Members
  40. void IEndpointBehavior.Validate(ServiceEndpoint endpoint)
  41. {
  42. if (endpoint == null)
  43. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("endpoint");
  44. ValidateEndpoint(endpoint);
  45. }
  46. void IEndpointBehavior.AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters) { }
  47. void IEndpointBehavior.ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) { }
  48. void IEndpointBehavior.ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime) { }
  49. #endregion
  50. #region IServiceBehavior Members
  51. public void Validate(ServiceDescription description, ServiceHostBase serviceHostBase)
  52. {
  53. if (description == null)
  54. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("description");
  55. for (int i = 0; i < description.Endpoints.Count; i++)
  56. {
  57. ServiceEndpoint endpoint = description.Endpoints[i];
  58. if (endpoint != null)
  59. {
  60. ValidateEndpoint(endpoint);
  61. }
  62. }
  63. }
  64. public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters) { }
  65. public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) { }
  66. #endregion
  67. struct BindingValidator
  68. {
  69. static Type[] unsupportedBindings = new Type[]
  70. {
  71. typeof(NetNamedPipeBinding),
  72. typeof(WSDualHttpBinding),
  73. typeof(WS2007FederationHttpBinding),
  74. typeof(WSFederationHttpBinding),
  75. typeof(NetMsmqBinding),
  76. #pragma warning disable 0618
  77. typeof(NetPeerTcpBinding),
  78. #pragma warning restore 0618
  79. typeof(MsmqIntegrationBinding),
  80. };
  81. static Type[] unsupportedBindingElements = new Type[]
  82. {
  83. typeof(AsymmetricSecurityBindingElement),
  84. typeof(CompositeDuplexBindingElement),
  85. typeof(MsmqTransportBindingElement),
  86. typeof(NamedPipeTransportBindingElement),
  87. typeof(OneWayBindingElement),
  88. #pragma warning disable 0618
  89. typeof(PeerCustomResolverBindingElement),
  90. typeof(PeerTransportBindingElement),
  91. typeof(PnrpPeerResolverBindingElement),
  92. #pragma warning restore 0618
  93. typeof(ReliableSessionBindingElement),
  94. typeof(SymmetricSecurityBindingElement),
  95. typeof(TransportSecurityBindingElement),
  96. typeof(MtomMessageEncodingBindingElement),
  97. };
  98. Binding binding;
  99. internal BindingValidator(Binding binding)
  100. {
  101. this.binding = binding;
  102. }
  103. internal void Validate()
  104. {
  105. Fx.Assert(binding != null, "BindingValidator was not constructed with a valid Binding instance");
  106. Type bindingType = binding.GetType();
  107. if (IsUnsupportedBindingType(bindingType))
  108. {
  109. UnsupportedSecurityCheck(SR.FullTrustOnlyBindingSecurityCheck1, bindingType);
  110. }
  111. // special-case error message for WSHttpBindings
  112. bool isWSHttpBinding = typeof(WSHttpBinding).IsAssignableFrom(bindingType);
  113. string sr = isWSHttpBinding ? SR.FullTrustOnlyBindingElementSecurityCheckWSHttpBinding1 : SR.FullTrustOnlyBindingElementSecurityCheck1;
  114. BindingElementCollection elements = binding.CreateBindingElements();
  115. foreach (BindingElement element in elements)
  116. {
  117. Type bindingElementType = element.GetType();
  118. if (element != null && IsUnsupportedBindingElementType(bindingElementType))
  119. {
  120. UnsupportedSecurityCheck(sr, bindingElementType);
  121. }
  122. }
  123. }
  124. bool IsUnsupportedBindingType(Type bindingType)
  125. {
  126. for (int i = 0; i < unsupportedBindings.Length; i++)
  127. {
  128. if (unsupportedBindings[i] == bindingType)
  129. return true;
  130. }
  131. return false;
  132. }
  133. bool IsUnsupportedBindingElementType(Type bindingElementType)
  134. {
  135. for (int i = 0; i < unsupportedBindingElements.Length; i++)
  136. {
  137. if (unsupportedBindingElements[i] == bindingElementType)
  138. return true;
  139. }
  140. return false;
  141. }
  142. static readonly PermissionSet fullTrust = new PermissionSet(PermissionState.Unrestricted);
  143. void UnsupportedSecurityCheck(string resource, Type type)
  144. {
  145. try
  146. {
  147. fullTrust.Demand();
  148. }
  149. catch (SecurityException)
  150. {
  151. throw new InvalidOperationException(SR.GetString(resource, binding.Name, type));
  152. }
  153. }
  154. }
  155. }
  156. }