openedx / edx-platform

The Open edX LMS & Studio, powering education sites around the world!
https://openedx.org
GNU Affero General Public License v3.0
7.43k stars 3.88k forks source link

Tracking context throws opaque_keys.InvalidKeyError with some enterprise URLs #31837

Open rgraber opened 1 year ago

rgraber commented 1 year ago

From an Enterprise ticket: enterprise saw some stacktraces in the logs related to enterprise enrollments. we investigated to see what was up, found some tracking context stuff that seems to need a regex tweak. original trace and investigation below…

2023-01-17 19:48:09,379 WARNING 38198 [common.djangoapps.track.contexts] [user None] [ip 161.131.0.240] contexts.py:38 - unable to parse course_id "UCT+SMA/enroll"
Traceback (most recent call last):
  File "/edx/app/edxapp/venvs/edxapp/lib/python3.8/site-packages/opaque_keys/__init__.py", line 187, in from_string
    namespace, rest = cls._separate_namespace(serialized)
  File "/edx/app/edxapp/venvs/edxapp/lib/python3.8/site-packages/opaque_keys/__init__.py", line 218, in _separate_namespace
    raise InvalidKeyError(cls, serialized)
opaque_keys.InvalidKeyError: <class 'opaque_keys.edx.keys.CourseKey'>: UCT+SMA/enroll
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/edx/app/edxapp/edx-platform/common/djangoapps/track/contexts.py", line 36, in course_context_from_url
    course_id = CourseKey.from_string(course_id_string)
  File "/edx/app/edxapp/venvs/edxapp/lib/python3.8/site-packages/opaque_keys/__init__.py", line 196, in from_string
    return cls.deprecated_fallback._from_deprecated_string(serialized)
  File "/edx/app/edxapp/venvs/edxapp/lib/python3.8/site-packages/opaque_keys/edx/locator.py", line 389, in _from_deprecated_string
    raise InvalidKeyError(cls, serialized)
opaque_keys.InvalidKeyError: <class 'opaque_keys.edx.locator.CourseLocator'>: UCT+SMA/enroll

Note on line 16 above, looks like we are not parsing the course key correctly

this is the regular expression involved in parsing the course key from the URL: https://github.com/openedx/edx-platform/blob/ebdcf2f34e4af5e69b68453ddc3624a6e2b82a95/common/djangoapps/track/contexts.py#L24 it works out to ^(.*?/course(s)?/)(?!v[0-9]+/[^/]+)(?P[^/+]+(/|+)[^/+]+(/|+)[^/?]+)

when passing an example from our logs: /enterprise/5d566680-12a8-4b85-89d8-d9eacbf0f9eb/course/WasedaX+SJB2-3.101x/enroll/ (using a tester like

)

we get a course_id of WasedaX+SJB2-3.101x/enroll hence the error.

jristau1984 commented 3 months ago

@spencertiberi can you see if Enterprise can pick up this bug resolution?