Handlebars-Net / Handlebars.Net

A real .NET Handlebars engine

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Consecutive slashes and more than 1 slash within square brackets does not escape properly

Hoeksema opened this issue · comments

Describe the bug

We have a use case where our JSON data contains a field like this:

{
  "https://some-uri.com/foo/bar_baz": {
    "id": 42
  }
}

And we want to parse out the 42 like so:

{{[https://some-uri.com/foo/bar_baz].id}}

Expected behavior:

According to the handlebars spec and various other implementations, this should yield 42. However, we instead see:

Unhandled exception. System.IndexOutOfRangeException: Index was outside the bounds of the array.

After some testing we figured out it is caused by // in the template string. We then tried removing just //:

{
  "https:some-uri.com/foo/bar_baz": {
    "id": 42
  }
}

{{[https:some-uri.com/foo/bar_baz].id}}

While the above doesn't throw an exception, it fails to resolve at all. (returns empty)

Some more debugging, we found that it is caused by the forward slashes, as the following does properly return 42 (keeping at most one /, not multiple):

{
  "https:some-uri.com/foobar_baz": {
    "id": 42
  }
}

{{[https:some-uri.com/foobar_baz].id}}

We expect that the original case properly returns 42, as [ ] should properly escape the whole URI. It seems like the current HandleBars.Net implementation special-cases escaping of / only for a single / within square brackets, and multiple consecutive / gives an unexpected exception.

Test to reproduce

using HandlebarsDotNet;
using HandlebarsDotNet.Extension.NewtonsoftJson;
using Newtonsoft.Json.Linq;

[Fact]
public void Descriptive_Test_Name_Here()
{
    var source = "{{[https://some-uri.com/foo/bar_baz].id}}";
    var dataJson = "{\"https://some-uri.com/foo/bar_baz\":{\"id\":42}}";

    var handlebars = Handlebars.Create();
    handlebars.Configuration.UseNewtonsoftJson();

    var template = handlebars.Compile(source);
    var actual = template(JObject.Parse(dataJson));

    Assert.Equal(42, actual);
}

Other related info


After submitting the issue

Please consider contributing to the project by submitting a PR with a fix to the issue.
This would help to solve your problem in a shorter time as well as help other users of the project.

In case you do not know where to start - feel free to ask for help in the issue thread.

Building an active community is essential for any project survival as time of maintainers is limited.