2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.distribution.reception.util;
23 import java.io.IOException;
24 import java.io.InvalidClassException;
25 import java.nio.file.Path;
26 import java.util.zip.ZipEntry;
27 import java.util.zip.ZipFile;
28 import org.onap.policy.common.utils.coder.CoderException;
29 import org.onap.policy.common.utils.coder.StandardCoder;
30 import org.onap.policy.common.utils.coder.StandardYamlCoder;
31 import org.onap.policy.distribution.reception.decoding.PolicyDecodingException;
32 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
35 * This class extracts and validates information from a CSAR file.
37 * @author Sirisha Manchikanti (sirisha.manchikanti@est.tech)
39 public class ReceptionUtil {
41 private ReceptionUtil() throws InvalidClassException {
42 throw new InvalidClassException("Can't instantiate a helper class!");
45 private static final StandardCoder coder = new StandardCoder();
46 private static final StandardYamlCoder yamlCoder = new StandardYamlCoder();
47 private static final long MAX_FILE_SIZE = 512L * 1024;
50 * Method to ensure validation of entries in the Zipfile. Attempts to solve path
51 * injection java security issues.
53 * @param entryName name of the ZipEntry to check
54 * @param csarPath Absolute path to the csar the ZipEntry is in
55 * @param entrySize size of the ZipEntry
56 * @throws PolicyDecodingException if the file size is too large
58 public static void validateZipEntry(String entryName, String csarPath, long entrySize)
59 throws PolicyDecodingException {
63 if (entrySize > MAX_FILE_SIZE) {
64 throw new PolicyDecodingException("Zip entry for " + entryName + " is too large " + entrySize);
67 // Now ensure that there is no path injection
69 var path = Path.of(csarPath, entryName).normalize();
71 // Throw an exception if path is outside the csar
73 if (!path.startsWith(csarPath)) {
74 throw new PolicyDecodingException("Potential path injection for zip entry " + entryName);
79 * Method to decode either a json or yaml file into an object.
81 * @param zipFile the zip file
82 * @param entry the entry to read in the zip file.
83 * @return the decoded ToscaServiceTemplate object.
84 * @throws CoderException IOException if the file decoding fails.
86 public static ToscaServiceTemplate decodeFile(ZipFile zipFile, final ZipEntry entry)
87 throws IOException, CoderException {
88 ToscaServiceTemplate toscaServiceTemplate = null;
89 if (entry.getName().endsWith(".json")) {
90 toscaServiceTemplate = coder.decode(zipFile.getInputStream(entry), ToscaServiceTemplate.class);
91 } else if (entry.getName().endsWith(".yml")) {
92 toscaServiceTemplate = yamlCoder.decode(zipFile.getInputStream(entry), ToscaServiceTemplate.class);
94 return toscaServiceTemplate;