You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2019/11/11 02:05:51 UTC

[james-project] 08/09: JAMES-2950 Fix bugged behavior of regex rewriter

This is an automated email from the ASF dual-hosted git repository.

btellier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit b43cd8c5354550fb5ae417869dab3550274439c0
Author: Rene Cordier <rc...@linagora.com>
AuthorDate: Mon Nov 4 11:42:17 2019 +0700

    JAMES-2950 Fix bugged behavior of regex rewriter
    
    With more restrictions on the Username and strong typing enforcement, it raised an issue with RegexRewriter.
    Its behavior was bugged. If there was an issue with the regex mapping processing, it would still return an
    Optional of User with "regex:{REGEX}".
    
    First it is wrong, if the process failed, we should return Optional.empty().
    
    Second, it appeared clearly that the corresponding cucumber tests were written badly to pass that buggy behavior, in a cheating way.
    Except that with the new restrictions on the Username, some characters used in regex are not allowed anymore, thus highlighting the problem.
    The regex mapping, to be valid during the rewrite process, needs to follow this pattern: <regular-expression>:<parameterized-string>
---
 .../org/apache/james/rrt/lib/UserRewritter.java    |  2 +-
 .../test/resources/cucumber/rewrite_tables.feature | 31 ++++++++++++----------
 2 files changed, 18 insertions(+), 15 deletions(-)

diff --git a/server/data/data-api/src/main/java/org/apache/james/rrt/lib/UserRewritter.java b/server/data/data-api/src/main/java/org/apache/james/rrt/lib/UserRewritter.java
index e21dde6..648ce17 100644
--- a/server/data/data-api/src/main/java/org/apache/james/rrt/lib/UserRewritter.java
+++ b/server/data/data-api/src/main/java/org/apache/james/rrt/lib/UserRewritter.java
@@ -90,7 +90,7 @@ public interface UserRewritter extends Serializable {
                         .map(Username::of);
                 } catch (PatternSyntaxException e) {
                     LOGGER.error("Exception during regexMap processing: ", e);
-                    return Optional.of(Username.of(Mapping.Type.Regex.asPrefix() + mapping));
+                    return Optional.empty();
                 }
             };
         }
diff --git a/server/data/data-library/src/test/resources/cucumber/rewrite_tables.feature b/server/data/data-library/src/test/resources/cucumber/rewrite_tables.feature
index 27802cd..bfc5d75 100644
--- a/server/data/data-library/src/test/resources/cucumber/rewrite_tables.feature
+++ b/server/data/data-library/src/test/resources/cucumber/rewrite_tables.feature
@@ -7,30 +7,33 @@ Feature: Rewrite Tables tests
 # Regexp mapping
 
   Scenario: stored regexp mapping should be retrieved when one mapping matching
-    Given store "(.*)@localhost" regexp mapping for user "test" at domain "localhost"
-    Then mappings for user "test" at domain "localhost" should contain only "regex:(.*)@localhost"
+    Given store "(.*)@localhost:user@localhost" regexp mapping for user "test" at domain "localhost"
+    Then mappings for user "test" at domain "localhost" should contain only "user@localhost"
 
   Scenario: stored regexp mapping should be retrieved when two mappings matching
-    Given store "(.*)@localhost" regexp mapping for user "test" at domain "localhost"
-    And store "(.+)@test" regexp mapping for user "test" at domain "localhost"
-    Then mappings for user "test" at domain "localhost" should contain only "regex:(.*)@localhost, regex:(.+)@test"
+    Given store "(.*)@localhost:user@localhost" regexp mapping for user "test" at domain "localhost"
+    And store "tes(.+)@localhost:user2@localhost" regexp mapping for user "test" at domain "localhost"
+    Then mappings for user "test" at domain "localhost" should contain only "user@localhost, user2@localhost"
 
   Scenario: stored regexp mapping should not be retrieved by another user
-    Given store "(.*)@localhost" regexp mapping for user "test" at domain "localhost"
-    And store "(.+)@test" regexp mapping for user "test" at domain "localhost"
+    Given store "(.*)@localhost:user@localhost" regexp mapping for user "test" at domain "localhost"
+    And store "tes(.+)@localhost:user2@localhost" regexp mapping for user "test" at domain "localhost"
     Then mappings for user "test2" at domain "localhost" should be empty
 
   Scenario: removing a stored regexp mapping should work
-    Given store "(.*)@localhost" regexp mapping for user "test" at domain "localhost"
-    And store "(.+)@test" regexp mapping for user "test" at domain "localhost"
-    When user "test" at domain "localhost" removes a regexp mapping "(.+)@test"
-    Then mappings for user "test" at domain "localhost" should contain only "regex:(.*)@localhost"
+    Given store "(.*)@localhost:user@localhost" regexp mapping for user "test" at domain "localhost"
+    And store "tes(.+)@localhost:user2@localhost" regexp mapping for user "test" at domain "localhost"
+    When user "test" at domain "localhost" removes a regexp mapping "tes(.+)@localhost:user2@localhost"
+    Then mappings for user "test" at domain "localhost" should contain only "user@localhost"
 
-  @readonly
   Scenario: storing an invalid regexp mapping should not work
     When store an invalid ".*):" regexp mapping for user "test" at domain "localhost"
     Then a "InvalidRegexException" exception should have been thrown
 
+  Scenario: storing a wrong formatted regexp mapping should not be retrieved
+    Given store "(.*)@localhost" regexp mapping for user "test" at domain "localhost"
+    Then mappings for user "test" at domain "localhost" should be empty
+
 # Address mapping
 
   Scenario: stored address mapping should be retrieved when one mapping matching
@@ -174,9 +177,9 @@ Feature: Rewrite Tables tests
 
   Scenario: mixed mapping should work
     Given store "test2@localhost" address mapping for user "test" at domain "localhost"
-    And store "(.*)@localhost" regexp mapping for user "test" at domain "localhost"
+    And store "(.*)@localhost:user@localhost" regexp mapping for user "test" at domain "localhost"
     And store "aliasdomain" alias domain mapping for domain "localhost"
-    Then mappings for user "test" at domain "localhost" should contain only "test2@localhost, regex:(.*)@localhost"
+    Then mappings for user "test" at domain "localhost" should contain only "test2@localhost, user@localhost"
 
 # Recursive mapping
 


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org