Skip to content

fix: avoid mutating redirect chain array when formatting#974

Closed
AI-Reviewer-QS wants to merge 1 commit intoChromeDevTools:mainfrom
AI-Reviewer-QS:fix/redirect-chain-mutation
Closed

fix: avoid mutating redirect chain array when formatting#974
AI-Reviewer-QS wants to merge 1 commit intoChromeDevTools:mainfrom
AI-Reviewer-QS:fix/redirect-chain-mutation

Conversation

@AI-Reviewer-QS
Copy link
Copy Markdown

Summary

  • toStringDetailed() and toJSONDetailed() call .reverse() on the array returned by request.redirectChain(), which mutates the original array in-place
  • Subsequent calls to these methods or any code that reads the redirect chain will see the reversed (incorrect) order
  • Fixed by copying the array with spread syntax before reversing: [...redirectChain].reverse()

Reproduction

const formatter = new NetworkFormatter(request, options);

// First call: redirect chain is [A -> B -> C], reversed to [C, B, A] ✓
formatter.toStringDetailed();

// Second call: chain is now [C, B, A] (mutated), reversed again to [A, B, C] ✗
formatter.toStringDetailed(); // Shows wrong order

Test plan

  • Verify redirect chain displays in correct order on first call
  • Verify redirect chain displays in correct order on repeated calls
  • Verify both toStringDetailed() and toJSONDetailed() produce consistent output

toStringDetailed() and toJSONDetailed() call .reverse() on the array
returned by request.redirectChain(), which mutates the original array
in-place. Subsequent calls to these methods or any code that reads the
redirect chain will see the reversed (incorrect) order.

Copy the array before reversing to avoid mutating shared state.
Copy link
Copy Markdown
Collaborator

@OrKoN OrKoN left a comment

Choose a reason for hiding this comment

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

The underlying implementation in Puppeteer is:

override redirectChain(): CdpHTTPRequest[] {
    return this._redirectChain.slice();
  }

which already creates a copy.

@OrKoN OrKoN closed this Feb 16, 2026
@OrKoN
Copy link
Copy Markdown
Collaborator

OrKoN commented Feb 16, 2026

cc @nroscino

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.

3 participants