From 8a22ef90118157cbeffb0047c20857bd6981dadf Mon Sep 17 00:00:00 2001 From: Tim Lovell-Smith Date: Fri, 1 Mar 2013 17:00:42 -0800 Subject: [PATCH 1/2] Fix the null reference exception which occurs when DependencyResolver.Current.GetService() returns null. And just in case, also eliminate the possibility of null refs caused by something setting DependencyResolver.Current to null. --- PoliteCaptcha/ReCaptchaGenerator.cs | 36 ++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/PoliteCaptcha/ReCaptchaGenerator.cs b/PoliteCaptcha/ReCaptchaGenerator.cs index ab2f112..6a9185a 100644 --- a/PoliteCaptcha/ReCaptchaGenerator.cs +++ b/PoliteCaptcha/ReCaptchaGenerator.cs @@ -12,7 +12,7 @@ namespace PoliteCaptcha /// public class ReCaptchaGenerator : ICaptchaGenerator { - readonly IConfigurationSource configSource; + readonly IConfigurationSource _configSource; public ReCaptchaGenerator() : this(new DefaultConfigurationSource()) @@ -21,7 +21,7 @@ public ReCaptchaGenerator() public ReCaptchaGenerator(IConfigurationSource configSource) { - this.configSource = configSource; + _configSource = configSource; } /// @@ -31,15 +31,16 @@ public ReCaptchaGenerator(IConfigurationSource configSource) /// An optional message to display above the CAPTCHA when it is displayed as a fallback. /// The reCAPTCHA HTML. public IHtmlString Generate( - HtmlHelper htmlHelper, + HtmlHelper htmlHelper, string fallbackMessage = null) { if (htmlHelper == null) + { throw new ArgumentNullException("htmlHelper"); + } - var configurationSource = DependencyResolver.Current.GetService(); - - var publicApiKey = configSource.GetConfigurationValue(Const.ReCaptchaPublicKeyAppSettingKey); + IConfigurationSource configurationSource = GetConfigurationSource(); + var publicApiKey = configurationSource.GetConfigurationValue(Const.ReCaptchaPublicKeyAppSettingKey); if (publicApiKey == null) { if (!htmlHelper.ViewContext.HttpContext.Request.IsLocal) @@ -68,9 +69,28 @@ public IHtmlString Generate( recaptchaControl.RenderControl(htmlWriter); var captchaHtml = htmlWriter.InnerWriter.ToString(); - var template = @"
{0}{1}
"; - + + const string template = @"
{0}{1}
"; return new MvcHtmlString(string.Format(template, fallbackMessage ?? Const.DefaulFallbackMessage, captchaHtml)); } + + private IConfigurationSource GetConfigurationSource() + { + if (_configSource != null) + { + return _configSource; + } + + if (DependencyResolver.Current != null) + { + var resolvedConfigSource = DependencyResolver.Current.GetService(); + if (resolvedConfigSource != null) + { + return resolvedConfigSource; + } + } + + return new DefaultConfigurationSource(); + } } } From 1137ea8a4dab032ec329925f33c08d657f0c0574 Mon Sep 17 00:00:00 2001 From: Tim Lovell-Smith Date: Fri, 1 Mar 2013 17:09:29 -0800 Subject: [PATCH 2/2] Fix the same null ref that can occur in the ReCaptchaValidator class. --- PoliteCaptcha/ReCaptchaValidator.cs | 42 +++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/PoliteCaptcha/ReCaptchaValidator.cs b/PoliteCaptcha/ReCaptchaValidator.cs index 374369e..2b76817 100644 --- a/PoliteCaptcha/ReCaptchaValidator.cs +++ b/PoliteCaptcha/ReCaptchaValidator.cs @@ -10,8 +10,8 @@ namespace PoliteCaptcha ///
public class ReCaptchaValidator : ICaptchaValidator { - readonly RecaptchaValidator recaptchaValidator; - readonly IConfigurationSource configSource; + readonly RecaptchaValidator _recaptchaValidator; + readonly IConfigurationSource _configSource; public ReCaptchaValidator() : this(new DefaultConfigurationSource()) @@ -20,8 +20,8 @@ public ReCaptchaValidator() public ReCaptchaValidator(IConfigurationSource configSource) { - this.configSource = configSource; - this.recaptchaValidator = new RecaptchaValidator(); + _configSource = configSource; + _recaptchaValidator = new RecaptchaValidator(); } /// @@ -32,11 +32,12 @@ public ReCaptchaValidator(IConfigurationSource configSource) public bool Validate(HttpContextBase httpContext) { if (httpContext == null) + { throw new ArgumentNullException("httpContext"); + } - var configurationSource = DependencyResolver.Current.GetService(); + var configurationSource = GetConfigurationSource(); var privateApiKey = configurationSource.GetConfigurationValue(Const.ReCaptchaPrivateKeyAppSettingKey); - if (privateApiKey == null) { if (!httpContext.Request.IsLocal) @@ -53,12 +54,31 @@ public bool Validate(HttpContextBase httpContext) if (string.IsNullOrWhiteSpace(response)) return false; - recaptchaValidator.PrivateKey = privateApiKey; - recaptchaValidator.RemoteIP = httpContext.Request.UserHostAddress; - recaptchaValidator.Challenge = challenge; - recaptchaValidator.Response = response; + _recaptchaValidator.PrivateKey = privateApiKey; + _recaptchaValidator.RemoteIP = httpContext.Request.UserHostAddress; + _recaptchaValidator.Challenge = challenge; + _recaptchaValidator.Response = response; + + return _recaptchaValidator.Validate().IsValid; + } + + private IConfigurationSource GetConfigurationSource() + { + if (_configSource != null) + { + return _configSource; + } + + if (DependencyResolver.Current != null) + { + var resolvedConfigSource = DependencyResolver.Current.GetService(); + if (resolvedConfigSource != null) + { + return resolvedConfigSource; + } + } - return recaptchaValidator.Validate().IsValid; + return new DefaultConfigurationSource(); } } } \ No newline at end of file