Skip to content

Commit 8feb4be

Browse files
committed
Refactor WebSocket communication to use client types, UUID client identifiers, origin verification, and localhost binding
- Update "websocket-server-process.ts" to validate the "Origin" header during the WebSocket handshake, explicitly rejecting any connections from "http://" or "https://" origins to prevent Cross-Site WebSocket Hijacking (CSWSH). - Update the `start()` method in `apps/editor/src/services/websocket-server-process.ts` to use `this.server.listen(DEFAULT_PORT, '127.0.0.1', () => {` to ensure the WebSocket server binds strictly to localhost and blocks LAN exposure. - rename 'security tokens' to client_type (e.g., ?client_type=browser or ?client_type=editor). no longer keep legacy distinction - Update `websocket-server-process.ts` to implement a self-termination that cleanly shuts down the process when all clients have disconnected. - Fix the WebSocket `verifyClient` implementation by replacing the easily bypassable `http://` and `https://` origin blocklist with a strict allowlist (e.g., `chrome-extension://`, `moz-extension://`) to prevent `Origin: null` bypass attacks. - Replace the sequential integer counter with a Cryptographically Secure Pseudo-Random Number Generator (CSPRNG). Use a standard UUIDv4 (e.g., crypto.randomUUID()) or a long, random alphanumeric string for the client_id. This makes it mathematically impossible for an attacker to guess the routing address of an active editor session. ├── apps │ ├── browser │ │ └── src │ │ ├── background │ │ │ ├── message-handler.ts │ │ │ └── websocket.ts │ │ ├── content-scripts │ │ │ └── send-prompt-content-script │ │ │ ├── chatbots │ │ │ │ ├── ai-studio.ts │ │ │ │ ├── arena.ts │ │ │ │ ├── chatgpt.ts │ │ │ │ ├── claude.ts │ │ │ │ ├── copilot.ts │ │ │ │ ├── deepseek.ts │ │ │ │ ├── doubao.ts │ │ │ │ ├── gemini.ts │ │ │ │ ├── github-copilot.ts │ │ │ │ ├── grok.ts │ │ │ │ ├── hugging-chat.ts │ │ │ │ ├── kimi.ts │ │ │ │ ├── meta.ts │ │ │ │ ├── mistral.ts │ │ │ │ ├── open-webui.ts │ │ │ │ ├── openrouter.ts │ │ │ │ ├── qwen.ts │ │ │ │ ├── together.ts │ │ │ │ ├── yuanbao.ts │ │ │ │ └── z-ai.ts │ │ │ ├── send-prompt-content-script.ts │ │ │ ├── types │ │ │ │ └── chatbot.ts │ │ │ └── utils │ │ │ └── add-apply-response-button.ts │ │ ├── manifest.json │ │ └── types │ │ └── messages.ts │ └── editor │ ├── src │ │ ├── commands │ │ │ └── apply-response-command │ │ │ ├── apply-response-command.ts │ │ │ ├── index.ts │ │ │ └── response-processor │ │ │ └── response-processor.ts │ │ ├── extension.ts │ │ ├── services │ │ │ ├── websocket-manager.ts │ │ │ └── websocket-server-process.ts │ │ └── views │ │ └── prompt │ │ └── backend │ │ └── message-handlers │ │ └── handle-open-website.ts │ └── webpack.config.ts └── packages └── shared └── src ├── constants │ └── websocket.ts └── types └── websocket-message.ts
1 parent e06b37e commit 8feb4be

10 files changed

Lines changed: 48 additions & 39 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ To maximize prompt cache hits when including selected files, unchanged files are
4646
<details>
4747
<summary><strong>Effective steering with examples</strong></summary>
4848

49-
Replace most skills with a simple checkbox tick. Providing examples can make your task instructions simpler and guide the model toward outputs you're looking for.
49+
Replace most skills with a simple checkbox tick. Providing examples right form the repository can make your task instructions simpler and guide the model toward outputs you're looking for.
5050

5151
</details>
5252

‎apps/browser/src/background/websocket.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { WebSocketMessage } from '@shared/types/websocket-message'
22
import { handle_messages } from './message-handler'
3-
import { DEFAULT_PORT, SECURITY_TOKENS } from '@shared/constants/websocket'
3+
import { DEFAULT_PORT, CLIENT_TYPE } from '@shared/constants/websocket'
44
import browser from 'webextension-polyfill'
55
let websocket: WebSocket | null = null
66
let is_reconnecting = false
@@ -76,7 +76,7 @@ export const connect_websocket = async (): Promise<void> => {
7676
storage['browser-profile-name'] as string | undefined
7777
)?.trim()
7878

