@@ -66,45 +66,47 @@ export function resolvePluginIconRoutePrefix(basePath?: string): string {
6666 return `${ normalizeBasePath ( basePath ) } ${ CONTROL_UI_PLUGIN_ICON_PATH_PREFIX } /` ;
6767}
6868
69- function parsePluginIconRequest ( urlRaw : string | undefined , basePath ?: string ) : string | null {
69+ type IconRequestPathResolution = { matched : false } | { matched : true ; value : string | null } ;
70+
71+ function parseIconRequestPath (
72+ urlRaw : string | undefined ,
73+ prefix : string ,
74+ isValid : ( value : string ) => boolean = Boolean ,
75+ ) : IconRequestPathResolution {
7076 if ( ! urlRaw ) {
71- return null ;
77+ return { matched : false } ;
7278 }
7379 const pathname = new URL ( urlRaw , "http://localhost" ) . pathname ;
74- const prefix = resolvePluginIconRoutePrefix ( basePath ) ;
7580 if ( ! pathname . startsWith ( prefix ) ) {
76- return null ;
81+ return { matched : false } ;
7782 }
78- const encodedPluginId = pathname . slice ( prefix . length ) ;
79- if ( ! encodedPluginId || encodedPluginId . includes ( "/" ) ) {
80- return null ;
83+ const encodedValue = pathname . slice ( prefix . length ) ;
84+ if ( ! encodedValue || encodedValue . includes ( "/" ) ) {
85+ return { matched : true , value : null } ;
8186 }
8287 try {
83- const pluginId = decodeURIComponent ( encodedPluginId ) ;
84- return PLUGIN_ID_RE . test ( pluginId ) ? pluginId : null ;
88+ const value = decodeURIComponent ( encodedValue ) ;
89+ return { matched : true , value : isValid ( value ) ? value : null } ;
8590 } catch {
86- return null ;
91+ return { matched : true , value : null } ;
8792 }
8893}
8994
90- function parseCatalogIconRequest ( urlRaw : string | undefined , basePath ?: string ) : string | null {
91- if ( ! urlRaw ) {
92- return null ;
93- }
94- const pathname = new URL ( urlRaw , "http://localhost" ) . pathname ;
95+ function parsePluginIconRequest (
96+ urlRaw : string | undefined ,
97+ basePath ?: string ,
98+ ) : IconRequestPathResolution {
99+ return parseIconRequestPath ( urlRaw , resolvePluginIconRoutePrefix ( basePath ) , ( value ) =>
100+ PLUGIN_ID_RE . test ( value ) ,
101+ ) ;
102+ }
103+
104+ function parseCatalogIconRequest (
105+ urlRaw : string | undefined ,
106+ basePath ?: string ,
107+ ) : IconRequestPathResolution {
95108 const prefix = `${ normalizeBasePath ( basePath ) } ${ CONTROL_UI_CATALOG_ICON_PATH_PREFIX } /` ;
96- if ( ! pathname . startsWith ( prefix ) ) {
97- return null ;
98- }
99- const encodedIconUrl = pathname . slice ( prefix . length ) ;
100- if ( ! encodedIconUrl || encodedIconUrl . includes ( "/" ) ) {
101- return null ;
102- }
103- try {
104- return decodeURIComponent ( encodedIconUrl ) || null ;
105- } catch {
106- return null ;
107- }
109+ return parseIconRequestPath ( urlRaw , prefix ) ;
108110}
109111
110112function normalizeMimeType ( contentType : string | undefined ) : string | undefined {
@@ -259,11 +261,13 @@ export async function handlePluginIconHttpRequest(
259261 rateLimiter ?: AuthRateLimiter ;
260262 } ,
261263) : Promise < boolean > {
262- const pluginId = parsePluginIconRequest ( req . url , opts . basePath ) ;
263- const catalogIconUrl = parseCatalogIconRequest ( req . url , opts . basePath ) ;
264- if ( ! pluginId && ! catalogIconUrl ) {
264+ const pluginRequest = parsePluginIconRequest ( req . url , opts . basePath ) ;
265+ const catalogRequest = parseCatalogIconRequest ( req . url , opts . basePath ) ;
266+ if ( ! pluginRequest . matched && ! catalogRequest . matched ) {
265267 return false ;
266268 }
269+ const pluginId = pluginRequest . matched ? pluginRequest . value : null ;
270+ const catalogIconUrl = catalogRequest . matched ? catalogRequest . value : null ;
267271 const method = req . method ;
268272 if ( method !== "GET" && method !== "HEAD" ) {
269273 sendMethodNotAllowed ( res , "GET, HEAD" ) ;
0 commit comments