using OpenWaf.Domain.Security; namespace OpenWaf.Tests.Unit; /// /// The pattern language behind a request rule. It is evaluated on the request /// path of every request, so its semantics are security semantics: an operator /// writes /.env* expecting /.env.local to be refused, and a /// matcher that disagrees does not fail loudly — it serves the file. /// public sealed class PathPatternTests { [Theory] [InlineData("/.env", "/.env", true)] // anchored exact [InlineData("/.env", "/.env.local", false)] // a prefix is not a match [InlineData("/.env*", "/.env", true)] // * matches the empty run [InlineData("/.env*", "/.env.local", true)] [InlineData("/admin*", "/admin/users/1", true)] // * crosses / [InlineData("/admin/*", "/admin/", true)] // * may match the empty run [InlineData("/wp-login.php", "/wp-login.php", true)] [InlineData("/?.html", "/a.html", true)] [InlineData("/?.html", "/ab.html", false)] [InlineData("/A*B", "/axxxB", true)] // case-insensitive [InlineData("/a*b", "/A.X.B", true)] // * crosses . as it crosses / [InlineData("/a-b", "/a.b", false)] // a literal dash is a dash [InlineData("/*", "/anything/at/all", true)] [InlineData("/x", "/x/", false)] [InlineData("/x/", "/x", false)] public void The_glob_means_what_the_operator_wrote(string pattern, string path, bool expected) => Assert.Equal(expected, PathPattern.Matches(pattern, path)); [Theory] [InlineData(null)] [InlineData("")] [InlineData(" ")] [InlineData(".env")] // cannot match a path, so it can only ever do nothing [InlineData("*/wp-admin")] [InlineData("*")] // every request on the site [InlineData("/*")] [InlineData("/**")] [InlineData("/?")] [InlineData("/.env\t")] // a control character is not a path byte [InlineData("/.env\u007f")] public void A_pattern_that_could_only_silently_do_nothing_is_refused(string? pattern) => Assert.NotNull(PathPattern.ExplainRejection(pattern)); [Theory] [InlineData("/.env*")] [InlineData("/admin/*")] [InlineData("/a?b")] [InlineData("/search?q")] // a query string is not matched, but the path may hold it [InlineData("/x y")] public void A_pattern_that_can_name_a_real_path_is_accepted(string pattern) => Assert.Null(PathPattern.ExplainRejection(pattern)); [Fact] public void The_rejection_says_what_is_wrong_rather_than_only_that_something_is() { Assert.Contains("must start with /", PathPattern.ExplainRejection("wp-admin")); Assert.Contains("more specific than every request", PathPattern.ExplainRejection("/*")); Assert.Contains($"{PathPattern.MaxLength} characters", PathPattern.ExplainRejection("/" + new string('a', PathPattern.MaxLength))); } [Fact] public void A_long_pattern_is_refused_because_it_is_matched_on_every_request() => Assert.False(PathPattern.IsValid("/" + new string('a', PathPattern.MaxLength))); [Fact] public void Matching_allocates_nothing_exotic_and_survives_a_path_of_repeated_a() { // The reason this is a glob and not a regex: (a+)+$ against 40 a's is // the textbook exponential backtrack, and the pattern would be the // operator's while the path is the visitor's. var pattern = "/a*a*a*a*a*a*a*b"; var path = "/" + new string('a', 400); Assert.False(PathPattern.Matches(pattern, path)); Assert.True(PathPattern.Matches(pattern, path + "b")); } }