Skip to content
This repository has been archived by the owner on Sep 20, 2022. It is now read-only.

Add COEP and COEP header middleware decorator and wildcard route #4

Merged
merged 1 commit into from
Aug 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions formgradernext/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
from notebook.notebookapp import NotebookApp
from notebook.utils import url_path_join as ujoin

from .middleware import coop_coep_headers

lms_version = os.environ.get("LMS_VERSION") or "0.1.0"

template_response = requests.get(
Expand All @@ -23,11 +25,17 @@
"</head>", '<script>var base_url = "{{ base_url }}";</script></head>'
)

# Hack(@gzuidhof): We need this until the index file in the bundle itself contains crossorigin (or crossorigin="anonymous") tags
# we need to specify crossorigin assets specifically due to the COOP and COEP headers.
template_html = template_html.replace("<script ", "<script crossorigin ")
template_html = template_html.replace("<link ", "<link crossorigin ")


class LMSHandler(BaseHandler):
@web.authenticated
@check_xsrf
@check_notebook_dir
@coop_coep_headers
def get(self):
html = (
Environment(loader=BaseLoader)
Expand All @@ -46,6 +54,7 @@ def get(self):

handlers = [
(r"/formgradernext/?", LMSHandler),
(r"/formgradernext/.*", LMSHandler),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternative to how it is done in #2.

]


Expand Down
16 changes: 16 additions & 0 deletions formgradernext/middleware.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from functools import wraps
from tornado.web import RequestHandler


def coop_coep_headers(f):
"""
Sets the COOP and COEP headers, which are required for cross origin isolation (which unlocks
certain features in embedded Starboard Notebook frames). These headers need to be present
all the way down (i.e. in the top level webpage, as well as )
"""
@wraps(f)
def handle(self: RequestHandler, *args, **kwargs):
self.set_header("Cross-Origin-Embedder-Policy", "require-corp")
self.set_header("Cross-Origin-Opener-Policy", "same-origin")
return f(self, *args, **kwargs)
return handle