Skip to content

Commit 14bd859

Browse files
Escape data-clipboard-content attribute in Image Uploads admin (#2301)
1 parent 5c03a5e commit 14bd859

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

blog/admin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from django.contrib import admin
44
from django.urls import reverse
5-
from django.utils.html import format_html, format_html_join
5+
from django.utils.html import escape, format_html, format_html_join
66
from django.utils.translation import gettext as _, gettext_lazy
77
from sorl.thumbnail import get_thumbnail
88

@@ -80,7 +80,7 @@ def _get_copy_button(self, obj, contentformat):
8080
source = contentformat.img(obj.image.url, obj.alt_text)
8181
return format_html(
8282
'<button type="button" data-clipboard-content="{}">{}</button>',
83-
source,
83+
escape(source),
8484
contentformat.label,
8585
)
8686

blog/tests.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import time_machine
66
from django.conf import settings
7+
from django.contrib import admin
78
from django.contrib.auth.models import Permission, User
89
from django.contrib.contenttypes.models import ContentType
910
from django.core.files.base import ContentFile
@@ -619,3 +620,18 @@ def test_alt_text_html_escape(self):
619620
ContentFormat.to_html(cf, img_tag),
620621
expected,
621622
)
623+
624+
def test_copy_button(self):
625+
i = ImageUpload.objects.create(
626+
title="test",
627+
alt_text='Alt text "here"',
628+
image=ContentFile(b".", name="test.png"),
629+
)
630+
self.assertInHTML(
631+
'<button type="button" data-clipboard-content='
632+
f'"&lt;img src=&quot;/m/{i.image}&quot; '
633+
'alt=&quot;Alt text &amp;quot;here&amp;quot;&quot;&gt;">'
634+
"Raw HTML"
635+
"</button>",
636+
admin.site.get_model_admin(ImageUpload).copy_buttons(i),
637+
)

0 commit comments

Comments
 (0)