Skip to content

Commit

Permalink
Improve the sanitize_next_parameter method with handling new cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladyslav Tymofeiev committed Jul 19, 2024
1 parent 452e1b9 commit bc998c9
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
20 changes: 20 additions & 0 deletions common/djangoapps/student/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,27 @@ def sanitize_next_parameter(next_param):
return next_param

if COURSE_URL_PATTERN.match(next_param):
# Sometimes the course id received with incorrect encoding/decoding, we need to
# replace it to the correct pattern:
# course-v1:test-sandbox PREP-CORE C -> course-v1:test-sandbox+PREP-CORE+C
#
# Note: We are not expect to have any spaces in course URL

if ' ' in next_param:
next_param = next_param.replace(' ', '+')
elif '%20' in next_param:
next_param = next_param.replace('%20', '+')

sanitized_next_parameter = re.sub(r'\+', '%2B', next_param)

log.info(
u"The course-like next parameter was detected '%(next_param)s'"
u" this will be replaced with sanitized version: '%(sanitized_next_parameter)s'",
{
"next_param": next_param,
"sanitized_next_parameter": sanitized_next_parameter,
}
)
return sanitized_next_parameter

return next_param
24 changes: 24 additions & 0 deletions common/djangoapps/student/tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,27 @@ def test_sanitize_next_param(self):
# Invalid pattern - keep the next_param as it is
next_param = 'some/other/path'
self.assertEqual(sanitize_next_parameter(next_param), next_param)

# Invalid URL with space - replace the ' ' with '+' and encode it
expected_result = 'courses/course-v1:abc-sandbox%2BACC-PTF%2BC/course'

next_param = 'courses/course-v1:abc-sandbox ACC-PTF C/course'
self.assertEqual(sanitize_next_parameter(next_param), expected_result)

next_param = 'courses/course-v1:abc-sandbox ACC-PTF+C/course'
self.assertEqual(sanitize_next_parameter(next_param), expected_result)

next_param = 'courses/course-v1:abc-sandbox+ACC-PTF C/course'
self.assertEqual(sanitize_next_parameter(next_param), expected_result)

# Invalid URL with encoded space - replace the '%20' with '+' and encode it
expected_result = 'courses/course-v1:abc-sandbox%2BACC-PTF%2BC/course'

next_param = 'courses/course-v1:abc-sandbox%20ACC-PTF%20C/course'
self.assertEqual(sanitize_next_parameter(next_param), expected_result)

next_param = 'courses/course-v1:abc-sandbox%20ACC-PTF+C/course'
self.assertEqual(sanitize_next_parameter(next_param), expected_result)

next_param = 'courses/course-v1:abc-sandbox+ACC-PTF%20C/course'
self.assertEqual(sanitize_next_parameter(next_param), expected_result)

0 comments on commit bc998c9

Please sign in to comment.