Skip to content
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

Check sender ID in the Dart Debug Extension #2289

Merged
merged 8 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions dwds/debug_extension_mv3/web/background.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ Future<void> _handleRuntimeMessages(
expectedType: MessageType.isAuthenticated,
expectedSender: Script.detector,
expectedRecipient: Script.background,
sender: sender,
messageHandler: (String isAuthenticated) async {
final dartTab = sender.tab;
if (dartTab == null) {
Expand All @@ -99,6 +100,7 @@ Future<void> _handleRuntimeMessages(
expectedType: MessageType.debugInfo,
expectedSender: Script.detector,
expectedRecipient: Script.background,
sender: sender,
messageHandler: (DebugInfo debugInfo) async {
final dartTab = sender.tab;
if (dartTab == null) {
Expand Down Expand Up @@ -128,6 +130,7 @@ Future<void> _handleRuntimeMessages(
expectedType: MessageType.debugStateChange,
expectedSender: Script.debuggerPanel,
expectedRecipient: Script.background,
sender: sender,
messageHandler: (DebugStateChange debugStateChange) {
final newState = debugStateChange.newState;
final tabId = debugStateChange.tabId;
Expand All @@ -142,6 +145,7 @@ Future<void> _handleRuntimeMessages(
expectedType: MessageType.multipleAppsDetected,
expectedSender: Script.detector,
expectedRecipient: Script.background,
sender: sender,
messageHandler: (String multipleAppsDetected) async {
final dartTab = sender.tab;
if (dartTab == null) {
Expand All @@ -163,6 +167,7 @@ Future<void> _handleRuntimeMessages(
expectedType: MessageType.appId,
expectedSender: Script.copier,
expectedRecipient: Script.background,
sender: sender,
messageHandler: (String appId) {
displayNotification('Copied app ID: $appId');
},
Expand Down
2 changes: 2 additions & 0 deletions dwds/debug_extension_mv3/web/chrome_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ class Runtime {

external String getURL(String path);

external String get id;

// Note: Not checking the lastError when one occurs throws a runtime exception.
external ChromeError? get lastError;

Expand Down
1 change: 1 addition & 0 deletions dwds/debug_extension_mv3/web/copier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ void _handleRuntimeMessages(
expectedType: MessageType.appId,
expectedSender: Script.background,
expectedRecipient: Script.copier,
sender: sender,
messageHandler: _copyAppId,
);

Expand Down
7 changes: 7 additions & 0 deletions dwds/debug_extension_mv3/web/messaging.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,12 @@ void interceptMessage<T>({
required MessageType expectedType,
required Script expectedSender,
required Script expectedRecipient,
required MessageSender sender,
required void Function(T message) messageHandler,
}) {
if (message == null) return;
if (!_isLegitimateSender(sender)) return;

try {
final decodedMessage = Message.fromJSON(message);
if (decodedMessage.type != expectedType ||
Expand Down Expand Up @@ -187,3 +190,7 @@ Future<bool> _sendMessage({
}
return completer.future;
}

// Verify the message sender is our extension.
bool _isLegitimateSender(MessageSender sender) =>
sender.id == chrome.runtime.id;
2 changes: 2 additions & 0 deletions dwds/debug_extension_mv3/web/panel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ void _handleRuntimeMessages(
expectedType: MessageType.debugStateChange,
expectedSender: Script.background,
expectedRecipient: Script.debuggerPanel,
sender: sender,
messageHandler: (DebugStateChange debugStateChange) async {
if (debugStateChange.tabId != _tabId) {
debugWarn(
Expand All @@ -107,6 +108,7 @@ void _handleRuntimeMessages(
expectedType: MessageType.connectFailure,
expectedSender: Script.background,
expectedRecipient: Script.debuggerPanel,
sender: sender,
messageHandler: (ConnectFailure connectFailure) async {
debugLog(
'Received connect failure for ${connectFailure.tabId} vs $_tabId',
Expand Down
Loading