c26286da3a509a556367c1d144c971dd347b81d8
[policy/distribution.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.distribution.reception.util;
22
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;
33
34 /**
35  * This class extracts and validates information from a CSAR file.
36  *
37  * @author Sirisha Manchikanti (sirisha.manchikanti@est.tech)
38  */
39 public class ReceptionUtil {
40
41     private ReceptionUtil() throws InvalidClassException {
42         throw new InvalidClassException("Can't instantiate a helper class!");
43     }
44
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;
48
49     /**
50      * Method to ensure validation of entries in the Zipfile. Attempts to solve path
51      * injection java security issues.
52      *
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
57      */
58     public static void validateZipEntry(String entryName, String csarPath, long entrySize)
59             throws PolicyDecodingException {
60         //
61         // Check file size
62         //
63         if (entrySize > MAX_FILE_SIZE) {
64             throw new PolicyDecodingException("Zip entry for " + entryName + " is too large " + entrySize);
65         }
66         //
67         // Now ensure that there is no path injection
68         //
69         var path = Path.of(csarPath, entryName).normalize();
70         //
71         // Throw an exception if path is outside the csar
72         //
73         if (!path.startsWith(csarPath)) {
74             throw new PolicyDecodingException("Potential path injection for zip entry " + entryName);
75         }
76     }
77
78     /**
79      * Method to decode either a json or yaml file into an object.
80      *
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.
85      */
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);
93         }
94         return toscaServiceTemplate;
95     }
96 }