fe7f2dcc971e9d7ccf0433094f67202a26e405f8
[policy/distribution.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
4  *  Copyright (C) 2019 Nordix Foundation.
5  *  Modifications Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.distribution.reception.decoding.policy.file;
24
25 import java.io.IOException;
26 import java.nio.file.Path;
27 import java.util.ArrayList;
28 import java.util.Collection;
29 import java.util.Enumeration;
30 import java.util.zip.ZipEntry;
31 import java.util.zip.ZipFile;
32 import org.onap.policy.common.parameters.ParameterService;
33 import org.onap.policy.common.utils.coder.CoderException;
34 import org.onap.policy.common.utils.coder.StandardCoder;
35 import org.onap.policy.distribution.model.Csar;
36 import org.onap.policy.distribution.model.PolicyInput;
37 import org.onap.policy.distribution.reception.decoding.PolicyDecoder;
38 import org.onap.policy.distribution.reception.decoding.PolicyDecodingException;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
41
42 /**
43  * This class extracts policy files from a CSAR file.
44  *
45  * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com)
46  */
47 public class PolicyDecoderFileInCsarToPolicy implements PolicyDecoder<Csar, ToscaEntity> {
48
49     private PolicyDecoderFileInCsarToPolicyParameterGroup decoderParameters;
50     private StandardCoder coder;
51     private static final long MAX_FILE_SIZE = 512L * 1024;
52
53     /**
54      * {@inheritDoc}.
55      */
56     @Override
57     public void configure(final String parameterGroupName) {
58         decoderParameters = ParameterService.get(parameterGroupName);
59         coder = new StandardCoder();
60     }
61
62     /**
63      * {@inheritDoc}.
64      */
65     @Override
66     public boolean canHandle(final PolicyInput policyInput) {
67         return policyInput.getClass().isAssignableFrom(Csar.class);
68     }
69
70     /**
71      * {@inheritDoc}.
72      */
73     @Override
74     public Collection<ToscaEntity> decode(final Csar csar) throws PolicyDecodingException {
75         final Collection<ToscaEntity> policyList = new ArrayList<>();
76
77         try (var zipFile = new ZipFile(csar.getCsarPath())) {
78             final Enumeration<? extends ZipEntry> entries = zipFile.entries();
79             while (entries.hasMoreElements()) {
80                 //
81                 // Sonar will flag this as a Security Hotspot
82                 // "Expanding archive files is security-sensitive"
83                 // isZipEntryValid ensures the file being read exists in the archive
84                 //
85                 final ZipEntry entry = entries.nextElement(); // NOSONAR
86                 if (isZipEntryValid(entry.getName(), csar.getCsarPath(), entry.getSize())) {
87                     final ToscaServiceTemplate policy =
88                             coder.decode(zipFile.getInputStream(entry), ToscaServiceTemplate.class);
89                     policyList.add(policy);
90                 }
91             }
92         } catch (final IOException | CoderException exp) {
93             throw new PolicyDecodingException("Failed decoding the policy", exp);
94         }
95
96         return policyList;
97     }
98
99     /**
100      * Method to filter out Policy type and Policy files. In addition,
101      * ensures validation of entries in the Zipfile. Attempts to solve path
102      * injection java security issues.
103      *
104      * @param entryName name of the ZipEntry to check
105      * @param csarPath Absolute path to the csar the ZipEntry is in
106      * @param entrySize size of the ZipEntry
107      * @return true if no injection detected, and it is a policy type  or policy file.
108      * @throws PolicyDecodingException if the file size is too large
109      */
110     private boolean isZipEntryValid(String entryName, String csarPath, long entrySize) throws PolicyDecodingException {
111         //
112         // We only care about policy types and policies
113         //
114         if (entryName.contains(decoderParameters.getPolicyTypeFileName())
115                 || entryName.contains(decoderParameters.getPolicyFileName())) {
116             //
117             // Check file size
118             //
119             if (entrySize > MAX_FILE_SIZE) {
120                 throw new PolicyDecodingException("Zip entry for " + entryName + " is too large " + entrySize);
121             }
122             //
123             // Now ensure that there is no path injection
124             //
125             var path = Path.of(csarPath, entryName).normalize();
126             //
127             // Throw an exception if path is outside the csar
128             //
129             if (! path.startsWith(csarPath)) {
130                 throw new PolicyDecodingException("Potential path injection for zip entry " + entryName);
131             }
132             return true;
133         }
134
135         return false;
136     }
137 }