From 0610a7a8c30024204fbef570f7b7bfcbbec325e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E0=A4=95=E0=A4=BE=E0=A4=B0=E0=A4=A4=E0=A5=8B=E0=A4=AB?= =?UTF-8?q?=E0=A5=8D=E0=A4=AB=E0=A5=87=E0=A4=B2=E0=A4=B8=E0=A5=8D=E0=A4=95?= =?UTF-8?q?=E0=A5=8D=E0=A4=B0=E0=A4=BF=E0=A4=AA=E0=A5=8D=E0=A4=9F=E2=84=A2?= Date: Wed, 5 Apr 2023 18:00:55 +0200 Subject: [PATCH] fix(core): App should not crash with a custom rest endpoint fixes #5880 --- packages/cli/src/Server.ts | 7 ++++++- packages/cli/src/commands/start.ts | 2 ++ packages/editor-ui/index.html | 1 + packages/editor-ui/src/Interface.ts | 1 + packages/editor-ui/src/shims.d.ts | 2 +- packages/editor-ui/src/stores/n8nRootStore.ts | 15 ++++----------- 6 files changed, 15 insertions(+), 13 deletions(-) diff --git a/packages/cli/src/Server.ts b/packages/cli/src/Server.ts index 3a6ffaad6de483..e35648b1c0500b 100644 --- a/packages/cli/src/Server.ts +++ b/packages/cli/src/Server.ts @@ -10,7 +10,7 @@ /* eslint-disable @typescript-eslint/no-unused-vars */ /* eslint-disable @typescript-eslint/no-unsafe-member-access */ /* eslint-disable @typescript-eslint/no-unsafe-assignment */ - +import assert from 'assert'; import { exec as callbackExec } from 'child_process'; import { access as fsAccess } from 'fs/promises'; import os from 'os'; @@ -452,6 +452,11 @@ class Server extends AbstractServer { ...excludeEndpoints.split(':'), ].filter((u) => !!u); + assert( + !ignoredEndpoints.includes(this.restEndpoint), + `REST endpoint cannot be set to any of these values: ${ignoredEndpoints.join()} `, + ); + // eslint-disable-next-line no-useless-escape const authIgnoreRegex = new RegExp(`^\/(${ignoredEndpoints.join('|')})\/?.*$`); diff --git a/packages/cli/src/commands/start.ts b/packages/cli/src/commands/start.ts index db53900de23935..359c79d584d54e 100644 --- a/packages/cli/src/commands/start.ts +++ b/packages/cli/src/commands/start.ts @@ -144,6 +144,7 @@ export class Start extends BaseCommand { private async generateStaticAssets() { // Read the index file and replace the path placeholder const n8nPath = config.getEnv('path'); + const restEndpoint = config.getEnv('endpoints.rest'); const hooksUrls = config.getEnv('externalFrontendHooksUrls'); let scriptsString = ''; @@ -167,6 +168,7 @@ export class Start extends BaseCommand { ]; if (filePath.endsWith('index.html')) { streams.push( + replaceStream('{{REST_ENDPOINT}}', restEndpoint, { ignoreCase: false }), replaceStream(closingTitleTag, closingTitleTag + scriptsString, { ignoreCase: false, }), diff --git a/packages/editor-ui/index.html b/packages/editor-ui/index.html index 35923d36b5a520..12dbfaeb9dc064 100644 --- a/packages/editor-ui/index.html +++ b/packages/editor-ui/index.html @@ -7,6 +7,7 @@ diff --git a/packages/editor-ui/src/Interface.ts b/packages/editor-ui/src/Interface.ts index 659b5d97c73bee..38003cb123a90c 100644 --- a/packages/editor-ui/src/Interface.ts +++ b/packages/editor-ui/src/Interface.ts @@ -985,6 +985,7 @@ export interface WorkflowsState { export interface RootState { baseUrl: string; + restEndpoint: string; defaultLocale: string; endpointWebhook: string; endpointWebhookTest: string; diff --git a/packages/editor-ui/src/shims.d.ts b/packages/editor-ui/src/shims.d.ts index f26c77ce8f7205..c231305c43c1c3 100644 --- a/packages/editor-ui/src/shims.d.ts +++ b/packages/editor-ui/src/shims.d.ts @@ -11,12 +11,12 @@ declare global { PROD: boolean; NODE_ENV: 'development' | 'production'; VUE_APP_URL_BASE_API: string; - VUE_APP_ENDPOINT_REST?: string; }; } interface Window { BASE_PATH: string; + REST_ENDPOINT: string; } namespace JSX { diff --git a/packages/editor-ui/src/stores/n8nRootStore.ts b/packages/editor-ui/src/stores/n8nRootStore.ts index e8b72a4e7936ab..317fc746e8b4ef 100644 --- a/packages/editor-ui/src/stores/n8nRootStore.ts +++ b/packages/editor-ui/src/stores/n8nRootStore.ts @@ -5,12 +5,13 @@ import { defineStore } from 'pinia'; import Vue from 'vue'; import { useNodeTypesStore } from './nodeTypes'; -const { VUE_APP_URL_BASE_API, VUE_APP_ENDPOINT_REST } = import.meta.env; +const { VUE_APP_URL_BASE_API } = import.meta.env; export const useRootStore = defineStore(STORES.ROOT, { state: (): RootState => ({ baseUrl: VUE_APP_URL_BASE_API ?? (window.BASE_PATH === '/{{BASE_PATH}}/' ? '/' : window.BASE_PATH), + restEndpoint: window.REST_ENDPOINT === '{{REST_ENDPOINT}}' ? 'rest' : window.REST_ENDPOINT, defaultLocale: 'en', endpointWebhook: 'webhook', endpointWebhookTest: 'webhook-test', @@ -41,20 +42,12 @@ export const useRootStore = defineStore(STORES.ROOT, { }, getRestUrl(): string { - let endpoint = 'rest'; - if (VUE_APP_ENDPOINT_REST) { - endpoint = VUE_APP_ENDPOINT_REST; - } - return `${this.baseUrl}${endpoint}`; + return `${this.baseUrl}${this.restEndpoint}`; }, getRestApiContext(): IRestApiContext { - let endpoint = 'rest'; - if (VUE_APP_ENDPOINT_REST) { - endpoint = VUE_APP_ENDPOINT_REST; - } return { - baseUrl: `${this.baseUrl}${endpoint}`, + baseUrl: this.getRestUrl, sessionId: this.sessionId, }; },