-
Notifications
You must be signed in to change notification settings - Fork 166
[AAP] Cap recursion depth in ObjectExtractor's list/dictionary paths #8958
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+59
−9
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
72657eb
[AAP] Cap recursion depth in ObjectExtractor's list/dictionary paths
dromanol 8ec87d9
Update tracer/src/Datadog.Trace/AppSec/ObjectExtractor.cs
dromanol 4bb5b38
Update tracer/src/Datadog.Trace/AppSec/ObjectExtractor.cs
dromanol 30f4aba
Remove unneeded test
dromanol File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -569,6 +569,12 @@ private static bool IsCollectionType(Type type) | |||||
| return EmptyDictionary; | ||||||
| } | ||||||
|
|
||||||
| depth++; | ||||||
| if (depth >= WafConstants.MaxContainerDepth) | ||||||
| { | ||||||
| return EmptyDictionary; | ||||||
| } | ||||||
|
|
||||||
| var gtkvp = typeof(KeyValuePair<,>); | ||||||
| var tkvp = gtkvp.MakeGenericType(dictType.GetGenericArguments()); | ||||||
| var keyProp = tkvp.GetProperty("Key"); | ||||||
|
|
@@ -603,7 +609,7 @@ private static bool IsCollectionType(Type type) | |||||
| } | ||||||
| else | ||||||
| { | ||||||
| var extractedvalue = ExtractType(dictValue.GetType(), dictValue, depth + 1, visited, extractorCache, createExtractors, useSimpleDictionaryFormat); | ||||||
| var extractedvalue = ExtractType(dictValue.GetType(), dictValue, depth, visited, extractorCache, createExtractors, useSimpleDictionaryFormat); | ||||||
| items.Add(dictKey, extractedvalue); | ||||||
| } | ||||||
|
|
||||||
|
|
@@ -635,6 +641,14 @@ private static bool IsCollectionType(Type type) | |||||
| return []; | ||||||
| } | ||||||
|
|
||||||
| // Mirrors ExtractProperties: once the incremented depth reaches the limit, stop recursing | ||||||
| // into children instead of descending unbounded (which previously risked a stack overflow). | ||||||
| depth++; | ||||||
| if (depth >= WafConstants.MaxContainerDepth) | ||||||
| { | ||||||
| return []; | ||||||
| } | ||||||
|
|
||||||
| var gtkvp = typeof(KeyValuePair<,>); | ||||||
| var tkvp = gtkvp.MakeGenericType(dictType.GetGenericArguments()); | ||||||
| var keyProp = tkvp.GetProperty("Key"); | ||||||
|
|
@@ -654,8 +668,8 @@ private static bool IsCollectionType(Type type) | |||||
|
|
||||||
| var pair = new Dictionary<string, object?>(2) | ||||||
| { | ||||||
| ["Key"] = dictKey is null ? null : ExtractType(dictKey.GetType(), dictKey, depth + 1, visited, extractorCache, createExtractors, useSimpleDictionaryFormat), | ||||||
| ["Value"] = dictValue is null ? null : ExtractType(dictValue.GetType(), dictValue, depth + 1, visited, extractorCache, createExtractors, useSimpleDictionaryFormat), | ||||||
| ["Key"] = dictKey is null ? null : ExtractType(dictKey.GetType(), dictKey, depth, visited, extractorCache, createExtractors, useSimpleDictionaryFormat), | ||||||
| ["Value"] = dictValue is null ? null : ExtractType(dictValue.GetType(), dictValue, depth, visited, extractorCache, createExtractors, useSimpleDictionaryFormat), | ||||||
| }; | ||||||
| items.Add(pair); | ||||||
|
|
||||||
|
|
@@ -682,9 +696,14 @@ private static object ExtractNonGenericDictionary( | |||||
| { | ||||||
| var capacity = Math.Min(WafConstants.MaxContainerSize, source.Count); | ||||||
|
|
||||||
| // Mirrors ExtractProperties: once the incremented depth reaches the limit, stop recursing | ||||||
| // into children instead of descending unbounded (which previously risked a stack overflow). | ||||||
|
Comment on lines
+699
to
+700
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| depth++; | ||||||
| var depthExceeded = depth >= WafConstants.MaxContainerDepth; | ||||||
|
|
||||||
| if (useSimpleDictionaryFormat) | ||||||
| { | ||||||
| if (!visited.Add(source)) | ||||||
| if (!visited.Add(source) || depthExceeded) | ||||||
| { | ||||||
| return EmptyDictionary; | ||||||
| } | ||||||
|
|
@@ -700,7 +719,7 @@ private static object ExtractNonGenericDictionary( | |||||
| continue; | ||||||
| } | ||||||
|
|
||||||
| map[key] = entry.Value is null ? null : ExtractType(entry.Value.GetType(), entry.Value, depth + 1, visited, extractorCache, createExtractors, useSimpleDictionaryFormat); | ||||||
| map[key] = entry.Value is null ? null : ExtractType(entry.Value.GetType(), entry.Value, depth, visited, extractorCache, createExtractors, useSimpleDictionaryFormat); | ||||||
| if (map.Count >= WafConstants.MaxContainerSize) | ||||||
| { | ||||||
| break; | ||||||
|
|
@@ -710,7 +729,7 @@ private static object ExtractNonGenericDictionary( | |||||
| return map; | ||||||
| } | ||||||
|
|
||||||
| if (!visited.Add(source)) | ||||||
| if (!visited.Add(source) || depthExceeded) | ||||||
| { | ||||||
| return new List<object?>(); | ||||||
| } | ||||||
|
|
@@ -722,8 +741,8 @@ private static object ExtractNonGenericDictionary( | |||||
| var entry = enumerator.Entry; | ||||||
| items.Add(new Dictionary<string, object?>(2) | ||||||
| { | ||||||
| ["Key"] = entry.Key is null ? null : ExtractType(entry.Key.GetType(), entry.Key, depth + 1, visited, extractorCache, createExtractors, useSimpleDictionaryFormat), | ||||||
| ["Value"] = entry.Value is null ? null : ExtractType(entry.Value.GetType(), entry.Value, depth + 1, visited, extractorCache, createExtractors, useSimpleDictionaryFormat), | ||||||
| ["Key"] = entry.Key is null ? null : ExtractType(entry.Key.GetType(), entry.Key, depth, visited, extractorCache, createExtractors, useSimpleDictionaryFormat), | ||||||
| ["Value"] = entry.Value is null ? null : ExtractType(entry.Value.GetType(), entry.Value, depth, visited, extractorCache, createExtractors, useSimpleDictionaryFormat), | ||||||
| }); | ||||||
| if (items.Count >= WafConstants.MaxContainerSize) | ||||||
| { | ||||||
|
|
@@ -754,6 +773,12 @@ private static object ExtractNonGenericDictionary( | |||||
| return []; | ||||||
| } | ||||||
|
|
||||||
| depth++; | ||||||
| if (depth >= WafConstants.MaxContainerDepth) | ||||||
| { | ||||||
| return []; | ||||||
| } | ||||||
|
|
||||||
| // Use ICollection for pre-sizing when available. Some types (e.g. HashSet<T>) implement | ||||||
| // ICollection<T> but not the non-generic ICollection, so fall back to default capacity. | ||||||
| var items = value is ICollection sourceColl | ||||||
|
|
@@ -768,7 +793,7 @@ private static object ExtractNonGenericDictionary( | |||||
| } | ||||||
| else | ||||||
| { | ||||||
| var extractedvalue = ExtractType(item.GetType(), item, depth + 1, visited, extractorCache, createExtractors, useSimpleDictionaryFormat); | ||||||
| var extractedvalue = ExtractType(item.GetType(), item, depth, visited, extractorCache, createExtractors, useSimpleDictionaryFormat); | ||||||
| items.Add(extractedvalue); | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.