Skip to content

Commit 282603f

Browse files
committed
Add (failing) test of PathUtils implicitly allowing a symlink
As for IndexDatabase, PathUtils should implicitly allow a symlink whose target is a canonical child of another explicitly-allowed symlink.
1 parent 5d6ffbc commit 282603f

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

opengrok-indexer/src/test/java/org/opengrok/indexer/util/PathUtilsTest.java

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828

2929
import java.io.File;
3030
import java.io.IOException;
31-
import java.net.URI;
3231
import java.nio.file.Files;
3332
import java.nio.file.Path;
3433
import java.nio.file.Paths;
@@ -44,7 +43,6 @@
4443
import org.opengrok.indexer.condition.ConditionalRun;
4544
import org.opengrok.indexer.condition.ConditionalRunRule;
4645
import org.opengrok.indexer.condition.UnixPresent;
47-
import org.opengrok.indexer.web.Util;
4846

4947
/**
5048
* Represents a container for tests of {@link PathUtils}.
@@ -146,6 +144,43 @@ public void shouldHandleLinksOfArbitraryDepthWithValidation()
146144
assertEquals("because link is OKed", "b/" + SYMLINK2, rel);
147145
}
148146

147+
@Test
148+
@ConditionalRun(UnixPresent.class)
149+
public void shouldHandleLinksToCanonicalChildrenOfAllowedLinks()
150+
throws IOException, ForbiddenSymlinkException {
151+
// Create real directories
152+
File sourceRoot = createTemporaryDirectory("srcroot");
153+
assertTrue(sourceRoot + " should be a dir", sourceRoot.isDirectory());
154+
155+
File realDir1 = createTemporaryDirectory("realdir1");
156+
assertTrue(realDir1 + " should be a dir", realDir1.isDirectory());
157+
158+
File realDir1b = new File(realDir1, "b");
159+
assertTrue(realDir1b + " should be created", realDir1b.mkdir());
160+
161+
// Create symlink #1 to realdir1/ in source root.
162+
final String SYMLINK1 = "symlink1";
163+
File symlink1 = new File(sourceRoot, SYMLINK1);
164+
Files.createSymbolicLink(symlink1.toPath(), realDir1.toPath());
165+
assertTrue(symlink1 + " should exist", symlink1.exists());
166+
167+
// Create symlink #2 to realdir1/b in source root.
168+
final String SYMLINK2 = "symlink2";
169+
File symlink2 = new File(realDir1b, SYMLINK2);
170+
Files.createSymbolicLink(symlink2.toPath(), realDir1b.toPath());
171+
assertTrue(symlink2 + " should exist", symlink2.exists());
172+
173+
// Test symlink2 v. realDir1 canonical with validation and an allowed symlink1
174+
Set<String> allowedSymLinks = new HashSet<>();
175+
allowedSymLinks.add(symlink1.getPath());
176+
177+
String realDir1Canon = realDir1.getCanonicalPath();
178+
String rel = PathUtils.getRelativeToCanonical(symlink2.toString(),
179+
realDir1Canon, allowedSymLinks, null);
180+
assertEquals("symlink2 should be allowed implicitly as a canonical child of symlink1",
181+
"b", rel);
182+
}
183+
149184
@Ignore("macOS has /var symlink, and I also made a second link, `myhome'.")
150185
@Test
151186
public void shouldResolvePrivateVarOnMacOS() throws IOException {

0 commit comments

Comments
 (0)