Skip to content

Add helpers to make it easier to use trusted types #114

Closed

Description

@ditman

I think HTMLScriptElement needs a src setter that accepts a TrustedScriptURL.

After conditionally creating a trustedUrl like this:

  // If TrustedTypes are available, prepare a trusted URL.
  web.TrustedScriptURL? trustedUrl;
  if ((web.window as JSObject).hasProperty('trustedTypes'.toJS).toDart) {
    // create a TrustedTypePolicy `policy` object
    trustedUrl = policy.createScriptURL(_url, null);
  }

I'm finding myself doing this:

  final web.HTMLScriptElement script =
      // ignore: avoid_dynamic_calls
      (web.document.createElement('script') as dynamic
        // ignore: unsafe_html
        ..src = (trustedUrl ?? _url)
        ..async = true
        ..defer = true) as web.HTMLScriptElement;

Docs:

(Another way would be to pass trustedUrl.toString(), but that kind of defeats the purpose of TrustedScriptURLs, and would still flag me as unsafe_html I think :/)

Activity

  1. sigmundch commented on Dec 7, 2023

    @sigmundch
    Member

    Thanks for filing the issue - I like the alternative you did in https://github.com/flutter/packages/pull/5581/files#diff-fef182cc76f54d7074aee070ced4ddd44ad9233ebf438472221dc834f5f342d9 where you added an extra setter with the right type. The workaround using dynamic relies on using the dart:html interceptor, and if one day we remove it (which we hope we do in the future), it would stop working.

  2. ditman commented on Dec 7, 2023

    @ditman
    MemberAuthor

    Yes, the workaround with dynamic felt very iffy, and it triggered a bunch of unsafe_html lints :) Being able to rename the setter was nice!

  3. changed the title [-]Cannot set `TrustedScriptURL` to `HTMLScriptElement.src`?[/-] [+]Add helpers to make it easier to use trusted types[/+] on Jan 9, 2024
  4. srujzs commented on Jan 9, 2024

    @srujzs
    Contributor

    I'm generalizing this issue a bit more to address some other use cases I'm seeing like checking whether trustedTypes is available, and the ability to not pass anything for args in createScriptURL.

  5. ditman commented on Feb 16, 2024

    @ditman
    MemberAuthor

    Trusted Types with the latest syntax has landed #173 again, but it still needs tweaks to be usable. I've created #175 so we don't forget :)

  6. kevmoo commented on Feb 16, 2024

    @kevmoo
    Member

    I wonder if we should be able to flag certain top-level..."things" as optional so the generator can understand them...

  7. ditman commented on Feb 16, 2024

    @ditman
    MemberAuthor

    TrustedTypes is one of the most egregious examples, though:

    I'm not aware of any other non-prefixed chrome-only "top level" APIs like this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions