From b6b58ad5843754a6e4de30f706119e28e120691c Mon Sep 17 00:00:00 2001 From: zhuyijun Date: Thu, 22 Aug 2024 18:14:44 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9EDM=E7=9A=84TrustedDmFunctionE?= =?UTF-8?q?num=E6=9E=9A=E4=B8=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../enums/dm/TrustedDmFunctionEnum.java | 66 +++++++++++++++++++ .../impl/dm/AbstractMapperByDm.java | 3 +- ...st.java => TrustedDmFunctionEnumTest.java} | 2 +- 3 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/enums/dm/TrustedDmFunctionEnum.java rename plugin/datasource/src/test/java/com/alibaba/nacos/plugin/datasource/enums/mysql/{TrustedMysqlFunctionEnumTest.java => TrustedDmFunctionEnumTest.java} (96%) diff --git a/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/enums/dm/TrustedDmFunctionEnum.java b/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/enums/dm/TrustedDmFunctionEnum.java new file mode 100644 index 000000000..559a265e5 --- /dev/null +++ b/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/enums/dm/TrustedDmFunctionEnum.java @@ -0,0 +1,66 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed 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 com.alibaba.nacos.plugin.datasource.enums.dm; + +import java.util.HashMap; +import java.util.Map; + +/** + * The TrustedSqlFunctionEnum enum class is used to enumerate and manage a list of trusted built-in SQL functions. + * By using this enum, you can verify whether a given SQL function is part of the trusted functions list + * to avoid potential SQL injection risks. + * + * @author blake.qiu + */ +public enum TrustedDmFunctionEnum { + + /** + * NOW(). + */ + NOW("NOW()", "NOW(3)"); + + private static final Map LOOKUP_MAP = new HashMap<>(); + + static { + for (TrustedDmFunctionEnum entry : TrustedDmFunctionEnum.values()) { + LOOKUP_MAP.put(entry.functionName, entry); + } + } + + private final String functionName; + + private final String function; + + TrustedDmFunctionEnum(String functionName, String function) { + this.functionName = functionName; + this.function = function; + } + + /** + * Get the function name. + * + * @param functionName function name + * @return function + */ + public static String getFunctionByName(String functionName) { + TrustedDmFunctionEnum entry = LOOKUP_MAP.get(functionName); + if (entry != null) { + return entry.function; + } + throw new IllegalArgumentException(String.format("Invalid function name: %s", functionName)); + } +} diff --git a/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/impl/dm/AbstractMapperByDm.java b/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/impl/dm/AbstractMapperByDm.java index 3ebf20794..d16948cca 100644 --- a/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/impl/dm/AbstractMapperByDm.java +++ b/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/impl/dm/AbstractMapperByDm.java @@ -16,6 +16,7 @@ package com.alibaba.nacos.plugin.datasource.impl.dm; +import com.alibaba.nacos.plugin.datasource.enums.dm.TrustedDmFunctionEnum; import com.alibaba.nacos.plugin.datasource.enums.mysql.TrustedMysqlFunctionEnum; import com.alibaba.nacos.plugin.datasource.mapper.AbstractMapper; @@ -28,6 +29,6 @@ public abstract class AbstractMapperByDm extends AbstractMapper { @Override public String getFunction(String functionName) { - return TrustedMysqlFunctionEnum.getFunctionByName(functionName); + return TrustedDmFunctionEnum.getFunctionByName(functionName); } } diff --git a/plugin/datasource/src/test/java/com/alibaba/nacos/plugin/datasource/enums/mysql/TrustedMysqlFunctionEnumTest.java b/plugin/datasource/src/test/java/com/alibaba/nacos/plugin/datasource/enums/mysql/TrustedDmFunctionEnumTest.java similarity index 96% rename from plugin/datasource/src/test/java/com/alibaba/nacos/plugin/datasource/enums/mysql/TrustedMysqlFunctionEnumTest.java rename to plugin/datasource/src/test/java/com/alibaba/nacos/plugin/datasource/enums/mysql/TrustedDmFunctionEnumTest.java index 7cb9dbcea..0358ec02a 100644 --- a/plugin/datasource/src/test/java/com/alibaba/nacos/plugin/datasource/enums/mysql/TrustedMysqlFunctionEnumTest.java +++ b/plugin/datasource/src/test/java/com/alibaba/nacos/plugin/datasource/enums/mysql/TrustedDmFunctionEnumTest.java @@ -24,7 +24,7 @@ import org.junit.jupiter.api.Test; * * @author blake.qiu */ -public class TrustedMysqlFunctionEnumTest { +public class TrustedDmFunctionEnumTest { @Test void testGetFunctionByName() {