diff --git a/dev/bots/service_worker_test.dart b/dev/bots/service_worker_test.dart index 98e26be173..5860ce9f8c 100644 --- a/dev/bots/service_worker_test.dart +++ b/dev/bots/service_worker_test.dart @@ -37,6 +37,8 @@ enum ServiceWorkerTestType { withFlutterJsEntrypointLoadedEvent, // Same as withFlutterJsEntrypointLoadedEvent, but with TrustedTypes enabled. withFlutterJsTrustedTypesOn, + // Same as withFlutterJsEntrypointLoadedEvent, but with nonce required. + withFlutterJsNonceOn, // Uses custom serviceWorkerVersion. withFlutterJsCustomServiceWorkerVersion, // Entrypoint generated by `flutter create`. @@ -53,6 +55,7 @@ Future main() async { await runWebServiceWorkerTest(headless: false, testType: ServiceWorkerTestType.withFlutterJsShort); await runWebServiceWorkerTest(headless: false, testType: ServiceWorkerTestType.withFlutterJsEntrypointLoadedEvent); await runWebServiceWorkerTest(headless: false, testType: ServiceWorkerTestType.withFlutterJsTrustedTypesOn); + await runWebServiceWorkerTest(headless: false, testType: ServiceWorkerTestType.withFlutterJsNonceOn); await runWebServiceWorkerTestWithCachingResources(headless: false, testType: ServiceWorkerTestType.withoutFlutterJs); await runWebServiceWorkerTestWithCachingResources(headless: false, testType: ServiceWorkerTestType.withFlutterJs); await runWebServiceWorkerTestWithCachingResources(headless: false, testType: ServiceWorkerTestType.withFlutterJsShort); @@ -120,6 +123,8 @@ String _testTypeToIndexFile(ServiceWorkerTestType type) { indexFile = 'index_with_flutterjs_entrypoint_loaded.html'; case ServiceWorkerTestType.withFlutterJsTrustedTypesOn: indexFile = 'index_with_flutterjs_el_tt_on.html'; + case ServiceWorkerTestType.withFlutterJsNonceOn: + indexFile = 'index_with_flutterjs_el_nonce.html'; case ServiceWorkerTestType.withFlutterJsCustomServiceWorkerVersion: indexFile = 'index_with_flutterjs_custom_sw_version.html'; case ServiceWorkerTestType.generatedEntrypoint: diff --git a/dev/bots/test.dart b/dev/bots/test.dart index fa0b884c2a..a8c06c8a5a 100644 --- a/dev/bots/test.dart +++ b/dev/bots/test.dart @@ -1260,6 +1260,7 @@ Future _runWebLongRunningTests() async { () => runWebServiceWorkerTest(headless: true, testType: ServiceWorkerTestType.withFlutterJsShort), () => runWebServiceWorkerTest(headless: true, testType: ServiceWorkerTestType.withFlutterJsEntrypointLoadedEvent), () => runWebServiceWorkerTest(headless: true, testType: ServiceWorkerTestType.withFlutterJsTrustedTypesOn), + () => runWebServiceWorkerTest(headless: true, testType: ServiceWorkerTestType.withFlutterJsNonceOn), () => runWebServiceWorkerTestWithCachingResources(headless: true, testType: ServiceWorkerTestType.withoutFlutterJs), () => runWebServiceWorkerTestWithCachingResources(headless: true, testType: ServiceWorkerTestType.withFlutterJs), () => runWebServiceWorkerTestWithCachingResources(headless: true, testType: ServiceWorkerTestType.withFlutterJsShort), diff --git a/dev/integration_tests/web/web/index_with_flutterjs_el_nonce.html b/dev/integration_tests/web/web/index_with_flutterjs_el_nonce.html new file mode 100644 index 0000000000..fe9c761c5d --- /dev/null +++ b/dev/integration_tests/web/web/index_with_flutterjs_el_nonce.html @@ -0,0 +1,49 @@ + + + + + + + + Integration test. App load with flutter.js and onEntrypointLoaded API. nonce required. + + + + + + + + + + + + + + + + + diff --git a/packages/flutter_tools/lib/src/web/file_generators/js/flutter.js b/packages/flutter_tools/lib/src/web/file_generators/js/flutter.js index 4fd0e51f34..ac0e9fc333 100644 --- a/packages/flutter_tools/lib/src/web/file_generators/js/flutter.js +++ b/packages/flutter_tools/lib/src/web/file_generators/js/flutter.js @@ -244,10 +244,10 @@ _flutter.loader = null; * Returns undefined when an `onEntrypointLoaded` callback is supplied in `options`. */ async loadEntrypoint(options) { - const { entrypointUrl = `${baseUri}main.dart.js`, onEntrypointLoaded } = + const { entrypointUrl = `${baseUri}main.dart.js`, onEntrypointLoaded, nonce } = options || {}; - return this._loadEntrypoint(entrypointUrl, onEntrypointLoaded); + return this._loadEntrypoint(entrypointUrl, onEntrypointLoaded, nonce); } /** @@ -286,12 +286,12 @@ _flutter.loader = null; * is loaded, or undefined if `onEntrypointLoaded` * is a function. */ - _loadEntrypoint(entrypointUrl, onEntrypointLoaded) { + _loadEntrypoint(entrypointUrl, onEntrypointLoaded, nonce) { const useCallback = typeof onEntrypointLoaded === "function"; if (!this._scriptLoaded) { this._scriptLoaded = true; - const scriptTag = this._createScriptTag(entrypointUrl); + const scriptTag = this._createScriptTag(entrypointUrl, nonce); if (useCallback) { // Just inject the script tag, and return nothing; Flutter will call // `didCreateEngineInitializer` when it's done. @@ -319,9 +319,12 @@ _flutter.loader = null; * @param {string} url * @returns {HTMLScriptElement} */ - _createScriptTag(url) { + _createScriptTag(url, nonce) { const scriptTag = document.createElement("script"); scriptTag.type = "application/javascript"; + if (nonce) { + scriptTag.nonce = nonce; + } // Apply TrustedTypes validation, if available. let trustedUrl = url; if (this._ttPolicy != null) {