sebastienros / esprima-dotnet

Esprima .NET (BSD license) is a .NET port of the esprima.org project. It is a standard-compliant ECMAScript parser (also popularly known as JavaScript).
BSD 3-Clause "New" or "Revised" License
430 stars 75 forks source link

DescendantNodes extension method throws NullReferenceException #230

Closed forensicmike closed 2 years ago

forensicmike commented 2 years ago

Hi,

Currently on the latest nuget package version of 2.1.3.

I am getting a NullReferenceException with the following code which is meant to locate all VariableDeclaration nodes in a script.

script.ChildNodes.SelectMany(z => z.DescendantNodesAndSelf().OfType<VariableDelcaration>()).ToList();

After some debugging, I traced the problem to this method contained within Esprima.Ast.NodeExtensions:

image

On the line node.ChildNodes, node is not null checked, and seems to be null quite a lot of the time.

I created my own extension method that checks for null and the exceptions went away. E.g.

private static IEnumerable<Node> DescendantNodes(List<Node> nodes)
        {
            while (nodes.Count > 0)
            {
                Node node = nodes[0];
                nodes.RemoveAt(0);
                yield return node;
                if (node is not null)
                {
                    nodes.InsertRange(0, node.ChildNodes);
                }
            }
        }
sebastienros commented 2 years ago

Can you share the JS script that generates a null value here?

forensicmike commented 2 years ago

Hi, I found a publicly available script I can share that seems to trigger it: https://raw.githubusercontent.com/0xdea/frida-scripts/master/raptor_frida_ios_trace.js

forensicmike commented 2 years ago

I should note that with the null check in place, everything else seems to work great.