Self hosted FLOSS fitness/workout, nutrition and weight tracker
Add safe URL handling for license attribution links
Roland Geider8ca02c43c885
diff --git a/wger/nutrition/tests/test_ingredient.py b/wger/nutrition/tests/test_ingredient.pyindex 4b8146d4b..cb972359c 100644--- a/wger/nutrition/tests/test_ingredient.py+++ b/wger/nutrition/tests/test_ingredient.py@@ -29,7 +29,10 @@ from django.urls import reversefrom rest_framework import status# wger-from wger.core.models import Language+from wger.core.models import (+ Language,+ License,+)from wger.core.tests import api_base_testfrom wger.core.tests.api_base_test import ApiBaseTestCasefrom wger.core.tests.base_testcase import (@@ -768,6 +771,51 @@ class ImageFromJsonSimpleTests(SimpleTestCase):self.assertEqual(img.size, 54321)+class AttributionLinkTestCase(SimpleTestCase):+ """+ Tests that AbstractLicenseModel.attribution_link only emits http(s) links.++ Image is used as a representative concrete subclass; attribution_link is+ defined once on the shared AbstractLicenseModel.+ """++ def test_javascript_scheme_is_not_rendered_as_link(self):+ """A javascript: URL must never end up inside an href attribute."""++ image = Image(+ license=License(pk=1, short_name='CC', url='javascript:alert(1)'),+ license_title='A photo',+ license_author='Some Author',+ license_object_url='javascript:alert(2)',+ license_author_url='javascript:alert(document.cookie)',+ license_derivative_source_url='javascript:void(0)',+ )++ result = image.attribution_link++ self.assertNotIn('javascript:', result)+ # The text content is still shown, just no longer linked+ self.assertIn('A photo', result)+ self.assertIn('Some Author', result)++ def test_http_urls_are_rendered_as_links(self):+ """Valid http(s) URLs are still emitted as links."""++ image = Image(+ license=License(pk=1, short_name='CC', url='https://example.com/license'),+ license_title='A photo',+ license_author='Some Author',+ license_object_url='https://example.com/photo',+ license_author_url='http://example.com/author',+ )++ result = image.attribution_link++ self.assertIn('href="https://example.com/photo"', result)+ self.assertIn('href="http://example.com/author"', result)+ self.assertIn('href="https://example.com/license"', result)++class IngredientThrottleScopeTestCase(WgerTestCase):"""Tests that ingredient viewsets are wired up with the right throttle scopes.diff --git a/wger/utils/models.py b/wger/utils/models.pyindex d2152cebc..58fe0a600 100644--- a/wger/utils/models.py+++ b/wger/utils/models.py@@ -12,6 +12,9 @@## You should have received a copy of the GNU Affero General Public License+# Standard Library+from urllib.parse import urlsplit+# Djangofrom django.db import modelsfrom django.utils.html import (@@ -30,6 +33,23 @@ Abstract model classes"""+def _safe_attribution_url(url: str) -> str:+ """+ Return ``url`` only if it uses an http(s) scheme, otherwise an empty string.++ ``attribution_link`` embeds these values into ``<a href="...">``; a+ ``javascript:`` (or ``data:`` etc.) scheme would otherwise become a+ clickable stored-XSS payload once the rendered link reaches a template.+ """+ if not url:+ return ''+ try:+ scheme = urlsplit(url).scheme+ except ValueError:+ return ''+ return url if scheme in ('http', 'https') else ''++class AbstractLicenseModel(models.Model):"""Abstract class that adds license information to a model@@ -90,25 +110,34 @@ class AbstractLicenseModel(models.Model):@propertydef attribution_link(self):- if self.license_object_url:- title = format_html('<a href="{}">{}</a>', self.license_object_url, self.license_title)+ # Only http(s) URLs may be embedded as links — a javascript:/data: URL+ # in any of these (sync-populated) fields would otherwise be rendered+ # as a clickable stored-XSS payload.+ object_url = _safe_attribution_url(self.license_object_url)+ author_url = _safe_attribution_url(self.license_author_url)+ derivative_url = _safe_attribution_url(self.license_derivative_source_url)+ license_url = _safe_attribution_url(self.license.url)++ if object_url:+ title = format_html('<a href="{}">{}</a>', object_url, self.license_title)else:title = escape(self.license_title)- if self.license_author_url:- author = format_html(- '<a href="{}">{}</a>', self.license_author_url, self.license_author- )+ if author_url:+ author = format_html('<a href="{}">{}</a>', author_url, self.license_author)else:author = escape(self.license_author)- license_link = format_html('<a href="{}">{}</a>', self.license.url, self.license.short_name)+ if license_url:+ license_link = format_html('<a href="{}">{}</a>', license_url, self.license.short_name)+ else:+ license_link = escape(self.license.short_name)derivative = ''- if self.license_derivative_source_url:+ if derivative_url:derivative = format_html(' / A derivative work from <a href="{}">the original work</a>',- self.license_derivative_source_url,+ derivative_url,)return format_html(