mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
fix(Code Node): Do not validate code within comments (#12938)
This commit is contained in:
parent
9590e5d58b
commit
cdfa22593b
|
@ -13,7 +13,13 @@ export function validateNoDisallowedMethodsInRunForEach(code: string, itemIndex:
|
||||||
|
|
||||||
const lineNumber =
|
const lineNumber =
|
||||||
code.split('\n').findIndex((line) => {
|
code.split('\n').findIndex((line) => {
|
||||||
return line.includes(disallowedMethod) && !line.startsWith('//') && !line.startsWith('*');
|
line = line.trimStart();
|
||||||
|
return (
|
||||||
|
line.includes(disallowedMethod) &&
|
||||||
|
!line.startsWith('//') &&
|
||||||
|
!line.startsWith('/*') &&
|
||||||
|
!line.startsWith('*')
|
||||||
|
);
|
||||||
}) + 1;
|
}) + 1;
|
||||||
|
|
||||||
const disallowedMethodFound = lineNumber !== 0;
|
const disallowedMethodFound = lineNumber !== 0;
|
||||||
|
|
51
packages/nodes-base/nodes/Code/test/JsCodeValidator.test.ts
Normal file
51
packages/nodes-base/nodes/Code/test/JsCodeValidator.test.ts
Normal file
|
@ -0,0 +1,51 @@
|
||||||
|
import { validateNoDisallowedMethodsInRunForEach } from '../JsCodeValidator';
|
||||||
|
|
||||||
|
describe('JsCodeValidator', () => {
|
||||||
|
describe('validateNoDisallowedMethodsInRunForEach', () => {
|
||||||
|
it('should not throw error if disallow method is used within single line comments', () => {
|
||||||
|
const code = [
|
||||||
|
"// Add a new field called 'myNewField' to the JSON of the item",
|
||||||
|
'$input.item.json.myNewField = 1;',
|
||||||
|
' // const xxx = $input.all()',
|
||||||
|
'return $input.item;',
|
||||||
|
].join('\n');
|
||||||
|
|
||||||
|
expect(() => validateNoDisallowedMethodsInRunForEach(code, 0)).not.toThrow();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not throw error if disallow method is used in single multi line comments', () => {
|
||||||
|
const code = [
|
||||||
|
"// Add a new field called 'myNewField' to the JSON of the item",
|
||||||
|
'$input.item.json.myNewField = 1;',
|
||||||
|
'/** const xxx = $input.all()*/',
|
||||||
|
'return $input.item;',
|
||||||
|
].join('\n');
|
||||||
|
|
||||||
|
expect(() => validateNoDisallowedMethodsInRunForEach(code, 0)).not.toThrow();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not throw error if disallow method is used within multi line comments', () => {
|
||||||
|
const code = [
|
||||||
|
"// Add a new field called 'myNewField' to the JSON of the item",
|
||||||
|
'$input.item.json.myNewField = 1;',
|
||||||
|
'/**',
|
||||||
|
'*const xxx = $input.all()',
|
||||||
|
'*/',
|
||||||
|
'return $input.item;',
|
||||||
|
].join('\n');
|
||||||
|
|
||||||
|
expect(() => validateNoDisallowedMethodsInRunForEach(code, 0)).not.toThrow();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should throw error if disallow method is used', () => {
|
||||||
|
const code = [
|
||||||
|
"// Add a new field called 'myNewField' to the JSON of the item",
|
||||||
|
'$input.item.json.myNewField = 1;',
|
||||||
|
'const xxx = $input.all()',
|
||||||
|
'return $input.item;',
|
||||||
|
].join('\n');
|
||||||
|
|
||||||
|
expect(() => validateNoDisallowedMethodsInRunForEach(code, 0)).toThrow();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in a new issue