Added a verification for URLs in proxy backend

This commit is contained in:
Nir Gazit
2021-03-02 10:34:02 +02:00
parent d7a5bfd57f
commit 1987c93416
3 changed files with 23 additions and 0 deletions
+5
View File
@@ -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) {