@@ -39,6 +39,7 @@ import {
3939} from "./auth-rate-limit.js" ;
4040import { authorizeHttpGatewayConnect , type ResolvedGatewayAuth } from "./auth.js" ;
4141import {
42+ CONTROL_UI_BASE_PATH_ATTRIBUTE ,
4243 CONTROL_UI_BOOTSTRAP_CONFIG_PATH ,
4344 CONTROL_UI_TERMINAL_ENABLED_ATTRIBUTE ,
4445 type ControlUiBootstrapConfig ,
@@ -187,6 +188,15 @@ function rewriteControlUiIndexHtmlPublicAssetHrefs(html: string, basePath: strin
187188 return next ;
188189}
189190
191+ function escapeHtmlAttribute ( value : string ) : string {
192+ return value
193+ . replaceAll ( "&" , "&" )
194+ . replaceAll ( '"' , """ )
195+ . replaceAll ( "<" , "<" )
196+ . replaceAll ( ">" , ">" )
197+ . replaceAll ( "'" , "'" ) ;
198+ }
199+
190200type ControlUiAvatarResolution =
191201 | { kind : "none" ; reason : string ; source ?: string | null }
192202 | { kind : "local" ; filePath : string ; source ?: string | null }
@@ -788,12 +798,16 @@ function serveResolvedIndexHtml(
788798 basePath ?: string ,
789799 allowWasm ?: boolean ,
790800) {
791- const withBasePath = rewriteControlUiIndexHtmlPublicAssetHrefs ( body , basePath ?? "" ) ;
801+ const normalizedBasePath = normalizeControlUiBasePath ( basePath ) ;
802+ const withBasePath = rewriteControlUiIndexHtmlPublicAssetHrefs ( body , normalizedBasePath ) ;
803+ const basePathAttribute = normalizedBasePath
804+ ? ` ${ CONTROL_UI_BASE_PATH_ATTRIBUTE } ="${ escapeHtmlAttribute ( normalizedBasePath ) } "`
805+ : "" ;
792806 // Let the app initialize fail-closed without guessing whether this document
793807 // was served with the terminal's WASM CSP allowance.
794808 const prepared = withBasePath . replace (
795809 / < h t m l \b / i,
796- `<html ${ CONTROL_UI_TERMINAL_ENABLED_ATTRIBUTE } ="${ allowWasm === true } "` ,
810+ `<html${ basePathAttribute } ${ CONTROL_UI_TERMINAL_ENABLED_ATTRIBUTE } ="${ allowWasm === true } "` ,
797811 ) ;
798812 const hashes = computeInlineScriptHashes ( prepared ) ;
799813 // Always set the document CSP here (the index carries inline scripts) so the
0 commit comments