BasicMiddleware 734 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. commit d866293e9eb82e70f9947415b278f3cc8c71afdb
  2. Author: Justin Kotalik <[email protected]>
  3. Date: Wed Jan 3 12:37:02 2018 -0800
  4. Increases timeout on Regex in Rewrite. (#280)
  5. diff --git a/src/Microsoft.AspNetCore.Rewrite/Internal/ApacheModRewrite/RuleBuilder.cs b/src/Microsoft.AspNetCore.Rewrite/Internal/ApacheModRewrite/RuleBuilder.cs
  6. index 33473ddbf78..2dd1c58c9e8 100644
  7. --- a/src/Microsoft.AspNetCore.Rewrite/Internal/ApacheModRewrite/RuleBuilder.cs
  8. +++ b/src/Microsoft.AspNetCore.Rewrite/Internal/ApacheModRewrite/RuleBuilder.cs
  9. @@ -18,7 +18,7 @@ namespace Microsoft.AspNetCore.Rewrite.Internal.ApacheModRewrite
  10. private UrlMatch _match;
  11. private CookieActionFactory _cookieActionFactory = new CookieActionFactory();
  12. - private readonly TimeSpan RegexTimeout = TimeSpan.FromMilliseconds(1);
  13. + private readonly TimeSpan _regexTimeout = TimeSpan.FromSeconds(1);
  14. public ApacheModRewriteRule Build()
  15. {
  16. @@ -68,11 +68,11 @@ namespace Microsoft.AspNetCore.Rewrite.Internal.ApacheModRewrite
  17. case ConditionType.Regex:
  18. if (flags.HasFlag(FlagType.NoCase))
  19. {
  20. - condition.Match = new RegexMatch(new Regex(input.Operand, RegexOptions.CultureInvariant | RegexOptions.Compiled | RegexOptions.IgnoreCase, RegexTimeout), input.Invert);
  21. + condition.Match = new RegexMatch(new Regex(input.Operand, RegexOptions.CultureInvariant | RegexOptions.Compiled | RegexOptions.IgnoreCase, _regexTimeout), input.Invert);
  22. }
  23. else
  24. {
  25. - condition.Match = new RegexMatch(new Regex(input.Operand, RegexOptions.CultureInvariant | RegexOptions.Compiled, RegexTimeout), input.Invert);
  26. + condition.Match = new RegexMatch(new Regex(input.Operand, RegexOptions.CultureInvariant | RegexOptions.Compiled, _regexTimeout), input.Invert);
  27. }
  28. break;
  29. case ConditionType.IntComp:
  30. @@ -160,11 +160,11 @@ namespace Microsoft.AspNetCore.Rewrite.Internal.ApacheModRewrite
  31. {
  32. if (flags.HasFlag(FlagType.NoCase))
  33. {
  34. - _match = new RegexMatch(new Regex(input.Operand, RegexOptions.CultureInvariant | RegexOptions.Compiled | RegexOptions.IgnoreCase, RegexTimeout), input.Invert);
  35. + _match = new RegexMatch(new Regex(input.Operand, RegexOptions.CultureInvariant | RegexOptions.Compiled | RegexOptions.IgnoreCase, _regexTimeout), input.Invert);
  36. }
  37. else
  38. {
  39. - _match = new RegexMatch(new Regex(input.Operand, RegexOptions.CultureInvariant | RegexOptions.Compiled, RegexTimeout), input.Invert);
  40. + _match = new RegexMatch(new Regex(input.Operand, RegexOptions.CultureInvariant | RegexOptions.Compiled, _regexTimeout), input.Invert);
  41. }
  42. }
  43. diff --git a/src/Microsoft.AspNetCore.Rewrite/Internal/IISUrlRewrite/UriMatchCondition.cs b/src/Microsoft.AspNetCore.Rewrite/Internal/IISUrlRewrite/UriMatchCondition.cs
  44. index 9f39d466706..c8378c6b778 100644
  45. --- a/src/Microsoft.AspNetCore.Rewrite/Internal/IISUrlRewrite/UriMatchCondition.cs
  46. +++ b/src/Microsoft.AspNetCore.Rewrite/Internal/IISUrlRewrite/UriMatchCondition.cs
  47. @@ -9,12 +9,17 @@ namespace Microsoft.AspNetCore.Rewrite.Internal.IISUrlRewrite
  48. {
  49. public class UriMatchCondition : Condition
  50. {
  51. + private TimeSpan _regexTimeout = TimeSpan.FromSeconds(1);
  52. +
  53. public UriMatchCondition(InputParser inputParser, string input, string pattern, UriMatchPart uriMatchPart, bool ignoreCase, bool negate)
  54. {
  55. + var regexOptions = RegexOptions.CultureInvariant | RegexOptions.Compiled;
  56. + regexOptions = ignoreCase ? regexOptions | RegexOptions.IgnoreCase : regexOptions;
  57. var regex = new Regex(
  58. pattern,
  59. - ignoreCase ? RegexOptions.CultureInvariant | RegexOptions.Compiled | RegexOptions.IgnoreCase : RegexOptions.CultureInvariant | RegexOptions.Compiled,
  60. - TimeSpan.FromMilliseconds(1));
  61. + regexOptions,
  62. + _regexTimeout
  63. + );
  64. Input = inputParser.ParseInputString(input, uriMatchPart);
  65. Match = new RegexMatch(regex, negate);
  66. }
  67. diff --git a/src/Microsoft.AspNetCore.Rewrite/Internal/IISUrlRewrite/UrlRewriteRuleBuilder.cs b/src/Microsoft.AspNetCore.Rewrite/Internal/IISUrlRewrite/UrlRewriteRuleBuilder.cs
  68. index ea855fb4a4b..69ed9621540 100644
  69. --- a/src/Microsoft.AspNetCore.Rewrite/Internal/IISUrlRewrite/UrlRewriteRuleBuilder.cs
  70. +++ b/src/Microsoft.AspNetCore.Rewrite/Internal/IISUrlRewrite/UrlRewriteRuleBuilder.cs
  71. @@ -10,7 +10,7 @@ namespace Microsoft.AspNetCore.Rewrite.Internal.IISUrlRewrite
  72. {
  73. public class UrlRewriteRuleBuilder
  74. {
  75. - private readonly TimeSpan RegexTimeout = TimeSpan.FromMilliseconds(1);
  76. + private readonly TimeSpan _regexTimeout = TimeSpan.FromSeconds(1);
  77. public string Name { get; set; }
  78. public bool Enabled { get; set; }
  79. @@ -48,12 +48,12 @@ namespace Microsoft.AspNetCore.Rewrite.Internal.IISUrlRewrite
  80. {
  81. if (ignoreCase)
  82. {
  83. - var regex = new Regex(input, RegexOptions.CultureInvariant | RegexOptions.Compiled | RegexOptions.IgnoreCase, RegexTimeout);
  84. + var regex = new Regex(input, RegexOptions.CultureInvariant | RegexOptions.Compiled | RegexOptions.IgnoreCase, _regexTimeout);
  85. _initialMatch = new RegexMatch(regex, negate);
  86. }
  87. else
  88. {
  89. - var regex = new Regex(input, RegexOptions.CultureInvariant | RegexOptions.Compiled, RegexTimeout);
  90. + var regex = new Regex(input, RegexOptions.CultureInvariant | RegexOptions.Compiled, _regexTimeout);
  91. _initialMatch = new RegexMatch(regex, negate);
  92. }
  93. break;