Skip to content

Commit

Permalink
fix(core): App should not crash with a custom rest endpoint
Browse files Browse the repository at this point in the history
fixes #5880
  • Loading branch information
netroy committed Apr 5, 2023
1 parent dba5be3 commit 9b4acaa
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 13 deletions.
7 changes: 6 additions & 1 deletion packages/cli/src/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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('|')})\/?.*$`);

Expand Down
2 changes: 2 additions & 0 deletions packages/cli/src/commands/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';
Expand All @@ -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,
}),
Expand Down
1 change: 1 addition & 0 deletions packages/editor-ui/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<link rel="icon" href="/favicon.ico" />
<script type="text/javascript">
window.BASE_PATH = '/{{BASE_PATH}}/';
window.REST_ENDPOINT = '{{REST_ENDPOINT}}';
</script>
<script>!function(t,e){var o,n,p,r;e.__SV||(window.posthog=e,e._i=[],e.init=function(i,s,a){function g(t,e){var o=e.split(".");2==o.length&&(t=t[o[0]],e=o[1]),t[e]=function(){t.push([e].concat(Array.prototype.slice.call(arguments,0)))}}(p=t.createElement("script")).type="text/javascript",p.async=!0,p.src=s.api_host+"/static/array.js",(r=t.getElementsByTagName("script")[0]).parentNode.insertBefore(p,r);var u=e;for(void 0!==a?u=e[a]=[]:a="posthog",u.people=u.people||[],u.toString=function(t){var e="posthog";return"posthog"!==a&&(e+="."+a),t||(e+=" (stub)"),e},u.people.toString=function(){return u.toString(1)+".people (stub)"},o="capture identify alias people.set people.set_once set_config register register_once unregister opt_out_capturing has_opted_out_capturing opt_in_capturing reset isFeatureEnabled getFeatureFlag onFeatureFlags reloadFeatureFlags".split(" "),n=0;n<o.length;n++)g(u,o[n]);e._i.push([i,s,a])},e.__SV=1)}(document,window.posthog||[])</script>

Expand Down
1 change: 1 addition & 0 deletions packages/editor-ui/src/Interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -985,6 +985,7 @@ export interface WorkflowsState {

export interface RootState {
baseUrl: string;
restEndpoint: string;
defaultLocale: string;
endpointWebhook: string;
endpointWebhookTest: string;
Expand Down
2 changes: 1 addition & 1 deletion packages/editor-ui/src/shims.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
18 changes: 7 additions & 11 deletions packages/editor-ui/src/stores/n8nRootStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ 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 || window.REST_ENDPOINT === '{{REST_ENDPOINT}}'
? 'rest'
: window.REST_ENDPOINT,
defaultLocale: 'en',
endpointWebhook: 'webhook',
endpointWebhookTest: 'webhook-test',
Expand Down Expand Up @@ -41,20 +45,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,
};
},
Expand Down

0 comments on commit 9b4acaa

Please sign in to comment.