fix(zone.js): add iteration cap to drainMicroTaskQueueSynchronously to prevent DoS#68906
Draft
arturovt wants to merge 1 commit into
Draft
fix(zone.js): add iteration cap to drainMicroTaskQueueSynchronously to prevent DoS#68906arturovt wants to merge 1 commit into
arturovt wants to merge 1 commit into
Conversation
…o prevent DoS Previously, the microtask drain loop had no upper bound on how many times it could refill and re-drain the queue. Since microtasks are allowed to enqueue additional microtasks while executing, a pathological task chain could keep the queue perpetually non-empty, causing the loop to spin indefinitely and block the main thread. This can happen in several ways: 1. Mutual Promise recursion — the simplest and most common case: ```js const loop = () => Promise.resolve().then(loop); loop(); ``` Each `.then()` callback schedules another microtask through Zone’s patched Promise implementation, so `_microTaskQueue` is refilled on every iteration and the drain loop never yields back to the event loop. 2. A compromised or buggy third-party dependency that continuously re-schedules itself using `Zone.current.scheduleMicroTask()` from inside its own callback, either intentionally or due to broken retry/polling logic. 3. An accidental zone-patched cycle in Angular application code, such as a Promise chain inside an HTTP interceptor, router guard, or `APP_INITIALIZER` that unintentionally re-triggers itself under certain runtime conditions. This change introduces `MAX_MICROTASK_DRAIN_ITERATIONS = 1000`. The counter increments once per pass through the drain loop, and if the limit is exceeded, the remaining queue is cleared to immediately unblock the main thread. `onUnhandledError` is then invoked with a descriptive error message that includes the remaining queued task count so the failure is surfaced visibly instead of hanging silently. The limit is intentionally set high enough to avoid impacting legitimate workloads (Angular’s own change detection and routing complete in well under 10 iterations), while still providing protection against runaway or malicious microtask cycles.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Previously, the microtask drain loop had no upper bound on how many times it could refill and re-drain the queue. Since microtasks are allowed to enqueue additional microtasks while executing, a pathological task chain could keep the queue perpetually non-empty, causing the loop to spin indefinitely and block the main thread.
This can happen in several ways:
Each
.then()callback schedules another microtask through Zone’s patched Promise implementation, so_microTaskQueueis refilled on every iteration and the drain loop never yields back to the event loop.A compromised or buggy third-party dependency that continuously re-schedules itself using
Zone.current.scheduleMicroTask()from inside its own callback, either intentionally or due to broken retry/polling logic.An accidental zone-patched cycle in Angular application code, such as a Promise chain inside an HTTP interceptor, router guard, or
APP_INITIALIZERthat unintentionally re-triggers itself under certain runtime conditions.This change introduces
MAX_MICROTASK_DRAIN_ITERATIONS = 1000. The counter increments once per pass through the drain loop, and if the limit is exceeded, the remaining queue is cleared to immediately unblock the main thread.onUnhandledErroris then invoked with a descriptive error message that includes the remaining queued task count so the failure is surfaced visibly instead of hanging silently.The limit is intentionally set high enough to avoid impacting legitimate workloads (Angular’s own change detection and routing complete in well under 10 iterations), while still providing protection against runaway or malicious microtask cycles.