diff --git a/packages/core-app-api/src/apis/implementations/FetchApi/BackstageProtocolResolverFetchMiddleware.ts b/packages/core-app-api/src/apis/implementations/FetchApi/BackstageProtocolResolverFetchMiddleware.ts index 03d08c3096..2144d0058d 100644 --- a/packages/core-app-api/src/apis/implementations/FetchApi/BackstageProtocolResolverFetchMiddleware.ts +++ b/packages/core-app-api/src/apis/implementations/FetchApi/BackstageProtocolResolverFetchMiddleware.ts @@ -43,13 +43,18 @@ export class BackstageProtocolResolverFetchMiddleware apply(next: FetchFunction): FetchFunction { return async (input, init) => { const request = new Request(input, init); - const { protocol, hostname, pathname, search, hash, username, password } = - new URL(request.url); + const prefix = 'backstage://'; - if (protocol !== 'backstage:') { + if (!request.url.startsWith(prefix)) { return next(input, init); } + // Switch to a known protocol, since browser URL parsing misbehaves wildly + // on foreign protocols + const { hostname, pathname, search, hash, username, password } = new URL( + `http://${request.url.substring(prefix.length)}`, + ); + let base = await this.discovery(hostname); if (username || password) { const baseUrl = new URL(base);