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

liuhongyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git


The following commit(s) were added to refs/heads/master by this push:
     new ddbeda46ee [type:fix] (admin-appAuth): Fix path validation logic 
defects (#5993)
ddbeda46ee is described below

commit ddbeda46ee3a3edc90f772bf4b8b1b8de817b81b
Author: yqw570994511 <[email protected]>
AuthorDate: Mon Apr 7 15:28:16 2025 +0800

    [type:fix] (admin-appAuth): Fix path validation logic defects (#5993)
    
    - Refactor original flawed condition check.
    
    - Extract validation method to resolve operator precedence issue.
    
    Co-authored-by: yuqianwei <qq120405>
    Co-authored-by: aias00 <[email protected]>
    Co-authored-by: moremind <[email protected]>
---
 .../admin/service/impl/AppAuthServiceImpl.java     | 14 ++++++--
 .../shenyu/admin/service/AppAuthServiceTest.java   | 37 ++++++++++++++++++++--
 2 files changed, 46 insertions(+), 5 deletions(-)

diff --git 
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/AppAuthServiceImpl.java
 
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/AppAuthServiceImpl.java
index 73be2cb91e..bfdb125268 100644
--- 
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/AppAuthServiceImpl.java
+++ 
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/AppAuthServiceImpl.java
@@ -103,7 +103,7 @@ public class AppAuthServiceImpl implements AppAuthService {
     @Transactional(rollbackFor = Exception.class)
     public ShenyuAdminResult applyCreate(final AuthApplyDTO authApplyDTO) {
         if (StringUtils.isBlank(authApplyDTO.getAppName())
-                || authApplyDTO.getOpen() && 
CollectionUtils.isEmpty(authApplyDTO.getPathList())) {
+                || hasMissingPathsWhenOpen(authApplyDTO)) {
             return 
ShenyuAdminResult.error(ShenyuResultMessage.PARAMETER_ERROR);
         }
         AppAuthDO appAuthDO = AppAuthDO.create(authApplyDTO);
@@ -143,7 +143,7 @@ public class AppAuthServiceImpl implements AppAuthService {
     @Transactional(rollbackFor = Exception.class)
     public ShenyuAdminResult applyUpdate(final AuthApplyDTO authApplyDTO) {
         if (StringUtils.isAnyBlank(authApplyDTO.getAppKey(), 
authApplyDTO.getAppName())
-                || authApplyDTO.getOpen() && 
CollectionUtils.isEmpty(authApplyDTO.getPathList())) {
+                || hasMissingPathsWhenOpen(authApplyDTO)) {
             return 
ShenyuAdminResult.error(ShenyuResultMessage.PARAMETER_ERROR);
         }
         AppAuthDO appAuthDO = 
appAuthMapper.findByAppKey(authApplyDTO.getAppKey());
@@ -768,4 +768,14 @@ public class AppAuthServiceImpl implements AppAuthService {
                         }));
     }
 
+    /**
+     * check whether the path list is empty when open.
+     *
+     * @param authApplyDTO auth apply dto
+     * @return true if the path list is empty when open
+     */
+    private boolean hasMissingPathsWhenOpen(final AuthApplyDTO authApplyDTO) {
+        return Boolean.TRUE.equals(authApplyDTO.getOpen()) && 
CollectionUtils.isEmpty(authApplyDTO.getPathList());
+    }
+
 }
diff --git 
a/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/AppAuthServiceTest.java
 
b/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/AppAuthServiceTest.java
index 114126e1c4..4db636a453 100644
--- 
a/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/AppAuthServiceTest.java
+++ 
b/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/AppAuthServiceTest.java
@@ -273,8 +273,23 @@ public final class AppAuthServiceTest {
     }
 
     private void testApplyCreateParameterError() {
-        AuthApplyDTO newAuthApplyDTO = new AuthApplyDTO();
-        ShenyuAdminResult parameterErrorResult = 
this.appAuthService.applyCreate(newAuthApplyDTO);
+        testApplyCreateAppNameBlank();
+        testApplyCreateMissingPathsWhenOpen();
+    }
+
+    private void testApplyCreateAppNameBlank() {
+        AuthApplyDTO authApplyDTO = new AuthApplyDTO();
+        authApplyDTO.setAppName("");
+        ShenyuAdminResult parameterErrorResult = 
this.appAuthService.applyCreate(authApplyDTO);
+        assertEquals(ShenyuResultMessage.PARAMETER_ERROR, 
parameterErrorResult.getMessage());
+    }
+
+    private void testApplyCreateMissingPathsWhenOpen() {
+        AuthApplyDTO authApplyDTO = new AuthApplyDTO();
+        authApplyDTO.setAppName("appName");
+        authApplyDTO.setOpen(true);
+        authApplyDTO.setPathList(Collections.emptyList());
+        ShenyuAdminResult parameterErrorResult = 
this.appAuthService.applyCreate(authApplyDTO);
         assertEquals(ShenyuResultMessage.PARAMETER_ERROR, 
parameterErrorResult.getMessage());
     }
 
@@ -287,7 +302,23 @@ public final class AppAuthServiceTest {
     }
 
     private void testApplyUpdateParameterError() {
-        ShenyuAdminResult parameterErrorResult = 
this.appAuthService.applyUpdate(new AuthApplyDTO());
+        testApplyUpdateAppNameBlank();
+        testApplyUpdateMissingPathsWhenOpen();
+    }
+
+    private void testApplyUpdateAppNameBlank() {
+        AuthApplyDTO authApplyDTO = new AuthApplyDTO();
+        authApplyDTO.setAppName("");
+        ShenyuAdminResult parameterErrorResult = 
this.appAuthService.applyUpdate(authApplyDTO);
+        assertEquals(ShenyuResultMessage.PARAMETER_ERROR, 
parameterErrorResult.getMessage());
+    }
+
+    private void testApplyUpdateMissingPathsWhenOpen() {
+        AuthApplyDTO authApplyDTO = new AuthApplyDTO();
+        authApplyDTO.setAppName("appName");
+        authApplyDTO.setOpen(true);
+        authApplyDTO.setPathList(Collections.emptyList());
+        ShenyuAdminResult parameterErrorResult = 
this.appAuthService.applyUpdate(authApplyDTO);
         assertEquals(ShenyuResultMessage.PARAMETER_ERROR, 
parameterErrorResult.getMessage());
     }
 

Reply via email to