Skip to content

fix(rag): reject invalid embed batch sizes - #4394

Merged
JSONbored merged 1 commit into
mainfrom
codex/fix-vulnerability-in-ai_embed_batch-handling
Jul 9, 2026
Merged

fix(rag): reject invalid embed batch sizes#4394
JSONbored merged 1 commit into
mainfrom
codex/fix-vulnerability-in-ai_embed_batch-handling

Conversation

@JSONbored

Copy link
Copy Markdown
Owner

Motivation

  • The environment parser accepted positive fractional AI_EMBED_BATCH values that floored to 0, which could be threaded into RAG operations and produce a non-advancing batching loop.
  • A zero batchSize leads to empty batches and a self-host embedding adapter returning { data: [] }, allowing the embedding loop to spin indefinitely and hang review/index/retrieval work.
  • The change prevents accidental denial-of-service on self-hosted deployments by validating the effective integer batch size before use.

Description

  • Change ragEmbedBatchFromEnv to floor the numeric value first and validate the floored integer, i.e. const batch = Math.floor(Number(value)); return Number.isFinite(batch) && batch > 0 ? batch : EMBED_BATCH;.
  • Add a defensive guard in embedTexts that computes effectiveBatchSize = Math.floor(batchSize) and returns null if effectiveBatchSize is not finite or is < 1, and use effectiveBatchSize for slicing and loop increments.
  • Add regression unit tests to assert fractional env parsing falls back to the default and that invalid/zero/NaN batchSize inputs cause embedTexts to return null without calling the embedding provider.

Testing

  • Ran the targeted unit suite npx vitest run test/unit/rag.test.ts which passed (all relevant rag tests succeeded).
  • Ran type checking via npm run typecheck which completed without errors.
  • Attempted full coverage with npm run test:coverage in this environment but it did not complete here (the focused unit coverage for the changed files was exercised and passed); the full CI gate should be run before merge.

Codex Task

@superagent-security

Copy link
Copy Markdown
Contributor

Superagent didn't find any vulnerabilities or security issues in this PR.

@codecov

codecov Bot commented Jul 9, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.95%. Comparing base (084cb38) to head (5ab34ff).
⚠️ Report is 12 commits behind head on main.
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4394   +/-   ##
=======================================
  Coverage   93.95%   93.95%           
=======================================
  Files         400      400           
  Lines       36820    36822    +2     
  Branches    13452    13453    +1     
=======================================
+ Hits        34595    34597    +2     
  Misses       1569     1569           
  Partials      656      656           
Files with missing lines Coverage Δ
src/review/rag.ts 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@JSONbored JSONbored self-assigned this Jul 9, 2026
@JSONbored JSONbored added the gittensor:bug Gittensor-scored bug fix — scores a 0.05x multiplier. label Jul 9, 2026
@JSONbored
JSONbored merged commit 96ad52a into main Jul 9, 2026
10 checks passed
@JSONbored
JSONbored deleted the codex/fix-vulnerability-in-ai_embed_batch-handling branch July 9, 2026 08:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gittensor:bug Gittensor-scored bug fix — scores a 0.05x multiplier.

Development

Successfully merging this pull request may close these issues.

1 participant