fix: protect video transcript handler from scraper (#34017)

Web scrapers do annoying stuff like visit urls they shouldn't know about and cause xblock handlers to break.
I tested this by:
Making sure video transcripts worked as normal while logged in
making sure that I got no 500s in my logs while attempting to view it logged out.
This commit is contained in:
connorhaugh
2024-01-08 18:14:03 -05:00
committed by GitHub
parent 83d104f50d
commit efaef4510e
2 changed files with 39 additions and 14 deletions

View File

@@ -315,6 +315,12 @@ class VideoStudentViewHandlers:
if dispatch.startswith('translation'):
language = dispatch.replace('translation', '').strip('/')
# Because scrapers hit video blocks, verify that a user exists.
# use the _request attr to get the django request object.
if not request._request.user: # pylint: disable=protected-access
log.info("Transcript: user must be logged or public view enabled to get transcript")
return Response(status=403)
if not language:
log.info("Invalid /translation request: no language.")
return Response(status=400)
@@ -324,6 +330,7 @@ class VideoStudentViewHandlers:
return Response(status=404)
if language != self.transcript_language:
self.transcript_language = language
try: