@Test @ConditionalRun(UnixPresent.class) public void shouldHandleLinksOfArbitraryDepthWithValidation() throws IOException, ForbiddenSymlinkException { File sourceRoot = createTemporaryDirectory("srcroot"); assertTrue("sourceRoot.isDirectory()", sourceRoot.isDirectory()); File realDir1 = createTemporaryDirectory("realdir1"); assertTrue("realDir1.isDirectory()", realDir1.isDirectory()); File realDir1b = new File(realDir1, "b"); realDir1b.mkdir(); assertTrue("realDir1b.isDirectory()", realDir1b.isDirectory()); File realDir2 = createTemporaryDirectory("realdir2"); assertTrue("realDir2.isDirectory()", realDir2.isDirectory()); final String SYMLINK1 = "symlink1"; File symlink1 = new File(sourceRoot, SYMLINK1); Files.createSymbolicLink(Paths.get(symlink1.getPath()), Paths.get(realDir1.getPath())); assertTrue("symlink1.exists()", symlink1.exists()); final String SYMLINK2 = "symlink2"; File symlink2 = new File(realDir1b, SYMLINK2); Files.createSymbolicLink(Paths.get(symlink2.getPath()), Paths.get(realDir2.getPath())); assertTrue("symlink2.exists()", symlink2.exists()); Path sympath = Paths.get(sourceRoot.getPath(), SYMLINK1, "b", SYMLINK2); assertTrue("2-link path exists", Files.exists(sympath)); String realDir1Canon = realDir1.getCanonicalPath(); String rel = PathUtils.getRelativeToCanonical(sympath.toString(), realDir1Canon); assertEquals("because links aren't validated", "b/" + SYMLINK2, rel); Set<String> allowedSymLinks = new HashSet<>(); ForbiddenSymlinkException expex = null; try { PathUtils.getRelativeToCanonical(sympath.toString(), realDir1Canon, allowedSymLinks, null); } catch (ForbiddenSymlinkException e) { expex = e; } Assert.assertNotNull("because no links allowed, arg1 is returned fully", expex); allowedSymLinks.add(symlink2.getPath()); rel = PathUtils.getRelativeToCanonical(sympath.toString(), realDir1Canon, allowedSymLinks, null); assertEquals("because link is OKed", "b/" + SYMLINK2, rel); }
public static String getRelativeToCanonical(String path, String canonical) throws IOException { try { return getRelativeToCanonical(path, canonical, null, null); } catch (ForbiddenSymlinkException e) { return path; } }
PathUtils { public static String getRelativeToCanonical(String path, String canonical) throws IOException { try { return getRelativeToCanonical(path, canonical, null, null); } catch (ForbiddenSymlinkException e) { return path; } } }
PathUtils { public static String getRelativeToCanonical(String path, String canonical) throws IOException { try { return getRelativeToCanonical(path, canonical, null, null); } catch (ForbiddenSymlinkException e) { return path; } } private  PathUtils(); }
PathUtils { public static String getRelativeToCanonical(String path, String canonical) throws IOException { try { return getRelativeToCanonical(path, canonical, null, null); } catch (ForbiddenSymlinkException e) { return path; } } private  PathUtils(); static String getRelativeToCanonical(String path, String canonical); static String getRelativeToCanonical(String path, String canonical,
            Set<String> allowedSymlinks, Set<String> canonicalRoots); }
PathUtils { public static String getRelativeToCanonical(String path, String canonical) throws IOException { try { return getRelativeToCanonical(path, canonical, null, null); } catch (ForbiddenSymlinkException e) { return path; } } private  PathUtils(); static String getRelativeToCanonical(String path, String canonical); static String getRelativeToCanonical(String path, String canonical,
            Set<String> allowedSymlinks, Set<String> canonicalRoots);  }