Repository navigation
Validate origin and source on DevTools extension postMessage events #10099
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
212 changes: 212 additions & 0 deletions
212
packages/devtools_app/test/extensions/embedded_extension_message_validation_web_test.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,212 @@ | ||
| // Copyright 2026 The Flutter Authors | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file or at https://developers.google.com/open-source/licenses/bsd. | ||
|
|
||
| @TestOn('browser') | ||
|
johnpryan marked this conversation as resolved.
Resolved
|
||
| library; | ||
|
|
||
| import 'dart:js_interop'; | ||
|
|
||
| import 'package:devtools_app/src/extensions/embedded/_controller_web.dart'; | ||
| import 'package:devtools_app/src/extensions/embedded/_view_web.dart'; | ||
| import 'package:devtools_app/src/shared/managers/notifications.dart'; | ||
| import 'package:devtools_app/src/shared/preferences/preferences.dart'; | ||
| import 'package:devtools_app_shared/utils.dart'; | ||
| import 'package:devtools_extensions/api.dart'; | ||
| import 'package:devtools_shared/devtools_extensions.dart'; | ||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:web/web.dart'; | ||
|
|
||
| void main() { | ||
| group('ExtensionIFrameController message validation', () { | ||
| late HTMLIFrameElement extensionIFrame; | ||
| late HTMLIFrameElement otherIFrame; | ||
| late _TestEmbeddedExtensionController embeddedController; | ||
| late ExtensionIFrameController iFrameController; | ||
| late NotificationService testNotificationService; | ||
|
|
||
| const expectedOrigin = 'http://localhost:9100'; | ||
| const extensionUrl = | ||
| '$expectedOrigin/devtools_extensions/test_ext/index.html?theme=dark'; | ||
|
|
||
| MessageEvent createNotificationEvent({ | ||
| required String origin, | ||
| required MessageEventSource? source, | ||
| String message = 'hello from extension', | ||
| }) { | ||
| return MessageEvent( | ||
| 'message', | ||
| MessageEventInit( | ||
| data: ShowNotificationExtensionEvent( | ||
| message: message, | ||
| ).toJson().jsify(), | ||
| origin: origin, | ||
| source: source, | ||
| ), | ||
| ); | ||
| } | ||
|
|
||
| setUp(() { | ||
| TestWidgetsFlutterBinding.ensureInitialized(); | ||
|
|
||
| testNotificationService = NotificationService(); | ||
| setGlobal(NotificationService, testNotificationService); | ||
| setGlobal(PreferencesController, PreferencesController()); | ||
|
|
||
| extensionIFrame = HTMLIFrameElement(); | ||
| document.body!.append(extensionIFrame); | ||
|
|
||
| otherIFrame = HTMLIFrameElement(); | ||
| document.body!.append(otherIFrame); | ||
|
|
||
| embeddedController = _TestEmbeddedExtensionController( | ||
| extensionUrl: extensionUrl, | ||
| extensionIFrame: extensionIFrame, | ||
| ); | ||
| iFrameController = ExtensionIFrameController(embeddedController)..init(); | ||
| }); | ||
|
|
||
| tearDown(() { | ||
| iFrameController.dispose(); | ||
| embeddedController.dispose(); | ||
| extensionIFrame.remove(); | ||
| otherIFrame.remove(); | ||
| removeGlobal(NotificationService); | ||
| removeGlobal(PreferencesController); | ||
| }); | ||
|
|
||
| test( | ||
| 'processes messages matching extension origin and iframe contentWindow', | ||
| () { | ||
| expect(extensionIFrame.contentWindow, isNotNull); | ||
|
|
||
| window.dispatchEvent( | ||
| createNotificationEvent( | ||
| origin: expectedOrigin, | ||
| source: extensionIFrame.contentWindow, | ||
| message: 'valid message', | ||
| ), | ||
| ); | ||
|
|
||
| expect(testNotificationService.activeMessages, hasLength(1)); | ||
| expect( | ||
| testNotificationService.activeMessages.single.text, | ||
| 'valid message', | ||
| ); | ||
| }, | ||
| ); | ||
|
|
||
| test('rejects messages from mismatched origins', () { | ||
| for (final badOrigin in [ | ||
| 'https://evil.example.com', | ||
| 'http://localhost:9999', | ||
| 'http://127.0.0.1:9100', | ||
| 'null', | ||
| '', | ||
| ]) { | ||
| window.dispatchEvent( | ||
| createNotificationEvent( | ||
| origin: badOrigin, | ||
| source: extensionIFrame.contentWindow, | ||
| message: 'spoofed origin: $badOrigin', | ||
| ), | ||
| ); | ||
| } | ||
|
|
||
| expect(testNotificationService.activeMessages, isEmpty); | ||
| }); | ||
|
|
||
| test('rejects messages from unexpected window sources', () { | ||
| window.dispatchEvent( | ||
| createNotificationEvent( | ||
| origin: expectedOrigin, | ||
| source: window, | ||
| message: 'from self window', | ||
| ), | ||
| ); | ||
| window.dispatchEvent( | ||
| createNotificationEvent( | ||
| origin: expectedOrigin, | ||
| source: window.parent, | ||
| message: 'from parent window', | ||
| ), | ||
| ); | ||
| window.dispatchEvent( | ||
| createNotificationEvent( | ||
| origin: expectedOrigin, | ||
| source: otherIFrame.contentWindow, | ||
| message: 'from other iframe', | ||
| ), | ||
| ); | ||
|
|
||
| expect(testNotificationService.activeMessages, isEmpty); | ||
| }); | ||
|
|
||
| test('rejects messages with null source (detached iframe)', () { | ||
| window.dispatchEvent( | ||
| createNotificationEvent( | ||
| origin: expectedOrigin, | ||
| source: null, | ||
| message: 'from detached iframe', | ||
| ), | ||
| ); | ||
|
|
||
| expect(testNotificationService.activeMessages, isEmpty); | ||
| }); | ||
|
|
||
| test('ignores non-MessageEvent events', () { | ||
| window.dispatchEvent(Event('message')); | ||
| expect(testNotificationService.activeMessages, isEmpty); | ||
| }); | ||
|
|
||
| test('safely rejects messages when extensionUrl is a data URI', () { | ||
| final dataController = _TestEmbeddedExtensionController( | ||
| extensionUrl: 'data:text/html;charset=utf-8,<html></html>', | ||
| extensionIFrame: otherIFrame, | ||
| ); | ||
| final dataIFrameController = ExtensionIFrameController(dataController) | ||
| ..init(); | ||
| addTearDown(() { | ||
| dataIFrameController.dispose(); | ||
| dataController.dispose(); | ||
| }); | ||
|
|
||
| window.dispatchEvent( | ||
| createNotificationEvent( | ||
| origin: 'null', | ||
| source: otherIFrame.contentWindow, | ||
| message: 'from data uri', | ||
| ), | ||
| ); | ||
|
|
||
| expect(testNotificationService.activeMessages, isEmpty); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| class _TestEmbeddedExtensionController extends EmbeddedExtensionControllerImpl { | ||
| _TestEmbeddedExtensionController({ | ||
| required this.extensionUrl, | ||
| required this.extensionIFrame, | ||
| }) : super( | ||
| DevToolsExtensionConfig.parse({ | ||
| DevToolsExtensionConfig.nameKey: 'test_ext', | ||
| DevToolsExtensionConfig.packageNameKey: 'test_ext', | ||
| DevToolsExtensionConfig.issueTrackerKey: | ||
| 'https://example.com/issues', | ||
| DevToolsExtensionConfig.versionKey: '1.0.0', | ||
| DevToolsExtensionConfig.materialIconCodePointKey: '0xe15b', | ||
| DevToolsExtensionConfig.extensionAssetsPathKey: '/path/to/ext', | ||
| DevToolsExtensionConfig.devtoolsOptionsUriKey: | ||
| 'file:///path/to/devtools_options.yaml', | ||
| DevToolsExtensionConfig.isPubliclyHostedKey: 'false', | ||
| DevToolsExtensionConfig.detectedFromStaticContextKey: 'false', | ||
| }), | ||
| ); | ||
|
|
||
| @override | ||
| final String extensionUrl; | ||
|
|
||
| @override | ||
| final HTMLIFrameElement extensionIFrame; | ||
| } | ||
|
johnpryan marked this conversation as resolved.
Resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.