fix(core): Revert back to the extended query-parser on express 5 (#15016)

Co-authored-by: Iván Ovejero <ivov.src@gmail.com>
This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™
2025-04-30 12:51:58 +02:00
committed by GitHub
parent c0b54832b3
commit 9541b5bb07
2 changed files with 92 additions and 114 deletions

View File

@@ -66,7 +66,7 @@ export abstract class AbstractServer {
constructor() {
this.app = express();
this.app.disable('x-powered-by');
this.app.set('query parser', 'extended');
this.app.engine('handlebars', expressHandlebars({ defaultLayout: false }));
this.app.set('view engine', 'handlebars');
this.app.set('views', TEMPLATES_DIR);

View File

@@ -1,5 +1,6 @@
import { readFileSync } from 'fs';
import type { IWorkflowBase } from 'n8n-workflow';
import { mock } from 'jest-mock-extended';
import type { INode, IWorkflowBase } from 'n8n-workflow';
import {
NodeConnectionTypes,
type INodeType,
@@ -8,10 +9,8 @@ import {
} from 'n8n-workflow';
import { agent as testAgent } from 'supertest';
import { ExternalHooks } from '@/external-hooks';
import type { User } from '@/databases/entities/user';
import { NodeTypes } from '@/node-types';
import { Push } from '@/push';
import { Telemetry } from '@/telemetry';
import { WebhookServer } from '@/webhooks/webhook-server';
import { createUser } from './shared/db/users';
@@ -23,163 +22,6 @@ import { mockInstance } from '../shared/mocking';
jest.unmock('node:fs');
mockInstance(Telemetry);
describe('Webhook API', () => {
mockInstance(ExternalHooks);
mockInstance(Push);
let agent: SuperAgentTest;
beforeAll(async () => {
await testDb.init();
});
afterAll(async () => {
await testDb.terminate();
});
describe('Content-Type support', () => {
beforeAll(async () => {
const node = new WebhookTestingNode();
const user = await createUser();
await createWorkflow(createWebhookWorkflow(node), user);
const nodeTypes = mockInstance(NodeTypes);
nodeTypes.getByName.mockReturnValue(node);
nodeTypes.getByNameAndVersion.mockReturnValue(node);
await initActiveWorkflowManager();
const server = new WebhookServer();
await server.start();
agent = testAgent(server.app);
});
afterAll(async () => {
await testDb.truncate(['Workflow']);
});
test('should handle JSON', async () => {
const response = await agent.post('/webhook/abcd').send({ test: true });
expect(response.statusCode).toEqual(200);
expect(response.body).toEqual({
type: 'application/json',
body: { test: true },
params: {},
});
});
test('should handle XML', async () => {
const response = await agent
.post('/webhook/abcd')
.set('content-type', 'application/xml')
.send(
'<?xml version="1.0" encoding="UTF-8"?><Outer attr="test"><Inner>value</Inner></Outer>',
);
expect(response.statusCode).toEqual(200);
expect(response.body).toEqual({
type: 'application/xml',
body: {
outer: {
$: {
attr: 'test',
},
inner: 'value',
},
},
params: {},
});
});
test('should handle form-urlencoded', async () => {
const response = await agent
.post('/webhook/abcd')
.set('content-type', 'application/x-www-form-urlencoded')
.send('x=5&y=str&z=false');
expect(response.statusCode).toEqual(200);
expect(response.body).toEqual({
type: 'application/x-www-form-urlencoded',
body: { x: '5', y: 'str', z: 'false' },
params: {},
});
});
test('should handle plain text', async () => {
const response = await agent
.post('/webhook/abcd')
.set('content-type', 'text/plain')
.send('{"key": "value"}');
expect(response.statusCode).toEqual(200);
expect(response.body).toEqual({
type: 'text/plain',
body: '{"key": "value"}',
params: {},
});
});
test('should handle multipart/form-data', async () => {
const response = await agent
.post('/webhook/abcd')
.field('field1', 'value1')
.field('field2', 'value2')
.field('field2', 'value3')
.attach('file1', Buffer.from('random-text'))
.attach('file2', Buffer.from('random-text'))
.attach('file2', Buffer.from('random-text'))
.set('content-type', 'multipart/form-data');
expect(response.statusCode).toEqual(200);
expect(response.body.type).toEqual('multipart/form-data');
const { data, files } = response.body.body;
expect(data).toEqual({ field1: 'value1', field2: ['value2', 'value3'] });
expect(files.file1).not.toBeInstanceOf(Array);
expect(files.file1.mimetype).toEqual('application/octet-stream');
expect(readFileSync(files.file1.filepath, 'utf-8')).toEqual('random-text');
expect(files.file2).toBeInstanceOf(Array);
expect(files.file2.length).toEqual(2);
});
});
describe('Params support', () => {
beforeAll(async () => {
const node = new WebhookTestingNode();
const user = await createUser();
await createWorkflow(createWebhookWorkflow(node, ':variable', 'PATCH'), user);
const nodeTypes = mockInstance(NodeTypes);
nodeTypes.getByName.mockReturnValue(node);
nodeTypes.getByNameAndVersion.mockReturnValue(node);
await initActiveWorkflowManager();
const server = new WebhookServer();
await server.start();
agent = testAgent(server.app);
});
afterAll(async () => {
await testDb.truncate(['Workflow']);
});
test('should handle params', async () => {
const response = await agent
.patch('/webhook/5ccef736-be16-4d10-b7fb-feed7a61ff22/test')
.send({ test: true });
expect(response.statusCode).toEqual(200);
expect(response.body).toEqual({
type: 'application/json',
body: { test: true },
params: {
variable: 'test',
},
});
await agent.post('/webhook/abcd').send({ test: true }).expect(404);
});
});
class WebhookTestingNode implements INodeType {
description: INodeTypeDescription = {
displayName: 'Webhook Testing Node',
@@ -215,33 +57,169 @@ describe('Webhook API', () => {
};
async webhook(this: IWebhookFunctions) {
const req = this.getRequestObject();
return {
webhookResponse: {
type: req.contentType,
body: req.body,
params: req.params,
},
};
const { contentType, body, params, query } = this.getRequestObject();
const webhookResponse: Record<string, any> = { contentType, body };
if (Object.keys(params).length) webhookResponse.params = params;
if (Object.keys(query).length) webhookResponse.query = query;
return { webhookResponse };
}
}
const createWebhookWorkflow = (
node: WebhookTestingNode,
path = 'abcd',
httpMethod = 'POST',
): Partial<IWorkflowBase> => ({
active: true,
nodes: [
{
describe('Webhook API', () => {
const nodeInstance = new WebhookTestingNode();
const node = mock<INode>({
name: 'Webhook',
type: node.description.name,
typeVersion: 1,
parameters: { httpMethod, path },
id: '74786112-fb73-4d80-bd9a-43982939b801',
type: nodeInstance.description.name,
webhookId: '5ccef736-be16-4d10-b7fb-feed7a61ff22',
position: [740, 420],
});
const workflowData = { active: true, nodes: [node] } as IWorkflowBase;
const nodeTypes = mockInstance(NodeTypes);
nodeTypes.getByName.mockReturnValue(nodeInstance);
nodeTypes.getByNameAndVersion.mockReturnValue(nodeInstance);
let user: User;
let agent: SuperAgentTest;
beforeAll(async () => {
await testDb.init();
user = await createUser();
const server = new WebhookServer();
await server.start();
agent = testAgent(server.app);
});
beforeEach(async () => {
await testDb.truncate(['Workflow']);
await createWorkflow(workflowData, user);
await initActiveWorkflowManager();
});
afterAll(async () => {
await testDb.terminate();
});
describe('Content-Type support', () => {
beforeAll(async () => {
node.parameters = { httpMethod: 'POST', path: 'abcd' };
});
test('should handle JSON', async () => {
const response = await agent.post('/webhook/abcd').send({ test: true });
expect(response.statusCode).toEqual(200);
expect(response.body).toEqual({
contentType: 'application/json',
body: { test: true },
});
});
test('should handle XML', async () => {
const response = await agent
.post('/webhook/abcd')
.set('content-type', 'application/xml')
.send(
'<?xml version="1.0" encoding="UTF-8"?><Outer attr="test"><Inner>value</Inner></Outer>',
);
expect(response.statusCode).toEqual(200);
expect(response.body).toEqual({
contentType: 'application/xml',
body: {
outer: {
$: {
attr: 'test',
},
inner: 'value',
},
},
],
});
});
test('should handle form-urlencoded', async () => {
const response = await agent
.post('/webhook/abcd')
.set('content-type', 'application/x-www-form-urlencoded')
.send('x=5&y=str&z=false');
expect(response.statusCode).toEqual(200);
expect(response.body).toEqual({
contentType: 'application/x-www-form-urlencoded',
body: { x: '5', y: 'str', z: 'false' },
});
});
test('should handle plain text', async () => {
const response = await agent
.post('/webhook/abcd')
.set('content-type', 'text/plain')
.send('{"key": "value"}');
expect(response.statusCode).toEqual(200);
expect(response.body).toEqual({
contentType: 'text/plain',
body: '{"key": "value"}',
});
});
test('should handle multipart/form-data', async () => {
const response = await agent
.post('/webhook/abcd')
.field('field1', 'value1')
.field('field2', 'value2')
.field('field2', 'value3')
.attach('file1', Buffer.from('random-text'))
.attach('file2', Buffer.from('random-text'))
.attach('file2', Buffer.from('random-text'))
.set('content-type', 'multipart/form-data');
expect(response.statusCode).toEqual(200);
expect(response.body.contentType).toEqual('multipart/form-data');
const { data, files } = response.body.body;
expect(data).toEqual({ field1: 'value1', field2: ['value2', 'value3'] });
expect(files.file1).not.toBeInstanceOf(Array);
expect(files.file1.mimetype).toEqual('application/octet-stream');
expect(readFileSync(files.file1.filepath, 'utf-8')).toEqual('random-text');
expect(files.file2).toBeInstanceOf(Array);
expect(files.file2.length).toEqual(2);
});
});
describe('Route-parameters support', () => {
beforeAll(async () => {
node.parameters = { httpMethod: 'PATCH', path: ':variable' };
});
test('should handle params', async () => {
const response = await agent
.patch('/webhook/5ccef736-be16-4d10-b7fb-feed7a61ff22/test')
.send({ test: true });
expect(response.statusCode).toEqual(200);
expect(response.body).toEqual({
contentType: 'application/json',
body: { test: true },
params: {
variable: 'test',
},
});
await agent.post('/webhook/abcd').send({ test: true }).expect(404);
});
});
describe('Query-parameters support', () => {
beforeAll(async () => {
node.parameters = { httpMethod: 'GET', path: 'testing' };
});
test('should use the extended query parser', async () => {
const response = await agent.get('/webhook/testing?filter[field]=value');
expect(response.statusCode).toEqual(200);
expect(response.body).toEqual({
query: {
filter: {
field: 'value',
},
},
});
});
});
});