From ae379ac7fb92e3705ff89f3bb3a9ad018f161930 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Mon, 6 Apr 2026 15:27:41 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=92=20fix:=20replace=20Math.random=20w?= =?UTF-8?q?ith=20crypto.randomInt=20for=20secure=20random=20text=20generat?= =?UTF-8?q?ion?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: beginwebdev2002 <102213457+beginwebdev2002@users.noreply.github.com> --- .github/scripts/utils.js | 10 +++++----- test-gen/index.js | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+), 5 deletions(-) create mode 100644 test-gen/index.js diff --git a/.github/scripts/utils.js b/.github/scripts/utils.js index 1283553..5641d75 100644 --- a/.github/scripts/utils.js +++ b/.github/scripts/utils.js @@ -1,6 +1,7 @@ import 'dotenv/config'; import { writeFile } from 'fs'; import { join } from 'path'; +import crypto from 'crypto'; export async function saveImage(bytes, filename = randomText() + '.png') { const localPath = join(process.cwd(), filename); @@ -19,12 +20,11 @@ export async function saveVideo(bytes, filename = randomText() + '.mp4') { } export function randomText() { - let symbols = [1, 2,3,5,6,7,8,9,0,'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'] - symbols = symbols.map(() => { - return symbols[Math.floor(Math.random() * symbols.length)] + const symbols = [1, 2,3,5,6,7,8,9,0,'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']; + const result = Array.from({ length: 35 }, () => { + return symbols[crypto.randomInt(0, symbols.length)]; }); - - return symbols.join(''); + return result.join(''); } diff --git a/test-gen/index.js b/test-gen/index.js new file mode 100644 index 0000000..dd460b1 --- /dev/null +++ b/test-gen/index.js @@ -0,0 +1,21 @@ +import test from 'node:test'; +import assert from 'node:assert'; +import { randomText } from '../.github/scripts/utils.js'; + +test('randomText should generate a string of length 35', () => { + const text = randomText(); + assert.strictEqual(typeof text, 'string', 'Generated text should be a string'); + assert.strictEqual(text.length, 35, 'Generated string should have a length of exactly 35 characters'); +}); + +test('randomText should generate random strings', () => { + const text1 = randomText(); + const text2 = randomText(); + assert.notStrictEqual(text1, text2, 'Consecutive calls should generate different strings'); +}); + +test('randomText should only contain specified symbols', () => { + const text = randomText(); + const allowedSymbols = /^[0-9a-z]{35}$/; + assert.match(text, allowedSymbols, 'String should only contain numbers and lowercase letters (except 4)'); +});