Skip to content

Commit

Permalink
fix(core): Account for double quotes in instance base URL (#11495)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivov authored Nov 1, 2024
1 parent aa7bb3a commit c5191e6
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
43 changes: 43 additions & 0 deletions packages/cli/src/services/__tests__/url.service.test.ts
Original file line number Diff line number Diff line change
@@ -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');
});
});
});
9 changes: 7 additions & 2 deletions packages/cli/src/services/url.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 += '/';
}
Expand All @@ -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;
}
Expand All @@ -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, '') ?? '';
}
}

0 comments on commit c5191e6

Please sign in to comment.