Skip to content

fix(security): replace insecure PRNG, harden JSON.parse, fix ESLint errors - #16

Merged
ruvnet merged 1 commit into
mainfrom
fix/security-audit-2026-05-23
May 23, 2026
Merged

fix(security): replace insecure PRNG, harden JSON.parse, fix ESLint errors#16
ruvnet merged 1 commit into
mainfrom
fix/security-audit-2026-05-23

Conversation

@ruvnet

@ruvnet ruvnet commented May 23, 2026

Copy link
Copy Markdown
Owner

Security Fixes

CWE-338 — Insecure Pseudo-Random Number Generator

Two ID generators used Math.random() which is not cryptographically secure and produces predictable values that can be guessed by an attacker:

File Change
src/memory/agentdb/client.ts Math.random().toString(36)crypto.randomBytes(5).toString('hex')
src/memory/reasoning-bank/bank.ts Math.random().toString(36)

Both files now use Node.js crypto.randomBytes via a top-level import { randomBytes } from 'crypto'.

Unguarded JSON.parse on file input

Three load() methods parsed file contents without error handling, meaning a malformed or corrupt saved-state file would throw an uncaught exception up the call stack with no context. Each now wraps JSON.parse(readFileSync(...)) in a try/catch that re-throws with the file path and original message:

  • src/optimize/bootstrap.ts
  • src/optimize/gepa.ts
  • src/optimize/miprov2.ts

ESLint Error Fixes (11 → 0 errors)

File Rule Fix
src/core/factory.ts no-unused-vars Remove phantom TResult from ModuleOptions interface (kept on function)
src/core/module.ts no-unused-vars Remove unused FieldDefinition import
src/lm/onnx.ts no-unused-vars Rename tokens_tokens (intentionally unused param)
src/lm/torch.ts no-unused-vars Rename options_options (intentionally unused param)
src/modules/chain-of-thought.ts prefer-const, no-unused-vars let jsonMatchconst; empty catch binding removed
src/modules/react.ts no-unused-vars, prefer-const steps_steps; let valueconst value
src/types/js-pytorch.d.ts no-misused-new Replace new() signatures on interfaces with proper LinearConstructor/ReLUConstructor interfaces

npm Audit

npm audit reported 0 vulnerabilities before and after these changes. The package.json already contains overrides for qs >= 6.15.0, protobufjs >= 7.5.8, and tar >= 7.5.11.

Test Plan

  • npm audit — 0 vulnerabilities
  • npx tsc --noEmit — clean (0 errors)
  • npx eslint 'src/**/*.ts' — 0 errors (147 pre-existing warnings, all no-explicit-any / missing return types)
  • npm test — 177/177 tests pass

…rrors

Security fixes (CWE-338):
- src/memory/agentdb/client.ts: replace Math.random() with crypto.randomBytes
  for ID generation; add top-level crypto import
- src/memory/reasoning-bank/bank.ts: replace Math.random() with crypto.randomBytes
  for knowledge-unit ID generation; add top-level crypto import
- Remove unused DEFAULT_SAFLA_CONFIG import from reasoning-bank/bank.ts

Input hardening:
- src/optimize/bootstrap.ts: wrap JSON.parse(readFileSync(...)) in try/catch
  with descriptive error message in load()
- src/optimize/gepa.ts: same JSON.parse hardening in load()
- src/optimize/miprov2.ts: same JSON.parse hardening in load()

ESLint error fixes (0 errors, was 11):
- src/core/factory.ts: remove phantom TResult from ModuleOptions interface;
  keep it on defineModule function signature
- src/core/module.ts: remove unused FieldDefinition import
- src/lm/onnx.ts: prefix unused 'tokens' parameter as '_tokens'
- src/lm/torch.ts: prefix unused 'options' parameter as '_options'
- src/modules/chain-of-thought.ts: let -> const for jsonMatch; drop unused
  error binding in catch clause
- src/modules/react.ts: rename 'steps' param to '_steps'; let -> const for
  value (prefer-const)
- src/types/js-pytorch.d.ts: fix no-misused-new by replacing interface new()
  signatures with separate LinearConstructor/ReLUConstructor interfaces

All 177 tests pass; TypeScript compilation clean.

Co-Authored-By: claude-flow <ruv@ruv.net>
@ruvnet
ruvnet merged commit 535d0e4 into main May 23, 2026
4 checks passed
@ruvnet
ruvnet deleted the fix/security-audit-2026-05-23 branch May 23, 2026 09:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant