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

EnsureCSPDoesNotBlockStringCompilation: Why do we need to call "Get Trusted Type compliant string" when isTrusted is true? #49367

Closed
fred-wang opened this issue Nov 26, 2024 · 6 comments

Comments

@fred-wang
Copy link
Contributor

cc @koto @lukewarlow

See https://w3c.github.io/webappsec-csp/#can-compile-strings

Steps 5-8 are as follows:

  1. Let sourceToValidate be a new TrustedScript object created in realm whose data is set to codeString if isTrusted is true, and codeString otherwise.
  2. Let sourceString be the result of executing the Get Trusted Type compliant string algorithm, with TrustedScript, realm, sourceToValidate, compilationSink, and 'script'.
  3. If the algorithm throws an error, throw an EvalError.
  4. If sourceString is not equal to codeString, throw an EvalError.

When isTrusted is false, then it makes sense to make sure codeString is not modified by "Get Trusted Type compliant string".

But when isTrusted is true, step 5) will just create a TrustedType with data set to codeString, step 6) will set set sourceString to codeString (https://www.w3.org/TR/trusted-types/#abstract-opdef-get-trusted-type-compliant-string exits early at step 1 and per https://www.w3.org/TR/trusted-types/#trusted-script stringified input is the same as returning the codeString data) ; step 7) will see no errors thrown and step 8) will see sourceString equal to codeString.

So why not just do If isTrusted is true then set sourceString to codeString, otherwise do the "Get Trusted Type compliant string" steps?

@fred-wang
Copy link
Contributor Author

When isTrusted is false, then it makes sense to make sure codeString is not modified by "Get Trusted Type compliant string".

Assuming there is a situation in #49371 where isTrusted can become false because of a mismatch between the passed arg's data and the passed string, I'm actually not even sure if step 8 is correct:

  • With a silly default policy that performs no changes, the algorithm would continue with the unmodified codeString. But there is no reason why codeString would be safer than bodyString or parameterStrings.
  • With a default policy that "sanitizes" codeString, the algorithm will throw an error due to a mismatch with codeString, while "Get Trusted Type compliant string" is actually meant to provide some kind of safety.

So maybe when isTrusted=false what we want instead is to redo the work of CreateDynamicFunction to build the source string of the function from the trusted data and then call "Get Trusted Type compliant string" on it. That would be similar to what we do for document.write: https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#document-write-steps is doing. But then the "kind" argument would need to be propagated via HostEnsureCanCompileStrings.

A less strict approach, is to remove condition 8 so the algorithm can continue with the sourceString that has been "sanitized" by the default policy.

@lukewarlow
Copy link
Member

It's explicitly designed so that if the default policy changes the string then it rejects for eval and Function.

Loosening that would require ecma changes that would require going back to them to request. It's stricter specifically based on historical feedback from tc39.

@fred-wang
Copy link
Contributor Author

OK, agree it provides some stronger check but I still feel this is less intuitive than something like what document.write() do. If kept as is, probably a note in the spec explaining the reasoning would help for people who don't have the full context of the tc39 discussions.

And I still don't see why this is necessary when isTrusted=true.

@fred-wang
Copy link
Contributor Author

The tests added in https://phabricator.services.mozilla.com/D230369 now check step 8 i.e. throw iff the codeString is modified by "Get Trusted Type compliant string" (eval-function-constructor.html is using a null default policy, so algorithm is exiting at step 7).

moz-wptsync-bot pushed a commit that referenced this issue Nov 30, 2024
Improve test coverage for [1], considering string checks for arguments
that implement TrustedScript [2] and the rejection condition on whether
"Get Trusted Type compliant string" modified the input [3].

[1] https://w3c.github.io/webappsec-csp/#can-compile-strings
[2] #49371
[3] #49367

Differential Revision: https://phabricator.services.mozilla.com/D230369

bugzilla-url: https://bugzilla.mozilla.org/show_bug.cgi?id=1934373
gecko-commit: cb03a787fe45e9a7bf5539008edbe0c0b79f1ca2
gecko-reviewers: smaug
i3roly pushed a commit to i3roly/firefox-dynasty that referenced this issue Nov 30, 2024
…on. r=smaug

Improve test coverage for [1], considering string checks for arguments
that implement TrustedScript [2] and the rejection condition on whether
"Get Trusted Type compliant string" modified the input [3].

[1] https://w3c.github.io/webappsec-csp/#can-compile-strings
[2] web-platform-tests/wpt#49371
[3] web-platform-tests/wpt#49367

Differential Revision: https://phabricator.services.mozilla.com/D230369
moz-v2v-gh pushed a commit to mozilla/gecko-dev that referenced this issue Nov 30, 2024
…on. r=smaug

Improve test coverage for [1], considering string checks for arguments
that implement TrustedScript [2] and the rejection condition on whether
"Get Trusted Type compliant string" modified the input [3].

[1] https://w3c.github.io/webappsec-csp/#can-compile-strings
[2] web-platform-tests/wpt#49371
[3] web-platform-tests/wpt#49367

Differential Revision: https://phabricator.services.mozilla.com/D230369
gecko-dev-updater pushed a commit to marco-c/gecko-dev-wordified-and-comments-removed that referenced this issue Dec 1, 2024
…on. r=smaug

Improve test coverage for [1], considering string checks for arguments
that implement TrustedScript [2] and the rejection condition on whether
"Get Trusted Type compliant string" modified the input [3].

[1] https://w3c.github.io/webappsec-csp/#can-compile-strings
[2] web-platform-tests/wpt#49371
[3] web-platform-tests/wpt#49367

Differential Revision: https://phabricator.services.mozilla.com/D230369

UltraBlame original commit: cb03a787fe45e9a7bf5539008edbe0c0b79f1ca2
gecko-dev-updater pushed a commit to marco-c/gecko-dev-comments-removed that referenced this issue Dec 1, 2024
…on. r=smaug

Improve test coverage for [1], considering string checks for arguments
that implement TrustedScript [2] and the rejection condition on whether
"Get Trusted Type compliant string" modified the input [3].

[1] https://w3c.github.io/webappsec-csp/#can-compile-strings
[2] web-platform-tests/wpt#49371
[3] web-platform-tests/wpt#49367

Differential Revision: https://phabricator.services.mozilla.com/D230369

UltraBlame original commit: cb03a787fe45e9a7bf5539008edbe0c0b79f1ca2
gecko-dev-updater pushed a commit to marco-c/gecko-dev-wordified that referenced this issue Dec 1, 2024
…on. r=smaug

Improve test coverage for [1], considering string checks for arguments
that implement TrustedScript [2] and the rejection condition on whether
"Get Trusted Type compliant string" modified the input [3].

[1] https://w3c.github.io/webappsec-csp/#can-compile-strings
[2] web-platform-tests/wpt#49371
[3] web-platform-tests/wpt#49367

Differential Revision: https://phabricator.services.mozilla.com/D230369

UltraBlame original commit: cb03a787fe45e9a7bf5539008edbe0c0b79f1ca2
fred-wang added a commit that referenced this issue Dec 2, 2024
Improve test coverage for [1], considering string checks for arguments
that implement TrustedScript [2] and the rejection condition on whether
"Get Trusted Type compliant string" modified the input [3].

[1] https://w3c.github.io/webappsec-csp/#can-compile-strings
[2] #49371
[3] #49367

Differential Revision: https://phabricator.services.mozilla.com/D230369

bugzilla-url: https://bugzilla.mozilla.org/show_bug.cgi?id=1934373
gecko-commit: cb03a787fe45e9a7bf5539008edbe0c0b79f1ca2
gecko-reviewers: smaug

Co-authored-by: Frédéric Wang <[email protected]>
@fred-wang
Copy link
Contributor Author

I just realized I opened this to the wrong repo. I meant to do it in CSP spec repo.

@fred-wang
Copy link
Contributor Author

Moved to w3c/webappsec-csp#698

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants