StubbleOrg / Stubble

Trimmed down {{mustache}} templates in .NET
Other
399 stars 58 forks source link

Possible null reference exception in compiled renderer #106

Closed natehitze closed 2 years ago

natehitze commented 4 years ago

It's possible to get the compiled renderer to throw a null reference exception by adding a null item to a list in the model and then iterating over the list and accessing a property in the template.

Model:

public class ExampleClass
{
    public string Bar { get; } = "Test"
}

var input = new 
{
    Examples = new List<ExampleClass>() { null }
}

Template:

{{#Examples}} {{Bar}} {{/Examples}}

If you execute a compiled renderer function for that template with the example input you will get a NullReferenceException. I ran into this on a simple template & input model, but it could be a nightmare to debug with a complex model & template since there's no debug information other than the exception type.

I think a null check in the expression returned by CompilerContext.Lookup would be helpful? In my case I would prefer an exception to be thrown that has some information such as the name of the variable that failed and location in the template or context path that led to the variable.

Romanx commented 2 years ago

Hey @natehitze,

Thanks for reporting this and submitting a PR. This was entirely an oversight in the compilation renderer design. I've approached the solution in a slightly different way which makes all access safe.

The feature is now on nuget a v1.10.8