@@ -236,6 +236,35 @@ function connectWs(port: number, token?: string): Promise<WebSocket> {
236236 } ) ;
237237}
238238
239+ function connectWsWithOrigin (
240+ port : number ,
241+ options : { token ?: string ; origin : string } ,
242+ ) : Promise < WebSocket > {
243+ return new Promise ( ( resolve , reject ) => {
244+ const query = options . token ? `?token=${ encodeURIComponent ( options . token ) } ` : "" ;
245+ const ws = new WebSocket ( `ws://127.0.0.1:${ port } /${ query } ` , {
246+ headers : {
247+ Origin : options . origin ,
248+ } ,
249+ } ) ;
250+ const pending : PendingMessages = { queue : [ ] , waiters : [ ] } ;
251+ pendingBySocket . set ( ws , pending ) ;
252+
253+ ws . on ( "message" , ( raw ) => {
254+ const parsed = JSON . parse ( String ( raw ) ) ;
255+ const waiter = pending . waiters . shift ( ) ;
256+ if ( waiter ) {
257+ waiter ( parsed ) ;
258+ return ;
259+ }
260+ pending . queue . push ( parsed ) ;
261+ } ) ;
262+
263+ ws . once ( "open" , ( ) => resolve ( ws ) ) ;
264+ ws . once ( "error" , ( ) => reject ( new Error ( "WebSocket connection failed" ) ) ) ;
265+ } ) ;
266+ }
267+
239268function waitForMessage ( ws : WebSocket ) : Promise < unknown > {
240269 const pending = pendingBySocket . get ( ws ) ;
241270 if ( ! pending ) {
@@ -388,6 +417,7 @@ describe("WebSocket Server", () => {
388417 logWebSocketEvents ?: boolean ;
389418 devUrl ?: string ;
390419 authToken ?: string ;
420+ host ?: string ;
391421 stateDir ?: string ;
392422 staticDir ?: string ;
393423 providerLayer ?: Layer . Layer < ProviderService , never > ;
@@ -414,7 +444,7 @@ describe("WebSocket Server", () => {
414444 const serverConfigLayer = Layer . succeed ( ServerConfig , {
415445 mode : "web" ,
416446 port : 0 ,
417- host : undefined ,
447+ host : options . host ?? "127.0.0.1" ,
418448 cwd : options . cwd ?? "/test/project" ,
419449 keybindingsConfigPath : path . join ( stateDir , "keybindings.json" ) ,
420450 stateDir,
@@ -1704,4 +1734,33 @@ describe("WebSocket Server", () => {
17041734 const welcome = ( await waitForMessage ( authorizedWs ) ) as WsPush ;
17051735 expect ( welcome . channel ) . toBe ( WS_CHANNELS . serverWelcome ) ;
17061736 } ) ;
1737+
1738+ it ( "rejects browser websocket upgrades from foreign origins" , async ( ) => {
1739+ server = await createTestServer ( { cwd : "/test" } ) ;
1740+ const addr = server . address ( ) ;
1741+ const port = typeof addr === "object" && addr !== null ? addr . port : 0 ;
1742+
1743+ await expect (
1744+ connectWsWithOrigin ( port , {
1745+ origin : "https://evil.example" ,
1746+ } ) ,
1747+ ) . rejects . toThrow ( "WebSocket connection failed" ) ;
1748+ } ) ;
1749+
1750+ it ( "accepts browser websocket upgrades from the configured dev origin" , async ( ) => {
1751+ server = await createTestServer ( {
1752+ cwd : "/test" ,
1753+ devUrl : "http://localhost:5173" ,
1754+ } ) ;
1755+ const addr = server . address ( ) ;
1756+ const port = typeof addr === "object" && addr !== null ? addr . port : 0 ;
1757+
1758+ const ws = await connectWsWithOrigin ( port , {
1759+ origin : "http://localhost:5173" ,
1760+ } ) ;
1761+ connections . push ( ws ) ;
1762+
1763+ const welcome = ( await waitForMessage ( ws ) ) as WsPush ;
1764+ expect ( welcome . channel ) . toBe ( WS_CHANNELS . serverWelcome ) ;
1765+ } ) ;
17071766} ) ;
0 commit comments