Skip to content

Commit e5e7749

Browse files
authored
[Improvement] Abnormal characters check (apache#15824)
* abnormal characters check * add test case * remove error log * fix code style * fix import
1 parent 883848f commit e5e7749

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ResourcesServiceImpl.java

+5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package org.apache.dolphinscheduler.api.service.impl;
1919

20+
import static org.apache.dolphinscheduler.api.utils.CheckUtils.checkFilePath;
2021
import static org.apache.dolphinscheduler.common.constants.Constants.ALIAS;
2122
import static org.apache.dolphinscheduler.common.constants.Constants.CONTENT;
2223
import static org.apache.dolphinscheduler.common.constants.Constants.EMPTY_STRING;
@@ -1290,6 +1291,10 @@ private void checkFullName(String userTenantCode, String fullName) {
12901291
if (FOLDER_SEPARATOR.equalsIgnoreCase(fullName)) {
12911292
return;
12921293
}
1294+
// abnormal characters check
1295+
if (!checkFilePath(fullName)) {
1296+
throw new ServiceException(Status.ILLEGAL_RESOURCE_PATH);
1297+
}
12931298
// Avoid returning to the parent directory
12941299
if (fullName.contains("../")) {
12951300
throw new ServiceException(Status.ILLEGAL_RESOURCE_PATH, fullName);

dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/utils/CheckUtils.java

+10
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,14 @@ private static boolean regexChecks(String str, Pattern pattern) {
158158

159159
return pattern.matcher(str).matches();
160160
}
161+
162+
/**
163+
* regex FilePath check,only use a to z, A to Z, 0 to 9, and _./-
164+
*
165+
* @param str input string
166+
* @return true if regex pattern is right, otherwise return false
167+
*/
168+
public static boolean checkFilePath(String str) {
169+
return regexChecks(str, Constants.REGEX_FILE_PATH);
170+
}
161171
}

dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/utils/CheckUtilsTest.java

+20
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,24 @@ public void testCheckPhone() {
9292
Assertions.assertTrue(CheckUtils.checkPhone("17362537263"));
9393
}
9494

95+
/**
96+
* check file path
97+
*/
98+
@Test
99+
public void testCheckFilePath() {
100+
// true
101+
Assertions.assertTrue(CheckUtils.checkFilePath("/"));
102+
Assertions.assertTrue(CheckUtils.checkFilePath("xx/"));
103+
Assertions.assertTrue(CheckUtils.checkFilePath("/xx"));
104+
Assertions.assertTrue(CheckUtils.checkFilePath("14567134578654"));
105+
Assertions.assertTrue(CheckUtils.checkFilePath("/admin/root/"));
106+
Assertions.assertTrue(CheckUtils.checkFilePath("/admin/root/1531531..13513/153135.."));
107+
// false
108+
Assertions.assertFalse(CheckUtils.checkFilePath(null));
109+
Assertions.assertFalse(CheckUtils.checkFilePath("file://xxx/ss"));
110+
Assertions.assertFalse(CheckUtils.checkFilePath("/xxx/ss;/dasd/123"));
111+
Assertions.assertFalse(CheckUtils.checkFilePath("/xxx/ss && /dasd/123"));
112+
Assertions.assertFalse(CheckUtils.checkFilePath("/xxx/ss || /dasd/123"));
113+
}
114+
95115
}

dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/constants/Constants.java

+5
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,11 @@ private Constants() {
252252
*/
253253
public static final Pattern REGEX_USER_NAME = Pattern.compile("^[a-zA-Z0-9._-]{3,39}$");
254254

255+
/**
256+
* file path regex
257+
*/
258+
public static final Pattern REGEX_FILE_PATH = Pattern.compile("^[a-zA-Z0-9_./-]+$");
259+
255260
/**
256261
* read permission
257262
*/

0 commit comments

Comments
 (0)