Skip to content

Commit

Permalink
Normative: Provide source text to HostEnsureCanCompileStrings
Browse files Browse the repository at this point in the history
This is a continuation of Mike Samuel's work in tc39#1498. Due to tc39#2670, this
change can be made simpler than it previously was.

This change provides the source text to be evaluated, and the grammar
symbol that should be used to parse it, to the host hook
HostEnsureCanCompileStrings.

One example of where this is needed is for allowing a Content Security
Policy to provide hashes for code executed via `eval()` or
`new Function()`:
w3c/webappsec-csp#623
This is useful on its own, but has come up again in the topic of
ShadowRealm-HTML integration. In a ShadowRealm you can either execute code
asynchronously, with ShadowRealm.p.importValue, or synchronously, with
ShadowRealm.p.evaluate. Because the latter uses `eval()` inside the
ShadowRealm, it's subject to CSP rules, so the only CSP policy that will
let you execute synchronously in the realm is `unsafe-eval`.

The original purpose of tc39#1498 was to support Trusted Types, which is still
a goal of this PR.

This is a separate needs-consensus PR, rather than being part of the
ShadowRealm proposal, because it's useful independently of ShadowRealm,
and also ShadowRealm would go forward regardless of whether this goes
forward.

Prior art: https://github.com/tc39/proposal-dynamic-code-brand-checks

Co-authored-by: Philip Chimento <pchimento@igalia.com>
  • Loading branch information
mikesamuel and ptomato committed Nov 17, 2023
1 parent 82c99bb commit 2a8a0a0
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions spec.html
Original file line number Diff line number Diff line change
Expand Up @@ -28902,7 +28902,7 @@ <h1>
1. If _x_ is not a String, return _x_.
1. Let _evalRealm_ be the current Realm Record.
1. NOTE: In the case of a direct eval, _evalRealm_ is the realm of both the caller of `eval` and of the `eval` function itself.
1. Perform ? HostEnsureCanCompileStrings(_evalRealm_).
1. Perform ? HostEnsureCanCompileStrings(_evalRealm_, |Script|, _x_).
1. Let _inFunction_ be *false*.
1. Let _inMethod_ be *false*.
1. Let _inDerivedConstructor_ be *false*.
Expand Down Expand Up @@ -28965,12 +28965,19 @@ <h1>
<h1>
HostEnsureCanCompileStrings (
_calleeRealm_: a Realm Record,
_goal_: a grammar symbol,
_bodyText_: an ECMAScript language value,
): either a normal completion containing ~unused~ or a throw completion
</h1>
<dl class="header">
<dt>description</dt>
<dd>It allows host environments to block certain ECMAScript functions which allow developers to interpret and evaluate strings as ECMAScript code.</dd>
</dl>
<p>
_goal_ is the nonterminal which will be used to parse _bodyText_.
For example, if called from %Function% it might be the grammar symbol |FunctionBody[~Yield, ~Await]| and from %eval% it might be the grammar symbol |Script|.
</p>
<p>Implementations of HostEnsureCanCompileStrings should not coerce _bodyText_ to a string, since callers will do that later, and HostEnsureCanCompileStrings must not base security decisions on both coercions producing the same string.<p>
<p>The default implementation of HostEnsureCanCompileStrings is to return NormalCompletion(~unused~).</p>
</emu-clause>

Expand Down Expand Up @@ -30157,8 +30164,6 @@ <h1>
<dd>_constructor_ is the constructor function that is performing this action. _newTarget_ is the constructor that `new` was initially applied to. _parameterArgs_ and _bodyArg_ reflect the argument values that were passed to _constructor_.</dd>
</dl>
<emu-alg>
1. Let _currentRealm_ be the current Realm Record.
1. Perform ? HostEnsureCanCompileStrings(_currentRealm_).
1. If _newTarget_ is *undefined*, set _newTarget_ to _constructor_.
1. If _kind_ is ~normal~, then
1. Let _prefix_ be *"function"*.
Expand All @@ -30185,6 +30190,8 @@ <h1>
1. Let _bodySym_ be the grammar symbol |AsyncGeneratorBody|.
1. Let _parameterSym_ be the grammar symbol |FormalParameters[+Yield, +Await]|.
1. Let _fallbackProto_ be *"%AsyncGeneratorFunction.prototype%"*.
1. Let _currentRealm_ be the current Realm Record.
1. Perform ? HostEnsureCanCompileStrings(_currentRealm_, _bodySym_, _bodyArg_).
1. Let _argCount_ be the number of elements in _parameterArgs_.
1. Let _P_ be the empty String.
1. If _argCount_ > 0, then
Expand Down

0 comments on commit 2a8a0a0

Please sign in to comment.