• David Iglesias's avatar
    [web] Add 'nonce' prop to flutter.js loadEntrypoint (#137204) · 15ccf24d
    David Iglesias authored
    ## Description
    
    This PR adds a `nonce` parameter to flutter.js' `loadEntrypoint` method.
    
    When set, loadEntrypoint will add a `nonce` attribute to the `main.dart.js` script tag, which allows Flutter to run in environments slightly more restricted by CSP; those that don't add `'self'` as a valid source for `script-src`.
    
    ----
    
    ### CSP directive
    
    After this change, the CSP directive for a Flutter Web index.html can be:
    
    ```
    script-src 'nonce-YOUR_NONCE_VALUE' 'wasm-unsafe-eval';
    font-src https://fonts.gstatic.com;
    style-src 'nonce-YOUR_NONCE_VALUE';
    ```
    
    When CSP is set via a `meta` tag (like in the test accompanying this change), and to use a service worker, the CSP needs an additional directive: [`worker-src 'self';`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/worker-src)
    
    When CSP set via response headers, the CSP that applies to `flutter_service_worker.js` is determined by its response headers. See **Web Workers API > [Content security policy](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers#content_security_policy)** in MDN.)
    
    ----
    
    ### Initialization
    
    If the CSP is set to disallow `script-src 'self'`, a nonce needs to also be passed to `loadEntrypoint`:
    
    ```javascript
      _flutter.loader.loadEntrypoint({
        nonce: 'SOME_NONCE',
        onEntrypointLoaded: (engineInitializer) async {
          const appRunner = await engineInitializer.initializeEngine({
            nonce: 'SOME_NONCE',
          });
          appRunner.runApp();
        },
      });
    ```
    
    (`nonce` shows twice for now, because the entrypoint loader script doesn't have direct access to the `initializeEngine` call.)
    
    ----
    
    ## Tests
    
    * Added a smoke test to ensure an app configured as described above starts.
    
    ## Issues
    
    * Fixes https://github.com/flutter/flutter/issues/126977
    15ccf24d
Name
Last commit
Last update
..
abstract_method_smoke_test Loading commit data...
android_custom_host_app Loading commit data...
android_embedding_v2_smoke_test Loading commit data...
android_host_app/.gradle Loading commit data...
android_host_app_v2_embedding Loading commit data...
android_module_host_with_custom_build_v2_embedding/app/src/main/java/io/flutter/addtoapp Loading commit data...
android_semantics_testing Loading commit data...
android_views Loading commit data...
channels Loading commit data...
deferred_components_test Loading commit data...
external_ui Loading commit data...
flavors Loading commit data...
flutter_gallery Loading commit data...
gradle_deprecated_settings Loading commit data...
hybrid_android_views Loading commit data...
ios_add2app_life_cycle Loading commit data...
ios_app_with_extensions Loading commit data...
ios_host_app Loading commit data...
ios_host_app_swift Loading commit data...
ios_platform_view_tests Loading commit data...
module_host_with_custom_build/.gradle Loading commit data...
module_host_with_custom_build_v2_embedding Loading commit data...
non_nullable Loading commit data...
platform_interaction Loading commit data...
release_smoke_test Loading commit data...
spell_check Loading commit data...
ui Loading commit data...
web Loading commit data...
web_compile_tests Loading commit data...
web_e2e_tests Loading commit data...
wide_gamut_test Loading commit data...
windows_startup_test Loading commit data...
README.md Loading commit data...