Added a verification for URLs in proxy backend
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
---
|
||||
'@backstage/plugin-proxy-backend': patch
|
||||
---
|
||||
|
||||
Added a verification for well formed URLs when processing proxy targets. Otherwise users gets a cryptic error message thrown from Express which makes it hard to debug.
|
||||
@@ -268,4 +268,13 @@ describe('buildMiddleware', () => {
|
||||
|
||||
expect(Object.keys(testClientResponse.headers!)).toEqual(['set-cookie']);
|
||||
});
|
||||
|
||||
it('rejects malformed target URLs', async () => {
|
||||
expect(() =>
|
||||
buildMiddleware('/api/', logger, 'test', 'backstage.io'),
|
||||
).toThrowError(/Proxy target is not a valid URL/);
|
||||
expect(() =>
|
||||
buildMiddleware('/api/', logger, 'test', { target: 'backstage.io' }),
|
||||
).toThrowError(/Proxy target is not a valid URL/);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -68,6 +68,15 @@ export function buildMiddleware(
|
||||
const fullConfig =
|
||||
typeof config === 'string' ? { target: config } : { ...config };
|
||||
|
||||
// Validate that target is a valid URL.
|
||||
try {
|
||||
// eslint-disable-next-line no-new
|
||||
new URL(fullConfig.target!);
|
||||
} catch {
|
||||
throw new Error(
|
||||
`Proxy target is not a valid URL: ${fullConfig.target ?? ''}`,
|
||||
);
|
||||
}
|
||||
// Default is to do a path rewrite that strips out the proxy's path prefix
|
||||
// and the rest of the route.
|
||||
if (fullConfig.pathRewrite === undefined) {
|
||||
|
||||
Reference in New Issue
Block a user