SEBWIN-314: Corrected simplified rule implementation for alphanumeric expression.
This commit is contained in:
		
							parent
							
								
									42eccef565
								
							
						
					
					
						commit
						4b415a5f45
					
				
					 2 changed files with 14 additions and 18 deletions
				
			
		| 
						 | 
					@ -80,24 +80,26 @@ namespace SafeExamBrowser.Browser.UnitTests.Filters.Rules
 | 
				
			||||||
		[TestMethod]
 | 
							[TestMethod]
 | 
				
			||||||
		public void TestAlphanumericExpression()
 | 
							public void TestAlphanumericExpression()
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			var expression = "hostname-123";
 | 
								var expression = "hostname123";
 | 
				
			||||||
			var positive = new[]
 | 
								var positive = new[]
 | 
				
			||||||
			{
 | 
								{
 | 
				
			||||||
				$"scheme://{expression}",
 | 
									$"scheme://{expression}"
 | 
				
			||||||
				// TODO: All these below should not be allowed!
 | 
								};
 | 
				
			||||||
 | 
								var negative = new[]
 | 
				
			||||||
 | 
								{
 | 
				
			||||||
 | 
									$"scheme://hostname",
 | 
				
			||||||
 | 
									$"scheme://hostname1",
 | 
				
			||||||
 | 
									$"scheme://hostname12",
 | 
				
			||||||
 | 
									$"scheme://hostname1234",
 | 
				
			||||||
				$"scheme://{expression}.org",
 | 
									$"scheme://{expression}.org",
 | 
				
			||||||
				$"scheme://www.{expression}.org",
 | 
									$"scheme://www.{expression}.org",
 | 
				
			||||||
				$"scheme://subdomain.{expression}.com",
 | 
									$"scheme://subdomain.{expression}.com",
 | 
				
			||||||
				$"scheme://www.realhost.{expression}",
 | 
									$"scheme://www.realhost.{expression}",
 | 
				
			||||||
				$"scheme://subdomain-1.subdomain-2.{expression}.org",
 | 
									$"scheme://subdomain-1.subdomain-2.{expression}.org",
 | 
				
			||||||
				$"scheme://user:password@www.{expression}.org/path/file.txt?param=123#fragment"
 | 
									$"scheme://user:password@www.{expression}.org/path/file.txt?param=123#fragment",
 | 
				
			||||||
			};
 | 
					 | 
				
			||||||
			var negative = new[]
 | 
					 | 
				
			||||||
			{
 | 
					 | 
				
			||||||
				$"scheme://hostname",
 | 
					 | 
				
			||||||
				$"scheme://{expression}4",
 | 
									$"scheme://{expression}4",
 | 
				
			||||||
				$"scheme://hostname.org",
 | 
									$"scheme://hostname.org",
 | 
				
			||||||
				$"scheme://hostname-12.org",
 | 
									$"scheme://hostname12.org",
 | 
				
			||||||
				$"scheme://{expression}4.org",
 | 
									$"scheme://{expression}4.org",
 | 
				
			||||||
				$"scheme://{expression}.realhost.org",
 | 
									$"scheme://{expression}.realhost.org",
 | 
				
			||||||
				$"scheme://subdomain.{expression}.realhost.org",
 | 
									$"scheme://subdomain.{expression}.realhost.org",
 | 
				
			||||||
| 
						 | 
					@ -892,7 +894,7 @@ namespace SafeExamBrowser.Browser.UnitTests.Filters.Rules
 | 
				
			||||||
			Test(expression, positive, negative);
 | 
								Test(expression, positive, negative);
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		private void Test(string expression, string[] positive, string[] negative, bool testLegacy = true) // TODO: Remove flag once issues cleared!
 | 
							private void Test(string expression, string[] positive, string[] negative, bool testLegacy = true)
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			var legacy = new LegacyFilter(expression);
 | 
								var legacy = new LegacyFilter(expression);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -93,20 +93,14 @@ namespace SafeExamBrowser.Browser.Filters.Rules
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		private void ParseHost(string expression)
 | 
							private void ParseHost(string expression)
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			var hasToplevelDomain = Regex.IsMatch(expression, @"\.+");
 | 
								var isAlphanumeric = Regex.IsMatch(expression, @"^[a-zA-Z0-9]+$");
 | 
				
			||||||
			var hasSubdomain = Regex.IsMatch(expression, @"\.{2,}");
 | 
					 | 
				
			||||||
			var matchExactSubdomain = expression.StartsWith(".");
 | 
								var matchExactSubdomain = expression.StartsWith(".");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			expression = matchExactSubdomain ? expression.Substring(1) : expression;
 | 
								expression = matchExactSubdomain ? expression.Substring(1) : expression;
 | 
				
			||||||
			expression = Regex.Escape(expression);
 | 
								expression = Regex.Escape(expression);
 | 
				
			||||||
			expression = ReplaceWildcard(expression);
 | 
								expression = ReplaceWildcard(expression);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			if (!hasToplevelDomain)
 | 
								if (!isAlphanumeric && !matchExactSubdomain)
 | 
				
			||||||
			{
 | 
					 | 
				
			||||||
				expression = $@"{expression}(\.[a-z]+)?";
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
			if (!hasSubdomain && !matchExactSubdomain)
 | 
					 | 
				
			||||||
			{
 | 
								{
 | 
				
			||||||
				expression = $@"(.+?\.)*{expression}";
 | 
									expression = $@"(.+?\.)*{expression}";
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue