From ae772a09b2ceb6c4298600696550ce1b2b78747d Mon Sep 17 00:00:00 2001 From: Frantisek Holop Date: Fri, 3 Apr 2020 22:18:43 +0200 Subject: [PATCH] Treat an empty SITE_KEY as error Recently I had a configuration error where the SITE_KEY remained unset and the empty defaults made the widget render without the captcha and subsequently fail every submission. While the form showed a validation error, it took days until someone reported it. Lack of logging made tracking down this issue needlessly long as well. When this widget is required, having an empty SITE_KEY and/or SITE_SECRET is simply an error and should be handled as such. While here, simplify the widget and add the SITE_KEY as the hidden input field's value= instead of creating a free floating - """.format( - g_recaptcha_sitekey=g_recaptcha_sitekey - ) - return html + mark_safe(invisible_recaptcha_html) + site_key = get_setting("SITE_KEY") + if not site_key: + raise ValueError("SITE_KEY not set") + attrs["value"] = site_key + + kwargs["attrs"] = attrs + super(InvisibleRecaptchaWidget, self).__init__(*args, **kwargs) class BaseInvisibleRecaptchaWidget(FormElementPluginWidget):