Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit54b55af

Browse files
committed
fix(devtools): Enhance message bus with event tagging and spam protection
One common problem encountered by the devtools content script is that it accepted almost any message send over the message bus. Some websites like `auth.openai.com` were spamming the bus and DDOS the devtools app.This commit introduces 2 defensives strategies :* event tagging : Only Event sent by the devtools app will make it to its backend* Bailout out when the event throughput is too high (we're getting spammed).fixes#62471#62450#55854
1 parente8c5603 commit54b55af

File tree

5 files changed

+53
-13
lines changed

5 files changed

+53
-13
lines changed

‎devtools/projects/shell-browser/src/app/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ ts_project(
186186
deps= [
187187
":chrome_message_bus_rjs",
188188
":same_page_message_bus_rjs",
189+
"//:node_modules/rxjs",
189190
"//devtools/projects/protocol:protocol_rjs",
190191
],
191192
)

‎devtools/projects/shell-browser/src/app/chrome-message-bus.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ export class ChromeMessageBus extends MessageBus<Events> {
7474
topic,
7575
args,
7676
__ignore_ng_zone__:true,
77+
__NG_DEVTOOLS_EVENT__:true,
7778
});
7879
returntrue;
7980
}

‎devtools/projects/shell-browser/src/app/content-script.ts

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
/// <reference types="chrome"/>
1010

11+
import{bufferTime,filter,fromEvent,tap}from'rxjs';
1112
import{ChromeMessageBus}from'./chrome-message-bus';
1213
import{SamePageMessageBus}from'./same-page-message-bus';
1314

@@ -94,20 +95,55 @@ if (!backendInitialized) {
9495
}
9596

9697
constproxyEventFromWindowToDevToolsExtension=(event:MessageEvent)=>{
97-
if(event.source===window&&event.data){
98-
try{
99-
chrome.runtime.sendMessage(event.data);
100-
}catch(e){
101-
const{message}=easError;
102-
if(message.includes('Extension context invalidated.')){
103-
console.error(
104-
'Angular DevTools: Disconnecting content script due to invalid extension context. Please reload the page.',
105-
);
106-
window.removeEventListener('message',proxyEventFromWindowToDevToolsExtension);
107-
}
108-
throwe;
98+
try{
99+
chrome.runtime.sendMessage(event.data);
100+
}catch(e){
101+
const{message}=easError;
102+
if(message.includes('Extension context invalidated.')){
103+
thrownewError(
104+
'Angular DevTools: Disconnecting content script due to invalid extension context. Please reload the page.',
105+
);
109106
}
107+
throwe;
110108
}
111109
};
112110

113-
window.addEventListener('message',proxyEventFromWindowToDevToolsExtension);
111+
/**
112+
* This introduces a spam protection mechanism for the message bus.
113+
*/
114+
constSPAM_THRESHOLD=1000;
115+
constSPAM_WINDOW_MS=1000;
116+
117+
letmessageCount=0;
118+
letwindowStart=Date.now();
119+
120+
// This is where we listen for messages sent from the Angular DevTools extension to the content script.
121+
// We have some logic to handle spam protection and event tagging.
122+
fromEvent<MessageEvent>(window,'message')
123+
.pipe(
124+
tap(()=>{
125+
// Sliding window counter logic
126+
constnow=Date.now();
127+
if(now-windowStart>SPAM_WINDOW_MS){
128+
messageCount=0;
129+
windowStart=now;
130+
}
131+
messageCount++;
132+
if(messageCount>SPAM_THRESHOLD){
133+
thrownewError(`Angular DevTools: Message channel spam detected! Bailing out.`);
134+
}
135+
}),
136+
filter((event)=>{
137+
// We check for the __NG_DEVTOOLS_EVENT__ property to ensure that we only forward
138+
// messages that are intended for the Angular DevTools extension.
139+
// This prevents the content script from forwarding arbitrary messages that could
140+
// potentially DDOS the extension or cause other issues.
141+
returnevent.source===window&&event.data&&event.data.__NG_DEVTOOLS_EVENT__;
142+
}),
143+
tap((event)=>{
144+
proxyEventFromWindowToDevToolsExtension(event);
145+
}),
146+
)
147+
// This listener is long-lived and will not complete on its own.
148+
// We intend to only unsubscribe from this observable on errors
149+
.subscribe({error:(e)=>console.error(e)});

‎devtools/projects/shell-browser/src/app/same-page-message-bus.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ export class SamePageMessageBus extends MessageBus<Events> {
7474
topic,
7575
args,
7676
__ignore_ng_zone__:true,
77+
__NG_DEVTOOLS_EVENT__:true,
7778
},
7879
'*',
7980
);

‎devtools/src/iframe-message-bus.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ export class IFrameMessageBus extends MessageBus<Events> {
7878
// event listeners but also not prevent the NgZone in the devtools app
7979
// from updating its UI.
8080
__ignore_ng_zone__:this._source==='angular-devtools',
81+
__NG_DEVTOOLS_EVENT__:true,
8182
},
8283
'*',
8384
);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp