fix: add element count limit per collection to prevent resource exhau…#9373
Open
karan68 wants to merge 1 commit intomicrosoft:mainfrom
Open
fix: add element count limit per collection to prevent resource exhau…#9373karan68 wants to merge 1 commit intomicrosoft:mainfrom
karan68 wants to merge 1 commit intomicrosoft:mainfrom
Conversation
…stion GetElementCollection, GetElementCollectionOfSingleType, and GetActionCollection loop all items in a JSON array with no cap. A card with 100,000 elements creates 100,000 parsed objects, leading to memory exhaustion and UI thread freeze when rendered. Adds c_maxElementsPerCollection (200) to ParseContext. Collections exceeding the limit are truncated with a parse warning. The reserve() call is also capped to prevent upfront over-allocation. Covers all 19 call sites: body, actions, columns, choices, facts, images, table rows/cells, carousel pages, inlines, etc.
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.
Summary
Adds a maximum element count limit (200) per collection in the shared C++ object model parser. Collections exceeding the limit are truncated with a parse warning.
Problem
GetElementCollection,GetElementCollectionOfSingleType, andGetActionCollectionin ParseUtil.h/ParseUtil.cpp loop over every item in a JSON array with no upper bound. Additionally,elements.reserve(elementArray.size())allocates memory for the full array size upfront.A card with 100,000 TextBlocks in the body creates 100,000 parsed C++ objects. When rendered, each becomes a XAML control — exhausting memory and freezing the UI thread.
Verified locally: A standalone C++ binary linked against the ObjectModel library confirmed:
This affects all renderers using the shared C++ model (UWP, WinUI3, Android, iOS). The parser currently provides no API for hosts to set a collection size limit —> even hosts that want to cap elements have no way to do so.
Changes
4 files changed, 120 insertions, 3 deletions:
How it works
All three collection parsing functions now check before each item:
Scope
Covers all 19 call sites through the 3 guarded functions:
Behavior
No breaking changes. Real-world Adaptive Cards rarely have more than 20-30 elements in a single collection. The limit of 200 is very generous.
Testing
4 unit tests added to ObjectModelTest.cpp:
Context
This is a defense-in-depth hardening for scenarios where AdaptiveCards renders untrusted content (e.g., Windows Widgets accepting 3rd-party card payloads). The parser was originally designed for trusted-source cards, but the threat model has expanded. There is currently no existing API or config for hosts to limit collection sizes.