Skip to content

Commit

Permalink
refactor: Add lint rule for unsafe property access with lodash get/se…
Browse files Browse the repository at this point in the history
…t (no-changelog) (#8587)
  • Loading branch information
elsmr authored and cstuncsik committed Feb 17, 2024
1 parent e387ea9 commit 4166a83
Show file tree
Hide file tree
Showing 22 changed files with 90 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ class N8nStructuredOutputParser<T extends z.ZodTypeAny> extends StructuredOutput
const parsed = (await super.parse(text)) as object;

return (
get(parsed, `${STRUCTURED_OUTPUT_KEY}.${STRUCTURED_OUTPUT_OBJECT_KEY}`) ??
get(parsed, `${STRUCTURED_OUTPUT_KEY}.${STRUCTURED_OUTPUT_ARRAY_KEY}`) ??
get(parsed, [STRUCTURED_OUTPUT_KEY, STRUCTURED_OUTPUT_OBJECT_KEY]) ??
get(parsed, [STRUCTURED_OUTPUT_KEY, STRUCTURED_OUTPUT_ARRAY_KEY]) ??
get(parsed, STRUCTURED_OUTPUT_KEY) ??
parsed
);
Expand Down
5 changes: 5 additions & 0 deletions packages/@n8n_io/eslint-config/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ const config = (module.exports = {

/** https://github.com/sindresorhus/eslint-plugin-unicorn */
'eslint-plugin-unicorn',

/** https://github.com/wix-incubator/eslint-plugin-lodash */
'eslint-plugin-lodash',
],

extends: [
Expand Down Expand Up @@ -458,6 +461,8 @@ const config = (module.exports = {

/** https://github.com/sindresorhus/eslint-plugin-unicorn/blob/main/docs/rules/no-useless-promise-resolve-reject.md */
'unicorn/no-useless-promise-resolve-reject': 'error',

'lodash/path-style': ['error', 'as-needed'],
},

overrides: [
Expand Down
1 change: 1 addition & 0 deletions packages/@n8n_io/eslint-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"eslint-config-prettier": "^9.0.0",
"eslint-import-resolver-typescript": "^3.6.1",
"eslint-plugin-import": "^2.29.0",
"eslint-plugin-lodash": "^7.4.0",
"eslint-plugin-n8n-local-rules": "^1.0.0",
"eslint-plugin-prettier": "^5.0.1",
"eslint-plugin-unicorn": "^49.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/NodeExecuteFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2548,7 +2548,7 @@ const addExecutionDataFunctions = async (
runExecutionData.executionData!.metadata = {};
}

let sourceTaskData = get(runExecutionData, `executionData.metadata[${sourceNodeName}]`);
let sourceTaskData = get(runExecutionData, ['executionData', 'metadata', sourceNodeName]);

if (!sourceTaskData) {
runExecutionData.executionData!.metadata[sourceNodeName] = [];
Expand Down
11 changes: 7 additions & 4 deletions packages/core/src/WorkflowExecute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,10 +337,13 @@ export class WorkflowExecute {
): boolean {
// for (const inputConnection of workflow.connectionsByDestinationNode[nodeToAdd].main[0]) {
for (const inputConnection of inputConnections) {
const nodeIncomingData = get(
runData,
`[${inputConnection.node}][${runIndex}].data.main[${inputConnection.index}]`,
);
const nodeIncomingData = get(runData, [
inputConnection.node,
runIndex,
'data',
'main',
inputConnection.index,
]);
if (nodeIncomingData !== undefined && (nodeIncomingData as object[]).length !== 0) {
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/editor-ui/src/components/CollectionParameter.vue
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,11 @@ function optionSelected(optionName: string) {
// The "fixedCollection" entries are different as they save values
// in an object and then underneath there is an array. So initialize
// them differently.
const retrievedObjectValue = get(props.nodeValues, `${props.path}.${optionName}`, {});
const retrievedObjectValue = get(props.nodeValues, [props.path, optionName], {});
newValue = retrievedObjectValue;
} else {
// Everything else saves them directly as an array.
const retrievedArrayValue = get(props.nodeValues, `${props.path}.${optionName}`, []) as Array<
const retrievedArrayValue = get(props.nodeValues, [props.path, optionName], []) as Array<
typeof option.default
>;
if (Array.isArray(retrievedArrayValue)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ export default defineComponent({
// Multiple values are allowed so append option to array
newParameterValue[optionParameter.name] = get(
this.nodeValues,
`${this.path}.${optionParameter.name}`,
[this.path, optionParameter.name],
[],
);
if (Array.isArray(optionParameter.default)) {
Expand Down
6 changes: 3 additions & 3 deletions packages/editor-ui/src/components/WorkflowLMChat.vue
Original file line number Diff line number Diff line change
Expand Up @@ -480,15 +480,15 @@ export default defineComponent({
const nodeResponseDataArray = get(
this.workflowsStore.getWorkflowExecution?.data?.resultData.runData,
`[${lastNodeExecuted}]`,
lastNodeExecuted,
) as ITaskData[];
const nodeResponseData = nodeResponseDataArray[nodeResponseDataArray.length - 1];
let responseMessage: string;
if (get(nodeResponseData, ['error'])) {
responseMessage = '[ERROR: ' + get(nodeResponseData, ['error', 'message']) + ']';
if (get(nodeResponseData, 'error')) {
responseMessage = '[ERROR: ' + get(nodeResponseData, 'error.message') + ']';
} else {
const responseData = get(nodeResponseData, 'data.main[0][0].json');
responseMessage = this.extractResponseMessage(responseData);
Expand Down
9 changes: 3 additions & 6 deletions packages/nodes-base/nodes/Aws/ELB/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,8 @@ export async function awsApiRequestSOAPAllItems(
region,
);

if (get(responseData, `${propertyNameArray[0]}.${propertyNameArray[1]}.NextMarker`)) {
query.Marker = get(
responseData,
`${propertyNameArray[0]}.${propertyNameArray[1]}.NextMarker`,
);
if (get(responseData, [propertyNameArray[0], propertyNameArray[1], 'NextMarker'])) {
query.Marker = get(responseData, [propertyNameArray[0], propertyNameArray[1], 'NextMarker']);
}
if (get(responseData, propertyName)) {
if (Array.isArray(get(responseData, propertyName))) {
Expand All @@ -156,7 +153,7 @@ export async function awsApiRequestSOAPAllItems(
}
}
} while (
get(responseData, `${propertyNameArray[0]}.${propertyNameArray[1]}.NextMarker`) !== undefined
get(responseData, [propertyNameArray[0], propertyNameArray[1], 'NextMarker']) !== undefined
);

return returnData;
Expand Down
14 changes: 7 additions & 7 deletions packages/nodes-base/nodes/Aws/Rekognition/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,11 @@ export async function awsApiRequestSOAPAllItems(
);

//https://forums.aws.amazon.com/thread.jspa?threadID=55746
if (get(responseData, `${propertyName.split('.')[0]}.NextContinuationToken`)) {
query['continuation-token'] = get(
responseData,
`${propertyName.split('.')[0]}.NextContinuationToken`,
);
if (get(responseData, [propertyName.split('.')[0], 'NextContinuationToken'])) {
query['continuation-token'] = get(responseData, [
propertyName.split('.')[0],
'NextContinuationToken',
]);
}
if (get(responseData, propertyName)) {
if (Array.isArray(get(responseData, propertyName))) {
Expand All @@ -157,8 +157,8 @@ export async function awsApiRequestSOAPAllItems(
return returnData;
}
} while (
get(responseData, `${propertyName.split('.')[0]}.IsTruncated`) !== undefined &&
get(responseData, `${propertyName.split('.')[0]}.IsTruncated`) !== 'false'
get(responseData, [propertyName.split('.')[0], 'IsTruncated']) !== undefined &&
get(responseData, [propertyName.split('.')[0], 'IsTruncated']) !== 'false'
);

return returnData;
Expand Down
14 changes: 7 additions & 7 deletions packages/nodes-base/nodes/Aws/S3/V1/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,11 @@ export async function awsApiRequestSOAPAllItems(
);

//https://forums.aws.amazon.com/thread.jspa?threadID=55746
if (get(responseData, `${propertyName.split('.')[0]}.NextContinuationToken`)) {
query['continuation-token'] = get(
responseData,
`${propertyName.split('.')[0]}.NextContinuationToken`,
);
if (get(responseData, [propertyName.split('.')[0], 'NextContinuationToken'])) {
query['continuation-token'] = get(responseData, [
propertyName.split('.')[0],
'NextContinuationToken',
]);
}
if (get(responseData, propertyName)) {
if (Array.isArray(get(responseData, propertyName))) {
Expand All @@ -155,8 +155,8 @@ export async function awsApiRequestSOAPAllItems(
return returnData;
}
} while (
get(responseData, `${propertyName.split('.')[0]}.IsTruncated`) !== undefined &&
get(responseData, `${propertyName.split('.')[0]}.IsTruncated`) !== 'false'
get(responseData, [propertyName.split('.')[0], 'IsTruncated']) !== undefined &&
get(responseData, [propertyName.split('.')[0], 'IsTruncated']) !== 'false'
);

return returnData;
Expand Down
14 changes: 7 additions & 7 deletions packages/nodes-base/nodes/Aws/S3/V2/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,11 @@ export async function awsApiRequestRESTAllItems(
region,
);
//https://forums.aws.amazon.com/thread.jspa?threadID=55746
if (get(responseData, `${propertyName.split('.')[0]}.NextContinuationToken`)) {
query['continuation-token'] = get(
responseData,
`${propertyName.split('.')[0]}.NextContinuationToken`,
);
if (get(responseData, [propertyName.split('.')[0], 'NextContinuationToken'])) {
query['continuation-token'] = get(responseData, [
propertyName.split('.')[0],
'NextContinuationToken',
]);
}
if (get(responseData, propertyName)) {
if (Array.isArray(get(responseData, propertyName))) {
Expand All @@ -125,8 +125,8 @@ export async function awsApiRequestRESTAllItems(
return returnData;
}
} while (
get(responseData, `${propertyName.split('.')[0]}.IsTruncated`) !== undefined &&
get(responseData, `${propertyName.split('.')[0]}.IsTruncated`) !== 'false'
get(responseData, [propertyName.split('.')[0], 'IsTruncated']) !== undefined &&
get(responseData, [propertyName.split('.')[0], 'IsTruncated']) !== 'false'
);
return returnData;
}
13 changes: 7 additions & 6 deletions packages/nodes-base/nodes/Aws/SES/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,12 @@ export async function awsApiRequestSOAPAllItems(
do {
responseData = await awsApiRequestSOAP.call(this, service, method, path, body, query);

if (get(responseData, `${propertyNameArray[0]}.${propertyNameArray[1]}.NextToken`)) {
query.NextToken = get(
responseData,
`${propertyNameArray[0]}.${propertyNameArray[1]}.NextToken`,
);
if (get(responseData, [propertyNameArray[0], propertyNameArray[1], 'NextToken'])) {
query.NextToken = get(responseData, [
propertyNameArray[0],
propertyNameArray[1],
'NextToken',
]);
}
if (get(responseData, propertyName)) {
if (Array.isArray(get(responseData, propertyName))) {
Expand All @@ -116,7 +117,7 @@ export async function awsApiRequestSOAPAllItems(
}
}
} while (
get(responseData, `${propertyNameArray[0]}.${propertyNameArray[1]}.NextToken`) !== undefined
get(responseData, [propertyNameArray[0], propertyNameArray[1], 'NextToken']) !== undefined
);

return returnData;
Expand Down
13 changes: 7 additions & 6 deletions packages/nodes-base/nodes/Aws/Transcribe/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,12 @@ export async function awsApiRequestRESTAllItems(
do {
responseData = await awsApiRequestREST.call(this, service, method, path, body, query);

if (get(responseData, `${propertyNameArray[0]}.${propertyNameArray[1]}.NextToken`)) {
query.NextToken = get(
responseData,
`${propertyNameArray[0]}.${propertyNameArray[1]}.NextToken`,
);
if (get(responseData, [propertyNameArray[0], propertyNameArray[1], 'NextToken'])) {
query.NextToken = get(responseData, [
propertyNameArray[0],
propertyNameArray[1],
'NextToken',
]);
}
if (get(responseData, propertyName)) {
if (Array.isArray(get(responseData, propertyName))) {
Expand All @@ -123,7 +124,7 @@ export async function awsApiRequestRESTAllItems(
}
}
} while (
get(responseData, `${propertyNameArray[0]}.${propertyNameArray[1]}.NextToken`) !== undefined
get(responseData, [propertyNameArray[0], propertyNameArray[1], 'NextToken']) !== undefined
);

return returnData;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ function combineItems(
entry.json[field] = match.json[field];
} else {
const value = get(match.json, field) || null;
set(entry, `json.${field}`, value);
set(entry, ['json', field], value);
}
});

Expand Down
2 changes: 1 addition & 1 deletion packages/nodes-base/nodes/Crypto/Crypto.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ export class Crypto implements INodeType {
newItem.binary = item.binary;
}

set(newItem, `json.${dataPropertyName}`, newValue);
set(newItem, ['json', dataPropertyName], newValue);

returnData.push(newItem);
} catch (error) {
Expand Down
4 changes: 1 addition & 3 deletions packages/nodes-base/nodes/CustomerIo/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ export async function customerIoApiRequest(

export function eventExists(currentEvents: string[], webhookEvents: IDataObject) {
for (const currentEvent of currentEvents) {
if (
get(webhookEvents, `${currentEvent.split('.')[0]}.${currentEvent.split('.')[1]}`) !== true
) {
if (get(webhookEvents, [currentEvent.split('.')[0], currentEvent.split('.')[1]]) !== true) {
return false;
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/nodes-base/nodes/DateTime/V1/DateTimeV1.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ export class DateTimeV1 implements INodeType {
newItem.binary = item.binary;
}

set(newItem, `json.${dataPropertyName}`, newDate);
set(newItem, ['json', dataPropertyName], newDate);

returnData.push(newItem);
}
Expand Down Expand Up @@ -565,7 +565,7 @@ export class DateTimeV1 implements INodeType {
newItem.binary = item.binary;
}

set(newItem, `json.${dataPropertyName}`, newDate.toISOString());
set(newItem, ['json', dataPropertyName], newDate.toISOString());

returnData.push(newItem);
}
Expand Down
6 changes: 3 additions & 3 deletions packages/nodes-base/nodes/Linear/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ export async function linearApiRequestAllItems(

do {
responseData = await linearApiRequest.call(this, body);
returnData.push.apply(returnData, get(responseData, `${propertyName}.nodes`) as IDataObject[]);
body.variables.after = get(responseData, `${propertyName}.pageInfo.endCursor`);
} while (get(responseData, `${propertyName}.pageInfo.hasNextPage`));
returnData.push.apply(returnData, get(responseData, [propertyName, 'nodes']) as IDataObject[]);
body.variables.after = get(responseData, [propertyName, 'pageInfo', 'endCursor']);
} while (get(responseData, [propertyName, 'pageInfo', 'hasNextPage']));
return returnData;
}

Expand Down
14 changes: 7 additions & 7 deletions packages/nodes-base/nodes/S3/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,11 @@ export async function s3ApiRequestSOAPAllItems(
);

//https://forums.aws.amazon.com/thread.jspa?threadID=55746
if (get(responseData, `${propertyName.split('.')[0]}.NextContinuationToken`)) {
query['continuation-token'] = get(
responseData,
`${propertyName.split('.')[0]}.NextContinuationToken`,
);
if (get(responseData, [propertyName.split('.')[0], 'NextContinuationToken'])) {
query['continuation-token'] = get(responseData, [
propertyName.split('.')[0],
'NextContinuationToken',
]);
}
if (get(responseData, propertyName)) {
if (Array.isArray(get(responseData, propertyName))) {
Expand All @@ -208,8 +208,8 @@ export async function s3ApiRequestSOAPAllItems(
return returnData;
}
} while (
get(responseData, `${propertyName.split('.')[0]}.IsTruncated`) !== undefined &&
get(responseData, `${propertyName.split('.')[0]}.IsTruncated`) !== 'false'
get(responseData, [propertyName.split('.')[0], 'IsTruncated']) !== undefined &&
get(responseData, [propertyName.split('.')[0], 'IsTruncated']) !== 'false'
);

return returnData;
Expand Down
4 changes: 2 additions & 2 deletions packages/nodes-base/nodes/WhatsApp/MessageFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ export async function mediaUploadFromItem(
if (!requestOptions.body) {
requestOptions.body = {};
}
set(requestOptions.body as IDataObject, `${operation}.id`, result.id);
set(requestOptions.body as IDataObject, [operation, 'id'], result.id);
if (operation === 'document') {
set(requestOptions.body as IDataObject, `${operation}.filename`, uploadData.fileName);
set(requestOptions.body as IDataObject, [operation, 'filename'], uploadData.fileName);
}

return requestOptions;
Expand Down
13 changes: 13 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4166a83

Please sign in to comment.