fix: add null check for iterator in forEachEntry function by Karthikeya1500 · Pull Request #10549 · axios/axios · GitHub
Skip to content

fix: add null check for iterator in forEachEntry function#10549

Open
Karthikeya1500 wants to merge 3 commits intoaxios:v1.xfrom
Karthikeya1500:v1.x
Open

fix: add null check for iterator in forEachEntry function#10549
Karthikeya1500 wants to merge 3 commits intoaxios:v1.xfrom
Karthikeya1500:v1.x

Conversation

@Karthikeya1500
Copy link
Copy Markdown

@Karthikeya1500 Karthikeya1500 commented Mar 23, 2026

Summary

Fixes potential TypeError crash in forEachEntry utility function when iterating over objects with invalid iterators.

Problem

The forEachEntry function didn't validate that obj[Symbol.iterator] is actually a function before calling it with .call(), leading to potential crashes when:

  • Object has Symbol.iterator property that's not a function
  • Object is null/undefined
  • Iterator property exists but is invalid

Solution

Added safety check using existing isFunction utility before attempting to call the iterator:

if (!isFunction(generator)) {
  return;
}

<!-- This is an auto-generated description by cubic. -->
---
## Summary by cubic
Prevents TypeError crashes in `forEachEntry` by validating the iterator is callable and that the returned iterator has a `next` function. Safely no-ops for invalid or missing iterators, including null/undefined.

## Description

- Add `isFunction` guard for `obj[Symbol.iterator]`; return early if not a function.
- Validate the returned iterator exists and has a callable `next`; return early if not.
- No behavior change for valid iterables.

## Testing

- No tests updated in this PR.
- Recommended: add unit tests for:
  - `Symbol.iterator` present but not a function.
  - Iterator object returned without `next`, or with non-function `next`.
  - `null` and `undefined` inputs.

<sup>Written for commit 3b801e1d2f72353d3ae7209c7bedbe74f9bac41d. Summary will update on new commits.</sup>

<!-- End of auto-generated description by cubic. -->

Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 1 file

Confidence score: 3/5

  • There is a concrete runtime risk in lib/utils.js: forEachEntry can still throw if [Symbol.iterator] returns a non-iterator, because _iterator.next() is called without validating the returned value.
  • Given the issue is moderately severe (5/10) with fairly high confidence (8/10) and can cause a crash in iteration paths, this carries some user-facing regression risk before merge.
  • Pay close attention to lib/utils.js - validate iterator objects before calling .next() to prevent crashes on malformed iterator returns.
Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="lib/utils.js">

<violation number="1" location="lib/utils.js:604">
P2: `forEachEntry` still crashes when `[Symbol.iterator]` returns a non-iterator because `_iterator.next()` is called without validating the returned object.</violation>
</file>

Since this is your first cubic review, here's how it works:

  • cubic automatically reviews your code and comments on bugs and improvements
  • Teach cubic by replying to its comments. cubic learns from your replies and gets better over time
  • Add one-off context when rerunning by tagging @cubic-dev-ai with guidance or docs links (including llms.txt)
  • Ask questions if you need clarification on any suggestion

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

Comment thread lib/utils.js
Karthikeya1500 and others added 2 commits March 23, 2026 18:47
Added extra check for iterator.next() after bot review feedback.
Prevents crashes when generator returns invalid iterator objects.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@Karthikeya1500
Copy link
Copy Markdown
Author

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