Skip to content
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

Fix: Ensures strict regex matching by adding negative lookbehind #68

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/UriTemplateTests/UriTemplateTableTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ public class UriTemplateTableTests
[Theory,
InlineData("/","root"),
InlineData("/baz/fod/burg",""),
InlineData("/baz/kit", "kit"),
InlineData("http://www.example.com/baz/kit", "kit"),
InlineData("/baz/fod", "baz"),
InlineData("/baz/fod/blob", "blob"),
InlineData("/glah/flid/blob", "goo")]
InlineData("/glah/flid/blob", "goo"),
InlineData("/settings/{id}", "set"),
InlineData("/organization/{id}/settings/iteminsights", "org")]
public void FindPathTemplates(string url, string key)
{
var table = new UriTemplateTable(); // Shorter paths and literal path segments should be added to the table first.
Expand All @@ -31,7 +33,10 @@ public void FindPathTemplates(string url, string key)
table.Add("baz", new UriTemplate("/baz/{bar}"));
table.Add("blob", new UriTemplate("/baz/{bar}/blob"));
table.Add("goo", new UriTemplate("/{goo}/{bar}/blob"));
table.Add("set", new UriTemplate("/settings/{id}"));
table.Add("org", new UriTemplate("/organization/{id}/settings/iteminsights"));

var uri = new Uri(url, UriKind.RelativeOrAbsolute);

var result = table.Match(new Uri(url, UriKind.RelativeOrAbsolute));

Expand Down
20 changes: 8 additions & 12 deletions src/UriTemplates/UriTemplate.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Tavis.UriTemplates
Expand Down Expand Up @@ -36,7 +32,7 @@ public class UriTemplate
private readonly string _template;
private readonly Dictionary<string, object> _Parameters;
private enum States { CopyingLiterals, ParsingExpression }


private readonly bool _resolvePartially;

Expand Down Expand Up @@ -191,8 +187,8 @@ private void ProcessExpression(StringBuilder currentExpression, Result result)
varSpec = new VarSpec(op);
if (success || !isFirst || _resolvePartially) varSpec.First = false;
if (!success && _resolvePartially) {result.Append(",") ; }
break;
break;


default:
if (IsVarNameChar(currentChar))
Expand Down Expand Up @@ -406,10 +402,10 @@ public static string CreateMatchingRegex(string uriTemplate)
default:
return GetExpression(paramNames);
}

});

return regex +"$";
return "(?<!.)\\" + regex + "$"; // add negative lookbehind to strictly match this regex
}

public static string CreateMatchingRegex2(string uriTemplate)
Expand Down Expand Up @@ -456,7 +452,7 @@ private static string GetQueryExpression(List<String> paramNames, string prefix)
sb.Append("(?:");
sb.Append(paramname);
sb.Append("=");

sb.Append("(?<");
sb.Append(paramname);
sb.Append(">");
Expand Down Expand Up @@ -519,8 +515,8 @@ private static string GetExpression(List<String> paramNames, string prefix = nul
return sb.ToString();
}


}


}
17 changes: 14 additions & 3 deletions src/UriTemplates/UriTemplateTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,28 @@ namespace Tavis.UriTemplates
public class UriTemplateTable
{
private Dictionary<string,UriTemplate> _Templates = new Dictionary<string,UriTemplate>();

public void Add(string key, UriTemplate template)
{
_Templates.Add(key,template);
}

public TemplateMatch Match(Uri url)
{
foreach (var template in _Templates )
if (url == null)
{
throw new ArgumentNullException(nameof(url), "Value cannot be null.");
}

Uri absolutePath = url;
if (url.IsAbsoluteUri)
{
absolutePath = new Uri(url.AbsolutePath, UriKind.Relative);
}

foreach (var template in _Templates)
{
var parameters = template.Value.GetParameters(url);
var parameters = template.Value.GetParameters(absolutePath);
if (parameters != null)
{
return new TemplateMatch() { Key = template.Key, Parameters = parameters, Template = template.Value };
Expand Down