SEC-551: Convert RegExpBasedFilterInvocationDefinitionMap and DaoX509AuthoritiesPopulator to use JDK regexps. Removed ORO dependency from the project.

This commit is contained in:
Luke Taylor 2007-09-10 23:09:36 +00:00
parent 6eb17c8546
commit 448e8cfb42
5 changed files with 25 additions and 73 deletions

View File

@ -77,11 +77,6 @@
<artifactId>commons-codec</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>oro</groupId>
<artifactId>oro</artifactId>
<version>2.0.8</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>

View File

@ -20,17 +20,13 @@ import org.acegisecurity.ConfigAttributeDefinition;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.oro.text.regex.MalformedPatternException;
import org.apache.oro.text.regex.Pattern;
import org.apache.oro.text.regex.PatternMatcher;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Perl5Matcher;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Vector;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
/**
@ -57,21 +53,13 @@ public class RegExpBasedFilterInvocationDefinitionMap extends AbstractFilterInvo
//~ Methods ========================================================================================================
public void addSecureUrl(String perl5RegExp, ConfigAttributeDefinition attr) {
Pattern compiledPattern;
Perl5Compiler compiler = new Perl5Compiler();
public void addSecureUrl(String regExp, ConfigAttributeDefinition attr) {
Pattern pattern = Pattern.compile(regExp);
try {
compiledPattern = compiler.compile(perl5RegExp, Perl5Compiler.READ_ONLY_MASK);
} catch (MalformedPatternException mpe) {
throw new IllegalArgumentException("Malformed regular expression: " + perl5RegExp);
}
requestMap.add(new EntryHolder(compiledPattern, attr));
requestMap.add(new EntryHolder(pattern, attr));
if (logger.isDebugEnabled()) {
logger.debug("Added regular expression: " + compiledPattern.getPattern().toString() + "; attributes: "
+ attr);
logger.debug("Added regular expression: " + regExp + "; attributes: " + attr);
}
}
@ -96,8 +84,6 @@ public class RegExpBasedFilterInvocationDefinitionMap extends AbstractFilterInvo
}
public ConfigAttributeDefinition lookupAttributes(String url) {
PatternMatcher matcher = new Perl5Matcher();
Iterator iter = requestMap.iterator();
if (isConvertUrlToLowercaseBeforeComparison()) {
@ -111,10 +97,12 @@ public class RegExpBasedFilterInvocationDefinitionMap extends AbstractFilterInvo
while (iter.hasNext()) {
EntryHolder entryHolder = (EntryHolder) iter.next();
boolean matched = matcher.matches(url, entryHolder.getCompiledPattern());
Matcher matcher = entryHolder.getCompiledPattern().matcher(url);
boolean matched = matcher.matches();
if (logger.isDebugEnabled()) {
logger.debug("Candidate is: '" + url + "'; pattern is " + entryHolder.getCompiledPattern().getPattern()
logger.debug("Candidate is: '" + url + "'; pattern is " + entryHolder.getCompiledPattern()
+ "; matched=" + matched);
}

View File

@ -28,13 +28,6 @@ import org.acegisecurity.userdetails.UserDetailsService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.oro.text.regex.MalformedPatternException;
import org.apache.oro.text.regex.MatchResult;
import org.apache.oro.text.regex.Pattern;
import org.apache.oro.text.regex.PatternMatcher;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Perl5Matcher;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.MessageSource;
@ -44,6 +37,9 @@ import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.util.Assert;
import java.security.cert.X509Certificate;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.regex.MatchResult;
/**
@ -70,32 +66,24 @@ public class DaoX509AuthoritiesPopulator implements X509AuthoritiesPopulator, In
Assert.notNull(userDetailsService, "An authenticationDao must be set");
Assert.notNull(this.messages, "A message source must be set");
Perl5Compiler compiler = new Perl5Compiler();
try {
subjectDNPattern = compiler.compile(subjectDNRegex,
Perl5Compiler.READ_ONLY_MASK | Perl5Compiler.CASE_INSENSITIVE_MASK);
} catch (MalformedPatternException mpe) {
throw new IllegalArgumentException("Malformed regular expression: " + subjectDNRegex);
}
subjectDNPattern = Pattern.compile(subjectDNRegex, Pattern.CASE_INSENSITIVE);
}
public UserDetails getUserDetails(X509Certificate clientCert) throws AuthenticationException {
String subjectDN = clientCert.getSubjectDN().getName();
PatternMatcher matcher = new Perl5Matcher();
if (!matcher.contains(subjectDN, subjectDNPattern)) {
Matcher matcher = subjectDNPattern.matcher(subjectDN);
if (!matcher.find()) {
throw new BadCredentialsException(messages.getMessage("DaoX509AuthoritiesPopulator.noMatching",
new Object[] {subjectDN}, "No matching pattern was found in subjectDN: {0}"));
}
MatchResult match = matcher.getMatch();
if (match.groups() != 2) { // 2 = 1 + the entire match
if (matcher.groupCount() != 1) {
throw new IllegalArgumentException("Regular expression must contain a single group ");
}
String userName = match.group(1);
String userName = matcher.group(1);
UserDetails user = this.userDetailsService.loadUserByUsername(userName);

View File

@ -25,6 +25,7 @@ import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import java.util.Iterator;
import java.util.regex.PatternSyntaxException;
/**
@ -38,7 +39,6 @@ public class FilterInvocationDefinitionSourceEditorTests extends TestCase {
//~ Constructors ===================================================================================================
public FilterInvocationDefinitionSourceEditorTests() {
super();
}
public FilterInvocationDefinitionSourceEditorTests(String arg0) {
@ -47,20 +47,11 @@ public class FilterInvocationDefinitionSourceEditorTests extends TestCase {
//~ Methods ========================================================================================================
public static void main(String[] args) {
junit.textui.TestRunner.run(FilterInvocationDefinitionSourceEditorTests.class);
}
public final void setUp() throws Exception {
super.setUp();
}
public void testConvertUrlToLowercaseDefaultSettingUnchangedByEditor() {
FilterInvocationDefinitionSourceEditor editor = new FilterInvocationDefinitionSourceEditor();
editor.setAsText("\\A/secure/super.*\\Z=ROLE_WE_DONT_HAVE\r\n\\A/secure/.*\\Z=ROLE_SUPERVISOR,ROLE_TELLER");
RegExpBasedFilterInvocationDefinitionMap map = (RegExpBasedFilterInvocationDefinitionMap) editor
.getValue();
RegExpBasedFilterInvocationDefinitionMap map = (RegExpBasedFilterInvocationDefinitionMap) editor.getValue();
assertFalse(map.isConvertUrlToLowercaseBeforeComparison());
}
@ -137,14 +128,13 @@ public class FilterInvocationDefinitionSourceEditorTests extends TestCase {
assertEquals(0, map.getMapSize());
}
public void testInvalidRegularExpressionsDetected()
throws Exception {
public void testInvalidRegularExpressionsDetected() throws Exception {
FilterInvocationDefinitionSourceEditor editor = new FilterInvocationDefinitionSourceEditor();
try {
editor.setAsText("*=SOME_ROLE");
} catch (IllegalArgumentException expected) {
assertEquals("Malformed regular expression: *", expected.getMessage());
fail("Expected PatternSyntaxException");
} catch (PatternSyntaxException expected) {
}
}
@ -284,7 +274,7 @@ public class FilterInvocationDefinitionSourceEditorTests extends TestCase {
assertEquals(expected, returned);
}
public void testWhitespaceAndCommentsAndLinesWithoutEqualsSignsAreIgnored() {
FilterInvocationDefinitionSourceEditor editor = new FilterInvocationDefinitionSourceEditor();
editor.setAsText(

View File

@ -36,7 +36,6 @@ public class RegExpBasedFilterDefinitionMapTests extends TestCase {
//~ Constructors ===================================================================================================
public RegExpBasedFilterDefinitionMapTests() {
super();
}
public RegExpBasedFilterDefinitionMapTests(String arg0) {
@ -45,14 +44,6 @@ public class RegExpBasedFilterDefinitionMapTests extends TestCase {
//~ Methods ========================================================================================================
public static void main(String[] args) {
junit.textui.TestRunner.run(RegExpBasedFilterDefinitionMapTests.class);
}
public final void setUp() throws Exception {
super.setUp();
}
public void testConvertUrlToLowercaseIsFalseByDefault() {
RegExpBasedFilterInvocationDefinitionMap map = new RegExpBasedFilterInvocationDefinitionMap();
assertFalse(map.isConvertUrlToLowercaseBeforeComparison());