From d5c3ae3d191b7041168c2671c0e3cb9f509c4fcf Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Sat, 10 Feb 2018 09:35:14 -0500 Subject: [PATCH] guard against infinite recursion and loops --- src/Android/AutofillService.cs | 39 ++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/src/Android/AutofillService.cs b/src/Android/AutofillService.cs index 861cfda88..8df33a30e 100644 --- a/src/Android/AutofillService.cs +++ b/src/Android/AutofillService.cs @@ -19,6 +19,7 @@ namespace Bit.Android { private NotificationChannel _notificationChannel; + private const string BitwardenTag = "bw_access"; private const int AutoFillNotificationId = 34573; private const string SystemUiPackage = "com.android.systemui"; private const string BitwardenPackage = "com.x8bit.bitwarden"; @@ -439,42 +440,44 @@ namespace Bit.Android private NodeList GetWindowNodes(AccessibilityNodeInfo n, AccessibilityEvent e, Func condition, bool disposeIfUnused, NodeList nodes = null, - int recursionDepth = 0, AccessibilityNodeInfo parentNode = null) + int recursionDepth = 0) { if(nodes == null) { nodes = new NodeList(); } - //global::Android.Util.Log.Info("bw_access", "node: " + n.ToString()); - //global::Android.Util.Log.Info("bw_access", "recursiveIterations = " + recursiveIterations); - - var sameAsParent = n?.GetHashCode() == parentNode?.GetHashCode(); - if(sameAsParent) + var dispose = disposeIfUnused; + if(n != null && recursionDepth < 50) { - global::Android.Util.Log.Info("bw_access", "child is same as parent!"); - } - - if(n != null && recursionDepth < 50 && !sameAsParent) - { - var dispose = disposeIfUnused; if(n.WindowId == e.WindowId && !(n.ViewIdResourceName?.StartsWith(SystemUiPackage) ?? false) && condition(n)) { dispose = false; nodes.Add(n); } - //global::Android.Util.Log.Info("bw_access", "ChildCount " + n.ChildCount); for(var i = 0; i < n.ChildCount; i++) { var childNode = n.GetChild(i); - GetWindowNodes(childNode, e, condition, true, nodes, recursionDepth++, n); + if(i > 100) + { + global::Android.Util.Log.Info(BitwardenTag, "Too many child iterations."); + } + else if(childNode.GetHashCode() == n.GetHashCode()) + { + global::Android.Util.Log.Info(BitwardenTag, + "Child node is the same as parent for some reason."); + } + else + { + GetWindowNodes(childNode, e, condition, true, nodes, recursionDepth++); + } } + } - if(dispose) - { - n.Dispose(); - } + if(dispose) + { + n?.Dispose(); } return nodes;