DR AAF CADI integration
[dmaap/datarouter.git] / datarouter-node / src / main / java / org / onap / dmaap / datarouter / node / PathUtil.java
diff --git a/datarouter-node/src/main/java/org/onap/dmaap/datarouter/node/PathUtil.java b/datarouter-node/src/main/java/org/onap/dmaap/datarouter/node/PathUtil.java
new file mode 100644 (file)
index 0000000..a403441
--- /dev/null
@@ -0,0 +1,88 @@
+/**\r
+ * -\r
+ * ============LICENSE_START=======================================================\r
+ * Copyright (C) 2019 Nordix Foundation.\r
+ * ================================================================================\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ * <p>\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ * <p>\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ * <p>\r
+ * SPDX-License-Identifier: Apache-2.0\r
+ * ============LICENSE_END=========================================================\r
+ */\r
+package org.onap.dmaap.datarouter.node;\r
+\r
+/**\r
+ * FORTIFY SCAN FIXES\r
+ * <p>This Utility is used for Fortify fixes. It Validates the path url formed from\r
+ *  the string passed in the request parameters.</p>\r
+ *\r
+ */\r
+class PathUtil {\r
+\r
+    /**\r
+     * This method takes String as the parameter and return the filtered path string.\r
+     * @param aString String to clean\r
+     * @return A cleaned String\r
+     */\r
+    static String cleanString(String aString) {\r
+        if (aString == null) return null;\r
+        String cleanString = "";\r
+        for (int i = 0; i < aString.length(); ++i) {\r
+            cleanString += cleanChar(aString.charAt(i));\r
+        }\r
+        return cleanString;\r
+    }\r
+\r
+    /**\r
+     * This method filters the valid special characters in path string.\r
+     * @param aChar The char to be cleaned\r
+     * @return The cleaned char\r
+     */\r
+    private static char cleanChar(char aChar) {\r
+        // 0 - 9\r
+        for (int i = 48; i < 58; ++i) {\r
+            if (aChar == i) return (char) i;\r
+        }\r
+        // 'A' - 'Z'\r
+        for (int i = 65; i < 91; ++i) {\r
+            if (aChar == i) return (char) i;\r
+        }\r
+        // 'a' - 'z'\r
+        for (int i = 97; i < 123; ++i) {\r
+            if (aChar == i) return (char) i;\r
+        }\r
+        // other valid characters\r
+        switch (aChar) {\r
+            case '/':\r
+                return '/';\r
+            case '.':\r
+                return '.';\r
+            case '-':\r
+                return '-';\r
+            case ':':\r
+                return ':';\r
+            case '?':\r
+                return '?';\r
+            case '&':\r
+                return '&';\r
+            case '=':\r
+                return '=';\r
+            case '#':\r
+                return '#';\r
+            case '_':\r
+                return '_';\r
+            case ' ':\r
+                return ' ';\r
+        }\r
+        return '%';\r
+    }\r
+}\r