Skip to content

Commit db919ff

Browse files
committed
Ensure RealmBase finds all matching extension based constraints
1 parent 6565a6c commit db919ff

4 files changed

Lines changed: 104 additions & 10 deletions

File tree

java/org/apache/catalina/realm/RealmBase.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -666,8 +666,6 @@ public SecurityConstraint[] findSecurityConstraints(Request request, Context con
666666
constraints[i].included(uri, method));
667667
}
668668

669-
boolean matched = false;
670-
int pos = -1;
671669
for (int j = 0; j < collection.length; j++) {
672670
String[] patterns = collection[j].findPatterns();
673671

@@ -677,6 +675,7 @@ public SecurityConstraint[] findSecurityConstraints(Request request, Context con
677675
continue;
678676
}
679677

678+
boolean matched = false;
680679
for (int k = 0; k < patterns.length && !matched; k++) {
681680
String pattern = patterns[k];
682681
if (pattern.startsWith("*.")) {
@@ -686,19 +685,18 @@ public SecurityConstraint[] findSecurityConstraints(Request request, Context con
686685
uri.length() - dot == pattern.length() - 1) {
687686
if (pattern.regionMatches(1, uri, dot, uri.length() - dot)) {
688687
matched = true;
689-
pos = j;
690688
}
691689
}
692690
}
693691
}
694-
}
695-
if (matched) {
696-
found = true;
697-
if (collection[pos].findMethod(method)) {
698-
if (results == null) {
699-
results = new ArrayList<>();
692+
if (matched) {
693+
found = true;
694+
if (collection[j].findMethod(method)) {
695+
if (results == null) {
696+
results = new ArrayList<>();
697+
}
698+
results.add(constraints[i]);
700699
}
701-
results.add(constraints[i]);
702700
}
703701
}
704702
}

test/org/apache/catalina/realm/TestRealmBase.java

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,4 +790,86 @@ public void testHttpConstraint() throws IOException {
790790
Assert.assertFalse(mapRealm.hasResourcePermission(
791791
request, response, constraintsDelete, null));
792792
}
793+
794+
795+
@Test
796+
public void testUncoveredMethods() throws IOException {
797+
// Create a constraint for ROLE1
798+
SecurityConstraint constraint = new SecurityConstraint();
799+
constraint.addAuthRole(ROLE1);
800+
// Add a collection for GET
801+
SecurityCollection getCollection = new SecurityCollection();
802+
getCollection.addMethod(Method.GET);
803+
getCollection.addPatternDecoded("*.html");
804+
constraint.addCollection(getCollection);
805+
// Add a collection for POST
806+
SecurityCollection postCollection = new SecurityCollection();
807+
postCollection.addMethod(Method.POST);
808+
postCollection.addPatternDecoded("*.html");
809+
constraint.addCollection(postCollection);
810+
811+
TesterMapRealm mapRealm = new TesterMapRealm();
812+
813+
// Set up the mock request and response
814+
TesterRequest request = new TesterRequest();
815+
Response response = new TesterResponse();
816+
Context context = request.getContext();
817+
context.addSecurityRole(ROLE1);
818+
context.addSecurityRole(ROLE2);
819+
request.getMappingData().context = context;
820+
821+
// Create the principals
822+
List<String> userRoles1 = new ArrayList<>();
823+
userRoles1.add(ROLE1);
824+
GenericPrincipal gp1 = new GenericPrincipal(USER1, userRoles1);
825+
826+
List<String> userRoles2 = new ArrayList<>();
827+
userRoles2.add(ROLE2);
828+
GenericPrincipal gp2 = new GenericPrincipal(USER2, userRoles2);
829+
830+
List<String> userRoles99 = new ArrayList<>();
831+
GenericPrincipal gp99 = new GenericPrincipal(USER99, userRoles99);
832+
833+
// Add the constraint to the context
834+
context.addConstraint(constraint);
835+
836+
837+
// Only user1 should be able to perform a GET
838+
request.setMethod(Method.GET);
839+
840+
SecurityConstraint[] constraintsGet =
841+
mapRealm.findSecurityConstraints(request, context);
842+
843+
request.setUserPrincipal(null);
844+
Assert.assertFalse(mapRealm.hasResourcePermission(
845+
request, response, constraintsGet, null));
846+
request.setUserPrincipal(gp1);
847+
Assert.assertTrue(mapRealm.hasResourcePermission(
848+
request, response, constraintsGet, null));
849+
request.setUserPrincipal(gp2);
850+
Assert.assertFalse(mapRealm.hasResourcePermission(
851+
request, response, constraintsGet, null));
852+
request.setUserPrincipal(gp99);
853+
Assert.assertFalse(mapRealm.hasResourcePermission(
854+
request, response, constraintsGet, null));
855+
856+
// Only user1 should be able to perform a POST
857+
request.setMethod(Method.POST);
858+
859+
SecurityConstraint[] constraintsPost =
860+
mapRealm.findSecurityConstraints(request, context);
861+
862+
request.setUserPrincipal(null);
863+
Assert.assertFalse(mapRealm.hasResourcePermission(
864+
request, response, constraintsPost, null));
865+
request.setUserPrincipal(gp1);
866+
Assert.assertTrue(mapRealm.hasResourcePermission(
867+
request, response, constraintsPost, null));
868+
request.setUserPrincipal(gp2);
869+
Assert.assertFalse(mapRealm.hasResourcePermission(
870+
request, response, constraintsPost, null));
871+
request.setUserPrincipal(gp99);
872+
Assert.assertFalse(mapRealm.hasResourcePermission(
873+
request, response, constraintsPost, null));
874+
}
793875
}

test/org/apache/tomcat/unittest/TesterRequest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.apache.catalina.Context;
3232
import org.apache.catalina.connector.Request;
3333
import org.apache.catalina.session.StandardSession;
34+
import org.apache.tomcat.util.buf.MessageBytes;
3435

3536
public class TesterRequest extends Request {
3637

@@ -81,6 +82,15 @@ public String getRequestURI() {
8182
return "/level1/level2/foo.html";
8283
}
8384

85+
86+
@Override
87+
public MessageBytes getRequestPathMB() {
88+
MessageBytes result = MessageBytes.newInstance();
89+
result.setString(getRequestURI());
90+
return result;
91+
}
92+
93+
8494
@Override
8595
public String getDecodedRequestURI() {
8696
// Decoding not required

webapps/docs/changelog.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,10 @@
172172
<fix>
173173
Correct the handling of invalid users with DIGEST authentication. (markt)
174174
</fix>
175+
<fix>
176+
Ensure <code>RealmBase</code> finds all matching extension based
177+
security constraints. (markt)
178+
</fix>
175179
</changelog>
176180
</subsection>
177181
<subsection name="Coyote">

0 commit comments

Comments
 (0)