Skip to content

Commit 4e3cfdb

Browse files
committed
fix: disable ALTCHA session validation by default
related: #2379 Signed-off-by: Keshav Priyadarshi <git@keshav.space>
1 parent 0679677 commit 4e3cfdb

5 files changed

Lines changed: 17 additions & 6 deletions

File tree

vulnerabilities/middleware/altcha_protection.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from django.shortcuts import redirect
1414
from django.utils.deprecation import MiddlewareMixin
1515

16-
SESSION_TIMEOUT = 900 # 15 minutes
16+
from vulnerablecode.settings import ALTCHA_SESSION_TIMEOUT
1717

1818
ALTCHA_PROTECTED_PREFIXES = (
1919
"/packages/",
@@ -25,8 +25,10 @@
2525

2626

2727
class AltchaProtectionMiddleware(MiddlewareMixin):
28-
2928
def __call__(self, request):
29+
if not ALTCHA_SESSION_TIMEOUT:
30+
return self.get_response(request)
31+
3032
protected = any(request.path.startswith(prefix) for prefix in ALTCHA_PROTECTED_PREFIXES)
3133

3234
if not protected:
@@ -38,7 +40,7 @@ def __call__(self, request):
3840
if not verified_at:
3941
return redirect(f"/altcha/?{urlencode({'next': next_url})}")
4042

41-
if time.time() - verified_at > SESSION_TIMEOUT:
43+
if time.time() - verified_at > ALTCHA_SESSION_TIMEOUT:
4244
request.session.pop("altcha_verified_at", None)
4345
return redirect(f"/altcha/?{urlencode({'next': next_url})}")
4446

vulnerabilities/tests/test_altcha_protection.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
from django.test import RequestFactory
1414

1515
from vulnerabilities.forms import AltchaForm
16-
from vulnerabilities.views import AltchaView
1716

1817

1918
@pytest.mark.django_db
@@ -39,6 +38,7 @@ def test_protected_url_allowed_with_valid_session(self, client):
3938
assert response.status_code != 302
4039

4140
def test_expired_session_redirects(self, client):
41+
4242
session = client.session
4343
session["altcha_verified_at"] = time.time() - 3601
4444
session.save()
@@ -62,6 +62,8 @@ def test_expired_session_is_removed(self, client):
6262
@pytest.mark.django_db
6363
class TestAltchaView:
6464
def test_form_valid_sets_session(self, monkeypatch):
65+
from vulnerabilities.views import AltchaView
66+
6567
now = 1234567890
6668

6769
monkeypatch.setattr(time, "time", lambda: now)

vulnerabilities/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
from vulnerabilities.forms import PackageSearchForm
4747
from vulnerabilities.forms import PipelineSchedulePackageForm
4848
from vulnerabilities.forms import VulnerabilitySearchForm
49-
from vulnerabilities.middleware.altcha_protection import SESSION_TIMEOUT as ALTCHA_SESSION_TIMEOUT
5049
from vulnerabilities.models import ISSUE_TYPE_CHOICES
5150
from vulnerabilities.models import AdvisorySetMember
5251
from vulnerabilities.models import AdvisoryToDoV2
@@ -66,6 +65,7 @@
6665
from vulnerabilities.utils import get_advisories_from_groups
6766
from vulnerabilities.utils import safe_altcha_redirect
6867
from vulnerablecode import __version__ as VULNERABLECODE_VERSION
68+
from vulnerablecode.settings import ALTCHA_SESSION_TIMEOUT
6969
from vulnerablecode.settings import env
7070

7171
PAGE_SIZE = 10

vulnerablecode/settings.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242

4343
ALTCHA_HMAC_KEY = env.str("ALTCHA_HMAC_KEY")
4444

45+
ALTCHA_SESSION_TIMEOUT = env.int("ALTCHA_SESSION_TIMEOUT", None)
46+
4547
# SECURITY WARNING: do not run with debug turned on in production
4648
DEBUG = env.bool("VULNERABLECODE_DEBUG", default=False)
4749

@@ -115,6 +117,7 @@
115117
"vulnerabilities.middleware.vcio_user_agent.VCIOUserAgentMiddleware",
116118
)
117119

120+
118121
ROOT_URLCONF = "vulnerablecode.urls"
119122

120123
WSGI_APPLICATION = "vulnerablecode.wsgi.application"
@@ -215,6 +218,7 @@
215218

216219
if IS_TESTS:
217220
VULNERABLECODEIO_REQUIRE_AUTHENTICATION = False
221+
ALTCHA_SESSION_TIMEOUT = 900
218222

219223
USE_L10N = True
220224

vulnerablecode/urls.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
from vulnerabilities.views import PipelineRunDetailView
4040
from vulnerabilities.views import PipelineRunListView
4141
from vulnerabilities.views import PipelineScheduleListView
42+
from vulnerablecode.settings import ALTCHA_SESSION_TIMEOUT
4243
from vulnerablecode.settings import DEBUG
4344
from vulnerablecode.settings import DEBUG_TOOLBAR
4445

@@ -122,7 +123,6 @@ def __init__(self, *args, **kwargs):
122123
AdvisoryDetails.as_view(),
123124
name="advisory_details",
124125
),
125-
path("altcha/", AltchaView.as_view(), name="altcha"),
126126
path(
127127
"packages/v2/search/",
128128
PackageSearchV2.as_view(),
@@ -169,6 +169,9 @@ def __init__(self, *args, **kwargs):
169169
# ),
170170
]
171171

172+
if ALTCHA_SESSION_TIMEOUT:
173+
urlpatterns += [path("altcha/", AltchaView.as_view(), name="altcha")]
174+
172175
if DEBUG:
173176
urlpatterns += [path("django-rq/", include("django_rq.urls"))]
174177

0 commit comments

Comments
 (0)