Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make script tags with relative paths work in ShadowRealm scopes #49386

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
60 changes: 43 additions & 17 deletions tools/serve/serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import multiprocessing
import os
import platform
import posixpath
import subprocess
import sys
import threading
Expand Down Expand Up @@ -183,7 +184,8 @@ def _get_script(self, request):
:param request: The Request being processed.
"""
for key, value in self._get_metadata(request):
replacement = self._script_replacement(key, value)
path = self._get_path(request.url_parts.path, True)
replacement = self._script_replacement(key, value, path)
if replacement:
yield replacement

Expand All @@ -207,6 +209,13 @@ def _meta_replacement(self, key, value):
# a specific metadata key: value pair.
pass

@abc.abstractmethod
def _script_replacement(self, key, value, path):
# Get the string to insert into the script part of the wrapper
# document, given a specific metadata key: value pair and the path of
# the document.
pass

@abc.abstractmethod
def check_exposure(self, request):
# Raise an exception if this handler shouldn't be exposed after all.
Expand Down Expand Up @@ -238,7 +247,7 @@ def _meta_replacement(self, key, value):
return '<title>%s</title>' % value
return None

def _script_replacement(self, key, value):
def _script_replacement(self, key, value, path):
if key == "script":
attribute = value.replace("&", "&amp;").replace('"', "&quot;")
return '<script src="%s"></script>' % attribute
Expand Down Expand Up @@ -456,9 +465,14 @@ class ShadowRealmInWindowHandler(HtmlWrapperHandler):
</script>
"""

def _script_replacement(self, key, value):
def _script_replacement(self, key, value, path):
if key == "script":
return 'await import("%s");' % value
# ShadowRealm does not share the outer realm's base URI
module_specifier = (
value
if posixpath.isabs(value)
else posixpath.join(posixpath.dirname(path), value))
return 'await import("%s");' % module_specifier
return None


Expand Down Expand Up @@ -508,9 +522,14 @@ class ShadowRealmInShadowRealmHandler(HtmlWrapperHandler):
</script>
"""

def _script_replacement(self, key, value):
def _script_replacement(self, key, value, path):
if key == "script":
return 'await import("%s");' % value
# ShadowRealm does not share the outer realm's base URI
module_specifier = (
value
if posixpath.isabs(value)
else posixpath.join(posixpath.dirname(path), value))
return 'await import("%s");' % module_specifier
return None


Expand Down Expand Up @@ -569,15 +588,15 @@ def _meta_replacement(self, key, value):
return None

@abc.abstractmethod
def _create_script_import(self, attribute):
# Take attribute (a string URL to a JS script) and return JS source to import the script
# into the worker.
def _create_script_import(self, attribute, path):
# Take attribute (a string URL to a JS script) and path (the document's
# path) and return JS source to import the script into the worker.
pass

def _script_replacement(self, key, value):
def _script_replacement(self, key, value, path):
if key == "script":
attribute = value.replace("\\", "\\\\").replace('"', '\\"')
return self._create_script_import(attribute)
return self._create_script_import(attribute, path)
if key == "title":
value = value.replace("\\", "\\\\").replace('"', '\\"')
return 'self.META_TITLE = "%s";' % value
Expand All @@ -598,7 +617,7 @@ class ClassicWorkerHandler(BaseWorkerHandler):
done();
"""

def _create_script_import(self, attribute):
def _create_script_import(self, attribute, path):
return 'importScripts("%s")' % attribute


Expand All @@ -616,7 +635,7 @@ class ModuleWorkerHandler(BaseWorkerHandler):
done();
"""

def _create_script_import(self, attribute):
def _create_script_import(self, attribute, path):
return 'import "%s";' % attribute


Expand Down Expand Up @@ -645,8 +664,13 @@ class ShadowRealmWorkerWrapperHandler(BaseWorkerHandler):
})();
"""

def _create_script_import(self, attribute):
return 'await import("%s");' % attribute
def _create_script_import(self, attribute, path):
# ShadowRealm does not share the outer realm's base URI
module_specifier = (
attribute
if posixpath.isabs(attribute)
else posixpath.join(posixpath.dirname(path), attribute))
return 'await import("%s");' % module_specifier


class ShadowRealmServiceWorkerWrapperHandler(BaseWorkerHandler):
Expand Down Expand Up @@ -677,7 +701,8 @@ class ShadowRealmServiceWorkerWrapperHandler(BaseWorkerHandler):
})();
"""

def _create_script_import(self, attribute):
def _create_script_import(self, attribute, path):
# fakeDynamicImport uses the outer realm's base URI
return 'await fakeDynamicImport("%s");' % attribute


Expand Down Expand Up @@ -708,7 +733,8 @@ class ShadowRealmAudioWorkletWrapperHandler(BaseWorkerHandler):
}
"""

def _create_script_import(self, attribute):
def _create_script_import(self, attribute, path):
# fakeDynamicImport uses the outer realm's base URI
return 'await fakeDynamicImport("%s");' % attribute


Expand Down