Skip to content

Commit 1f2dd37

Browse files
jdufresnecarltongibson
authored andcommitted
[3.0.x] Fixed CVE-2020-13596 -- Fixed potential XSS in admin ForeignKeyRawIdWidget.
1 parent 256d297 commit 1f2dd37

File tree

5 files changed

+36
-3
lines changed

5 files changed

+36
-3
lines changed

django/contrib/admin/widgets.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from django.urls import reverse
1313
from django.urls.exceptions import NoReverseMatch
1414
from django.utils.html import smart_urlquote
15-
from django.utils.safestring import mark_safe
15+
from django.utils.http import urlencode
1616
from django.utils.text import Truncator
1717
from django.utils.translation import get_language, gettext as _
1818

@@ -150,8 +150,8 @@ def get_context(self, name, value, attrs):
150150

151151
params = self.url_parameters()
152152
if params:
153-
related_url += '?' + '&'.join('%s=%s' % (k, v) for k, v in params.items())
154-
context['related_url'] = mark_safe(related_url)
153+
related_url += '?' + urlencode(params)
154+
context['related_url'] = related_url
155155
context['link_title'] = _('Lookup')
156156
# The JavaScript code looks for this class.
157157
context['widget']['attrs'].setdefault('class', 'vForeignKeyRawIdAdminField')

docs/releases/2.2.13.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ Django 2.2.13 release notes
66

77
Django 2.2.13 fixes two security issues and a regression in 2.2.12.
88

9+
CVE-2020-13596: Possible XSS via admin ``ForeignKeyRawIdWidget``
10+
================================================================
11+
12+
Query parameters for the admin ``ForeignKeyRawIdWidget`` were not properly URL
13+
encoded, posing an XSS attack vector. ``ForeignKeyRawIdWidget`` now
14+
ensures query parameters are correctly URL encoded.
15+
916
Bugfixes
1017
========
1118

docs/releases/3.0.7.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ Django 3.0.7 release notes
66

77
Django 3.0.7 fixes two security issues and several bugs in 3.0.6.
88

9+
CVE-2020-13596: Possible XSS via admin ``ForeignKeyRawIdWidget``
10+
================================================================
11+
12+
Query parameters for the admin ``ForeignKeyRawIdWidget`` were not properly URL
13+
encoded, posing an XSS attack vector. ``ForeignKeyRawIdWidget`` now
14+
ensures query parameters are correctly URL encoded.
15+
916
Bugfixes
1017
========
1118

tests/admin_widgets/models.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ def __str__(self):
2727
return self.name
2828

2929

30+
class UnsafeLimitChoicesTo(models.Model):
31+
band = models.ForeignKey(
32+
Band,
33+
models.CASCADE,
34+
limit_choices_to={'name': '"&><escapeme'},
35+
)
36+
37+
3038
class Album(models.Model):
3139
band = models.ForeignKey(Band, models.CASCADE)
3240
featuring = models.ManyToManyField(Band, related_name='featured')

tests/admin_widgets/tests.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from .models import (
2323
Advisor, Album, Band, Bee, Car, Company, Event, Honeycomb, Individual,
2424
Inventory, Member, MyFileField, Profile, School, Student,
25+
UnsafeLimitChoicesTo,
2526
)
2627
from .widgetadmin import site as widget_admin_site
2728

@@ -586,6 +587,16 @@ def test_proper_manager_for_label_lookup(self):
586587
'Hidden</a></strong>' % {'pk': hidden.pk}
587588
)
588589

590+
def test_render_unsafe_limit_choices_to(self):
591+
rel = UnsafeLimitChoicesTo._meta.get_field('band').remote_field
592+
w = widgets.ForeignKeyRawIdWidget(rel, widget_admin_site)
593+
self.assertHTMLEqual(
594+
w.render('test', None),
595+
'<input type="text" name="test" class="vForeignKeyRawIdAdminField">\n'
596+
'<a href="/admin_widgets/band/?name=%22%26%3E%3Cescapeme&amp;_to_field=id" '
597+
'class="related-lookup" id="lookup_id_test" title="Lookup"></a>'
598+
)
599+
589600

590601
@override_settings(ROOT_URLCONF='admin_widgets.urls')
591602
class ManyToManyRawIdWidgetTest(TestCase):

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy