From 527fba51c26ea033740f90369b25c3c485cc9ff9 Mon Sep 17 00:00:00 2001 From: mubbsharanwar Date: Thu, 28 Aug 2025 13:30:26 +0500 Subject: [PATCH] fix: fix script tag quot escaped Truncator escapes quot to encoding to prevent XSS. --- .../discussion/rest_api/discussions_notifications.py | 5 ++++- .../rest_api/tests/test_discussions_notifications.py | 12 +++++------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/lms/djangoapps/discussion/rest_api/discussions_notifications.py b/lms/djangoapps/discussion/rest_api/discussions_notifications.py index 8249f170273d..8efb2e8acb83 100644 --- a/lms/djangoapps/discussion/rest_api/discussions_notifications.py +++ b/lms/djangoapps/discussion/rest_api/discussions_notifications.py @@ -2,6 +2,7 @@ Discussion notifications sender util. """ import re +import html from bs4 import BeautifulSoup, Tag from django.conf import settings @@ -447,7 +448,9 @@ def clean_thread_html_body(html_body): """ Get post body with tags removed and limited to 500 characters """ - html_body = BeautifulSoup(Truncator(html_body).chars(500, html=True), 'html.parser') + truncated_body = Truncator(html_body).chars(500, html=True) + truncated_body = html.unescape(truncated_body) + html_body = BeautifulSoup(truncated_body, 'html.parser') tags_to_remove = [ "a", "link", # Link Tags diff --git a/lms/djangoapps/discussion/rest_api/tests/test_discussions_notifications.py b/lms/djangoapps/discussion/rest_api/tests/test_discussions_notifications.py index 247bd23540cf..aaa920a0ff78 100644 --- a/lms/djangoapps/discussion/rest_api/tests/test_discussions_notifications.py +++ b/lms/djangoapps/discussion/rest_api/tests/test_discussions_notifications.py @@ -2,7 +2,6 @@ Unit tests for the DiscussionNotificationSender class """ import re -import django import unittest from unittest.mock import MagicMock, patch @@ -109,13 +108,12 @@ def test_html_tags_removal(self):

Script test:

Some other content that should remain.

""" - excepted_script_quot = 'alert("hello");' if django.VERSION >= (5, 0) else 'alert("hello");' expected_output = ( - f'

This is a link to a page.

' - f'

Here is an image:

' - f'

Embedded video:

' - f'

Script test: {excepted_script_quot}

' - f'

Some other content that should remain.

' + '

This is a link to a page.

' + '

Here is an image:

' + '

Embedded video:

' + '

Script test: alert("hello");

' + '

Some other content that should remain.

' ) result = clean_thread_html_body(html_body)