obfuscator-io-deobfuscator icon indicating copy to clipboard operation
obfuscator-io-deobfuscator copied to clipboard

Fix infinite recursion in Babel AST traversal by adding path.skip() after replaceWithMultiple

Open Copilot opened this issue 10 months ago • 2 comments

Problem

Stack overflow was occurring due to infinite recursion in Babel's AST traversal during transformation passes, specifically in controlFlowRecoverer.ts. When the control flow recovery transformation used nextPath.replaceWithMultiple(statements) to replace AST nodes, the traversal would re-visit the newly inserted nodes, potentially matching the same transformation patterns and causing infinite loops.

Root Cause

The issue was in src/deobfuscator/transformations/controlFlow/controlFlowRecoverer.ts at line 81:

path.remove();
nextPath.replaceWithMultiple(statements);  // <- Missing path.skip() after this
self.setChanged();

After calling replaceWithMultiple(), the Babel traversal would continue and potentially re-process the newly inserted statements, leading to infinite recursion if those statements matched the same control flow patterns.

Solution

Added path.skip() immediately after the replaceWithMultiple() call to prevent re-traversal of the modified AST section:

path.remove();
nextPath.replaceWithMultiple(statements);
path.skip();  // <- Prevents re-traversal and infinite recursion
self.setChanged();

This follows the standard Babel pattern of calling path.skip() after AST mutations to prevent the traversal from revisiting the modified parts of the AST tree.

Analysis of Other Files

  • antiTamperRemover.ts: No changes needed - only uses path.remove() operations which don't require path.skip()
  • Other transformation files: Not mentioned in the issue scope, keeping changes minimal as requested

Testing

  • ✅ All existing tests pass
  • ✅ TypeScript compilation succeeds
  • ✅ Custom test confirms ControlFlowRecoverer runs without infinite loops
  • ✅ No regressions detected

Change Summary

  • Files modified: 1 file
  • Lines added: 1 line
  • Lines deleted: 0 lines
  • Net change: Minimal surgical fix

This change resolves the stack overflow issue while maintaining all existing functionality and following Babel best practices for AST transformation.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot avatar Jun 20 '25 20:06 Copilot

Can you provide a sample that triggers the bug you are trying to fix?

ben-sb avatar Jun 21 '25 14:06 ben-sb

I apparently made a mistake and created an issue in the wrong repo. This was meant to be on my fork. So you can close this PR.

I made a bunch of fixes over at https://github.com/CharlieEriksen/obfuscator-io-deobfuscator, to fix a bunch of performance issues. Feel free to pull over anything you want.

CharlieEriksen avatar Aug 19 '25 06:08 CharlieEriksen