The Wayback Machine - https://web.archive.org/web/20220320201456/https://github.com/angular/angular/pull/39209
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

fix(core): make development mode Trusted Types compatible #39209

Closed
wants to merge 2 commits into from

Conversation

bjarkler
Copy link
Contributor

@bjarkler bjarkler commented Oct 9, 2020

Introduce a Trusted Types policy that is only available in development
mode. It allows arbitrary unsafe conversions to Trusted Types to support
development features.

Address a Trusted Types violation that occurs in createNamedArrayType
during development mode. Instead of passing strings directly to "new
Function", use the Trusted Types compatible function constructor exposed
by the Trusted Types development policy.

Implement a workaround to make "new Function" work with Trusted Types,
as described here:
https://github.com/w3c/webappsec-trusted-types/wiki/Trusted-Types-for-function-constructor

This is based on #39207. See the individual commits for more details.

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • angular.io application / infrastructure changes
  • Other... Please describe:

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

This is part of an ongoing effort to add support for Trusted Types to Angular.

@pullapprove pullapprove bot requested review from AndrewKushnir and IgorMinar Oct 9, 2020
@google-cla google-cla bot added the cla: yes label Oct 9, 2020
packages/core/src/util/security/trusted_types.ts Outdated Show resolved Hide resolved
packages/core/src/util/security/trusted_types.ts Outdated Show resolved Hide resolved
packages/core/src/util/security/trusted_types.ts Outdated Show resolved Hide resolved
// Use globalThis['eval'] to hide the fact that we're using eval, since
// otherwise the compiler won't know that this can be tree shaken.
Copy link
Member

@gkalpak gkalpak Oct 10, 2020

Choose a reason for hiding this comment

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

Can you expand on this a bit. How does globalThis['eval'] allows tree-shaking?

Copy link
Contributor Author

@bjarkler bjarkler Oct 11, 2020

Choose a reason for hiding this comment

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

I've updated the comment to hopefully explain this a bit better. But to elaborate, eval is a black box from the perspective of the compiler, and prevents it from doing certain kinds of optimizations. When I initially used eval directly the modules that used Trusted Types were not stripped out of the resulting JS binary, even if they were not used at all and would typically be tree shaken out. This caused a lot of size tests to fail. Replacing this with an indirection like global['eval'] tricks the compiler into ignoring that this is an eval, enabling it to use its optimizations and the tests to pass.

Copy link
Contributor

@IgorMinar IgorMinar left a comment

the main (the last two) commits of this PR look good to me - I left just one suggestion to better defend against prod optimizations.

packages/core/src/util/security/trusted_types.ts Outdated Show resolved Hide resolved
bjarkler added 2 commits Oct 13, 2020
Chrome currently does not support passing TrustedScript to the Function
constructor, and instead fails with a Trusted Types violation when
called. As the Function constructor is used in a handful of places
within Angular, such as in the JIT compiler and named_array_type, the
workaround proposed on the following page is implemented:
https://github.com/w3c/webappsec-trusted-types/wiki/Trusted-Types-for-function-constructor

To be precise, it constructs a string representing an anonymous function
in a way that is equivalent to what the Function constructor does,
promotes it to a TrustedScript and then calls eval.

To facilitate backwards compatibility, new Function is used directly in
environments that do not support Trusted Types.
Address a Trusted Types violation that occurs in createNamedArrayType
during development mode. Instead of passing strings directly to "new
Function", use the Trusted Types compatible function constructor exposed
by the Trusted Types policy.
@ngbot ngbot bot added this to the needsTriage milestone Oct 13, 2020
@IgorMinar IgorMinar removed the request for review from AndrewKushnir Oct 13, 2020
@pullapprove pullapprove bot requested a review from AndrewKushnir Oct 13, 2020
Copy link
Contributor

@IgorMinar IgorMinar left a comment

LGTM!

Reviewed-for: global-approvers, fw-security

@IgorMinar IgorMinar removed the request for review from AndrewKushnir Oct 13, 2020
atscott added a commit that referenced this issue Oct 14, 2020
Address a Trusted Types violation that occurs in createNamedArrayType
during development mode. Instead of passing strings directly to "new
Function", use the Trusted Types compatible function constructor exposed
by the Trusted Types policy.

PR Close #39209
@angular-automatic-lock-bot
Copy link

@angular-automatic-lock-bot angular-automatic-lock-bot bot commented Nov 14, 2020

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Nov 14, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

4 participants