2  * ============LICENSE_START=======================================================
 
   3  *  Copyright (C) 2022 Nordix Foundation.
 
   4  *  Modifications Copyright (C) 2022 Nordix Foundation.
 
   5  * ================================================================================
 
   6  * Licensed under the Apache License, Version 2.0 (the "License");
 
   7  * you may not use this file except in compliance with the License.
 
   8  * You may obtain a copy of the License at
 
  10  *      http://www.apache.org/licenses/LICENSE-2.0
 
  12  * Unless required by applicable law or agreed to in writing, software
 
  13  * distributed under the License is distributed on an "AS IS" BASIS,
 
  14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  15  * See the License for the specific language governing permissions and
 
  16  * limitations under the License.
 
  18  * SPDX-License-Identifier: Apache-2.0
 
  19  * ============LICENSE_END=========================================================
 
  22 package org.onap.policy.distribution.reception.util;
 
  24 import java.io.IOException;
 
  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 static StandardCoder coder = new StandardCoder();
 
  42     private static StandardYamlCoder yamlCoder = new StandardYamlCoder();
 
  43     private static final long MAX_FILE_SIZE = 512L * 1024;
 
  46      * Method to ensure validation of entries in the Zipfile. Attempts to solve path
 
  47      * injection java security issues.
 
  49      * @param entryName name of the ZipEntry to check
 
  50      * @param csarPath Absolute path to the csar the ZipEntry is in
 
  51      * @param entrySize size of the ZipEntry
 
  52      * @throws PolicyDecodingException if the file size is too large
 
  54     public static void validateZipEntry(String entryName, String csarPath, long entrySize)
 
  55             throws PolicyDecodingException {
 
  59         if (entrySize > MAX_FILE_SIZE) {
 
  60             throw new PolicyDecodingException("Zip entry for " + entryName + " is too large " + entrySize);
 
  63         // Now ensure that there is no path injection
 
  65         var path = Path.of(csarPath, entryName).normalize();
 
  67         // Throw an exception if path is outside the csar
 
  69         if (! path.startsWith(csarPath)) {
 
  70             throw new PolicyDecodingException("Potential path injection for zip entry " + entryName);
 
  75      * Method to decode either a json or yaml file into an object.
 
  77      * @param zipFile the zip file
 
  78      * @param entry the entry to read in the zip file.
 
  79      * @return the decoded ToscaServiceTemplate object.
 
  80      * @throws CoderException IOException if the file decoding fails.
 
  82     public static ToscaServiceTemplate decodeFile(ZipFile zipFile, final ZipEntry entry)
 
  83             throws IOException, CoderException {
 
  84         ToscaServiceTemplate toscaServiceTemplate = null;
 
  85         if (entry.getName().endsWith(".json")) {
 
  86             toscaServiceTemplate = coder.decode(zipFile.getInputStream(entry), ToscaServiceTemplate.class);
 
  87         } else if (entry.getName().endsWith(".yml")) {
 
  88             toscaServiceTemplate = yamlCoder.decode(zipFile.getInputStream(entry), ToscaServiceTemplate.class);
 
  90         return toscaServiceTemplate;