Skip to content

Commit ac3e080

Browse files
committed
Secure WebSocket server authentication with authorization headers and challenge-response verification
- is it valid feedback? [Pasted text] ├── apps │ ├── browser │ │ └── src │ │ └── background │ │ └── websocket.ts │ └── editor │ ├── src │ │ ├── extension.ts │ │ ├── services │ │ │ ├── websocket-manager.ts │ │ │ └── websocket-server-process.ts │ │ └── utils │ │ └── shared-token.ts │ └── webpack.config.ts ├── docs │ └── privacy.mdx └── packages └── shared └── src ├── constants │ └── websocket.ts └── types └── websocket-message.ts
1 parent 9b69e1a commit ac3e080

2 files changed

Lines changed: 71 additions & 15 deletions

File tree

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

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as child_process from 'child_process'
22
import * as http from 'http'
33
import * as net from 'net'
44
import * as path from 'path'
5+
import * as crypto from 'crypto'
56
import * as WebSocket from 'ws'
67
import * as vscode from 'vscode'
78
import {
@@ -115,15 +116,37 @@ export class WebSocketManager {
115116
): Promise<void> {
116117
return new Promise((resolve, reject) => {
117118
let attempts = 0
119+
const challenge = crypto.randomBytes(16).toString('hex')
120+
const token = get_or_create_shared_token()
121+
const expected_signature = crypto
122+
.createHmac('sha256', token)
123+
.update(challenge)
124+
.digest('hex')
118125

119126
const check = () => {
120-
const req = http.get(`http://localhost:${this.port}/health`, (res) => {
121-
if (res.statusCode === 200) {
122-
resolve()
123-
} else {
124-
retry()
127+
const req = http.get(
128+
`http://localhost:${this.port}/health?challenge=${challenge}`,
129+
(res) => {
130+
let data = ''
131+
res.on('data', (chunk) => {
132+
data += chunk
133+
})
134+
res.on('end', () => {
135+
if (res.statusCode === 200) {
136+
try {
137+
const json = JSON.parse(data)
138+
if (json.signature === expected_signature) {
139+
resolve()
140+
return
141+
}
142+
} catch (e) {
143+
// Ignore parse errors and fall through to retry
144+
}
145+
}
146+
retry()
147+
})
125148
}
126-
})
149+
)
127150
req.on('error', retry)
128151
req.end()
129152
}
@@ -166,11 +189,26 @@ export class WebSocketManager {
166189
this._schedule_reconnect()
167190
return
168191
}
192+
} else {
193+
// If the port was already in use, verify it's our server before connecting
194+
try {
195+
await this._wait_for_server_ready(1, 0)
196+
} catch (error) {
197+
Logger.error({
198+
function_name: '_connect_as_client',
199+
message: 'Existing server on port failed ownership verification',
200+
data: error
201+
})
202+
this._schedule_reconnect()
203+
return
204+
}
169205
}
170206

171207
const token = get_or_create_shared_token()
172-
const ws_url = `ws://localhost:${this.port}?client_type=${CLIENT_TYPE.EDITOR}&token=${token}`
173-
this.client = new WebSocket.WebSocket(ws_url)
208+
const ws_url = `ws://localhost:${this.port}?client_type=${CLIENT_TYPE.EDITOR}`
209+
this.client = new WebSocket.WebSocket(ws_url, {
210+
headers: { Authorization: `Bearer ${token}` }
211+
})
174212

175213
this.client.on('open', () => {
176214
Logger.info({

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

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,12 @@ class WebSocketServer {
6969

7070
if (client_type == CLIENT_TYPE.EDITOR) {
7171
// 3. Editors: Enforce the shared file token
72-
const provided_token = url.searchParams.get('token')
72+
const auth_header = info.req.headers.authorization
73+
let provided_token: string | null = null
74+
if (auth_header && auth_header.startsWith('Bearer ')) {
75+
provided_token = auth_header.substring(7)
76+
}
77+
7378
const {
7479
get_or_create_shared_token
7580
// eslint-disable-next-line @typescript-eslint/no-var-requires
@@ -122,13 +127,26 @@ class WebSocketServer {
122127
return
123128
}
124129

125-
if (req.url == '/health') {
130+
const url = new URL(req.url || '', `http://localhost:${DEFAULT_PORT}`)
131+
if (url.pathname == '/health') {
132+
const challenge = url.searchParams.get('challenge')
133+
const response: any = { status: 'ok' }
134+
135+
if (challenge) {
136+
// eslint-disable-next-line @typescript-eslint/no-var-requires
137+
const {
138+
get_or_create_shared_token
139+
} = require('../utils/shared-token')
140+
const token = get_or_create_shared_token()
141+
const signature = crypto
142+
.createHmac('sha256', token)
143+
.update(challenge)
144+
.digest('hex')
145+
response.signature = signature
146+
}
147+
126148
res.writeHead(200, { 'Content-Type': 'application/json' })
127-
res.end(
128-
JSON.stringify({
129-
status: 'ok'
130-
})
131-
)
149+
res.end(JSON.stringify(response))
132150
return
133151
}
134152

0 commit comments

Comments
 (0)