1754a7a39SVladimir Kotal /* 2754a7a39SVladimir Kotal * CDDL HEADER START 3754a7a39SVladimir Kotal * 4754a7a39SVladimir Kotal * The contents of this file are subject to the terms of the 5754a7a39SVladimir Kotal * Common Development and Distribution License (the "License"). 6754a7a39SVladimir Kotal * You may not use this file except in compliance with the License. 7754a7a39SVladimir Kotal * 8754a7a39SVladimir Kotal * See LICENSE.txt included in this distribution for the specific 9754a7a39SVladimir Kotal * language governing permissions and limitations under the License. 10754a7a39SVladimir Kotal * 11754a7a39SVladimir Kotal * When distributing Covered Code, include this CDDL HEADER in each 12754a7a39SVladimir Kotal * file and include the License file at LICENSE.txt. 13754a7a39SVladimir Kotal * If applicable, add the following below this CDDL HEADER, with the 14754a7a39SVladimir Kotal * fields enclosed by brackets "[]" replaced with your own identifying 15754a7a39SVladimir Kotal * information: Portions Copyright [yyyy] [name of copyright owner] 16754a7a39SVladimir Kotal * 17754a7a39SVladimir Kotal * CDDL HEADER END 18754a7a39SVladimir Kotal */ 19754a7a39SVladimir Kotal 20754a7a39SVladimir Kotal /* 215082f8ecSVladimir Kotal * Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved. 22754a7a39SVladimir Kotal */ 23754a7a39SVladimir Kotal package org.opengrok.web.util; 24754a7a39SVladimir Kotal 25754a7a39SVladimir Kotal import org.opengrok.indexer.configuration.RuntimeEnvironment; 26754a7a39SVladimir Kotal 27754a7a39SVladimir Kotal import java.io.File; 28754a7a39SVladimir Kotal import java.io.FileNotFoundException; 295082f8ecSVladimir Kotal import java.io.IOException; 305082f8ecSVladimir Kotal import java.nio.file.InvalidPathException; 31754a7a39SVladimir Kotal 32754a7a39SVladimir Kotal public class FileUtil { 33754a7a39SVladimir Kotal 34754a7a39SVladimir Kotal private static final RuntimeEnvironment env = RuntimeEnvironment.getInstance(); 35754a7a39SVladimir Kotal 36754a7a39SVladimir Kotal // private to enforce static FileUtil()37754a7a39SVladimir Kotal private FileUtil() { 38754a7a39SVladimir Kotal } 39754a7a39SVladimir Kotal 40754a7a39SVladimir Kotal /** 41754a7a39SVladimir Kotal * @param path path relative to source root 42754a7a39SVladimir Kotal * @return file object corresponding to the file under source root 435082f8ecSVladimir Kotal * @throws FileNotFoundException if the file constructed from the {@code path} parameter and source root 445082f8ecSVladimir Kotal * does not exist 455082f8ecSVladimir Kotal * @throws InvalidPathException if the file constructed from the {@code path} parameter and source root 465082f8ecSVladimir Kotal * leads outside source root 47754a7a39SVladimir Kotal * @throws NoPathParameterException if the {@code path} parameter is null 48754a7a39SVladimir Kotal */ 49*2f9f7cb1SVladimir Kotal @SuppressWarnings("lgtm[java/path-injection]") toFile(String path)505082f8ecSVladimir Kotal public static File toFile(String path) throws NoPathParameterException, IOException { 51754a7a39SVladimir Kotal if (path == null) { 52754a7a39SVladimir Kotal throw new NoPathParameterException("Missing path parameter"); 53754a7a39SVladimir Kotal } 54754a7a39SVladimir Kotal 55754a7a39SVladimir Kotal File file = new File(env.getSourceRootFile(), path); 565082f8ecSVladimir Kotal 575082f8ecSVladimir Kotal if (!file.getCanonicalPath().startsWith(env.getSourceRootPath() + File.separator)) { 585082f8ecSVladimir Kotal throw new InvalidPathException(path, "File points to outside of source root"); 595082f8ecSVladimir Kotal } 605082f8ecSVladimir Kotal 615082f8ecSVladimir Kotal if (!file.exists()) { 625082f8ecSVladimir Kotal throw new FileNotFoundException("File " + file + " not found"); 63754a7a39SVladimir Kotal } 64754a7a39SVladimir Kotal 65754a7a39SVladimir Kotal return file; 66754a7a39SVladimir Kotal } 67754a7a39SVladimir Kotal } 68