JAMES-2529 Emailer matching should be allowed for JMAP filtering addresses

Previous versions was matching too widely as `"me" <[email protected]>` was 
matching `"me" <[email protected]>`.

We need to significantly complexify the boolean logic as a user might just 
specify the personal part (`me`) which will be tranlated as `"me" <me>` and 
should be matching `"me" <[email protected]>`...


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/85fa1c50
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/85fa1c50
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/85fa1c50

Branch: refs/heads/master
Commit: 85fa1c50cc3d715c950914352635f5f728436c0a
Parents: 9fc30a4
Author: Benoit Tellier <[email protected]>
Authored: Tue Oct 30 10:09:44 2018 +0700
Committer: Benoit Tellier <[email protected]>
Committed: Wed Oct 31 08:48:30 2018 +0700

----------------------------------------------------------------------
 .../jmap/mailet/filter/ContentMatcher.java      | 16 +++++++++----
 .../jmap/mailet/filter/JMAPFilteringTest.java   | 25 ++++++++++++++++++++
 2 files changed, 37 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/85fa1c50/server/protocols/jmap/src/main/java/org/apache/james/jmap/mailet/filter/ContentMatcher.java
----------------------------------------------------------------------
diff --git 
a/server/protocols/jmap/src/main/java/org/apache/james/jmap/mailet/filter/ContentMatcher.java
 
b/server/protocols/jmap/src/main/java/org/apache/james/jmap/mailet/filter/ContentMatcher.java
index 884f1b6..754ee82 100644
--- 
a/server/protocols/jmap/src/main/java/org/apache/james/jmap/mailet/filter/ContentMatcher.java
+++ 
b/server/protocols/jmap/src/main/java/org/apache/james/jmap/mailet/filter/ContentMatcher.java
@@ -62,10 +62,18 @@ public interface ContentMatcher {
             }
         }
 
-        boolean exactMatch(AddressHeader other) {
+        boolean matchesIgnoreCase(AddressHeader other) {
+            boolean sameAddress = OptionalUtils.matches(address, 
other.address, String::equalsIgnoreCase);
+            boolean samePersonal = OptionalUtils.matches(personal, 
other.personal, String::equalsIgnoreCase);
+            boolean personalMatchesAddress = OptionalUtils.matches(personal, 
other.address, String::equalsIgnoreCase);
+            boolean addressMatchesPersonal = OptionalUtils.matches(address, 
other.personal, String::equalsIgnoreCase);
+
             return fullAddress.equalsIgnoreCase(other.fullAddress)
-                || OptionalUtils.matches(address, other.address, 
String::equalsIgnoreCase)
-                || OptionalUtils.matches(personal, other.personal, 
String::equalsIgnoreCase);
+                || (sameAddress && samePersonal)
+                || (sameAddress && !personal.isPresent())
+                || (samePersonal && !address.isPresent())
+                || (personalMatchesAddress && sameAddress)
+                || (addressMatchesPersonal && samePersonal);
         }
     }
 
@@ -78,7 +86,7 @@ public interface ContentMatcher {
                 .orElse(new AddressHeader(valueToMatch));
 
             return contents.map(ContentMatcher::asAddressHeader)
-                .anyMatch(addressHeaderToMatch::exactMatch);
+                .anyMatch(addressHeaderToMatch::matchesIgnoreCase);
         }
 
     }

http://git-wip-us.apache.org/repos/asf/james-project/blob/85fa1c50/server/protocols/jmap/src/test/java/org/apache/james/jmap/mailet/filter/JMAPFilteringTest.java
----------------------------------------------------------------------
diff --git 
a/server/protocols/jmap/src/test/java/org/apache/james/jmap/mailet/filter/JMAPFilteringTest.java
 
b/server/protocols/jmap/src/test/java/org/apache/james/jmap/mailet/filter/JMAPFilteringTest.java
index 2105c43..0aab648 100644
--- 
a/server/protocols/jmap/src/test/java/org/apache/james/jmap/mailet/filter/JMAPFilteringTest.java
+++ 
b/server/protocols/jmap/src/test/java/org/apache/james/jmap/mailet/filter/JMAPFilteringTest.java
@@ -292,6 +292,11 @@ class JMAPFilteringTest {
                         .valueToMatch(USER_1_FULL_ADDRESS),
 
                     argumentBuilder(fieldAndHeader.field)
+                        .description("Exact match in a full " + 
fieldAndHeader.headerName + " header with a symetric emailer")
+                        .header(fieldAndHeader.headerName, 
"\"[email protected]\" <[email protected]>")
+                        .valueToMatch("[email protected]"),
+
+                    argumentBuilder(fieldAndHeader.field)
                         .description("Username exact match in a username only 
" + fieldAndHeader.headerName + " header")
                         .header(fieldAndHeader.headerName, USER_1_USERNAME)
                         .valueToMatch(USER_1_USERNAME),
@@ -491,6 +496,26 @@ class JMAPFilteringTest {
                         .valueToMatch(SHOULD_NOT_MATCH),
 
                     argumentBuilder(fieldAndHeader.field)
+                        .description("Nomatch when different address in a 
fully specified emailer for " + fieldAndHeader.headerName + " field")
+                        .header(fieldAndHeader.headerName, "\"me\" 
<[email protected]>")
+                        .valueToMatch("\"me\" <[email protected]>"),
+
+                    argumentBuilder(fieldAndHeader.field)
+                        .description("Nomatch when different username in a 
fully specified emailer for " + fieldAndHeader.headerName + " field")
+                        .header(fieldAndHeader.headerName, "\"notme\" 
<[email protected]>")
+                        .valueToMatch("\"definitlyme\" <[email protected]>"),
+
+                    argumentBuilder(fieldAndHeader.field)
+                        .description("No match in a full " + 
fieldAndHeader.headerName + " header with a symetric emailer - different 
personal")
+                        .header(fieldAndHeader.headerName, 
"\"[email protected]\" <[email protected]>")
+                        .valueToMatch("\"[email protected]\" <[email protected]>"),
+
+                    argumentBuilder(fieldAndHeader.field)
+                        .description("No match in a full " + 
fieldAndHeader.headerName + " header with a symetric emailer - different 
address")
+                        .header(fieldAndHeader.headerName, 
"\"[email protected]\" <[email protected]>")
+                        .valueToMatch("\"[email protected]\" <[email protected]>"),
+
+                    argumentBuilder(fieldAndHeader.field)
                         .description("Nomatch in a missing " + 
fieldAndHeader.headerName + " header")
                         .valueToMatch(SHOULD_NOT_MATCH),
 


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to