|
33 | 33 | import java.io.OutputStream; |
34 | 34 | import java.nio.file.Files; |
35 | 35 | import java.nio.file.Path; |
36 | | -import java.nio.file.Paths; |
37 | 36 | import java.util.Arrays; |
38 | 37 | import java.util.HashMap; |
39 | 38 | import java.util.Map; |
40 | 39 | import java.util.Properties; |
41 | 40 |
|
| 41 | +import static com.walmartlabs.concord.common.IOUtils.assertInPath; |
| 42 | + |
42 | 43 | public class ResourceTaskCommon { |
43 | 44 |
|
44 | 45 | private static final String RESOURCE_PREFIX = "resource_"; |
@@ -233,25 +234,15 @@ public static String prettyPrintYaml(Object value, int indent) throws IOExceptio |
233 | 234 | } |
234 | 235 |
|
235 | 236 | private Path normalizePath(String path) { |
236 | | - Path p = Paths.get(path); |
237 | | - if (p.isAbsolute()) { |
238 | | - return p; |
239 | | - } |
240 | | - return workDir.resolve(path); |
| 237 | + return assertWorkDirPath(path); |
241 | 238 | } |
242 | 239 |
|
243 | 240 | private Path assertWorkDirPath(String path) { |
244 | | - if (path == null) { |
245 | | - throw new IllegalArgumentException("Path cannot be null"); |
246 | | - } |
247 | | - Path dst = Paths.get(path); |
248 | | - if (!dst.isAbsolute()) { |
249 | | - dst = workDir.resolve(path).normalize().toAbsolutePath(); |
250 | | - } |
251 | | - if (!dst.startsWith(workDir)) { |
252 | | - throw new IllegalArgumentException("Invalid path: " + path); |
| 241 | + try { |
| 242 | + return assertInPath(workDir,path); |
| 243 | + } catch (IOException ex) { |
| 244 | + throw new IllegalArgumentException("Not authorized to access file outside of working directory: " + path); |
253 | 245 | } |
254 | | - return dst; |
255 | 246 | } |
256 | 247 |
|
257 | 248 | private static ObjectWriter createYamlWriter() { |
|
0 commit comments