diff options
author | Jon Marius Venstad <venstad@gmail.com> | 2022-03-24 12:59:47 +0100 |
---|---|---|
committer | Jon Marius Venstad <venstad@gmail.com> | 2022-03-24 12:59:47 +0100 |
commit | 04927f9142d5582d79757b9b66e2924f7d84dd93 (patch) | |
tree | e2a692f50d248adcdf15cfa738e9ca9091784004 /config-application-package/src/main/java/com/yahoo | |
parent | 73b080b527e303a60a365b2bb95b72dcc2f81f4c (diff) |
Add a top layer path to virtual file system
Diffstat (limited to 'config-application-package/src/main/java/com/yahoo')
3 files changed, 18 insertions, 10 deletions
diff --git a/config-application-package/src/main/java/com/yahoo/config/application/FileSystemWrapper.java b/config-application-package/src/main/java/com/yahoo/config/application/FileSystemWrapper.java index b9dd4943149..04b4de10fc9 100644 --- a/config-application-package/src/main/java/com/yahoo/config/application/FileSystemWrapper.java +++ b/config-application-package/src/main/java/com/yahoo/config/application/FileSystemWrapper.java @@ -16,20 +16,22 @@ import java.util.function.Predicate; */ public class FileSystemWrapper { - Predicate<Path> existence; - ThrowingFunction<Path, byte[], IOException> reader; + final Path root; + final Predicate<Path> existence; + final ThrowingFunction<Path, byte[], IOException> reader; - private FileSystemWrapper(Predicate<Path> existence, ThrowingFunction<Path, byte[], IOException> reader) { + private FileSystemWrapper(Path root, Predicate<Path> existence, ThrowingFunction<Path, byte[], IOException> reader) { + this.root = root; this.existence = existence; this.reader = reader; } - public static FileSystemWrapper ofFiles(Predicate<Path> existence, ThrowingFunction<Path, byte[], IOException> reader) { - return new FileSystemWrapper(existence, reader); + public static FileSystemWrapper ofFiles(Path root, Predicate<Path> existence, ThrowingFunction<Path, byte[], IOException> reader) { + return new FileSystemWrapper(root, existence, reader); } - public static FileSystemWrapper getDefault() { - return ofFiles(Files::exists, Files::readAllBytes); + public static FileSystemWrapper getDefault(Path root) { + return ofFiles(root, Files::exists, Files::readAllBytes); } public FileWrapper wrap(Path path) { @@ -39,7 +41,13 @@ public class FileSystemWrapper { public class FileWrapper { private final Path path; - private FileWrapper(Path path) { this.path = path; } + private FileWrapper(Path path) { + Path relative = root.relativize(path).normalize(); + if (relative.isAbsolute() || relative.startsWith("..")) + throw new IllegalArgumentException(path + " is not a descendant of " + root); + + this.path = path; + } public Path path() { return path; } public boolean exists() { return existence.test(path); } diff --git a/config-application-package/src/main/java/com/yahoo/config/application/IncludeProcessor.java b/config-application-package/src/main/java/com/yahoo/config/application/IncludeProcessor.java index 51f9aecd812..cf6f339fa74 100644 --- a/config-application-package/src/main/java/com/yahoo/config/application/IncludeProcessor.java +++ b/config-application-package/src/main/java/com/yahoo/config/application/IncludeProcessor.java @@ -29,7 +29,7 @@ class IncludeProcessor implements PreProcessor { private final FileWrapper application; public IncludeProcessor(File application) { - this(FileSystemWrapper.getDefault().wrap(application.toPath())); + this(FileSystemWrapper.getDefault(application.toPath()).wrap(application.toPath())); } public IncludeProcessor(FileWrapper application) { diff --git a/config-application-package/src/main/java/com/yahoo/config/application/XmlPreProcessor.java b/config-application-package/src/main/java/com/yahoo/config/application/XmlPreProcessor.java index 36ab5e86266..640cc7bcfa7 100644 --- a/config-application-package/src/main/java/com/yahoo/config/application/XmlPreProcessor.java +++ b/config-application-package/src/main/java/com/yahoo/config/application/XmlPreProcessor.java @@ -44,7 +44,7 @@ public class XmlPreProcessor { } public XmlPreProcessor(File applicationDir, Reader xmlInput, InstanceName instance, Environment environment, RegionName region) { - this(FileSystemWrapper.getDefault().wrap(applicationDir.toPath()), xmlInput, instance, environment, region); + this(FileSystemWrapper.getDefault(applicationDir.toPath()).wrap(applicationDir.toPath()), xmlInput, instance, environment, region); } public XmlPreProcessor(FileWrapper applicationDir, Reader xmlInput, InstanceName instance, Environment environment, RegionName region) { |