You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@streampark.apache.org by mo...@apache.org on 2022/10/06 02:56:42 UTC
[incubator-streampark] branch dev updated: Add file type check (#1746)
This is an automated email from the ASF dual-hosted git repository.
monreid pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/incubator-streampark.git
The following commit(s) were added to refs/heads/dev by this push:
new 3a829d7c0 Add file type check (#1746)
3a829d7c0 is described below
commit 3a829d7c090d4398d413e76cf80874a6ae12b14a
Author: 阿洋 <xi...@126.com>
AuthorDate: Thu Oct 6 10:56:35 2022 +0800
Add file type check (#1746)
fix code checkstyle
---
.../console/base/config/WebMvcConfig.java | 14 ++++
.../streampark/console/base/enums/FileType.java | 42 ++++++++++
.../base/exception/IllegalFileTypeException.java | 28 +++++++
.../interceptor/FileHeaderCheckInterceptor.java | 92 ++++++++++++++++++++++
4 files changed, 176 insertions(+)
diff --git a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/base/config/WebMvcConfig.java b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/base/config/WebMvcConfig.java
index 0933d566c..80e51b105 100644
--- a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/base/config/WebMvcConfig.java
+++ b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/base/config/WebMvcConfig.java
@@ -17,6 +17,8 @@
package org.apache.streampark.console.base.config;
+import org.apache.streampark.console.base.interceptor.FileHeaderCheckInterceptor;
+
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
@@ -30,14 +32,20 @@ import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
+import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
+import javax.annotation.Resource;
+
import java.text.SimpleDateFormat;
import java.util.List;
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
+ @Resource
+ private FileHeaderCheckInterceptor fileHeaderCheckInterceptor;
+
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(new ByteArrayHttpMessageConverter());
@@ -72,4 +80,10 @@ public class WebMvcConfig implements WebMvcConfigurer {
converter.setObjectMapper(mapper);
return converter;
}
+
+ @Override
+ public void addInterceptors(InterceptorRegistry registry) {
+ registry.addInterceptor(fileHeaderCheckInterceptor)
+ .addPathPatterns("/flink/app/upload");
+ }
}
diff --git a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/base/enums/FileType.java b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/base/enums/FileType.java
new file mode 100644
index 000000000..a559432a9
--- /dev/null
+++ b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/base/enums/FileType.java
@@ -0,0 +1,42 @@
+/*
+ * 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.streampark.console.base.enums;
+
+public enum FileType {
+
+ /**
+ * JAR Archive
+ */
+ JAR("JAR", "504B03040A00000");
+
+ private final String alias;
+ private final String magicNumber;
+
+ FileType(String alias, String magicNumber) {
+ this.alias = alias;
+ this.magicNumber = magicNumber;
+ }
+
+ public String getAlias() {
+ return alias;
+ }
+
+ public String getMagicNumber() {
+ return magicNumber;
+ }
+}
diff --git a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/base/exception/IllegalFileTypeException.java b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/base/exception/IllegalFileTypeException.java
new file mode 100644
index 000000000..de8028c84
--- /dev/null
+++ b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/base/exception/IllegalFileTypeException.java
@@ -0,0 +1,28 @@
+/*
+ * 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.streampark.console.base.exception;
+
+public class IllegalFileTypeException extends ApiException {
+ public IllegalFileTypeException(String message) {
+ super(message);
+ }
+
+ public IllegalFileTypeException(String message, Throwable cause) {
+ super(message, cause);
+ }
+}
diff --git a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/base/interceptor/FileHeaderCheckInterceptor.java b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/base/interceptor/FileHeaderCheckInterceptor.java
new file mode 100644
index 000000000..9a6d3ac3d
--- /dev/null
+++ b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/base/interceptor/FileHeaderCheckInterceptor.java
@@ -0,0 +1,92 @@
+/*
+ * 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.streampark.console.base.interceptor;
+
+import org.apache.streampark.console.base.enums.FileType;
+import org.apache.streampark.console.base.exception.IllegalFileTypeException;
+
+import org.springframework.stereotype.Component;
+import org.springframework.web.multipart.MultipartFile;
+import org.springframework.web.multipart.MultipartHttpServletRequest;
+import org.springframework.web.servlet.HandlerInterceptor;
+import org.springframework.web.servlet.ModelAndView;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+@Component
+public class FileHeaderCheckInterceptor implements HandlerInterceptor {
+
+ private static List<String> fileHeaders = new ArrayList<>();
+ private int headerLength = 8;
+ static {
+ fileHeaders.add(FileType.JAR.getMagicNumber());
+ }
+
+ @Override
+ public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
+ if (request != null && request instanceof MultipartHttpServletRequest) {
+ MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
+ Map<String, MultipartFile> files = multipartRequest.getFileMap();
+ Iterator<String> iterator = files.keySet().iterator();
+ while (iterator.hasNext()) {
+ String formKey = iterator.next();
+ MultipartFile multipartFile = multipartRequest.getFile(formKey);
+ byte[] file = multipartFile.getBytes();
+ if (file.length > headerLength) {
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < headerLength; i++) {
+ int v = file[i] & 0xFF;
+ String hv = Integer.toHexString(v);
+ if (hv.length() < 2) {
+ sb.append(0);
+ }
+ sb.append(hv);
+ }
+ boolean isFound = false;
+ String fileHead = sb.toString().toUpperCase();
+ for (String header : fileHeaders) {
+ if (fileHead.startsWith(header)) {
+ isFound = true;
+ break;
+ }
+ }
+ if (!isFound) {
+ throw new IllegalFileTypeException("Illegal file type, please check");
+ }
+ }
+ }
+ }
+ return true;
+ }
+
+ @Override
+ public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
+ HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
+ }
+
+ @Override
+ public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
+ HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
+ }
+}