1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-01 16:36:06 +00:00

fix(accounts): the mailed OTP link keeps its token out of the access logs

The link to the OTP QR code carried its signed token in the path, so
gunicorn's access log (docker logs, the journal) and nginx's held it:
anyone reading them within the hour could open the code and take the
TOTP secret. The token is now the URL's fragment, which no request
carries; the page posts it (otp_qr.js) and the answer shows the code.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kk4h2TcUiaWmU5MUszYSea
This commit is contained in:
catborise 2026-11-09 11:36:21 +03:00
parent 9323d61743
commit d34b1eb2bf
10 changed files with 50 additions and 15 deletions

View file

@ -1,5 +1,5 @@
{% extends "base.html" %}
{% load i18n %}
{% load i18n static %}
{% block title %}{% trans "OTP QR Code" %}{% endblock %}
@ -8,8 +8,17 @@
{% block content %}
<div class="card">
<div class="card-body">
{% if qr_svg %}
<p>{% blocktrans %}Scan this QR code to get OTP for account '{{ user }}'{% endblocktrans %}</p>
{{ qr_svg }}
{% else %}
<form method="post" id="otp-qr-form">{% csrf_token %}<input type="hidden" name="token"></form>
<p class="mb-0">{% trans "Open the link from the mail to see the OTP QR code." %}</p>
{% endif %}
</div>
</div>
{% endblock content %}
{% block script %}
{% if not qr_svg %}<script src="{% static 'js/otp_qr.js' %}"></script>{% endif %}
{% endblock script %}

View file

@ -99,18 +99,24 @@ class AccountsUtilsTestCase(TestCase):
self.assertEqual((message.subject, message.to, message.alternatives), ("OTP QR Code", ["user@example.com"], []))
self.assertNotIn(device.bin_key.hex(), message.body)
self.assertNotIn("otpauth", message.body)
link = re.search(r"http://testserver(/\S+)", message.body).group(1)
page = self.client.get(link)
self.assertEqual(page["Cache-Control"], "no-store")
self.assertEqual(len(html.fromstring(page.content).xpath("//svg")), 1)
# the token is the fragment, which no request (no access log) carries
path, token = re.search(r"http://testserver(/\S+)#(\S+)", message.body).groups()
self.assertEqual(path, reverse("accounts:otp_qr"))
page = html.fromstring(self.client.get(path).content)
self.assertEqual((page.xpath("//svg"), page.xpath("//form[@id='otp-qr-form']/input[@name='token']/@type")), ([], ["hidden"]))
self.assertEqual(page.xpath("//script/@src")[-1], "/static/js/otp_qr.js")
shown = self.client.post(path, {"token": token})
self.assertEqual(shown["Cache-Control"], "no-store")
self.assertEqual(len(html.fromstring(shown.content).xpath("//svg")), 1)
@override_settings(OTP_ENABLED=True)
def test_a_link_that_is_old_or_not_signed_shows_nothing(self):
get_user_totp_device(self.user)
token = OTP_LINK.sign(str(self.user.pk))
with patch("django.core.signing.time.time", return_value=time.time() + OTP_LINK_MAX_AGE + 1):
self.assertEqual(self.client.get(reverse("accounts:otp_qr", args=[token])).status_code, 404)
self.assertEqual(self.client.get(reverse("accounts:otp_qr", args=[f"{self.user.pk}:x:y"])).status_code, 404)
self.assertEqual(self.client.post(reverse("accounts:otp_qr"), {"token": token}).status_code, 404)
self.assertEqual(self.client.post(reverse("accounts:otp_qr"), {"token": f"{self.user.pk}:x:y"}).status_code, 404)
self.assertEqual(self.client.post(reverse("accounts:otp_qr")).status_code, 404)
def test_the_qr_code_is_what_django_qr_code_drew(self):
# sha256 of the SVG django-qr-code 4.2.0 made for this URL

View file

@ -38,7 +38,7 @@ urlpatterns = [
path("api_token/created/", views.api_token_created, name="api_token_created"),
path("api_token/<str:token_key>/delete/", views.api_token_delete, name="api_token_delete"),
path("email_otp/", views.email_otp, name="email_otp"),
path("otp_qr/<str:token>/", views.otp_qr, name="otp_qr"),
path("otp_qr/", views.otp_qr, name="otp_qr"),
path(
"admin_email_otp/<int:user_id>/",
views.admin_email_otp,

View file

@ -68,8 +68,9 @@ OTP_LINK_MAX_AGE = 3600
def send_email_with_otp(request, user):
"""Mail the user a link to the page of their OTP QR code, good for
OTP_LINK_MAX_AGE: the QR code's secret is never in a mailbox."""
url = request.build_absolute_uri(reverse("accounts:otp_qr", args=[OTP_LINK.sign(str(user.pk))]))
OTP_LINK_MAX_AGE: the QR code's secret is never in a mailbox. The token
is the URL's fragment, which no request carries: it is in no access log."""
url = request.build_absolute_uri(reverse("accounts:otp_qr")) + "#" + OTP_LINK.sign(str(user.pk))
send_mail(
_("OTP QR Code"),
_("Open this link within an hour to see the OTP QR code of account '%(user)s':\n\n%(url)s")

View file

@ -353,12 +353,15 @@ def email_otp(request):
@login_not_required
def otp_qr(request, token):
"""The OTP QR code of the user a mailed link (send_email_with_otp) names."""
def otp_qr(request):
"""The OTP QR code of the user a mailed link (send_email_with_otp) names.
The link's token is in its fragment: the page posts it (otp_qr.js)."""
if not settings.OTP_ENABLED:
raise Http404
if request.method != "POST":
return render(request, "accounts/otp_qr.html")
try:
user_id = OTP_LINK.unsign(token, max_age=OTP_LINK_MAX_AGE)
user_id = OTP_LINK.unsign(request.POST.get("token", ""), max_age=OTP_LINK_MAX_AGE)
except BadSignature:
raise Http404
user = get_object_or_404(get_user_model(), pk=user_id, is_active=True)