Skip to content

Commit d4a20b7

Browse files
committed
Remove websocket token authentication and pin extension origin using a fixed manifest key
- remove the ws-token authentication flow because it breaks remote-ssh connections
1 parent b7e223e commit d4a20b7

4 files changed

Lines changed: 5 additions & 65 deletions

File tree

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

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,6 @@ const ports_to_check = async (): Promise<number[]> => {
1010
return [DEFAULT_PORT, ...forwarded_ports]
1111
}
1212

13-
const get_ws_token = async (): Promise<string | null> => {
14-
try {
15-
const response = await fetch(chrome.runtime.getURL('ws-token'))
16-
if (!response.ok) return null
17-
const token = (await response.text()).trim()
18-
return token || null
19-
} catch {
20-
return null
21-
}
22-
}
23-
2413
const STALE_AFTER_MS = 20000
2514
const CONNECT_TIMEOUT_MS = 2000
2615
const RETRY_IDLE_MS = 5000
@@ -143,16 +132,7 @@ export const connect_websocket = async (port: number): Promise<void> => {
143132
}
144133

145134
try {
146-
const token = await get_ws_token()
147-
if (is_busy(c)) return
148-
149-
const params = new URLSearchParams({
150-
client_type: CLIENT_TYPE.BROWSER
151-
})
152-
if (token) {
153-
params.set('token', token)
154-
}
155-
const ws_url = `ws://localhost:${port}?${params.toString()}`
135+
const ws_url = `ws://localhost:${port}?client_type=${CLIENT_TYPE.BROWSER}`
156136

157137
const ws = new WebSocket(ws_url)
158138
c.ws = ws

‎apps/browser/src/manifest.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"short_name": "CWC",
55
"description": "Autofill CWC-constructed markdown prompts in chatbots.",
66
"version": "1.0.0",
7+
"key": "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAygNzRqaqLbsMPmNdmCD/5N+yr87OF2KPUhHIsNbAZlPm2tBKu+PA0YdpOl4KV6574LxmSsWw3PLm3FtA+li6nll7+cUbF4f0ZHg8DyaodbahRUqs+XWDoSb/PBnlGD7B2I6+vnOrlC+QSPWnH3hxO+1l2RrkOwQ6gCMUU8HKi8N+Zug8Twmv3Z1RooAgxteIhCq03REsW3gS2gnAoekRiF7fuZbAGpkqzalZU5lzOO1/inVWcOIWd4bSfljAXtyLfqHLopjx18rmfYrGghFPk39mt+nBdK9ReIC/cxPMng1URUwxMuXcgFubmLm9HtvkCFbvGX7bKWqS8432PKkdjQIDAQAB",
78
"homepage_url": "https://github.com/CodeWebChat/CWC",
89
"icons": {
910
"16": "icons/icon-16.png",

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

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import * as crypto from 'crypto'
2-
import * as fs from 'fs'
32
import * as http from 'http'
4-
import * as os from 'os'
5-
import * as path from 'path'
63
import * as process from 'process'
74
const WebSocket = require('ws')
85
import { DEFAULT_PORT, CLIENT_TYPE } from '@shared/constants/websocket'
@@ -39,16 +36,11 @@ class WebSocketServer {
3936
const origin = info.origin || info.req.headers.origin
4037
if (
4138
typeof origin !== 'string' ||
42-
!origin.startsWith('chrome-extension://')
39+
origin !== 'chrome-extension://onnabdgflgnhdahhnmlcgakjgpjeeljg'
4340
) {
4441
return cb(false, 403, 'Forbidden: Invalid origin')
4542
}
4643

47-
const token = url.searchParams.get('token')
48-
if (!this._verify_browser_token(token)) {
49-
return cb(false, 401, 'Unauthorized: Invalid token')
50-
}
51-
5244
if (
5345
this.browser_client &&
5446
this.browser_client.ws.readyState === WebSocket.OPEN
@@ -74,22 +66,6 @@ class WebSocketServer {
7466
console.log(`Starting WebSocket server process (PID: ${process.pid})`)
7567
}
7668

77-
private _verify_browser_token(token: string | null): boolean {
78-
if (!token) return false
79-
const token_path = path.join(os.homedir(), '.cwc', 'ws-token')
80-
try {
81-
if (!fs.existsSync(token_path)) return false
82-
const expected_token = fs.readFileSync(token_path, 'utf-8').trim()
83-
if (!expected_token) return false
84-
const token_buf = Buffer.from(token)
85-
const expected_buf = Buffer.from(expected_token)
86-
if (token_buf.length !== expected_buf.length) return false
87-
return crypto.timingSafeEqual(token_buf, expected_buf)
88-
} catch {
89-
return false
90-
}
91-
}
92-
9369
private _schedule_shutdown() {
9470
if (!this.shutdown_timer) {
9571
this.shutdown_timer = setTimeout(() => {

‎apps/editor/src/views/prompt/backend/message-handlers/handle-run-browser.ts‎

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import * as vscode from 'vscode'
22
import * as path from 'path'
33
import * as os from 'os'
44
import * as fs from 'fs'
5-
import * as crypto from 'crypto'
65
import { spawn } from 'child_process'
76
import {
87
Browser,
@@ -18,21 +17,7 @@ const launch_browser = (params: {
1817
executable_path: string
1918
user_data_dir: string
2019
extension_path: string
21-
cwc_dir: string
2220
}) => {
23-
const token_path = path.join(params.cwc_dir, 'ws-token')
24-
let token = ''
25-
if (fs.existsSync(token_path)) {
26-
token = fs.readFileSync(token_path, 'utf-8').trim()
27-
}
28-
if (!token) {
29-
token = crypto.randomBytes(32).toString('hex')
30-
fs.writeFileSync(token_path, token, { mode: 0o600 })
31-
}
32-
if (fs.existsSync(params.extension_path)) {
33-
fs.copyFileSync(token_path, path.join(params.extension_path, 'ws-token'))
34-
}
35-
3621
const args = [
3722
`--user-data-dir=${params.user_data_dir}`,
3823
`--load-extension=${params.extension_path}`,
@@ -185,8 +170,7 @@ export const handle_run_browser = async (
185170
launch_browser({
186171
executable_path: cached.path,
187172
user_data_dir,
188-
extension_path,
189-
cwc_dir
173+
extension_path
190174
})
191175
return
192176
}
@@ -280,8 +264,7 @@ export const handle_run_browser = async (
280264
launch_browser({
281265
executable_path,
282266
user_data_dir,
283-
extension_path,
284-
cwc_dir
267+
extension_path
285268
})
286269
} catch (error: any) {
287270
if (error.message === 'Download cancelled') {

0 commit comments

Comments
 (0)