This is an automated email from the ASF dual-hosted git repository.
ShannonDing pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/rocketmq.git
The following commit(s) were added to refs/heads/develop by this push:
new 38ddb4841c [ISSUE #11544] fix(auth): guard against null environment in
Environment#isMatch (#11545)
38ddb4841c is described below
commit 38ddb4841cdc4432c32c62ff427064dc56fa0a1f
Author: leaveWhite9088 <[email protected]>
AuthorDate: Mon Nov 23 19:28:41 2026 +0800
[ISSUE #11544] fix(auth): guard against null environment in
Environment#isMatch (#11545)
* [ISSUE #11544] fix(auth): guard against null environment in
Environment#isMatch
* [ISSUE #11544] test(auth): add EnvironmentTest for null and unconstrained
environment matching
---
.../auth/authorization/model/Environment.java | 2 +-
.../chain/AclAuthorizationHandlerTest.java | 26 +++++++++++++
.../auth/authorization/model/EnvironmentTest.java | 45 ++++++++++++++++++++++
3 files changed, 72 insertions(+), 1 deletion(-)
diff --git
a/auth/src/main/java/org/apache/rocketmq/auth/authorization/model/Environment.java
b/auth/src/main/java/org/apache/rocketmq/auth/authorization/model/Environment.java
index f514e20750..5273f30831 100644
---
a/auth/src/main/java/org/apache/rocketmq/auth/authorization/model/Environment.java
+++
b/auth/src/main/java/org/apache/rocketmq/auth/authorization/model/Environment.java
@@ -46,7 +46,7 @@ public class Environment {
if (CollectionUtils.isEmpty(this.sourceIps)) {
return true;
}
- if (CollectionUtils.isEmpty(environment.getSourceIps())) {
+ if (environment == null ||
CollectionUtils.isEmpty(environment.getSourceIps())) {
return false;
}
String targetIp = environment.getSourceIps().get(0);
diff --git
a/auth/src/test/java/org/apache/rocketmq/auth/authorization/chain/AclAuthorizationHandlerTest.java
b/auth/src/test/java/org/apache/rocketmq/auth/authorization/chain/AclAuthorizationHandlerTest.java
index 30a2518c7f..a5955d4bc6 100644
---
a/auth/src/test/java/org/apache/rocketmq/auth/authorization/chain/AclAuthorizationHandlerTest.java
+++
b/auth/src/test/java/org/apache/rocketmq/auth/authorization/chain/AclAuthorizationHandlerTest.java
@@ -28,6 +28,7 @@ import
org.apache.rocketmq.auth.authorization.exception.AuthorizationException;
import org.apache.rocketmq.auth.authorization.factory.AuthorizationFactory;
import
org.apache.rocketmq.auth.authorization.manager.AuthorizationMetadataManager;
import org.apache.rocketmq.auth.authorization.model.Acl;
+import org.apache.rocketmq.auth.authorization.model.Environment;
import org.apache.rocketmq.auth.authorization.model.Policy;
import org.apache.rocketmq.auth.authorization.model.PolicyEntry;
import org.apache.rocketmq.auth.authorization.model.Resource;
@@ -340,6 +341,31 @@ public class AclAuthorizationHandlerTest {
Assert.assertEquals("User:user has no permission to access Topic:t1
from 127.0.0.1, the decision is deny.", authorizationException.getMessage());
}
+ @Test
+ public void testNullSourceIpWithEnvironmentConstraint() {
+ if (MixAll.isMac()) {
+ return;
+ }
+ User user = User.of("user", "pwd");
+ authenticationMetadataManager.createUser(user).join();
+
+ PolicyEntry allowFromSubnet = PolicyEntry.of(
+ Resource.of(ResourceType.TOPIC, "t1", ResourcePattern.LITERAL),
+ Collections.singletonList(Action.SUB),
Environment.of("192.168.1.0/24"), Decision.ALLOW);
+ Policy policy = Policy.of(PolicyType.CUSTOM,
Collections.singletonList(allowFromSubnet));
+ authorizationMetadataManager.createAcl(Acl.of(user, policy)).join();
+
+ DefaultAuthorizationContext ctx = buildContext(user,
Resource.ofTopic("t1"), Action.SUB, null);
+ AuthorizationException authorizationException =
Assert.assertThrows(AuthorizationException.class, () -> {
+ try {
+ handler.handle(ctx, nextChain).join();
+ } catch (Throwable e) {
+ AuthTestHelper.handleException(e);
+ }
+ });
+ Assert.assertEquals("User:user has no permission to access Topic:t1
from null, no matched policies.", authorizationException.getMessage());
+ }
+
private DefaultAuthorizationContext buildContext(Subject subject, Resource
resource, Action action, String sourceIp) {
return DefaultAuthorizationContext.of(subject, resource, action,
sourceIp);
}
diff --git
a/auth/src/test/java/org/apache/rocketmq/auth/authorization/model/EnvironmentTest.java
b/auth/src/test/java/org/apache/rocketmq/auth/authorization/model/EnvironmentTest.java
new file mode 100644
index 0000000000..f5a1ea9056
--- /dev/null
+++
b/auth/src/test/java/org/apache/rocketmq/auth/authorization/model/EnvironmentTest.java
@@ -0,0 +1,45 @@
+/*
+ * 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.rocketmq.auth.authorization.model;
+
+import java.util.Collections;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class EnvironmentTest {
+
+ @Test
+ public void testConstrainedEnvironmentIsMatch() {
+ Environment constrained = Environment.of("192.168.1.0/24");
+ Assert.assertNotNull(constrained);
+
+ Assert.assertFalse(constrained.isMatch(null));
+ Assert.assertFalse(constrained.isMatch(new Environment()));
+ Assert.assertFalse(constrained.isMatch(Environment.of("10.0.0.1")));
+ Assert.assertTrue(constrained.isMatch(Environment.of("192.168.1.10")));
+ }
+
+ @Test
+ public void testUnconstrainedEnvironmentIsMatch() {
+ Environment unconstrained = new Environment();
+ Assert.assertTrue(unconstrained.isMatch(null));
+
Assert.assertTrue(unconstrained.isMatch(Environment.of("192.168.1.10")));
+
+ unconstrained.setSourceIps(Collections.emptyList());
+ Assert.assertTrue(unconstrained.isMatch(null));
+ }
+}