/Archive/2.0/Pegasus.Library/Pegasus.Library/Web/Security/SecureWebPage/RequestEvaluator.cs

# · C# · 113 lines · 55 code · 17 blank · 41 comment · 14 complexity · e27a4aec12624fa45249d33eae05d713 MD5 · raw file

  1. using System;
  2. using System.Web;
  3. using System.Web.Configuration;
  4. namespace Pegasus.Web.Security.SecureWebPage
  5. {
  6. /// <summary>
  7. /// Represents an evaluator for requests that
  8. /// </summary>
  9. public sealed class RequestEvaluator {
  10. /// <summary>
  11. /// Evaluates a given request against specified settings for the type of security action required
  12. /// to fulfill the request properly.
  13. /// </summary>
  14. /// <param name="request">The request to evaluate.</param>
  15. /// <param name="settings">The settings to evaluate against.</param>
  16. /// <returns>A SecurityType value for the appropriate action.</returns>
  17. public static SecurityType Evaluate(HttpRequest request, SecureWebPageSettings settings) {
  18. // Initialize the result to Ignore.
  19. SecurityType Result = SecurityType.Ignore;
  20. // Determine if this request should be ignored based on the settings' Mode.
  21. if (RequestMatchesMode(request, settings.Mode)) {
  22. // Get the relative file path of the current request from the application root.
  23. string RelativeFilePath = request.Url.AbsolutePath.Remove(0, request.ApplicationPath.Length).ToLower();
  24. if (RelativeFilePath.StartsWith("/"))
  25. // Remove any leading "/".
  26. RelativeFilePath = RelativeFilePath.Substring(1);
  27. // Get the relative directory of the current request by removing the last segment of the RelativeFilePath.
  28. string RelativeDirectory = string.Empty;
  29. int i = RelativeFilePath.LastIndexOf('/');
  30. if (i >= 0)
  31. RelativeDirectory = RelativeFilePath.Substring(0, i);
  32. // Determine if there is a matching file path for the current request.
  33. i = settings.Files.IndexOf(RelativeFilePath);
  34. if (i >= 0)
  35. Result = settings.Files[i].Secure;
  36. else {
  37. // Try to find a matching directory path.
  38. int j = -1;
  39. i = 0;
  40. while (i < settings.Directories.Count) {
  41. // Try to match the beginning of the directory if recursion is allowed (partial match).
  42. if ((settings.Directories[i].Recurse && RelativeDirectory.StartsWith(settings.Directories[i].Path, StringComparison.CurrentCultureIgnoreCase) ||
  43. RelativeDirectory.Equals(settings.Directories[i].Path, StringComparison.CurrentCultureIgnoreCase)) &&
  44. (j == -1 || settings.Directories[i].Path.Length > settings.Directories[j].Path.Length))
  45. // First or longer partial match found (deepest recursion is the best match).
  46. j = i;
  47. i++;
  48. }
  49. if (j > -1)
  50. // Indicate a match for a partially matched directory allowing recursion.
  51. Result = settings.Directories[j].Secure;
  52. else
  53. // No match indicates an insecure result.
  54. Result = SecurityType.Insecure;
  55. }
  56. }
  57. return Result;
  58. }
  59. /// <summary>
  60. /// Evaluates a given request against configured settings for the type of security action required
  61. /// to fulfill the request properly.
  62. /// </summary>
  63. /// <param name="request">The request to evaluate.</param>
  64. /// <returns>A SecurityType value for the appropriate action.</returns>
  65. public static SecurityType Evaluate(HttpRequest request) {
  66. // Get the settings for the secureWebPages section.
  67. SecureWebPageSettings Settings = WebConfigurationManager.GetSection("secureWebPages") as SecureWebPageSettings;
  68. return Evaluate(request, Settings);
  69. }
  70. /// <summary>
  71. /// Tests the given request to see if it matches the specified mode.
  72. /// </summary>
  73. /// <param name="request">A HttpRequest to test.</param>
  74. /// <param name="mode">The SecureWebPageMode used in the test.</param>
  75. /// <returns>
  76. /// Returns true if the request matches the mode as follows:
  77. /// <list type="disc">
  78. /// <item>If mode is On.</item>
  79. /// <item>If mode is set to RemoteOnly and the request is from a computer other than the server.</item>
  80. /// <item>If mode is set to LocalOnly and the request is from the server.</item>
  81. /// </list>
  82. /// </returns>
  83. private static bool RequestMatchesMode(HttpRequest request, SecureWebPageMode mode) {
  84. switch (mode) {
  85. case SecureWebPageMode.On:
  86. return true;
  87. case SecureWebPageMode.RemoteOnly:
  88. return (request.ServerVariables["REMOTE_ADDR"] != request.ServerVariables["LOCAL_ADDR"]);
  89. case SecureWebPageMode.LocalOnly:
  90. return (request.ServerVariables["REMOTE_ADDR"] == request.ServerVariables["LOCAL_ADDR"]);
  91. default:
  92. return false;
  93. }
  94. }
  95. }
  96. }