Skip to content

Commit 35b1af4

Browse files
committed
fix(core): move auth logic from rpc package to core
1 parent 24d0032 commit 35b1af4

File tree

4 files changed

+30
-34
lines changed

4 files changed

+30
-34
lines changed

packages/core/src/node/ws.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ import type { ConnectionMeta, DevToolsNodeContext, DevToolsNodeRpcSession, DevTo
33
import type { WebSocket } from 'ws'
44
import type { RpcFunctionsHost } from './host-functions'
55
import { AsyncLocalStorage } from 'node:async_hooks'
6+
import process from 'node:process'
67
import { createRpcServer } from '@vitejs/devtools-rpc'
78
import { createWsRpcPreset } from '@vitejs/devtools-rpc/presets/ws/server'
89
import c from 'ansis'
910
import { getPort } from 'get-port-please'
1011
import { MARK_CHECK } from './constants'
12+
import { getInternalContext } from './context-internal'
1113

1214
export interface CreateWsServerOptions {
1315
cwd: string
@@ -26,11 +28,28 @@ export async function createWsServer(options: CreateWsServerOptions) {
2628

2729
const wsClients = new Set<WebSocket>()
2830

31+
const context = options.context
32+
const contextInternal = getInternalContext(context)
33+
34+
const isClientAuthDisabled = context.mode === 'build' || context.viteConfig.devtools?.clientAuth === false || process.env.VITE_DEVTOOLS_DISABLE_CLIENT_AUTH === 'true'
35+
if (isClientAuthDisabled) {
36+
console.warn('[Vite DevTools] Client authentication is disabled. Any browser can connect to the devtools and access to your server and filesystem.')
37+
}
38+
2939
const preset = createWsRpcPreset({
3040
port,
3141
host,
32-
context: options.context,
33-
onConnected: (ws, meta) => {
42+
onConnected: (ws, req, meta) => {
43+
const url = new URL(req.url ?? '', 'http://localhost')
44+
const authId = url.searchParams.get('vite_devtools_auth_id') ?? undefined
45+
if (isClientAuthDisabled) {
46+
meta.isTrusted = true
47+
}
48+
else if (authId && contextInternal.storage.auth.get().trusted[authId]) {
49+
meta.isTrusted = true
50+
meta.clientAuthId = authId
51+
}
52+
3453
wsClients.add(ws)
3554
console.log(c.green`${MARK_CHECK} Websocket client [${meta.id}] connected`)
3655
},

packages/rpc/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
}
4343
},
4444
"dependencies": {
45+
"@vitejs/devtools-kit": "workspace:*",
4546
"birpc": "catalog:deps",
4647
"structured-clone-es": "catalog:deps"
4748
},

packages/rpc/src/presets/ws/server.ts

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
import type { DevToolsNodeContext, DevToolsNodeRpcSessionMeta } from '@vitejs/devtools-kit'
1+
import type { DevToolsNodeRpcSessionMeta } from '@vitejs/devtools-kit'
22
import type { BirpcGroup, BirpcOptions, ChannelOptions } from 'birpc'
3+
import type { IncomingMessage } from 'node:http'
34
import type { WebSocket } from 'ws'
45
import type { RpcServerPreset } from '..'
5-
import process from 'node:process'
66
import { parse, stringify } from 'structured-clone-es'
77
import { WebSocketServer } from 'ws'
88
import { defineRpcServerPreset } from '..'
9-
import { getInternalContext } from '../../../../core/src/node/context-internal'
109

1110
export interface WebSocketRpcServerOptions {
1211
port: number
1312
host?: string
14-
context: DevToolsNodeContext
15-
onConnected?: (ws: WebSocket, meta: DevToolsNodeRpcSessionMeta) => void
13+
onConnected?: (ws: WebSocket, req: IncomingMessage, meta: DevToolsNodeRpcSessionMeta) => void
1614
onDisconnected?: (ws: WebSocket, meta: DevToolsNodeRpcSessionMeta) => void
1715
}
1816

@@ -35,16 +33,8 @@ export const createWsRpcPreset: RpcServerPreset<
3533
host = 'localhost',
3634
onConnected = NOOP,
3735
onDisconnected = NOOP,
38-
context,
3936
} = options
4037

41-
const isClientAuthDisabled = context.mode === 'build' || context.viteConfig.devtools?.clientAuth === false || process.env.VITE_DEVTOOLS_DISABLE_CLIENT_AUTH === 'true'
42-
if (isClientAuthDisabled) {
43-
console.warn('[Vite DevTools] Client authentication is disabled. Any browser can connect to the devtools and access to your server and filesystem.')
44-
}
45-
46-
const internal = getInternalContext(context)
47-
4838
const wss = new WebSocketServer({
4939
port,
5040
host,
@@ -60,21 +50,9 @@ export const createWsRpcPreset: RpcServerPreset<
6050
} = options ?? {}
6151

6252
wss.on('connection', (ws, req) => {
63-
const url = new URL(req.url ?? '', 'http://localhost')
64-
const authId = url.searchParams.get('vite_devtools_auth_id') ?? undefined
65-
let isTrusted = false
66-
if (isClientAuthDisabled) {
67-
isTrusted = true
68-
}
69-
else if (authId && internal.storage.auth.get().trusted[authId]) {
70-
isTrusted = true
71-
}
72-
7353
const meta: DevToolsNodeRpcSessionMeta = {
7454
id: id++,
7555
ws,
76-
isTrusted,
77-
clientAuthId: authId,
7856
}
7957

8058
const channel: ChannelOptions = {
@@ -105,7 +83,7 @@ export const createWsRpcPreset: RpcServerPreset<
10583
})
10684
onDisconnected(ws, meta)
10785
})
108-
onConnected(ws, meta)
86+
onConnected(ws, req, meta)
10987
})
11088
}
11189
})

pnpm-lock.yaml

Lines changed: 4 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)