Skip to content

Commit

Permalink
revert nodes-base changes
Browse files Browse the repository at this point in the history
  • Loading branch information
netroy committed Oct 2, 2023
1 parent 38b9b8d commit 410a249
Show file tree
Hide file tree
Showing 6 changed files with 7 additions and 77 deletions.
2 changes: 1 addition & 1 deletion packages/nodes-base/nodes/Code/Code.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export class Code implements INodeType {
const sandbox = getSandbox();
let items: INodeExecutionData[];
try {
items = (await sandbox.runCodeAllItems()) as INodeExecutionData[];
items = await sandbox.runCodeAllItems();
} catch (error) {
if (!this.continueOnFail()) throw error;
items = [{ json: { error: error.message } }];
Expand Down
40 changes: 5 additions & 35 deletions packages/nodes-base/nodes/Code/JavaScriptSandbox.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NodeVM, makeResolverFromLegacyOptions, type Resolver } from '@n8n/vm2';
import { NodeVM, makeResolverFromLegacyOptions } from '@n8n/vm2';
import type { IExecuteFunctions, INodeExecutionData } from 'n8n-workflow';

import { ValidationError } from './ValidationError';
Expand Down Expand Up @@ -27,7 +27,6 @@ export class JavaScriptSandbox extends Sandbox {
private jsCode: string,
itemIndex: number | undefined,
helpers: IExecuteFunctions['helpers'],
options?: { resolver?: Resolver },
) {
super(
{
Expand All @@ -42,28 +41,17 @@ export class JavaScriptSandbox extends Sandbox {
this.vm = new NodeVM({
console: 'redirect',
sandbox: context,
require: options?.resolver ?? vmResolver,
require: vmResolver,
wasm: false,
});

this.vm.on('console.log', (...args: unknown[]) => this.emit('output', ...args));
}

async runCode(): Promise<unknown> {
const script = `module.exports = async function() {${this.jsCode}\n}()`;
try {
return await this.vm.run(script, __dirname);
} catch (error) {
throw new ExecutionError(error);
}
}

async runCodeAllItems(options?: {
multiOutput?: boolean;
}): Promise<INodeExecutionData[] | INodeExecutionData[][]> {
async runCodeAllItems(): Promise<INodeExecutionData[]> {
const script = `module.exports = async function() {${this.jsCode}\n}()`;

let executionResult: INodeExecutionData | INodeExecutionData[] | INodeExecutionData[][];
let executionResult: INodeExecutionData | INodeExecutionData[];

try {
executionResult = await this.vm.run(script, __dirname);
Expand All @@ -79,25 +67,7 @@ export class JavaScriptSandbox extends Sandbox {

if (executionResult === null) return [];

if (options?.multiOutput === true) {
// Check if executionResult is an array of arrays
if (!Array.isArray(executionResult) || executionResult.some((item) => !Array.isArray(item))) {
throw new ValidationError({
message: "The code doesn't return an array of arrays",
description:
'Please return an array of arrays. One array for the different outputs and one for the different items that get returned.',
itemIndex: this.itemIndex,
});
}

return executionResult.map((data) => {
return this.validateRunCodeAllItems(data);
});
}

return this.validateRunCodeAllItems(
executionResult as INodeExecutionData | INodeExecutionData[],
);
return this.validateRunCodeAllItems(executionResult);
}

async runCodeEachItem(): Promise<INodeExecutionData | undefined> {
Expand Down
4 changes: 0 additions & 4 deletions packages/nodes-base/nodes/Code/PythonSandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ export class PythonSandbox extends Sandbox {
}, {} as PythonSandboxContext);
}

async runCode(): Promise<unknown> {
return this.runCodeInPython<unknown>();
}

async runCodeAllItems() {
const executionResult = await this.runCodeInPython<INodeExecutionData[]>();
return this.validateRunCodeAllItems(executionResult);
Expand Down
4 changes: 1 addition & 3 deletions packages/nodes-base/nodes/Code/Sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ export abstract class Sandbox extends EventEmitter {
super();
}

abstract runCode(): Promise<unknown>;

abstract runCodeAllItems(): Promise<INodeExecutionData[] | INodeExecutionData[][]>;
abstract runCodeAllItems(): Promise<INodeExecutionData[]>;

abstract runCodeEachItem(): Promise<INodeExecutionData | undefined>;

Expand Down
24 changes: 0 additions & 24 deletions packages/nodes-base/nodes/Webhook/Webhook.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ export class Webhook extends Node {
async webhook(context: IWebhookFunctions): Promise<IWebhookResponseData> {
const options = context.getNodeParameter('options', {}) as {
binaryData: boolean;
domainAllowlist: string;
ignoreBots: boolean;
rawBody: Buffer;
responseData?: string;
Expand All @@ -105,29 +104,6 @@ export class Webhook extends Node {
const resp = context.getResponseObject();

try {
if (options.domainAllowlist) {
const domainAllowlist = options.domainAllowlist.split(',').map((entry) => entry.trim());
const origin = req.headers.origin;
if (!origin) {
throw new WebhookAuthorizationError(403);
}

const originUrl = new URL(origin);
const isAllowlisted = domainAllowlist.find((entry) => {
const isOriginMatch = originUrl.hostname === entry;
const isWildcard = entry.startsWith('*.');
const isWildcardSubdomainMatch =
isWildcard && originUrl.hostname.endsWith(entry.slice(1));
const isWildcardOriginMatch = isWildcard && originUrl.hostname === entry.slice(2);

return isOriginMatch || isWildcardSubdomainMatch || isWildcardOriginMatch;
});

if (!isAllowlisted) {
throw new WebhookAuthorizationError(403);
}
}

if (options.ignoreBots && isbot(req.headers['user-agent']))
throw new WebhookAuthorizationError(403);
await this.validateAuth(context);
Expand Down
10 changes: 0 additions & 10 deletions packages/nodes-base/nodes/Webhook/description.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,16 +221,6 @@ export const optionsProperty: INodeProperties = {
description:
'Name of the binary property to write the data of the received file to. If the data gets received via "Form-Data Multipart" it will be the prefix and a number starting with 0 will be attached to it.',
},
{
displayName: 'Domain Allowlist',
name: 'domainAllowlist',
type: 'string',
default: '',
description:
'Domains to allow requests from. Separate multiple by comma. Wildcards are supported.',
placeholder: '*.example.com',
displayOptions: {},
},
{
displayName: 'Ignore Bots',
name: 'ignoreBots',
Expand Down

0 comments on commit 410a249

Please sign in to comment.