PageRenderTime 832ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/release/v4.0/Microsoft.Security.Application.SecurityRuntimeEngine/ResponseInspectorFilter.cs

#
C# | 252 lines | 115 code | 24 blank | 113 comment | 9 complexity | df27d8bdd112eb699ebcf1506a65916f MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="ResponseInspectorFilter.cs" company="Microsoft Corporation">
  3. // Copyright (c) 2010 All Rights Reserved, Microsoft Corporation
  4. //
  5. // This source is subject to the Microsoft Permissive License.
  6. // Please see the License.txt file for more information.
  7. // All other rights reserved.
  8. //
  9. // THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  10. // KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  11. // IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  12. // PARTICULAR PURPOSE.
  13. // </copyright>
  14. // <summary>
  15. // A stream which passes the original response through request inspectors.
  16. // </summary>
  17. // --------------------------------------------------------------------------------------------------------------------
  18. namespace Microsoft.Security.Application.SecurityRuntimeEngine
  19. {
  20. using System;
  21. using System.Collections.Generic;
  22. using System.IO;
  23. using System.Linq;
  24. using System.Web;
  25. using PlugIns;
  26. /// <summary>
  27. /// A stream which passes the original response through request inspectors.
  28. /// </summary>
  29. internal sealed class ResponseInspectorFilter : Stream
  30. {
  31. /// <summary>
  32. /// The response inspectors to pass the stream through.
  33. /// </summary>
  34. private readonly IEnumerable<IResponseInspector> inspectors;
  35. /// <summary>
  36. /// The original source stream.
  37. /// </summary>
  38. private readonly Stream source;
  39. /// <summary>
  40. /// The HTTP context for the response.
  41. /// </summary>
  42. private readonly HttpContextBase context;
  43. /// <summary>
  44. /// Initializes a new instance of the <see cref="ResponseInspectorFilter"/> class.
  45. /// </summary>
  46. /// <param name="inspectors">The request inspectors to pass the stream through.</param>
  47. /// <param name="source">The source stream.</param>
  48. /// <param name="context">The context for the response.</param>
  49. internal ResponseInspectorFilter(IEnumerable<IResponseInspector> inspectors, Stream source, HttpContextBase context)
  50. {
  51. this.inspectors = inspectors;
  52. this.source = source;
  53. this.context = context;
  54. }
  55. /// <summary>
  56. /// Gets a value indicating whether the current stream supports reading.
  57. /// </summary>
  58. /// <returns>Always false.</returns>
  59. public override bool CanRead
  60. {
  61. get
  62. {
  63. return false;
  64. }
  65. }
  66. /// <summary>
  67. /// Gets a value indicating whether the current stream supports seeking.
  68. /// </summary>
  69. /// <returns>Always false.</returns>
  70. public override bool CanSeek
  71. {
  72. get
  73. {
  74. return false;
  75. }
  76. }
  77. /// <summary>
  78. /// Gets a value indicating whether the current stream supports writing.
  79. /// </summary>
  80. /// <returns>Always true.</returns>
  81. public override bool CanWrite
  82. {
  83. get
  84. {
  85. return true;
  86. }
  87. }
  88. /// <summary>
  89. /// Gets the length in bytes of the stream.
  90. /// </summary>
  91. /// <returns>
  92. /// A long value representing the length of the stream in bytes.
  93. /// </returns>
  94. /// <exception cref="T:System.NotSupportedException">
  95. /// Always thrown.
  96. /// </exception>
  97. public override long Length
  98. {
  99. get
  100. {
  101. throw new NotSupportedException();
  102. }
  103. }
  104. /// <summary>
  105. /// Gets or sets the position within the current stream.
  106. /// </summary>
  107. /// <returns>
  108. /// The current position within the stream.
  109. /// </returns>
  110. /// <exception cref="T:System.NotSupportedException">
  111. /// Always thrown
  112. /// </exception>
  113. public override long Position
  114. {
  115. get
  116. {
  117. throw new NotSupportedException();
  118. }
  119. set
  120. {
  121. throw new NotSupportedException();
  122. }
  123. }
  124. /// <summary>
  125. /// Clears all buffers for this stream and causes any buffered data to be written to the underlying device.
  126. /// </summary>
  127. /// <exception cref="T:System.IO.IOException">
  128. /// An I/O error occurs.
  129. /// </exception>
  130. public override void Flush()
  131. {
  132. this.source.Flush();
  133. }
  134. /// <summary>
  135. /// Reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
  136. /// </summary>
  137. /// <param name="buffer">An array of bytes. When this method returns, the buffer contains the specified byte array with the values between <paramref name="offset"/> and (<paramref name="offset"/> + <paramref name="count"/> - 1) replaced by the bytes read from the current source.</param>
  138. /// <param name="offset">The zero-based byte offset in <paramref name="buffer"/> at which to begin storing the data read from the current stream.</param>
  139. /// <param name="count">The maximum number of bytes to be read from the current stream.</param>
  140. /// <returns>
  141. /// The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached.
  142. /// </returns>
  143. /// <exception cref="T:System.NotSupportedException">
  144. /// Always thrown.
  145. /// </exception>
  146. public override int Read(byte[] buffer, int offset, int count)
  147. {
  148. throw new NotSupportedException();
  149. }
  150. /// <summary>
  151. /// Sets the position within the current stream.
  152. /// </summary>
  153. /// <param name="offset">A byte offset relative to the <paramref name="origin"/> parameter.</param>
  154. /// <param name="origin">A value of type <see cref="T:System.IO.SeekOrigin"/> indicating the reference point used to obtain the new position.</param>
  155. /// <returns>
  156. /// The new position within the current stream.
  157. /// </returns>
  158. /// <exception cref="T:System.NotSupportedException">
  159. /// Always thrown.
  160. /// </exception>
  161. public override long Seek(long offset, SeekOrigin origin)
  162. {
  163. throw new NotSupportedException();
  164. }
  165. /// <summary>
  166. /// Sets the length of the current stream.
  167. /// </summary>
  168. /// <param name="value">The desired length of the current stream in bytes.</param>
  169. /// <exception cref="T:System.NotSupportedException">
  170. /// Always thrown.
  171. /// </exception>
  172. public override void SetLength(long value)
  173. {
  174. throw new NotSupportedException();
  175. }
  176. /// <summary>
  177. /// Writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.
  178. /// </summary>
  179. /// <param name="buffer">An array of bytes. This method copies <paramref name="count"/> bytes from <paramref name="buffer"/> to the current stream.</param>
  180. /// <param name="offset">The zero-based byte offset in <paramref name="buffer"/> at which to begin copying bytes to the current stream.</param>
  181. /// <param name="count">The number of bytes to be written to the current stream.</param>
  182. /// <exception cref="T:System.ArgumentNullException">
  183. /// <paramref name="buffer"/> is null.
  184. /// </exception>
  185. public override void Write(byte[] buffer, int offset, int count)
  186. {
  187. if (this.context != null && SecurityRuntimeInspection.IsRequestStopped(this.context))
  188. {
  189. this.source.Write(buffer, 0, buffer.Length);
  190. return;
  191. }
  192. // Retrieve the number of suspect inspections that this request and response has had so far.
  193. int suspectRequestCount = SecurityRuntimeInspection.GetSuspectCountBeforeInspection(this.context);
  194. if (buffer == null)
  195. {
  196. throw new ArgumentNullException("buffer");
  197. }
  198. string contentType = SecurityRuntimeInspection.GetContentType(this.context);
  199. foreach (IInspectionResult result in this.inspectors.Select(inspector => inspector.Inspect(this.context.Request, contentType, ref buffer)))
  200. {
  201. switch (result.Severity)
  202. {
  203. case InspectionResultSeverity.Halt:
  204. SecurityRuntimeInspection.StopRequest(result, this.context);
  205. throw new ResponseStoppedException(result.StopReason);
  206. case InspectionResultSeverity.Suspect:
  207. suspectRequestCount++;
  208. break;
  209. default:
  210. break;
  211. }
  212. this.source.Write(buffer, 0, buffer.Length);
  213. }
  214. // If we're over the maximum number of suspect results throw an exception and stop processing.
  215. if (SecurityRuntimeSettings.Settings.AllowedSuspectResults != -1 &&
  216. suspectRequestCount > SecurityRuntimeSettings.Settings.AllowedSuspectResults)
  217. {
  218. SecurityRuntimeInspection.StopRequest(new TooManySuspectInspectionsResult(Properties.Resources.ResponseInspectionStoppedMessage), this.context);
  219. throw new ResponseStoppedException(Properties.Resources.ResponseInspectionStoppedMessage);
  220. }
  221. // And finally if we're still good, and we're keeping the suspect inspections count between stages then save it away.
  222. if (!SecurityRuntimeSettings.Settings.ResetSuspectCountBetweenStages)
  223. {
  224. SecurityRuntimeInspection.SetSuspectCountAfterInspection(this.context, suspectRequestCount);
  225. }
  226. }
  227. }
  228. }