Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

captcha: Derive whether to render the widget from the form itself.

Alex Vandiver629cd6142711
diff --git a/templates/zerver/altcha_form_field.html b/templates/zerver/altcha_form_field.html
index 1b44ff7503b..6d31eda930f 100644
--- a/templates/zerver/altcha_form_field.html
+++ b/templates/zerver/altcha_form_field.html
@@ -1,4 +1,4 @@
-{% if has_captcha %}
+{% if "captcha" in form.fields %}
{% if form.captcha.errors %}
{% for error in form.captcha.errors %}
<p class="help-inline text-error">{{ error }}</p>
diff --git a/zerver/lib/captcha.py b/zerver/lib/captcha.py
index 28b16305d91..4524869662a 100644
--- a/zerver/lib/captcha.py
+++ b/zerver/lib/captcha.py
@@ -15,6 +15,10 @@ from django.utils.translation import gettext_lazy
from typing_extensions import override
+def captcha_enabled() -> bool:
+ return bool(settings.USING_CAPTCHA and settings.ALTCHA_HMAC_KEY)
+
+
class AltchaWidget(forms.TextInput):
@override
def render(
@@ -55,7 +59,7 @@ class CaptchaFormMixin(forms.Form):
def __init__(self, *args: Any, request: HttpRequest, **kwargs: Any) -> None:
super().__init__(*args, **kwargs)
self.request = request
- if not settings.USING_CAPTCHA or not settings.ALTCHA_HMAC_KEY:
+ if not captcha_enabled():
del self.fields["captcha"]
@override
@@ -74,7 +78,7 @@ class CaptchaFormMixin(forms.Form):
def validate_captcha_payload(request: HttpRequest, captcha_payload: str) -> None:
- if not settings.USING_CAPTCHA or not settings.ALTCHA_HMAC_KEY: # nocoverage
+ if not captcha_enabled(): # nocoverage
raise forms.ValidationError(_("Challenges are not enabled."))
try:
diff --git a/zerver/tests/test_realm_creation.py b/zerver/tests/test_realm_creation.py
index 1421f29866d..e5da403d606 100644
--- a/zerver/tests/test_realm_creation.py
+++ b/zerver/tests/test_realm_creation.py
@@ -1230,3 +1230,13 @@ class RealmCreationTest(ZulipTestCase):
# And the challenge has been stripped out of the session
self.assertEqual(self.client.session["altcha_challenges"], [])
+
+ @override_settings(OPEN_REALM_CREATION=True, USING_CAPTCHA=True, ALTCHA_HMAC_KEY="")
+ def test_create_realm_captcha_without_secret(self) -> None:
+ # With USING_CAPTCHA enabled but no altcha_hmac secret
+ # configured, the captcha is disabled entirely, rather than
+ # rendering a widget whose solutions the server could never
+ # validate.
+ result = self.client_get("/new/")
+ self.assertEqual(result.status_code, 200)
+ self.assert_not_in_success_response(["altcha-widget"], result)
diff --git a/zerver/views/antispam.py b/zerver/views/antispam.py
index 425ff366e32..326ffc7e0b2 100644
--- a/zerver/views/antispam.py
+++ b/zerver/views/antispam.py
@@ -7,6 +7,7 @@ from django.http import HttpRequest, HttpResponseBase
from django.utils.timezone import now as timezone_now
from django.utils.translation import gettext as _
+from zerver.lib.captcha import captcha_enabled
from zerver.lib.exceptions import JsonableError
from zerver.lib.response import json_success
from zerver.lib.typed_endpoint import typed_endpoint_without_parameters
@@ -16,7 +17,7 @@ from zerver.lib.typed_endpoint import typed_endpoint_without_parameters
def get_challenge(
request: HttpRequest,
) -> HttpResponseBase:
- if not settings.USING_CAPTCHA or not settings.ALTCHA_HMAC_KEY: # nocoverage
+ if not captcha_enabled(): # nocoverage
raise JsonableError(_("Challenges are not enabled."))
now = timezone_now()
diff --git a/zerver/views/registration.py b/zerver/views/registration.py
index 55150ef7125..c05bc57934e 100644
--- a/zerver/views/registration.py
+++ b/zerver/views/registration.py
@@ -1544,7 +1544,6 @@ def create_realm(request: HttpRequest, confirmation_key: str | None = None) -> H
context = get_realm_create_form_context()
context.update(
{
- "has_captcha": settings.USING_CAPTCHA,
"form": form,
"current_url": request.get_full_path,
}
@@ -1708,7 +1707,6 @@ def create_demo_organization(
context = get_realm_create_form_context()
context.update(
{
- "has_captcha": settings.USING_CAPTCHA,
"form": form,
"current_url": request.get_full_path,
"how_realm_creator_found_zulip_options": RealmAuditLog.HOW_REALM_CREATOR_FOUND_ZULIP_OPTIONS.items(),
diff --git a/zproject/computed_settings.py b/zproject/computed_settings.py
index 30e261dd4da..a006c79061e 100644
--- a/zproject/computed_settings.py
+++ b/zproject/computed_settings.py
@@ -464,7 +464,7 @@ else:
TOR_EXIT_NODE_FILE_PATH = "/var/lib/zulip/tor-exit-nodes.json"
if USING_CAPTCHA:
- ALTCHA_HMAC_KEY = get_secret("altcha_hmac")
+ ALTCHA_HMAC_KEY = get_secret("altcha_hmac", "")
else:
ALTCHA_HMAC_KEY = ""