feat(core): Use WebCrypto to generate all random numbers and strings (#9786)

This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™
2024-06-19 13:33:57 +02:00
committed by GitHub
parent cfc4db00e3
commit 65c5609ab5
49 changed files with 254 additions and 214 deletions

View File

@@ -1,5 +1,5 @@
import type { INode, INodeParameters, INodeProperties, INodeTypeDescription } from '@/Interfaces';
import type { Workflow } from '../src';
import type { Workflow } from '@/Workflow';
import { getNodeParameters, getNodeHints, isSingleExecution } from '@/NodeHelpers';

View File

@@ -1,14 +1,18 @@
import { v5 as uuidv5, v3 as uuidv3, v4 as uuidv4, v1 as uuidv1 } from 'uuid';
import { mock } from 'jest-mock-extended';
import {
ANONYMIZATION_CHARACTER as CHAR,
generateNodesGraph,
getDomainBase,
getDomainPath,
} from '@/TelemetryHelpers';
import { ApplicationError, STICKY_NODE_TYPE, type IWorkflowBase } from '@/index';
import { nodeTypes } from './ExpressionExtensions/Helpers';
import { mock } from 'jest-mock-extended';
import * as nodeHelpers from '@/NodeHelpers';
import type { IWorkflowBase } from '@/Interfaces';
import { STICKY_NODE_TYPE } from '@/Constants';
import { ApplicationError } from '@/errors';
import { randomInt } from '@/utils';
describe('getDomainBase should return protocol plus domain', () => {
test('in valid URLs', () => {
@@ -872,22 +876,12 @@ function uuidUrls(
];
}
function digit() {
return Math.floor(Math.random() * 10);
}
function positiveDigit(): number {
const d = digit();
return d === 0 ? positiveDigit() : d;
}
function numericId(length = positiveDigit()) {
return Array.from({ length }, digit).join('');
function numericId(length = randomInt(1, 10)) {
return Array.from({ length }, () => randomInt(10)).join('');
}
function alphanumericId() {
return chooseRandomly([`john${numericId()}`, `title${numericId(1)}`, numericId()]);
}
const chooseRandomly = <T>(array: T[]) => array[Math.floor(Math.random() * array.length)];
const chooseRandomly = <T>(array: T[]) => array[randomInt(array.length)];

View File

@@ -1,4 +1,4 @@
import { WorkflowActivationError } from '@/index';
import { WorkflowActivationError } from '@/errors';
describe('WorkflowActivationError', () => {
it('should default to `error` level', () => {

View File

@@ -0,0 +1,7 @@
import { randomFillSync } from 'crypto';
Object.defineProperty(globalThis, 'crypto', {
value: {
getRandomValues: (buffer: NodeJS.ArrayBufferView) => randomFillSync(buffer),
},
});

View File

@@ -1,5 +1,14 @@
import { ALPHABET } from '@/Constants';
import { ApplicationError } from '@/errors/application.error';
import { jsonParse, jsonStringify, deepCopy, isObjectEmpty, fileTypeFromMimeType } from '@/utils';
import {
jsonParse,
jsonStringify,
deepCopy,
isObjectEmpty,
fileTypeFromMimeType,
randomInt,
randomString,
} from '@/utils';
describe('isObjectEmpty', () => {
it('should handle null and undefined', () => {
@@ -237,3 +246,47 @@ describe('fileTypeFromMimeType', () => {
expect(fileTypeFromMimeType('application/pdf')).toEqual('pdf');
});
});
const repeat = (fn: () => void, times = 10) => Array(times).fill(0).forEach(fn);
describe('randomInt', () => {
it('should generate random integers', () => {
repeat(() => {
const result = randomInt(10);
expect(result).toBeLessThanOrEqual(10);
expect(result).toBeGreaterThanOrEqual(0);
});
});
it('should generate random in range', () => {
repeat(() => {
const result = randomInt(10, 100);
expect(result).toBeLessThanOrEqual(100);
expect(result).toBeGreaterThanOrEqual(10);
});
});
});
describe('randomString', () => {
it('should return a random string of the specified length', () => {
repeat(() => {
const result = randomString(42);
expect(result).toHaveLength(42);
});
});
it('should return a random string of the in the length range', () => {
repeat(() => {
const result = randomString(10, 100);
expect(result.length).toBeGreaterThanOrEqual(10);
expect(result.length).toBeLessThanOrEqual(100);
});
});
it('should only contain characters from the specified character set', () => {
repeat(() => {
const result = randomString(1000);
result.split('').every((char) => ALPHABET.includes(char));
});
});
});