[web] Add nonce configuration. (flutter/engine#42829)
This PR adds a `nonce` JS configuration attribute so users can pass a nonce value to their flutter engine initialization code. This `nonce` is used to mark all scripts/styles needed by Flutter web that are considered `unsafe-inline` by CSP. In this change, there are only two tags that benefit from this: * canvaskit.js * inline styles for text editing Before this change, the most strict CSP that allows a Flutter Web app to run would look like: ``` script-src 'self' 'nonce-flutter-init-scripts' 'wasm-unsafe-eval' https://www.gstatic.com/flutter-canvaskit/; font-src https://fonts.gstatic.com; style-src 'unsafe-inline'; ``` After this change, CSP could be tightened to: ``` script-src 'self' 'nonce-YOUR_NONCE_VALUE' 'wasm-unsafe-eval'; font-src https://fonts.gstatic.com; style-src 'nonce-YOUR_NONCE_VALUE'; ``` By initializing the Flutter web app with something like this: ```html <meta http-equiv="Content-Security-Policy" content="script-src 'self' 'nonce-YOUR_NONCE_VALUE' 'wasm-unsafe-eval'; font-src https://fonts.gstatic.com; style-src 'nonce-YOUR_NONCE_VALUE';"> ... <script nonce="YOUR_NONCE_VALUE"> _flutter.loader.loadEntrypoint({ onEntrypointLoaded: async function(engineInitializer) { let appRunner = await engineInitializer.initializeEngine({ nonce: 'YOUR_NONCE_VALUE', }); appRunner.runApp(); } }); </script> ``` ## Issues Fixes https://github.com/flutter/flutter/issues/126977 (does not address `flutter.js`, that's a [different story](https://github.com/flutter/flutter/issues/128061)) Helps with https://github.com/flutter/flutter/issues/80221 --- [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
This commit is contained in:
@@ -3596,7 +3596,8 @@ Future<void> _downloadOneOf(Iterable<String> urls) async {
|
||||
/// Returns a [Future] that completes with `true` if the CanvasKit JavaScript
|
||||
/// file was successfully downloaded, or `false` if it failed.
|
||||
Future<bool> _downloadCanvasKitJs(String url) {
|
||||
final DomHTMLScriptElement canvasKitScript = createDomHTMLScriptElement();
|
||||
final DomHTMLScriptElement canvasKitScript =
|
||||
createDomHTMLScriptElement(configuration.nonce);
|
||||
canvasKitScript.src = createTrustedScriptUrl(url);
|
||||
|
||||
final Completer<bool> canvasKitLoadCompleter = Completer<bool>();
|
||||
|
||||
@@ -269,6 +269,11 @@ class FlutterConfiguration {
|
||||
/// to render, or `null` if the user hasn't specified anything.
|
||||
DomElement? get hostElement => _configuration?.hostElement;
|
||||
|
||||
/// Returns a `nonce` to allowlist the inline styles that Flutter web needs.
|
||||
///
|
||||
/// See: https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/nonce
|
||||
String? get nonce => _configuration?.nonce;
|
||||
|
||||
/// Returns the [requestedRendererType] to be used with the current Flutter
|
||||
/// application, normally 'canvaskit' or 'auto'.
|
||||
///
|
||||
@@ -320,6 +325,10 @@ extension JsFlutterConfigurationExtension on JsFlutterConfiguration {
|
||||
|
||||
external DomElement? get hostElement;
|
||||
|
||||
@JS('nonce')
|
||||
external JSString? get _nonce;
|
||||
String? get nonce => _nonce?.toDart;
|
||||
|
||||
@JS('renderer')
|
||||
external JSString? get _renderer;
|
||||
String? get renderer => _renderer?.toDart;
|
||||
|
||||
@@ -929,10 +929,20 @@ extension DomHTMLScriptElementExtension on DomHTMLScriptElement {
|
||||
external set _src(JSAny value);
|
||||
set src(Object /* String|TrustedScriptURL */ value) =>
|
||||
_src = value.toJSAnyShallow;
|
||||
|
||||
@JS('nonce')
|
||||
external set _nonce(JSString? value);
|
||||
set nonce(String? value) => _nonce = value?.toJS;
|
||||
}
|
||||
|
||||
DomHTMLScriptElement createDomHTMLScriptElement() =>
|
||||
domDocument.createElement('script') as DomHTMLScriptElement;
|
||||
DomHTMLScriptElement createDomHTMLScriptElement(String? nonce) {
|
||||
final DomHTMLScriptElement script =
|
||||
domDocument.createElement('script') as DomHTMLScriptElement;
|
||||
if (nonce != null) {
|
||||
script.nonce = nonce;
|
||||
}
|
||||
return script;
|
||||
}
|
||||
|
||||
@JS()
|
||||
@staticInterop
|
||||
@@ -971,11 +981,25 @@ extension DomHTMLStyleElementExtension on DomHTMLStyleElement {
|
||||
external set _type(JSString? value);
|
||||
set type(String? value) => _type = value?.toJS;
|
||||
|
||||
@JS('nonce')
|
||||
external set _nonce(JSString? value);
|
||||
set nonce(String? value) => _nonce = value?.toJS;
|
||||
|
||||
@JS('nonce')
|
||||
external JSString? get _nonce;
|
||||
String? get nonce => _nonce?.toDart;
|
||||
|
||||
external DomStyleSheet? get sheet;
|
||||
}
|
||||
|
||||
DomHTMLStyleElement createDomHTMLStyleElement() =>
|
||||
domDocument.createElement('style') as DomHTMLStyleElement;
|
||||
DomHTMLStyleElement createDomHTMLStyleElement(String? nonce) {
|
||||
final DomHTMLStyleElement style =
|
||||
domDocument.createElement('style') as DomHTMLStyleElement;
|
||||
if (nonce != null) {
|
||||
style.nonce = nonce;
|
||||
}
|
||||
return style;
|
||||
}
|
||||
|
||||
@JS()
|
||||
@staticInterop
|
||||
|
||||
@@ -188,7 +188,7 @@ class FlutterViewEmbedder {
|
||||
});
|
||||
_glassPaneShadow = shadowRoot;
|
||||
|
||||
final DomHTMLStyleElement shadowRootStyleElement = createDomHTMLStyleElement();
|
||||
final DomHTMLStyleElement shadowRootStyleElement = createDomHTMLStyleElement(configuration.nonce);
|
||||
shadowRootStyleElement.id = 'flt-internals-stylesheet';
|
||||
// The shadowRootStyleElement must be appended to the DOM, or its `sheet` will be null later.
|
||||
shadowRoot.appendChild(shadowRootStyleElement);
|
||||
@@ -198,7 +198,7 @@ class FlutterViewEmbedder {
|
||||
);
|
||||
|
||||
_textEditingHostNode =
|
||||
createTextEditingHostNode(flutterViewElement, defaultCssFont);
|
||||
createTextEditingHostNode(flutterViewElement, defaultCssFont, configuration.nonce);
|
||||
|
||||
// Don't allow the scene to receive pointer events.
|
||||
_sceneHostElement = domDocument.createElement('flt-scene-host')
|
||||
@@ -434,10 +434,10 @@ FlutterViewEmbedder ensureFlutterViewEmbedderInitialized() =>
|
||||
|
||||
/// Creates a node to host text editing elements and applies a stylesheet
|
||||
/// to Flutter nodes that exist outside of the shadowDOM.
|
||||
DomElement createTextEditingHostNode(DomElement root, String defaultFont) {
|
||||
DomElement createTextEditingHostNode(DomElement root, String defaultFont, String? nonce) {
|
||||
final DomElement domElement =
|
||||
domDocument.createElement('flt-text-editing-host');
|
||||
final DomHTMLStyleElement styleElement = createDomHTMLStyleElement();
|
||||
final DomHTMLStyleElement styleElement = createDomHTMLStyleElement(nonce);
|
||||
|
||||
styleElement.id = 'flt-text-editing-stylesheet';
|
||||
root.appendChild(styleElement);
|
||||
|
||||
@@ -16,17 +16,25 @@ void testMain() {
|
||||
late DomHTMLStyleElement styleElement;
|
||||
|
||||
setUp(() {
|
||||
styleElement = createDomHTMLStyleElement();
|
||||
styleElement = createDomHTMLStyleElement(null);
|
||||
domDocument.body!.append(styleElement);
|
||||
applyGlobalCssRulesToSheet(
|
||||
styleElement,
|
||||
defaultCssFont: _kDefaultCssFont,
|
||||
);
|
||||
});
|
||||
|
||||
tearDown(() {
|
||||
styleElement.remove();
|
||||
});
|
||||
|
||||
test('createDomHTMLStyleElement sets a nonce value, when passed', () {
|
||||
expect(styleElement.nonce, isEmpty);
|
||||
|
||||
final DomHTMLStyleElement style = createDomHTMLStyleElement('a-nonce-value');
|
||||
expect(style.nonce, 'a-nonce-value');
|
||||
});
|
||||
|
||||
test('(Self-test) hasCssRule can extract rules', () {
|
||||
final bool hasRule = hasCssRule(styleElement,
|
||||
selector: '.flt-text-editing::placeholder', declaration: 'opacity: 0');
|
||||
|
||||
Reference in New Issue
Block a user