Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): Improve handling of invalid objects in cleanupParameterData (no-chanhelog) #8910

Merged
merged 1 commit into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions packages/core/src/NodeExecuteFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { Agent, type AgentOptions } from 'https';
import get from 'lodash/get';
import isEmpty from 'lodash/isEmpty';
import pick from 'lodash/pick';
import { DateTime } from 'luxon';
import { extension, lookup } from 'mime-types';
import type {
BinaryHelperFunctions,
Expand Down Expand Up @@ -2096,7 +2097,7 @@ export async function getCredentials(
* Clean up parameter data to make sure that only valid data gets returned
* INFO: Currently only converts Luxon Dates as we know for sure it will not be breaking
*/
function cleanupParameterData(inputData: NodeParameterValueType): void {
export function cleanupParameterData(inputData: NodeParameterValueType): void {
if (typeof inputData !== 'object' || inputData === null) {
return;
}
Expand All @@ -2107,14 +2108,15 @@ function cleanupParameterData(inputData: NodeParameterValueType): void {
}

if (typeof inputData === 'object') {
Object.keys(inputData).forEach((key) => {
if (typeof inputData[key as keyof typeof inputData] === 'object') {
if (inputData[key as keyof typeof inputData]?.constructor.name === 'DateTime') {
type Key = keyof typeof inputData;
(Object.keys(inputData) as Key[]).forEach((key) => {
const value = inputData[key];
if (typeof value === 'object') {
if (value instanceof DateTime) {
// Is a special luxon date so convert to string
inputData[key as keyof typeof inputData] =
inputData[key as keyof typeof inputData]?.toString();
inputData[key] = value.toString();
} else {
cleanupParameterData(inputData[key as keyof typeof inputData]);
cleanupParameterData(value);
}
}
});
Expand Down
26 changes: 26 additions & 0 deletions packages/core/test/NodeExecuteFunctions.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
cleanupParameterData,
copyInputItems,
getBinaryDataBuffer,
parseIncomingMessage,
Expand All @@ -7,6 +8,7 @@ import {
removeEmptyBody,
setBinaryDataBuffer,
} from '@/NodeExecuteFunctions';
import { DateTime } from 'luxon';
import { mkdtempSync, readFileSync } from 'fs';
import type { IncomingMessage } from 'http';
import { mock } from 'jest-mock-extended';
Expand All @@ -18,6 +20,7 @@ import type {
IRequestOptions,
ITaskDataConnections,
IWorkflowExecuteAdditionalData,
NodeParameterValue,
Workflow,
WorkflowHooks,
} from 'n8n-workflow';
Expand Down Expand Up @@ -414,6 +417,29 @@ describe('NodeExecuteFunctions', () => {
});
});

describe('cleanupParameterData', () => {
it('should stringify Luxon dates in-place', () => {
const input = { x: 1, y: DateTime.now() as unknown as NodeParameterValue };
expect(typeof input.y).toBe('object');
cleanupParameterData(input);
expect(typeof input.y).toBe('string');
});

it('should handle objects with nameless constructors', () => {
const input = { x: 1, y: { constructor: {} } as NodeParameterValue };
expect(typeof input.y).toBe('object');
cleanupParameterData(input);
expect(typeof input.y).toBe('object');
});

it('should handle objects without a constructor', () => {
const input = { x: 1, y: { constructor: undefined } as unknown as NodeParameterValue };
expect(typeof input.y).toBe('object');
cleanupParameterData(input);
expect(typeof input.y).toBe('object');
});
});

describe('copyInputItems', () => {
it('should pick only selected properties', () => {
const output = copyInputItems(
Expand Down
Loading