gnodet-bot commented on code in PR #28522:
URL: https://github.com/apache/camel/pull/28522#discussion_r4377262301
##########
components/camel-shiro/src/main/java/org/apache/camel/component/shiro/security/ShiroSecurityProcessor.java:
##########
@@ -124,9 +126,39 @@ private void applySecurityPolicy(Exchange exchange) throws
Exception {
authorizeUser(currentUser, exchange);
} finally {
if (policy.isAlwaysReauthenticate()) {
- currentUser.logout();
+ try {
+ currentUser.logout();
+ } finally {
+ ThreadContext.unbindSubject();
+ ThreadContext.unbindSecurityManager();
Review Comment:
⚠️ **ThreadContext leak path when `alwaysReauthenticate=false`**:
`resolveSubject()` binds a new subject + security manager to `ThreadContext`
(lines 154–155) whenever the thread doesn't already carry the right context.
But this `finally` block only cleans up when `isAlwaysReauthenticate()` is
true. If `authenticateUser()` or `authorizeUser()` throws **and**
`alwaysReauthenticate=false`, those bindings persist on the returned thread —
the next exchange starts from a dirty state.
Consider also unbinding in the exception path when
`alwaysReauthenticate=false`:
```suggestion
} finally {
if (policy.isAlwaysReauthenticate()) {
try {
currentUser.logout();
} finally {
ThreadContext.unbindSubject();
ThreadContext.unbindSecurityManager();
}
}
}
```
(The actual fix needs to distinguish success vs exception path — e.g. a
`boolean threw` flag set in the catch, or move cleanup to an exception handler.)
##########
components/camel-shiro/src/main/java/org/apache/camel/component/shiro/security/ShiroSecurityProcessor.java:
##########
@@ -124,9 +126,39 @@ private void applySecurityPolicy(Exchange exchange) throws
Exception {
authorizeUser(currentUser, exchange);
} finally {
if (policy.isAlwaysReauthenticate()) {
- currentUser.logout();
+ try {
+ currentUser.logout();
+ } finally {
+ ThreadContext.unbindSubject();
+ ThreadContext.unbindSecurityManager();
+ }
+ }
+ }
+ }
+
+ private Subject resolveSubject() {
+ Subject currentUser = null;
+ Subject threadSubject = ThreadContext.getSubject();
+ if (threadSubject instanceof DelegatingSubject) {
+ DelegatingSubject delegatingSubject = (DelegatingSubject)
threadSubject;
Review Comment:
💡 **Nit — Java 16+ pattern matching**: Combine the `instanceof` check and
cast:
```suggestion
if (threadSubject instanceof DelegatingSubject delegatingSubject) {
```
##########
components/camel-shiro/src/test/java/org/apache/camel/component/shiro/security/ShiroMultiplePolicyIsolationTest.java:
##########
@@ -0,0 +1,188 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.shiro.security;
+
+import java.nio.charset.StandardCharsets;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.camel.EndpointInject;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit6.CamelTestSupport;
+import org.apache.shiro.authc.AuthenticationException;
+import org.apache.shiro.config.Ini;
+import org.junit.jupiter.api.Test;
+
+public class ShiroMultiplePolicyIsolationTest extends CamelTestSupport {
+
+ private static final byte[] KEY_A =
"0123456789abcdef".getBytes(StandardCharsets.US_ASCII);
+ private static final byte[] KEY_B =
"fedcba9876543210".getBytes(StandardCharsets.US_ASCII);
+
+ @EndpointInject("mock:successA")
+ protected MockEndpoint successEndpointA;
+
+ @EndpointInject("mock:failureA")
+ protected MockEndpoint failureEndpointA;
+
+ @EndpointInject("mock:successB")
+ protected MockEndpoint successEndpointB;
+
+ @EndpointInject("mock:failureB")
+ protected MockEndpoint failureEndpointB;
+
+ private ShiroSecurityPolicy policyA;
+ private ShiroSecurityPolicy policyB;
+ private ShiroSecurityPolicy policyNoReauthA;
+ private ShiroSecurityPolicy policyNoReauthB;
+
+ @EndpointInject("mock:successNoReauthA")
+ protected MockEndpoint successEndpointNoReauthA;
+
+ @EndpointInject("mock:failureNoReauthA")
+ protected MockEndpoint failureEndpointNoReauthA;
+
+ @EndpointInject("mock:successNoReauthB")
+ protected MockEndpoint successEndpointNoReauthB;
+
+ @EndpointInject("mock:failureNoReauthB")
+ protected MockEndpoint failureEndpointNoReauthB;
+
+ @Test
+ public void testPolicyIsolationBetweenDifferentRealms() throws Exception {
+ successEndpointA.expectedMessageCount(1);
+ failureEndpointA.expectedMessageCount(1);
+ successEndpointB.expectedMessageCount(1);
+ failureEndpointB.expectedMessageCount(1);
+
+ // 1. Valid user for policy A should succeed on route A
+ Map<String, Object> headersAlice = new HashMap<>();
+ headersAlice.put(ShiroSecurityConstants.SHIRO_SECURITY_USERNAME,
"alice");
+ headersAlice.put(ShiroSecurityConstants.SHIRO_SECURITY_PASSWORD,
"passwordA");
+ template.sendBodyAndHeaders("direct:routeA", "payloadA", headersAlice);
+
+ // 2. Valid user for policy B should fail on route A
+ Map<String, Object> headersBobOnA = new HashMap<>();
+ headersBobOnA.put(ShiroSecurityConstants.SHIRO_SECURITY_USERNAME,
"bob");
+ headersBobOnA.put(ShiroSecurityConstants.SHIRO_SECURITY_PASSWORD,
"passwordB");
+ template.sendBodyAndHeaders("direct:routeA", "payloadA",
headersBobOnA);
+
+ // 3. Valid user for policy B should succeed on route B
+ Map<String, Object> headersBobOnB = new HashMap<>();
+ headersBobOnB.put(ShiroSecurityConstants.SHIRO_SECURITY_USERNAME,
"bob");
+ headersBobOnB.put(ShiroSecurityConstants.SHIRO_SECURITY_PASSWORD,
"passwordB");
+ template.sendBodyAndHeaders("direct:routeB", "payloadB",
headersBobOnB);
+
+ // 4. User for policy A should fail on route B
+ Map<String, Object> headersAliceOnB = new HashMap<>();
+ headersAliceOnB.put(ShiroSecurityConstants.SHIRO_SECURITY_USERNAME,
"alice");
+ headersAliceOnB.put(ShiroSecurityConstants.SHIRO_SECURITY_PASSWORD,
"passwordA");
+ template.sendBodyAndHeaders("direct:routeB", "payloadB",
headersAliceOnB);
+
+ successEndpointA.assertIsSatisfied();
+ failureEndpointA.assertIsSatisfied();
+ successEndpointB.assertIsSatisfied();
+ failureEndpointB.assertIsSatisfied();
+ }
+
+ @Test
+ public void testPolicyIsolationWithReauthenticateFalse() throws Exception {
+ successEndpointNoReauthA.expectedMessageCount(1);
+ failureEndpointNoReauthA.expectedMessageCount(1);
+ successEndpointNoReauthB.expectedMessageCount(1);
+ failureEndpointNoReauthB.expectedMessageCount(1);
+
+ // 1. Valid user for policy A on route A
+ Map<String, Object> headersAlice = new HashMap<>();
+ headersAlice.put(ShiroSecurityConstants.SHIRO_SECURITY_USERNAME,
"alice");
+ headersAlice.put(ShiroSecurityConstants.SHIRO_SECURITY_PASSWORD,
"passwordA");
+ template.sendBodyAndHeaders("direct:routeNoReauthA", "payloadA",
headersAlice);
+
+ // 2. Valid user for policy B on route B
+ Map<String, Object> headersBob = new HashMap<>();
+ headersBob.put(ShiroSecurityConstants.SHIRO_SECURITY_USERNAME, "bob");
+ headersBob.put(ShiroSecurityConstants.SHIRO_SECURITY_PASSWORD,
"passwordB");
+ template.sendBodyAndHeaders("direct:routeNoReauthB", "payloadB",
headersBob);
+
+ // 3. User for policy B should fail on route A even after B succeeded
+ Map<String, Object> headersBobOnA = new HashMap<>();
+ headersBobOnA.put(ShiroSecurityConstants.SHIRO_SECURITY_USERNAME,
"bob");
+ headersBobOnA.put(ShiroSecurityConstants.SHIRO_SECURITY_PASSWORD,
"passwordB");
+ template.sendBodyAndHeaders("direct:routeNoReauthA", "payloadA",
headersBobOnA);
+
+ // 4. User for policy A should fail on route B
+ Map<String, Object> headersAliceOnB = new HashMap<>();
+ headersAliceOnB.put(ShiroSecurityConstants.SHIRO_SECURITY_USERNAME,
"alice");
+ headersAliceOnB.put(ShiroSecurityConstants.SHIRO_SECURITY_PASSWORD,
"passwordA");
+ template.sendBodyAndHeaders("direct:routeNoReauthB", "payloadB",
headersAliceOnB);
+
+ successEndpointNoReauthA.assertIsSatisfied();
+ failureEndpointNoReauthA.assertIsSatisfied();
+ successEndpointNoReauthB.assertIsSatisfied();
+ failureEndpointNoReauthB.assertIsSatisfied();
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() {
+ Ini iniA = new Ini();
+ iniA.setSectionProperty("users", "alice", "passwordA, roleA");
+ policyA = new ShiroSecurityPolicy(iniA, KEY_A);
+ policyNoReauthA = new ShiroSecurityPolicy(iniA, KEY_A, false);
+
+ Ini iniB = new Ini();
+ iniB.setSectionProperty("users", "bob", "passwordB, roleB");
+ policyB = new ShiroSecurityPolicy(iniB, KEY_B);
+ policyNoReauthB = new ShiroSecurityPolicy(iniB, KEY_B, false);
+
+ return new RouteBuilder() {
+ @Override
+ public void configure() {
+ onException(AuthenticationException.class)
+ .handled(true)
+ .to("mock:failureA");
Review Comment:
💡 **Minor test design**: The context-level `onException` handler routes all
`AuthenticationException` from `routeA` to `mock:failureA`. The other three
routes each use per-route handlers. This asymmetry means an exception that
leaks past a route-level boundary could silently land in `failureA` instead of
the expected endpoint, making cross-route assertions potentially misleading.
Consider giving `routeA` its own per-route `onException` handler (mirroring the
pattern already used by `routeB`, `routeNoReauthA`, `routeNoReauthB`) and
removing the context-level one.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]