From 0ef57eb136914a4dd0de1396095c80d8eddf73ac Mon Sep 17 00:00:00 2001 From: Agrendalath Date: Mon, 25 Oct 2021 21:15:45 +0200 Subject: [PATCH] fix: do not index solutions in CAPA blocks Most tags that could contain solutions or hints were already being removed, but the regex did not include the case when they contained attributes. --- common/lib/xmodule/xmodule/capa_module.py | 18 ++++++--- .../xmodule/xmodule/tests/test_capa_module.py | 37 +++++++++++-------- 2 files changed, 35 insertions(+), 20 deletions(-) diff --git a/common/lib/xmodule/xmodule/capa_module.py b/common/lib/xmodule/xmodule/capa_module.py index 33dfc3ed61..064c0c81f8 100644 --- a/common/lib/xmodule/xmodule/capa_module.py +++ b/common/lib/xmodule/xmodule/capa_module.py @@ -552,14 +552,22 @@ class ProblemBlock( # Make optioninput's options index friendly by replacing the actual tag with the values capa_content = re.sub(r'\s*|\S*<\/optioninput>', r'\1', self.data) - # Removing solutions and hints, as well as script and style + # Remove the following tags with content that can leak hints or solutions: + # - `solution` (with optional attributes) and `solutionset`. + # - `targetedfeedback` (with optional attributes) and `targetedfeedbackset`. + # - `answer` (with optional attributes). + # - `script` (with optional attributes). + # - `style` (with optional attributes). + # - various types of hints (with optional attributes) and `hintpart`. capa_content = re.sub( re.compile( r""" - .*? | - | - | - <[a-z]*hint.*?>.*? + .*? | + .*? | + .*? | + .*? | + .*? | + <[a-z]*hint.*?>.*? """, re.DOTALL | re.VERBOSE), diff --git a/common/lib/xmodule/xmodule/tests/test_capa_module.py b/common/lib/xmodule/xmodule/tests/test_capa_module.py index 2b2b8e3428..26ff575f81 100644 --- a/common/lib/xmodule/xmodule/tests/test_capa_module.py +++ b/common/lib/xmodule/xmodule/tests/test_capa_module.py @@ -2562,25 +2562,32 @@ class ProblemBlockXMLTest(unittest.TestCase): # lint-amnesty, pylint: disable=m def test_solutions_not_indexed(self): xml = textwrap.dedent(""" - -
-

Explanation

+ Test solution. + Test solution with attribute. + + Test solutionset. + Test solution within solutionset. + -

This is what the 1st solution.

+ Test feedback. + Test feedback with attribute. + + Test FeedbackSet. + Test feedback within feedbackset. + -
-
+ Test answer. + Test answer with attribute. - -
-

Explanation

- -

This is the 2nd solution.

- -
-
+ + + + + Test choicehint. + Test hint. + Test hintpart.
""") name = "Blank Common Capa Problem" @@ -2695,7 +2702,7 @@ class ProblemBlockXMLTest(unittest.TestCase): # lint-amnesty, pylint: disable=m """) name = "Non latin Input" descriptor = self._create_descriptor(sample_text_input_problem_xml, name=name) - capa_content = " FX1_VAL='Καλημέρα' Δοκιμή με μεταβλητές με Ελληνικούς χαρακτήρες μέσα σε python: $FX1_VAL " + capa_content = " Δοκιμή με μεταβλητές με Ελληνικούς χαρακτήρες μέσα σε python: $FX1_VAL " descriptor_dict = descriptor.index_dictionary() assert descriptor_dict['content']['capa_content'] == smart_str(capa_content)