79-
let ws_url = `ws://localhost:${DEFAULT_PORT}?token=${SECURITY_TOKENS.BROWSER}&version=${version}&user_agent=${encodeURIComponent(user_agent)}`
79+
let ws_url = `ws://localhost:${DEFAULT_PORT}?client_type=${CLIENT_TYPE.BROWSER}&version=${version}&user_agent=${encodeURIComponent(user_agent)}`
8080
if (profile_name) {
8181
ws_url += `&profile_name=${encodeURIComponent(profile_name)}`
8282
}

‎apps/browser/src/content-scripts/send-prompt-content-script/send-prompt-content-script.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ const main = async () => {
200200
const stored_data = storage[storage_key] as {
201201
text: string
202202
current_chat: Chat
203-
client_id: number
203+
client_id: string
204204
raw_instructions?: string
205205
inject_apply_response_button?: boolean
206206
}

‎apps/browser/src/content-scripts/send-prompt-content-script/types/chatbot.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export type Chatbot = {
77
set_reasoning_effort?: (chat: Chat) => Promise<void>
88
enter_system_instructions?: (chat: Chat) => Promise<void>
99
setup_observer?: (params: {
10-
client_id: number
10+
client_id: string
1111
raw_instructions?: string
1212
inject_button: boolean
1313
}) => void

‎apps/browser/src/content-scripts/send-prompt-content-script/utils/add-apply-response-button.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
import { show_response_ready_notification } from './show-response-ready-notification'
1010

1111
export function add_apply_response_button(params: {
12-
client_id: number
12+
client_id: string
1313
raw_instructions?: string
1414
footer: Element
1515
get_code_from_block?: (code_block: Element) => string | null | undefined

‎apps/browser/src/types/messages.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ type ChatInitializedMessage = {
44

55
type ApplyResponseMessage = {
66
action: 'apply-response'
7-
client_id: number
7+
client_id: string
88
raw_instructions?: string
99
}
1010

‎apps/editor/src/services/websocket-manager.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
WebSocketMessage
1212
} from '@shared/types/websocket-message'
1313
import { CHATBOTS } from '@shared/constants/chatbots'
14-
import { DEFAULT_PORT, SECURITY_TOKENS } from '@shared/constants/websocket'
14+
import { DEFAULT_PORT, CLIENT_TYPE } from '@shared/constants/websocket'
1515
import { Logger } from '@shared/utils/logger'
1616
import { WebConfiguration } from '@/types/web-configuration'
1717
import { ConfigWebConfigurationFormat } from '@/utils/web-configuration-format-converters'
@@ -26,14 +26,14 @@ import { t } from '@/i18n'
2626
export class WebSocketManager {
2727
private extension_context: vscode.ExtensionContext
2828
private port: number = DEFAULT_PORT
29-
private security_token: string = SECURITY_TOKENS.EDITOR
29+
private client_type: string = CLIENT_TYPE.EDITOR
3030
private client: WebSocket.WebSocket | null = null
3131
private _on_connection_status_change: vscode.EventEmitter<boolean> =
3232
new vscode.EventEmitter<boolean>()
3333
private connected_browsers: ConnectedBrowser[] = []
3434
private reconnect_timer: NodeJS.Timeout | null = null
3535
private has_connected_browsers: boolean = false
36-
private client_id: number | null = null
36+
private client_id: string | null = null
3737

3838
public readonly on_connection_status_change: vscode.Event<boolean> =
3939
this._on_connection_status_change.event
@@ -168,7 +168,7 @@ export class WebSocketManager {
168168
}
169169
}
170170

171-
const ws_url = `ws://localhost:${this.port}?token=${this.security_token}`
171+
const ws_url = `ws://localhost:${this.port}?client_type=${this.client_type}`
172172
this.client = new WebSocket.WebSocket(ws_url)
173173

174174
this.client.on('open', () => {

‎apps/editor/src/services/websocket-server-process.ts‎

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
import * as crypto from 'crypto'
12
import * as http from 'http'
23
import * as process from 'process'
34
// eslint-disable-next-line @typescript-eslint/no-var-requires
45
const WebSocket = require('ws')
56

6-
import { DEFAULT_PORT, SECURITY_TOKENS } from '@shared/constants/websocket'
7+
import { DEFAULT_PORT, CLIENT_TYPE } from '@shared/constants/websocket'
78

89
interface BrowserClient {
910
ws: WebSocket
@@ -16,13 +17,12 @@ interface BrowserClient {
1617

1718
interface VSCodeClient {
1819
ws: WebSocket
19-
client_id: number
20+
client_id: string
2021
is_alive: boolean
2122
}
2223

2324
class WebSocketServer {
24-
private vscode_clients: Map<number, VSCodeClient> = new Map()
25-
private vscode_client_counter: number = 0
25+
private vscode_clients: Map<string, VSCodeClient> = new Map()
2626
private browser_clients: Map<number, BrowserClient> = new Map()
2727
private browser_client_counter: number = 0
2828
private connections: Set<WebSocket> = new Set()
@@ -31,7 +31,20 @@ class WebSocketServer {
3131

3232
constructor() {
3333
this.server = this._create_http_server()
34-
this.wss = new WebSocket.Server({ server: this.server })
34+
this.wss = new WebSocket.Server({
35+
server: this.server,
36+
verifyClient: (info: any, cb: any) => {
37+
const origin = info.req.headers.origin
38+
if (origin) {
39+
const allowed_prefixes = ['chrome-extension://', 'moz-extension://']
40+
if (!allowed_prefixes.some((prefix) => origin.startsWith(prefix))) {
41+
cb(false, 403, 'Forbidden')
42+
return
43+
}
44+
}
45+
cb(true)
46+
}
47+
})
3548
this._setup_websocket_server()
3649

3750
setInterval(() => this._ping_clients(), 10 * 1000)
@@ -74,21 +87,17 @@ class WebSocketServer {
7487

7588
private _handle_connection(ws: any, request: any) {
7689
const url = new URL(request.url || '', `http://localhost:${DEFAULT_PORT}`)
77-
const token = url.searchParams.get('token')
90+
const client_type = url.searchParams.get('client_type')
7891

7992
if (
80-
token != SECURITY_TOKENS.BROWSER &&
81-
token != SECURITY_TOKENS.EDITOR &&
82-
token != SECURITY_TOKENS.LEGACY_BROWSER &&
83-
token != SECURITY_TOKENS.LEGACY_EDITOR
93+
client_type != CLIENT_TYPE.BROWSER &&
94+
client_type != CLIENT_TYPE.EDITOR
8495
) {
85-
ws.close(1008, 'Invalid security token')
96+
ws.close(1008, 'Invalid client type')
8697
return
8798
}
8899

89-
const is_browser_client =
90-
token == SECURITY_TOKENS.BROWSER ||
91-
token == SECURITY_TOKENS.LEGACY_BROWSER
100+
const is_browser_client = client_type == CLIENT_TYPE.BROWSER
92101

93102
if (is_browser_client) {
94103
this._handle_browser_connection(ws, url)
@@ -213,7 +222,7 @@ class WebSocketServer {
213222
}
214223
this._notify_vscode_clients()
215224
} else {
216-
let disconnected_client_id: number | null = null
225+
let disconnected_client_id: string | null = null
217226
for (const [client_id, client] of this.vscode_clients.entries()) {
218227
if (client.ws === ws) {
219228
disconnected_client_id = client_id
@@ -235,11 +244,14 @@ class WebSocketServer {
235244
}
236245
}
237246
this.connections.delete(ws)
247+
248+
if (this.connections.size == 0) {
249+
this._shutdown()
250+
}
238251
}
239252

240-
private _generate_client_id(): number {
241-
this.vscode_client_counter += 1
242-
return this.vscode_client_counter
253+
private _generate_client_id(): string {
254+
return crypto.randomUUID()
243255
}
244256

245257
private _get_connected_browsers_list() {
@@ -291,7 +303,7 @@ class WebSocketServer {
291303
}
292304

293305
start() {
294-
this.server.listen(DEFAULT_PORT, () => {
306+
this.server.listen(DEFAULT_PORT, '127.0.0.1', () => {
295307
console.log(
296308
`WebSocket server is running on ws://localhost:${DEFAULT_PORT}`
297309
)
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
export const DEFAULT_PORT = 55155
2-
export const SECURITY_TOKENS = {
3-
BROWSER: 'code-web-chat-browser',
4-
EDITOR: 'code-web-chat-editor',
5-
// TODO: Remove legacy tokens after at least Dec 2026
6-
LEGACY_BROWSER: 'gemini-coder',
7-
LEGACY_EDITOR: 'gemini-coder-vscode'
2+
export const CLIENT_TYPE = {
3+
BROWSER: 'browser',
4+
EDITOR: 'editor'
85
}

‎packages/shared/src/types/websocket-message.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export type InitializeChatMessage = {
1717
action: 'initialize-chat'
1818
text: string
1919
url: string
20-
client_id: number // Client ID to identify which editor sent this message
20+
client_id: string
2121
model?: string
2222
target_browser_id?: number
2323
reasoning_effort?: string
@@ -34,13 +34,13 @@ export type BrowserConnectionStatusMessage = {
3434

3535
export type ApplyResponseMessage = {
3636
action: 'apply-response'
37-
client_id: number
37+
client_id: string
3838
raw_instructions?: string
3939
}
4040

4141
export type ClientIdAssignmentMessage = {
4242
action: 'client-id-assignment'
43-
client_id: number
43+
client_id: string
4444
}
4545

4646
export type PingMessage = {

0 commit comments

Comments
 (0)