blob: 22c6cba9379af707905bf3b6dfd2ba3b98822193 [file] [log] [blame]
Ray Milkeyfa436442018-07-25 12:31:48 -07001/*
2 * Copyright 2018-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onlab.util;
18
19import java.io.File;
20import java.io.IOException;
21import java.util.zip.ZipEntry;
22
23/**
24 * Utilities for validation of Zip files.
25 */
26public final class ZipValidator {
27
28 /**
29 * Do not allow construction.
30 */
31 private ZipValidator() {
32
33 }
34
35 /**
36 * Validates a zip entry. Checks that the file being created does not
37 * lie outside the target directory.
38 *
39 * See https://snyk.io/research/zip-slip-vulnerability for more information.
40 *
41 * @param entry ZipEntry to check
42 * @param destinationDir target directory
43 * @return true if the Entry resolves to a file inside the target directory; false otherwise
44 */
45 public static boolean validateZipEntry(ZipEntry entry, File destinationDir) {
46 try {
47 String canonicalDestinationDirPath = destinationDir.getCanonicalPath();
48 File destinationFile = new File(destinationDir, entry.getName());
49 String canonicalDestinationFile = destinationFile.getCanonicalPath();
50 return canonicalDestinationFile.startsWith(canonicalDestinationDirPath + File.separator);
51 } catch (IOException ioe) {
52 return false;
53 }
54 }
55
56}