@@ -4,6 +4,7 @@ import { describe, expect, it } from "vite-plus/test";
44import {
55 injectMcpAppCsp ,
66 injectMcpAppThemeBootstrap ,
7+ injectMcpAppCookieBootstrap ,
78 mcpAppContentSecurityPolicy ,
89 mcpAppToolCallableByApp ,
910 readMcpAppCsp ,
@@ -209,3 +210,61 @@ describe("mcpAppFromToolItem", () => {
209210 expect ( output ?. t3McpApp ?. server ) . toBe ( "weather" ) ;
210211 } ) ;
211212} ) ;
213+
214+ describe ( "MCP App cookie bootstrap" , ( ) => {
215+ const vendor = `<script>document.cookie.split(";");document.cookie="analytics=1";document.root="rendered";</script>` ;
216+ const execute = ( html : string , document : object ) => {
217+ for ( const script of html . matchAll ( / < s c r i p t > ( [ \s \S ] * ?) < \/ s c r i p t > / g) ) {
218+ NodeVM . runInNewContext ( script [ 1 ] ! , { document } ) ;
219+ }
220+ } ;
221+
222+ it ( "lets cookie-reading startup code render in an opaque document without granting storage" , ( ) => {
223+ const blocked = new DOMException ( "Opaque origin" , "SecurityError" ) ;
224+ const prototype = Object . defineProperty ( { } , "cookie" , {
225+ get ( ) {
226+ throw blocked ;
227+ } ,
228+ set ( ) {
229+ throw blocked ;
230+ } ,
231+ } ) ;
232+ const document = Object . assign (
233+ Object . create ( prototype ) as { cookie : string ; root : string ; indexedDB : unknown } ,
234+ { root : "" } ,
235+ ) ;
236+ Object . defineProperty ( document , "indexedDB" , {
237+ get ( ) {
238+ throw blocked ;
239+ } ,
240+ } ) ;
241+ const saved = injectMcpAppCsp ( vendor , { connectDomains : [ "https://analytics.example" ] } ) ;
242+ const html = injectMcpAppCookieBootstrap ( saved ) ;
243+ expect ( html . indexOf ( "Content-Security-Policy" ) ) . toBeLessThan ( html . indexOf ( "<script>" ) ) ;
244+ expect ( html ) . toContain ( "connect-src https://analytics.example" ) ;
245+ execute ( html , document ) ;
246+ expect ( document . root ) . toBe ( "rendered" ) ;
247+ expect ( document . cookie ) . toBe ( "" ) ;
248+ document . cookie = "session=secret" ;
249+ expect ( document . cookie ) . toBe ( "" ) ;
250+ expect ( ( ) => document . indexedDB ) . toThrow ( blocked ) ;
251+ expect ( ( ) => Reflect . get ( prototype , "cookie" ) ) . toThrow ( blocked ) ;
252+ } ) ;
253+
254+ it ( "leaves functioning cookie reads and writes unchanged" , ( ) => {
255+ const document = { cookie : "existing=1" , root : "" } ;
256+ execute ( injectMcpAppCookieBootstrap ( vendor ) , document ) ;
257+ expect ( document ) . toEqual ( { cookie : "analytics=1" , root : "rendered" } ) ;
258+ } ) ;
259+
260+ it ( "leaves other cookie failures unchanged" , ( ) => {
261+ const failure = new TypeError ( "Unexpected cookie failure" ) ;
262+ const document = Object . defineProperty ( { } , "cookie" , {
263+ get ( ) {
264+ throw failure ;
265+ } ,
266+ } ) ;
267+ expect ( ( ) => execute ( injectMcpAppCookieBootstrap ( vendor ) , document ) ) . toThrow ( failure ) ;
268+ expect ( ( ) => Reflect . get ( document , "cookie" ) ) . toThrow ( failure ) ;
269+ } ) ;
270+ } ) ;
0 commit comments