You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@shiro.apache.org by bd...@apache.org on 2022/07/25 21:04:27 UTC

[shiro] branch backport-regex-pattern-matcher created (now f42ae2c7)

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

bdemers pushed a change to branch backport-regex-pattern-matcher
in repository https://gitbox.apache.org/repos/asf/shiro.git


      at f42ae2c7 Add support for case-insensitive matching to RegExPatternMatcher

This branch includes the following new commits:

     new f42ae2c7 Add support for case-insensitive matching to RegExPatternMatcher

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[shiro] 01/01: Add support for case-insensitive matching to RegExPatternMatcher

Posted by bd...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

bdemers pushed a commit to branch backport-regex-pattern-matcher
in repository https://gitbox.apache.org/repos/asf/shiro.git

commit f42ae2c79ca7268535f4fa907d36572b953a88a6
Author: Brian Demers <bd...@apache.org>
AuthorDate: Tue May 31 20:06:56 2022 -0400

    Add support for case-insensitive matching to RegExPatternMatcher
---
 .../org/apache/shiro/util/RegExPatternMatcher.java | 26 +++++++++++-
 .../apache/shiro/util/RegExPatternMatcherTest.java | 46 ++++++++++++++++++----
 2 files changed, 62 insertions(+), 10 deletions(-)

diff --git a/core/src/main/java/org/apache/shiro/util/RegExPatternMatcher.java b/core/src/main/java/org/apache/shiro/util/RegExPatternMatcher.java
index b251ac50..b07ce3d0 100644
--- a/core/src/main/java/org/apache/shiro/util/RegExPatternMatcher.java
+++ b/core/src/main/java/org/apache/shiro/util/RegExPatternMatcher.java
@@ -29,12 +29,18 @@ import java.util.regex.Matcher;
  */
 public class RegExPatternMatcher implements PatternMatcher {
 
+    private static final int DEFAULT = Pattern.DOTALL;
+
+    private static final int CASE_INSENSITIVE = DEFAULT | Pattern.CASE_INSENSITIVE;
+
+    private boolean caseInsensitive = false;
+
     /**
      * Simple implementation that merely uses the default pattern comparison logic provided by the
      * JDK.
      * <p/>This implementation essentially executes the following:
      * <pre>
-     * Pattern p = Pattern.compile(pattern);
+     * Pattern p = Pattern.compile(pattern, Pattern.DOTALL);
      * Matcher m = p.matcher(source);
      * return m.matches();</pre>
      * @param pattern the pattern to match against
@@ -45,8 +51,24 @@ public class RegExPatternMatcher implements PatternMatcher {
         if (pattern == null) {
             throw new IllegalArgumentException("pattern argument cannot be null.");
         }
-        Pattern p = Pattern.compile(pattern);
+        Pattern p = Pattern.compile(pattern, caseInsensitive ? CASE_INSENSITIVE : DEFAULT);
         Matcher m = p.matcher(source);
         return m.matches();
     }
+
+    /**
+     * Returns true if regex match should be case-insensitive.
+     * @return true if regex match should be case-insensitive.
+     */
+    public boolean isCaseInsensitive() {
+        return caseInsensitive;
+    }
+
+    /**
+     * Adds the Pattern.CASE_INSENSITIVE flag when compiling patterns.
+     * @param caseInsensitive true if patterns should match case-insensitive.
+     */
+    public void setCaseInsensitive(boolean caseInsensitive) {
+        this.caseInsensitive = caseInsensitive;
+    }
 }
diff --git a/core/src/test/java/org/apache/shiro/util/RegExPatternMatcherTest.java b/core/src/test/java/org/apache/shiro/util/RegExPatternMatcherTest.java
index 5246ba60..dc42368d 100644
--- a/core/src/test/java/org/apache/shiro/util/RegExPatternMatcherTest.java
+++ b/core/src/test/java/org/apache/shiro/util/RegExPatternMatcherTest.java
@@ -21,8 +21,6 @@ package org.apache.shiro.util;
 import org.junit.Test;
 import static org.junit.Assert.*;
 
-import java.util.regex.Pattern;
-
 /**
  * Unit tests for the {@link RegExPatternMatcher}.
  *
@@ -32,12 +30,44 @@ public class RegExPatternMatcherTest {
 
     @Test
     public void testSimplePattern() {
-        PatternMatcher pm = new RegExPatternMatcher();
-        String pattern = "a*b";
-        String test = "aaaaaaab";
-        //not necessary for the test, but Idea performs auto validation when it sees this:
-        Pattern.compile(pattern);
-        assertTrue(pm.matches(pattern, test));
+        assertPatternMatch("a*b", "aaaaaaab");
+    }
+
+    @Test
+    public void testMatchesWithCarriageReturn() {
+        assertPatternMatch(".*", "/blah\n");
+    }
+
+    @Test
+    public void testMatchesWithLineFeed() {
+        assertPatternMatch(".*", "/blah\r");
+    }
+
+    @Test
+    public void testCaseInsensitive() {
+        RegExPatternMatcher pm = new RegExPatternMatcher();
+        pm.setCaseInsensitive(true);
+        assertPatternMatch("/blah", "/BlaH", pm);
     }
 
+    @Test
+    public void testCaseSensitive() {
+        assertPatternNotMatch("/blah", "/BlaH");
+    }
+
+    private void assertPatternMatch(String pattern, String path) {
+        assertPatternMatch(pattern, path, new RegExPatternMatcher());
+    }
+
+    private void assertPatternMatch(String pattern, String path, PatternMatcher pm) {
+        assertTrue("Expected path '" + path + "' to match pattern '" + pattern + "'" , pm.matches(pattern, path));
+    }
+
+    private void assertPatternNotMatch(String pattern, String path) {
+        assertPatternNotMatch(pattern, path, new RegExPatternMatcher());
+    }
+
+    private void assertPatternNotMatch(String pattern, String path, PatternMatcher pm) {
+        assertFalse("Expected path '" + path + "' to NOT match pattern '" + pattern + "'" , pm.matches(pattern, path));
+    }
 }