11import type { IncomingMessage } from "node:http" ;
2+ import type { AddressInfo } from "node:net" ;
23import { describe , expect , it } from "vitest" ;
34import { buildMcpAppSandboxPath } from "../agents/mcp-app-sandbox.js" ;
45import { createSandboxHostHttpServer } from "./mcp-app-sandbox-http.js" ;
@@ -12,6 +13,25 @@ function request(url: string, method: "GET" | "HEAD" | "POST" = "GET") {
1213 return { res, end, setHeader } ;
1314}
1415
16+ async function withSandboxHost ( run : ( origin : string ) => Promise < void > ) : Promise < void > {
17+ const server = createSandboxHostHttpServer ( ) ;
18+ await new Promise < void > ( ( resolve , reject ) => {
19+ server . once ( "error" , reject ) ;
20+ server . listen ( 0 , "127.0.0.1" , ( ) => {
21+ server . off ( "error" , reject ) ;
22+ resolve ( ) ;
23+ } ) ;
24+ } ) ;
25+ const address = server . address ( ) as AddressInfo ;
26+ try {
27+ await run ( `http://127.0.0.1:${ address . port } ` ) ;
28+ } finally {
29+ await new Promise < void > ( ( resolve , reject ) => {
30+ server . close ( ( error ) => ( error ? reject ( error ) : resolve ( ) ) ) ;
31+ } ) ;
32+ }
33+ }
34+
1535describe ( "MCP App sandbox HTTP origin" , ( ) => {
1636 it ( "serves only the proxy endpoint with metadata-derived CSP" , ( ) => {
1737 const result = request (
@@ -80,4 +100,50 @@ describe("MCP App sandbox HTTP origin", () => {
80100 expect . stringContaining ( "connect-src https://xn--bcher-kva.example" ) ,
81101 ) ;
82102 } ) ;
103+
104+ it . each ( [
105+ {
106+ label : "sandbox HTML" ,
107+ path : buildMcpAppSandboxPath ( ) ,
108+ statusCode : 200 ,
109+ } ,
110+ {
111+ label : "missing path" ,
112+ path : "/missing" ,
113+ statusCode : 404 ,
114+ } ,
115+ {
116+ label : "malformed policy" ,
117+ path : `${ buildMcpAppSandboxPath ( ) } ?csp=not-json` ,
118+ statusCode : 400 ,
119+ } ,
120+ ] ) (
121+ "keeps GET and HEAD representation metadata aligned for $label" ,
122+ async ( { path, statusCode } ) => {
123+ await withSandboxHost ( async ( origin ) => {
124+ const get = await fetch ( `${ origin } ${ path } ` ) ;
125+ const head = await fetch ( `${ origin } ${ path } ` , { method : "HEAD" } ) ;
126+ const getBody = await get . text ( ) ;
127+
128+ expect ( get . status ) . toBe ( statusCode ) ;
129+ expect ( head . status ) . toBe ( statusCode ) ;
130+ expect ( getBody ) . not . toBe ( "" ) ;
131+ expect ( await head . text ( ) ) . toBe ( "" ) ;
132+ expect ( get . headers . get ( "content-length" ) ) . toBe ( String ( Buffer . byteLength ( getBody ) ) ) ;
133+ for ( const header of [
134+ "content-type" ,
135+ "content-length" ,
136+ "cache-control" ,
137+ "content-security-policy" ,
138+ "permissions-policy" ,
139+ "cross-origin-resource-policy" ,
140+ "origin-agent-cluster" ,
141+ "referrer-policy" ,
142+ "x-content-type-options" ,
143+ ] ) {
144+ expect ( head . headers . get ( header ) , header ) . toBe ( get . headers . get ( header ) ) ;
145+ }
146+ } ) ;
147+ } ,
148+ ) ;
83149} ) ;
0 commit comments