Skip to content

Commit c5191e6

Browse files
authored
fix(core): Account for double quotes in instance base URL (n8n-io#11495)
1 parent aa7bb3a commit c5191e6

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import type { GlobalConfig } from '@n8n/config';
2+
import { mock } from 'jest-mock-extended';
3+
4+
import config from '@/config';
5+
6+
import { UrlService } from '../url.service';
7+
8+
describe('UrlService', () => {
9+
beforeEach(() => {
10+
process.env.WEBHOOK_URL = undefined;
11+
config.load(config.default);
12+
});
13+
14+
describe('getInstanceBaseUrl', () => {
15+
it('should set URL from N8N_EDITOR_BASE_URL', () => {
16+
config.set('editorBaseUrl', 'https://example.com/');
17+
process.env.WEBHOOK_URL = undefined;
18+
const urlService = new UrlService(mock<GlobalConfig>());
19+
expect(urlService.getInstanceBaseUrl()).toBe('https://example.com');
20+
});
21+
22+
it('should set URL from WEBHOOK_URL', () => {
23+
config.set('editorBaseUrl', '');
24+
process.env.WEBHOOK_URL = 'https://example.com/';
25+
const urlService = new UrlService(mock<GlobalConfig>());
26+
expect(urlService.getInstanceBaseUrl()).toBe('https://example.com');
27+
});
28+
29+
it('should trim quotes when setting URL from N8N_EDITOR_BASE_URL', () => {
30+
config.set('editorBaseUrl', '"https://example.com"');
31+
process.env.WEBHOOK_URL = undefined;
32+
const urlService = new UrlService(mock<GlobalConfig>());
33+
expect(urlService.getInstanceBaseUrl()).toBe('https://example.com');
34+
});
35+
36+
it('should trim quotes when setting URL from WEBHOOK_URL', () => {
37+
config.set('editorBaseUrl', '');
38+
process.env.WEBHOOK_URL = '"https://example.com/"';
39+
const urlService = new UrlService(mock<GlobalConfig>());
40+
expect(urlService.getInstanceBaseUrl()).toBe('https://example.com');
41+
});
42+
});
43+
});

packages/cli/src/services/url.service.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export class UrlService {
1414

1515
/** Returns the base URL of the webhooks */
1616
getWebhookBaseUrl() {
17-
let urlBaseWebhook = process.env.WEBHOOK_URL ?? this.baseUrl;
17+
let urlBaseWebhook = this.trimQuotes(process.env.WEBHOOK_URL) || this.baseUrl;
1818
if (!urlBaseWebhook.endsWith('/')) {
1919
urlBaseWebhook += '/';
2020
}
@@ -23,7 +23,7 @@ export class UrlService {
2323

2424
/** Return the n8n instance base URL without trailing slash */
2525
getInstanceBaseUrl(): string {
26-
const n8nBaseUrl = config.getEnv('editorBaseUrl') || this.getWebhookBaseUrl();
26+
const n8nBaseUrl = this.trimQuotes(config.getEnv('editorBaseUrl')) || this.getWebhookBaseUrl();
2727

2828
return n8nBaseUrl.endsWith('/') ? n8nBaseUrl.slice(0, n8nBaseUrl.length - 1) : n8nBaseUrl;
2929
}
@@ -36,4 +36,9 @@ export class UrlService {
3636
}
3737
return `${protocol}://${host}:${port}${path}`;
3838
}
39+
40+
/** Remove leading and trailing double quotes from a URL. */
41+
private trimQuotes(url?: string) {
42+
return url?.replace(/^["]+|["]+$/g, '') ?? '';
43+
}
3944
}

0 commit comments

Comments
 (0)