mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-14 08:34:07 -08:00
fix(core): Account for double quotes in instance base URL (#11495)
This commit is contained in:
parent
aa7bb3ac08
commit
c5191e697a
43
packages/cli/src/services/__tests__/url.service.test.ts
Normal file
43
packages/cli/src/services/__tests__/url.service.test.ts
Normal file
|
@ -0,0 +1,43 @@
|
|||
import type { GlobalConfig } from '@n8n/config';
|
||||
import { mock } from 'jest-mock-extended';
|
||||
|
||||
import config from '@/config';
|
||||
|
||||
import { UrlService } from '../url.service';
|
||||
|
||||
describe('UrlService', () => {
|
||||
beforeEach(() => {
|
||||
process.env.WEBHOOK_URL = undefined;
|
||||
config.load(config.default);
|
||||
});
|
||||
|
||||
describe('getInstanceBaseUrl', () => {
|
||||
it('should set URL from N8N_EDITOR_BASE_URL', () => {
|
||||
config.set('editorBaseUrl', 'https://example.com/');
|
||||
process.env.WEBHOOK_URL = undefined;
|
||||
const urlService = new UrlService(mock<GlobalConfig>());
|
||||
expect(urlService.getInstanceBaseUrl()).toBe('https://example.com');
|
||||
});
|
||||
|
||||
it('should set URL from WEBHOOK_URL', () => {
|
||||
config.set('editorBaseUrl', '');
|
||||
process.env.WEBHOOK_URL = 'https://example.com/';
|
||||
const urlService = new UrlService(mock<GlobalConfig>());
|
||||
expect(urlService.getInstanceBaseUrl()).toBe('https://example.com');
|
||||
});
|
||||
|
||||
it('should trim quotes when setting URL from N8N_EDITOR_BASE_URL', () => {
|
||||
config.set('editorBaseUrl', '"https://example.com"');
|
||||
process.env.WEBHOOK_URL = undefined;
|
||||
const urlService = new UrlService(mock<GlobalConfig>());
|
||||
expect(urlService.getInstanceBaseUrl()).toBe('https://example.com');
|
||||
});
|
||||
|
||||
it('should trim quotes when setting URL from WEBHOOK_URL', () => {
|
||||
config.set('editorBaseUrl', '');
|
||||
process.env.WEBHOOK_URL = '"https://example.com/"';
|
||||
const urlService = new UrlService(mock<GlobalConfig>());
|
||||
expect(urlService.getInstanceBaseUrl()).toBe('https://example.com');
|
||||
});
|
||||
});
|
||||
});
|
|
@ -14,7 +14,7 @@ export class UrlService {
|
|||
|
||||
/** Returns the base URL of the webhooks */
|
||||
getWebhookBaseUrl() {
|
||||
let urlBaseWebhook = process.env.WEBHOOK_URL ?? this.baseUrl;
|
||||
let urlBaseWebhook = this.trimQuotes(process.env.WEBHOOK_URL) || this.baseUrl;
|
||||
if (!urlBaseWebhook.endsWith('/')) {
|
||||
urlBaseWebhook += '/';
|
||||
}
|
||||
|
@ -23,7 +23,7 @@ export class UrlService {
|
|||
|
||||
/** Return the n8n instance base URL without trailing slash */
|
||||
getInstanceBaseUrl(): string {
|
||||
const n8nBaseUrl = config.getEnv('editorBaseUrl') || this.getWebhookBaseUrl();
|
||||
const n8nBaseUrl = this.trimQuotes(config.getEnv('editorBaseUrl')) || this.getWebhookBaseUrl();
|
||||
|
||||
return n8nBaseUrl.endsWith('/') ? n8nBaseUrl.slice(0, n8nBaseUrl.length - 1) : n8nBaseUrl;
|
||||
}
|
||||
|
@ -36,4 +36,9 @@ export class UrlService {
|
|||
}
|
||||
return `${protocol}://${host}:${port}${path}`;
|
||||
}
|
||||
|
||||
/** Remove leading and trailing double quotes from a URL. */
|
||||
private trimQuotes(url?: string) {
|
||||
return url?.replace(/^["]+|["]+$/g, '') ?? '';
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue