@@ -367,6 +367,41 @@ describe("bundle LSP runtime", () => {
367367 await runtime . dispose ( ) ;
368368 } ) ;
369369
370+ it ( "rejects invalid UTF-8 in LSP JSON bodies" , async ( ) => {
371+ configureSingleLspServer ( ) ;
372+ const child = new MockChildProcess ( "" , new Set ( [ "initialize" ] ) ) ;
373+ spawnMock . mockReturnValue ( child ) ;
374+
375+ const runtime = await createBundleLspToolRuntime ( { workspaceDir : "/tmp/workspace" } ) ;
376+ const hoverTool = runtime . tools . find ( ( tool ) => tool . name === "lsp_hover_typescript" ) ;
377+ if ( ! hoverTool ) {
378+ throw new Error ( "expected hover tool" ) ;
379+ }
380+ const request = hoverTool . execute ( "call-1" , {
381+ uri : "file:///tmp/workspace/index.ts" ,
382+ line : 0 ,
383+ character : 0 ,
384+ } ) ;
385+ const hoverRequest = child . receivedMessages . find (
386+ ( message ) => message . method === "textDocument/hover" ,
387+ ) ;
388+ if ( typeof hoverRequest ?. id !== "number" ) {
389+ throw new Error ( "expected numeric hover request id" ) ;
390+ }
391+ const body = Buffer . concat ( [
392+ Buffer . from ( `{"jsonrpc":"2.0","id":${ hoverRequest . id } ,"result":{"contents":"` ) ,
393+ Buffer . from ( [ 0xff ] ) ,
394+ Buffer . from ( '"}}' ) ,
395+ ] ) ;
396+ const header = Buffer . from ( `Content-Length: ${ body . length } \r\n\r\n` , "ascii" ) ;
397+ child . stdout . write ( Buffer . concat ( [ header , body ] ) ) ;
398+
399+ await expect ( request ) . rejects . toThrow ( / L S P f r a m i n g e r r o r : b o d y i s n o t v a l i d U T F - 8 / i) ;
400+ expect ( killProcessTreeMock ) . toHaveBeenCalledWith ( 4321 , { graceMs : 1000 , detached : true } ) ;
401+
402+ await runtime . dispose ( ) ;
403+ } ) ;
404+
370405 it . each ( [
371406 {
372407 name : "a suffixed Content-Length value" ,
0 commit comments