9c0bab43cc328f2abc1dd86578e19309ca5c0841
[policy/distribution.git] /
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.distribution.reception.util;
23
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;
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 static StandardCoder coder = new StandardCoder();
42     private static StandardYamlCoder yamlCoder = new StandardYamlCoder();
43     private static final long MAX_FILE_SIZE = 512L * 1024;
44
45     /**
46      * Method to ensure validation of entries in the Zipfile. Attempts to solve path
47      * injection java security issues.
48      *
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
53      */
54     public static void validateZipEntry(String entryName, String csarPath, long entrySize)
55             throws PolicyDecodingException {
56         //
57         // Check file size
58         //
59         if (entrySize > MAX_FILE_SIZE) {
60             throw new PolicyDecodingException("Zip entry for " + entryName + " is too large " + entrySize);
61         }
62         //
63         // Now ensure that there is no path injection
64         //
65         var path = Path.of(csarPath, entryName).normalize();
66         //
67         // Throw an exception if path is outside the csar
68         //
69         if (! path.startsWith(csarPath)) {
70             throw new PolicyDecodingException("Potential path injection for zip entry " + entryName);
71         }
72     }
73
74     /**
75      * Method to decode either a json or yaml file into an object.
76      *
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.
81      */
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);
89         }
90         return toscaServiceTemplate;
91     }
92 }